Skip to content

Normal distribution

Esteban Zapata Rojas edited this page Jun 8, 2019 · 5 revisions

Instance methods

Cumulative function

It returns the probability P(x <= X) for an specified X.

[64] pry(main)> Distribution::Normal.new(3, 4)
=> #<Statistics::Distribution::Normal:0x0000000001b774c8 @mean=3.0, @standard_deviation=4.0, @variance=16.0>
[65] pry(main)> Distribution::Normal.new(3, 4).cumulative_function(9)
=> 0.9331927987311419

Probability density function

It returns the density for the specified value.

[44] pry(main)> normal_dist = Distribution::Normal.new(0, 0.447)
=> #<Statistics::Distribution::Normal:0x007fdfcbe9ff40 @mean=0.0, @standard_deviation=0.447, @variance=0.19980900000000001>
[45] pry(main)> result = (-5..5).map do |number|
[45] pry(main)*   normal_dist.density_function(number)
[45] pry(main)* end  
=> [6.042973541185679e-28,
 3.649365471962887e-18,
 1.477868786619915e-10,
 4.013342708303908e-05,
 0.07308503658363669,
 0.8924883230457107,
 0.07308503658363669,
 4.013342708303908e-05,
 1.477868786619915e-10,
 3.649365471962887e-18,
 6.042973541185679e-28]

Variance

It returns the expected variance for the normal distribution. It is the standard deviation to the second power.

[56] pry(main)> normal_dist.variance
=> 0.19980900000000001

Mode

It is an alias method to the mean accessor.

[57] pry(main)> normal_dist.mode
=> 0.0

Random

It returns a random number/sample following a Normal distribution. You can pass two arguments:

  • elements: # where # is the size of the random sample to be generated. If not specified, 1 by default.
  • seed: # where # is any number to be used as a seed generator. If not specified, Random.new_seed by default.
> normal = Distribution::Normal.new(2,3)
=> #<Statistics::Distribution::Normal:0x000000017bbca8 @mean=2.0, @standard_deviation=3.0, @variance=9.0>
> normal.random
=> 2.797637606172138
> normal.random(elements: 3)
=> [0.003654448083310058, 0.6652130091843838, 1.7335919585253503]
> normal.random(seed: Random.new_seed)
=> 2.3877425736687523

Standard Normal Distribution

This distribution is a normal distribution with mean = 0 and standard_deviation = 1. It is defined as a class that inherits from Normal distribution. It contains the same methods as the Normal distribution. The Probability Density function is an override method with the generalization of the normal PDF.

You can also have the quantile function for the standard normal, by instantiating the Inverse Standard Normal Distribution, which allows you to retrieve the Z value based on a probability value P:

2.5.1 :001 > Distribution::InverseStandardNormal.new.cumulative_function(0.3)
 => -0.5244005132792943 
2.5.1 :002 > Distribution::InverseStandardNormal.new.cumulative_function(0.9)
 => 1.281551564140072 
2.5.1 :003 > Distribution::InverseStandardNormal.new.cumulative_function(0)
 => -Infinity 
2.5.1 :004 > Distribution::InverseStandardNormal.new.cumulative_function(1)
 => Infinity