Class: XRVG::RandomFilter

Inherits:
Filter
  • Object
show all
Includes:
Attributable
Defined in:
lib/samplation.rb

Overview

RandomFilter class, to resample randomly

Attributes

attribute :mindiff, 0.0
attribute :sort, false

:mindiff specifies which minimum interval must be preserved between random values. Of course, the number of sampling must then be above a given value so that this constraint can be verified.

Instance Method Summary collapse

Methods inherited from Filter

#initialize, #transform, with

Methods included from Samplable

#apply_sample, #apply_samples, build, #mean, #sample, #samples

Methods included from FloatFunctor

#addfilter, #alternate, #apply, #applyhash, #compute, #filter, #generate, #geo, #geofull, #modify, #process, #random, #shuffle, #sin, #ssort, #transform, #trigger

Constructor Details

This class inherits a constructor from XRVG::Filter

Instance Method Details

#basictransform(v) ⇒ Object

do (0.0..1.0).rand to speed up computation if @mindiff is 0.0



393
394
395
# File 'lib/samplation.rb', line 393

def basictransform( v )
  return (0.0..1.0).rand
end

#rawtransforms(nsamples) ⇒ Object

make sampling by trying to check :mindiff constraint

generate an exception if not possible



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/samplation.rb', line 355

def rawtransforms(nsamples)
  size  = 1.0
  nsplit = nsamples - 1
  rsize = size - nsplit * @mindiff
  if rsize < 0.0
    raise("RandomFilter rawtransforms error: nsamples #{nsamples} mindiff #{@mindiff} are incompatible")
  end
  
  mindiffs = Array.new( nsplit, @mindiff )
  
  # compute now nsplit values whose sum is <= to rsize
  randarray = [0.0]
  subarray = (0.0..rsize).rand( nsplit-1 )
  if not subarray.is_a? Array
    subarray = [subarray]
  end
  randarray += subarray
  randarray.push( rsize )
  randarray.sort!

  rsizes = Array.new
  randarray.each_cons(2) { |min, max| rsizes.push( (0.0..max-min).rand ) }

  rsum = rsizes.sum
  root = (0.0..(rsize-rsum)).rand

  Trace("mindiffs #{mindiffs.inspect} rsizes #{rsizes.inspect} rsize #{rsize} rsum #{rsum} root #{root}")

  preresult = []
  mindiffs.zip( rsizes ) {|mindiff, rsize| preresult.push( mindiff + rsize )}

  result = [root]
  preresult.each {|v| newv = result[-1] + v; result << newv }

  return result
end

#transforms(inputs, type) ⇒ Object

:nodoc:



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/samplation.rb', line 397

def transforms( inputs, type )  #:nodoc:
  if type == :split
    @sort = true
  end
  if @mindiff == 0.0
    result = inputs.map { |v| self.basictransform( v ) }
    if @sort
	result = result.sort
    end
    if type == :split
	result[0]  = 0.0
	result[-1] = 1.0
    end
  else
    result = self.rawtransforms( inputs.size )
    if not @sort
	result = result.shuffle
    end
  end
  return result
end