Class: Statistical::Rng::TwoPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/statistical/rng/two_point.rb

Overview

Random number generator to model the two point distribution used for working with problem where the state space has only two points with distinct probabilities

Direct Known Subclasses

Bernoulli

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dobj = nil, seed = Random.new_seed) ⇒ TwoPoint

Companion RNG class for the two-point distribution. Requires a distrbution object of the corresponding type. Defaults to the standard bernoulli if no arguments are given

Author:

  • Vaibhav Yenamandra



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/statistical/rng/two_point.rb', line 22

def initialize(dobj = nil, seed = Random.new_seed)
  unless dobj.nil? || dobj.is_a?(Statistical::Distribution::TwoPoint)
    raise TypeError,
          "Expected Distribution object or nil, found #{dobj.class}"
  end
  dobj = Statistical::Distribution::TwoPoint.new if dobj.nil?
  @generator = Random.new(seed)
  @sdist = dobj
  @p = dobj.p
  @q = dobj.q
  @states = dobj.states
end

Instance Attribute Details

#generatorObject (readonly)

Returns the value of attribute generator.



10
11
12
# File 'lib/statistical/rng/two_point.rb', line 10

def generator
  @generator
end

#pObject (readonly)

Returns the value of attribute p.



10
11
12
# File 'lib/statistical/rng/two_point.rb', line 10

def p
  @p
end

#qObject (readonly)

Returns the value of attribute q.



10
11
12
# File 'lib/statistical/rng/two_point.rb', line 10

def q
  @q
end

#statesObject (readonly)

Returns the value of attribute states.



10
11
12
# File 'lib/statistical/rng/two_point.rb', line 10

def states
  @states
end

Instance Method Details

#randObject

Return the next random number from the sequence following the given distribution

Returns:

  • next random number in the sequence



39
40
41
# File 'lib/statistical/rng/two_point.rb', line 39

def rand
  return @sdist.quantile(@generator.rand)
end

#supportObject

The support set over which the distribution exists



62
63
64
# File 'lib/statistical/rng/two_point.rb', line 62

def support
  @sdist.support
end

#typeObject

Return the type of the source distribution

Returns:

  • source distribution’s type



57
58
59
# File 'lib/statistical/rng/two_point.rb', line 57

def type
  @sdist.class
end