Sha256: e79e883c84f621cb768c50a6fb74e006846f6200df0cfabc505af649959f032f

Contents?: true

Size: 743 Bytes

Versions: 6

Compression:

Stored size: 743 Bytes

Contents

# From http://stackoverflow.com/a/9266488
class RailsDataExplorer
  module Statistics
    class RngGaussian
      def initialize(mean = 0.0, sd = 1.0, rng = lambda { Kernel.rand })
        @mean, @sd, @rng = mean, sd, rng
        @compute_next_pair = false
      end

      def rand
        if (@compute_next_pair = !@compute_next_pair)
          # Compute a pair of random values with normal distribution.
          # See http://en.wikipedia.org/wiki/Box-Muller_transform
          theta = 2 * Math::PI * @rng.call
          scale = @sd * Math.sqrt(-2 * Math.log(1 - @rng.call))
          @g1 = @mean + scale * Math.sin(theta)
          @g0 = @mean + scale * Math.cos(theta)
        else
          @g1
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails-data-explorer-0.2.3 lib/rails-data-explorer/statistics/rng_gaussian.rb
rails-data-explorer-0.2.2 lib/rails-data-explorer/statistics/rng_gaussian.rb
rails-data-explorer-0.2.1 lib/rails-data-explorer/statistics/rng_gaussian.rb
rails-data-explorer-0.2.0 lib/rails-data-explorer/statistics/rng_gaussian.rb
rails-data-explorer-0.1.0 lib/rails-data-explorer/statistics/rng_gaussian.rb
rails-data-explorer-0.0.1 lib/rails-data-explorer/statistics/rng_gaussian.rb