Class: XRVG::RandomFilter

Inherits:
Filter 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 included from Attributable

included, #initialize

Methods inherited from Filter

#initialize, #transform, with

Methods included from Samplable

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

Methods included from FloatFunctor

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

Instance Method Details

#basictransform(v) ⇒ Object

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



445
446
447
# File 'lib/samplation.rb', line 445

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



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/samplation.rb', line 407

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:



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/samplation.rb', line 449

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
  if self.withboundaries
    result[0] = inputs[0]
    result[-1] = inputs[-1]
  end
  return result
end