Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,27 @@
|
|||
# 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
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue