Rebol3 Code Examplex


Statistics/Normal distribution

Rebol [
    title: "Rosetta code: Statistics/Normal distribution"
    file:  %Statistics-Normal_distribution.r3
    url:   https://rosettacode.org/wiki/Statistics/Normal_distribution
    author: @ldci
    needs: 3.19.0
]

random/seed now/time/precise
randMax: 2147483647  ;; max integer value
nMax: 500000         ;; number of random values, can be modified

;; Normal random numbers generator using Marsaglia algorithm.
;; Generates 2 independent series of random values in the range [-1.0, 1.0].
generate: function [
    n [integer!]  ;; number of pairs to generate
][
    m: n * 2
    values: make vector! [f64! :m]  ;; vector to hold generated values
    for i 1 m 2 [
        rsq: 0.0
        ;; Repeat while radius squared is outside the unit circle or zero
        while [any [(rsq >= 1.0) (rsq == 0.0)]] [
            x: (2.0 * random randMax) / randMax - 1.0
            y: (2.0 * random randMax) / randMax - 1.0
            rsq: (x * x) + (y * y)
        ]
        f: sqrt ((-2.0 * log-e rsq) / rsq)
        values/(i): x * f
        values/(i + 1): y * f
    ]
    values
]

print-histogram: function [
    "Print a histogram of values in a vector, assuming range [-3.0, 3.0)."
    values [vector!] "Input data vector"
][
    width: 50.0           ;; width of histogram bars
    low:  -3.0            ;; lower bound of histogram
    high:  3.0            ;; upper bound of histogram
    delta: 0.1            ;; bin width
    n: values/length      ;; length of data
    nbins: to integer! ((high - low) / delta)  ;; number of bins
    bins: make vector! [i32! :nbins]  ;; initialize bins vector
    repeat i n [
        j: 1 + round/floor ((values/:i - low) / delta)
        if all [(j >= 1) (j <= nbins)] [
            bins/:j: bins/:j + 1  ;; increment bin counter
        ]
    ]
    maxi: bins/maximum  ;; max count in any bin
    repeat j nbins [
        lbin: round/to (j - 1 * delta + low) 0.01
        hbin: round/to (j     * delta + low) 0.01
        s: ajoin ["[" pad lbin -5 SP pad hbin -5 "] "]  ;; bin label string
        append/dup s #"▉" width * bins/:j / maxi       ;; bar scaled to max count
        append s ajoin [" " round/to (bins/:j * 100 / n) 0.01 "%"]  ;; append percentage
        print s
    ]
]

;;********************** Main ***********************

print "Be patient! Generating Data and Gaussian Histogram..."
print-hline

time: dt [
    values: generate nMax   ;; generate nMax pairs of random values
    print-histogram values  ;; print histogram of generated values
]

print-hline
print [nMax * 2 "Values processed in:" round/to third time 0.01 "sec"]
print ["Mean: " values/mean]
print ["STD : " values/sample-deviation]
print-hline

Output:

Be patient! Generating Data and Gaussian Histogram...
------------------------------------------------------------------------------------------------------------
 [-2.95 -2.85] ▉ 0.07%
 [-2.85 -2.75] ▉▉ 0.09%
 [-2.75 -2.65] ▉▉ 0.12%
 [-2.65 -2.55] ▉▉ 0.15%
 [-2.55 -2.45] ▉▉▉ 0.2%
 [-2.45 -2.35] ▉▉▉▉ 0.25%
 [-2.35 -2.25] ▉▉▉▉▉ 0.32%
 [-2.25 -2.15] ▉▉▉▉▉ 0.39%
 [-2.15 -2.05] ▉▉▉▉▉▉▉ 0.5%
 [-2.05 -1.95] ▉▉▉▉▉▉▉▉ 0.59%
 [-1.95 -1.85] ▉▉▉▉▉▉▉▉▉▉ 0.72%
 [-1.85 -1.75] ▉▉▉▉▉▉▉▉▉▉▉ 0.86%
 [-1.75 -1.65] ▉▉▉▉▉▉▉▉▉▉▉▉▉ 1.01%
 [-1.65 -1.55] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 1.21%
 [-1.55 -1.45] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 1.4%
 [-1.45 -1.35] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 1.6%
 [-1.35 -1.25] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 1.82%
 [-1.25 -1.15] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 2.05%
 [-1.15 -1.05] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 2.3%
 [-1.05 -0.95] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 2.55%
 [-0.95 -0.85] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 2.77%
 [-0.85 -0.75] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.04%
 [-0.75 -0.65] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.23%
 [-0.65 -0.55] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.45%
 [-0.55 -0.45] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.6%
 [-0.45 -0.35] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.77%
 [-0.35 -0.25] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.85%
 [-0.25 -0.15] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.92%
 [-0.15 -0.05] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.96%
  [-0.05 0.05] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.97%
   [0.05 0.15] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.93%
   [0.15 0.25] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.86%
   [0.25 0.35] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.73%
   [0.35 0.45] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.62%
   [0.45 0.55] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.43%
   [0.55 0.65] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.22%
   [0.65 0.75] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3.04%
   [0.75 0.85] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 2.76%
   [0.85 0.95] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 2.54%
   [0.95 1.05] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 2.3%
   [1.05 1.15] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 2.06%
   [1.15 1.25] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 1.85%
   [1.25 1.35] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 1.6%
   [1.35 1.45] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 1.4%
   [1.45 1.55] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 1.18%
   [1.55 1.65] ▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 1.04%
   [1.65 1.75] ▉▉▉▉▉▉▉▉▉▉▉ 0.86%
   [1.75 1.85] ▉▉▉▉▉▉▉▉▉▉ 0.72%
   [1.85 1.95] ▉▉▉▉▉▉▉▉ 0.59%
   [1.95 2.05] ▉▉▉▉▉▉▉ 0.5%
   [2.05 2.15] ▉▉▉▉▉▉ 0.4%
   [2.15 2.25] ▉▉▉▉▉ 0.32%
   [2.25 2.35] ▉▉▉▉ 0.26%
   [2.35 2.45] ▉▉▉ 0.2%
   [2.45 2.55] ▉▉ 0.16%
   [2.55 2.65] ▉▉ 0.12%
   [2.65 2.75] ▉▉ 0.09%
   [2.75 2.85] ▉ 0.07%
   [2.85 2.95] ▉ 0.05%
   [2.95 3.05] ▉ 0.04%
------------------------------------------------------------------------------------------------------------
1000000 Values processed in: 1.72 sec
Mean:  0.000528306263575084
STD :  1.00077298380933
------------------------------------------------------------------------------------------------------------