Statistics/Normal distribution: Difference between revisions

Added a Ruby implementation.
(Added a Ruby implementation.)
Line 2,862:
──
</pre>
=={{header|Ruby}}==
{{works with|Ruby|2.7}}
'''The Implementation'''
<lang ruby># Class to implement a Normal distribution, generated from a Uniform distribution.
# Uses the Marsaglia polar method.
 
class NormalFromUniform
# Initialize an instance.
def initialize()
@next = nil
end
# Generate and return the next Normal distribution value.
def rand()
if @next
retval, @next = @next, nil
return retval
else
u = v = s = nil
loop do
u = Random.rand(-1.0..1.0)
v = Random.rand(-1.0..1.0)
s = u**2 + v**2
break if (s > 0.0) && (s <= 1.0)
end
f = Math.sqrt(-2.0 * Math.log(s) / s)
@next = v * f
return u * f
end
end
end</lang>
'''The Task'''
{{libheader|enumerable-statistics}}
<lang ruby>require('enumerable/statistics')
 
def show_stats_and_histogram(data, bins)
puts("size = #{data.length} mean = #{data.mean()} stddev = #{data.stdev()}")
hist = data.histogram(bins)
scale = 100.0 / hist.weights.max
inx_beg = nil
inx_end = nil
hist.weights.length.times do |inx|
histstars = (0.5 + (scale * hist.weights[inx])).to_i
inx_beg = inx if !inx_beg && (histstars > 0)
inx_end = inx if (histstars > 0)
end
(inx_beg..inx_end).each do |inx|
bincenter = 0.5 * (hist.edges[inx] + hist.edges[inx + 1])
histstars = (0.5 + (scale * hist.weights[inx])).to_i
puts('%6.2f: %s' % [bincenter, '*' * histstars])
end
end
 
puts
puts('Uniform random number generator:')
show_stats_and_histogram(1000000.times.map { Random.rand(-1.0..1.0) }, 20)
 
puts
puts('Normal random numbers using the Marsaglia polar method:')
gen_normal = NormalFromUniform.new
show_stats_and_histogram(100.times.map { gen_normal.rand }, 40)
show_stats_and_histogram(10000.times.map { gen_normal.rand }, 60)
show_stats_and_histogram(1000000.times.map { gen_normal.rand }, 120)</lang>
{{out}}
<pre style="height: 200ex; overflow: scroll">
Uniform random number generator:
size = 1000000 mean = 0.0001483724103528628 stddev = 0.5773085740222473
-0.95: ****************************************************************************************************
-0.85: ****************************************************************************************************
-0.75: ***************************************************************************************************
-0.65: ***************************************************************************************************
-0.55: ***************************************************************************************************
-0.45: ****************************************************************************************************
-0.35: ****************************************************************************************************
-0.25: ****************************************************************************************************
-0.15: ****************************************************************************************************
-0.05: ***************************************************************************************************
0.05: ****************************************************************************************************
0.15: ****************************************************************************************************
0.25: ****************************************************************************************************
0.35: ***************************************************************************************************
0.45: ***************************************************************************************************
0.55: ****************************************************************************************************
0.65: ****************************************************************************************************
0.75: ****************************************************************************************************
0.85: ****************************************************************************************************
0.95: ***************************************************************************************************
 
Normal random numbers using the Marsaglia polar method:
size = 100 mean = 0.1611961166227389 stddev = 0.9827581078952005
-2.30: **********
-2.10:
-1.90: **********
-1.70: ********************
-1.50:
-1.30: **********
-1.10: **********************************************************************
-0.90: ************************************************************
-0.70: **********************************************************************
-0.50: ************************************************************
-0.30: ********************************************************************************
-0.10: ********************
0.10: ********************************************************************************
0.30: ****************************************************************************************************
0.50: ******************************************************************************************
0.70: ********************************************************************************
0.90: ************************************************************
1.10: ******************************
1.30: **************************************************
1.50:
1.70: ********************
1.90: **************************************************
2.10: **********
2.30: ********************
size = 10000 mean = -0.004863294071004369 stddev = 0.9984395022628252
-3.30: *
-3.10: *
-2.90: **
-2.70: **
-2.50: ****
-2.30: *********
-2.10: ***********
-1.90: ****************
-1.70: ***********************
-1.50: *****************************
-1.30: *****************************************
-1.10: *************************************************
-0.90: ******************************************************************
-0.70: ******************************************************************************
-0.50: ***************************************************************************************
-0.30: *********************************************************************************************
-0.10: ***********************************************************************************************
0.10: ****************************************************************************************************
0.30: **************************************************************************************
0.50: ************************************************************************************
0.70: *******************************************************************************
0.90: ****************************************************************
1.10: ***************************************************
1.30: ********************************************
1.50: *******************************
1.70: **********************
1.90: ****************
2.10: **********
2.30: ******
2.50: *****
2.70: **
2.90: *
3.10: *
size = 1000000 mean = 0.0007049206911295587 stddev = 1.0000356747411552
-3.15: *
-3.05: *
-2.95: *
-2.85: **
-2.75: **
-2.65: ***
-2.55: ****
-2.45: *****
-2.35: ******
-2.25: ********
-2.15: **********
-2.05: ************
-1.95: ***************
-1.85: ******************
-1.75: *********************
-1.65: *************************
-1.55: ******************************
-1.45: ***********************************
-1.35: ****************************************
-1.25: *********************************************
-1.15: ***************************************************
-1.05: *********************************************************
-0.95: ***************************************************************
-0.85: *********************************************************************
-0.75: **************************************************************************
-0.65: *********************************************************************************
-0.55: *************************************************************************************
-0.45: *****************************************************************************************
-0.35: ********************************************************************************************
-0.25: ************************************************************************************************
-0.15: **************************************************************************************************
-0.05: ****************************************************************************************************
0.05: ***************************************************************************************************
0.15: **************************************************************************************************
0.25: ************************************************************************************************
0.35: *********************************************************************************************
0.45: ******************************************************************************************
0.55: *************************************************************************************
0.65: ********************************************************************************
0.75: **************************************************************************
0.85: *********************************************************************
0.95: ***************************************************************
1.05: *********************************************************
1.15: ****************************************************
1.25: **********************************************
1.35: ****************************************
1.45: **********************************
1.55: ******************************
1.65: *************************
1.75: **********************
1.85: ******************
1.95: ***************
2.05: ************
2.15: **********
2.25: ********
2.35: ******
2.45: *****
2.55: ****
2.65: ***
2.75: **
2.85: **
2.95: *
3.05: *
3.15: *
</pre>