Class: Distrb::Multivariate::Normal::TransformIndependentSamples
- Inherits:
-
Object
- Object
- Distrb::Multivariate::Normal::TransformIndependentSamples
- 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
-
#covariance ⇒ Object
readonly
Returns the value of attribute covariance.
-
#mean ⇒ Object
readonly
Returns the value of attribute mean.
-
#normal ⇒ Object
readonly
Returns the value of attribute normal.
Instance Method Summary collapse
-
#initialize(mean, covariance = nil) ⇒ TransformIndependentSamples
constructor
A new instance of TransformIndependentSamples.
- #sample ⇒ Object
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
#covariance ⇒ Object (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 |
#mean ⇒ Object (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 |
#normal ⇒ Object (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
#sample ⇒ Object
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 |