Sha256: 57fd6f0f42389c6af4f0d0d54a821706fa99e3365c815cd6feb52c9a22254aa3

Contents?: true

Size: 622 Bytes

Versions: 22

Compression:

Stored size: 622 Bytes

Contents

#!/usr/bin/env ruby
require("gsl")
include Math

def ran_power_law(slope, range, rng)
  x = rng.uniform
  xmin = GSL::pow(10.0, range*(1.0 + slope));
  x2 = (1 - xmin)*x + xmin;
  return GSL::pow(x2, 1.0/(1+slope));
end

h = GSL::Histogram.alloc(19, [1, 20])
rng = GSL::Rng.alloc
for i in 0..5000
  x = ran_power_law(-3.2, 1000, rng)
  h.increment(x)
end

result = h.fit_power
x = GSL::Vector.logspace2(1, 20, 19)
y = result[0]*GSL::pow(x, result[1])
GSL::graph(h, [x, y], "-l x -l y -x 1 20 -y 1 10000 -C -g 3")

puts("Expected power index: -3.2")
printf("Estimated power index: %5.4f +/- %5.4f\n", result[1], result[3])

Version data entries

22 entries across 22 versions & 4 rubygems

Version Path
gsl-2.1.0.3 examples/histogram/power.rb
gsl-2.1.0.2 examples/histogram/power.rb
gsl-2.1.0.1 examples/histogram/power.rb
gsl-2.1.0 examples/histogram/power.rb
gsl-1.16.0.6 examples/histogram/power.rb
rb-gsl-1.16.0.5 examples/histogram/power.rb
rb-gsl-1.16.0.4 examples/histogram/power.rb
rb-gsl-1.16.0.3 examples/histogram/power.rb
rb-gsl-1.16.0.3.rc1 examples/histogram/power.rb
gsl-nmatrix-1.15.3.2 examples/histogram/power.rb
gsl-nmatrix-1.15.3.1 examples/histogram/power.rb
rb-gsl-1.16.0.2 examples/histogram/power.rb
rb-gsl-1.16.0.1 examples/histogram/power.rb
rb-gsl-1.16.0 examples/histogram/power.rb
rb-gsl-1.15.3.2 examples/histogram/power.rb
rb-gsl-1.15.3.1 examples/histogram/power.rb
blackwinter-gsl-1.15.3.2 examples/histogram/power.rb
gsl-nmatrix-1.15.3.0 examples/histogram/power.rb
gsl-1.15.3 examples/histogram/power.rb
gsl-1.14.7 examples/histogram/power.rb