Class: Distrb::Multivariate::Normal::TransformIndependentSamples

Inherits:
Object
  • Object
show all
Defined in:
lib/distrb/multivariate/normal/transform_independent_samples.rb

Overview

Generating random values from multivariate normal distribution can be done with independent samples from normal distribution. In this way, a square root of the covariance matrix is required. We use SVD to get the matrix. See: stats.stackexchange.com/a/159322/89764

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mean, covariance = nil) ⇒ TransformIndependentSamples

Returns a new instance of TransformIndependentSamples.



13
14
15
16
17
# File 'lib/distrb/multivariate/normal/transform_independent_samples.rb', line 13

def initialize mean, covariance = nil
  @normal = Distrb::Normal.new
  @mean = mean
  @covariance = covariance || GSL::Matrix.eye(mean.length)
end

Instance Attribute Details

#covarianceObject (readonly)

Returns the value of attribute covariance.



11
12
13
# File 'lib/distrb/multivariate/normal/transform_independent_samples.rb', line 11

def covariance
  @covariance
end

#meanObject (readonly)

Returns the value of attribute mean.



11
12
13
# File 'lib/distrb/multivariate/normal/transform_independent_samples.rb', line 11

def mean
  @mean
end

#normalObject (readonly)

Returns the value of attribute normal.



11
12
13
# File 'lib/distrb/multivariate/normal/transform_independent_samples.rb', line 11

def normal
  @normal
end

Instance Method Details

#sampleObject



19
20
21
22
# File 'lib/distrb/multivariate/normal/transform_independent_samples.rb', line 19

def sample
  x = GSL::Vector.alloc(Array.new(self.mean.length) { self.normal.sample })
  (sqrt_covariance * x + self.mean).to_a
end