Qu

normal

Builtin index · Statistics & ML

result = normal(mu, sigma, [rows], [cols], [seed=])

Input: mu, sigma, rows, cols, seed · Output: result

What it is

Draws from a Gaussian. mu (number) is the mean; sigma (number) is the standard deviation; rows and cols (numbers, optional) give the shape of the draw, one number for a vector and two for a matrix; seed= (number, optional) makes the draw repeatable.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = normal(mu, sigma, [rows], [cols], [seed=])
mu numbermu (number) is the mean.
sigma numbersigma (number) is the standard deviation.
rows number, vector, or matrix, optionalrows and cols (numbers, optional) give the shape of the draw, one number for a vector and two for a matrix.
cols number, vector, or matrix, optionalrows and cols (numbers, optional) give the shape of the draw, one number for a vector and two for a matrix.
seed number, optionalseed= (number, optional) makes the draw repeatable.
result number, vector, or matrix
Returns a number, vector, or (rows, cols) matrix.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

u = uniform(10, 20, 5)          # 5 draws, continuous uniform on [10, 20)

g = normal(5, 2, 1000, seed=1)  # 1000 draws, mean 5, std 2 โ€” randn is mu=0/sigma=1

d = randi(1, 6, 10)             # 10 dice rolls, inclusive both ends

k = poisson(4, 500)             # 500 draws from Poisson(lambda=4)

t = exponential(2, 500)         # 500 draws, rate=2 (mean = 1/rate = 0.5)

b = binomial(20, 0.3, 500)      # 500 draws, 20 trials at p=0.3 each

from numerics-dsp

draws = normal(10, 2, 4000, seed = 77)
print("mean {round(mean(draws), 3)}, std {round(std(draws), 3)}")
hist(draws, bins = 40, color = "#4169e1")
title("normal(mu = 10, sigma = 2), 4000 draws")
xlabel("value")
output
mean 9.982, std 1.993
ยท hist: figure 15 (4000 points, 40 bins)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 knn tree forest bayes x=-1.058, y=-0.826 x=0.594, y=0.636 x=0.88, y=1.305 x=-0.379, y=-0.489 x=0.152, y=1.215 x=1.407, y=0.754 x=0.231, y=-0.942 x=-1.291, y=-0.12 x=-0.085, y=-0.627 x=0.304, y=-0.101 x=0.037, y=0.004 x=-0.705, y=-0.429 x=0.241, y=-0.559 x=-0.582, y=-1.167 x=-0.277, y=-0.685 x=-1.636, y=-1.342 x=-0.865, y=-0.799 x=0.131, y=0.029 x=-1.581, y=-1.773 x=-2.048, y=-2.111 x=2.947, y=2.32 x=-0.456, y=-1.875 x=0.908, y=0.097 x=0.85, y=1.1 x=0.517, y=0.032 x=0.003, y=0.246 x=-0.201, y=0.212 x=0.466, y=-0.074 x=0.426, y=1.096 x=-0.066, y=-0.164 x=1.021, y=1.029 x=0.113, y=0.035 x=-0.998, y=-0.494 x=-0.066, y=-0.991 x=2.823, y=0.731 x=-0.046, y=-0.746 x=-0.029, y=-0.9 x=-0.031, y=0.196 x=-0.616, y=0.539 x=-1.349, y=-0.313 x=-1.085, y=-1.65 x=-1.452, y=-1.608 x=0.973, y=0.867 x=0.737, y=0.242 x=-0.185, y=0.468 x=1.175, y=1.485 x=-1.836, y=-0.822 x=-0.157, y=-0.076 x=0.407, y=0.307 x=-1.365, y=-1.022 x=-0.847, y=1.143 x=-0.762, y=0.021 x=-1.044, y=-0.505 x=0.478, y=1.123 x=0.316, y=-0.102 x=0.965, y=0.825 x=-0.751, y=-0.957 x=-0.988, y=-0.033 x=-1.22, y=-0.385 x=-0.681, y=-0.432 x=-0.621, y=0.256 x=0.918, y=0.744 x=1.182, y=-0.477 x=-0.039, y=0.425 x=0.585, y=-0.031 x=0.297, y=-0.535 x=0.112, y=-0.357 x=0.039, y=0.8 x=0.16, y=0.201 x=1.261, y=-0.955 x=0.95, y=0.238 x=-0.013, y=0.393 x=-1.577, y=-0.343 x=-0.67, y=-0.101 x=-0.631, y=-1.302 x=-0.84, y=-0.949 x=1.692, y=0.99 x=0.389, y=0.473 x=-0.261, y=-0.509 x=-0.491, y=-0.033 x=0.634, y=0.806 x=-0.342, y=0.622 x=-1.674, y=-1.63 x=0.039, y=-0.203 x=0.175, y=-0.688 x=0.206, y=0.293 x=1.405, y=0.994 x=-0.644, y=-0.672 x=-0.623, y=-0.7 x=1.569, y=0.384 x=-0.533, y=-0.154 x=0.682, y=-0.014 x=1.089, y=1.13 x=0.342, y=0.984 x=1.161, y=0.991 x=-0.232, y=0.289 x=-0.607, y=-0.737 x=0.295, y=0.07 x=0.275, y=0.25 x=-1.141, y=-0.456 x=0.461, y=0.554 x=-0.671, y=-0.906 x=0.513, y=1.154 x=0.499, y=-0.224 x=1.753, y=1.474 x=0.103, y=0.61 x=-0.351, y=0.258 x=0.553, y=-0.788 x=0.298, y=0.904 x=0.472, y=0.843 x=-0.94, y=-0.939 x=-1.062, y=-1.629 x=0.467, y=0.806 x=-0.653, y=-1.083 x=1.172, y=1.15 x=0.206, y=0.15 x=-1.077, y=-0.827 x=0.153, y=-0.987 x=0.202, y=0.439 x=0.011, y=-0.385 x=-0.67, y=0.029 x=1.367, y=0.502 x=0.955, y=0.531 x=0.352, y=0.238 x=1.543, y=0.703 x=-1.029, y=-0.474 x=0.104, y=0.508 x=0.877, y=-0.209 x=-1.366, y=-1.921 x=-1.583, y=-1.15 x=1.748, y=0.901 x=0.359, y=0.563 x=0.309, y=-0.002 x=1.322, y=1.929 x=-0.57, y=-0.79 x=-0.857, y=0.127 x=0.177, y=-0.37 x=-1.161, y=-1.563 x=0.371, y=1.281 x=-0.811, y=-1.39 x=0.168, y=1.089 x=-0.599, y=0.823 x=-0.452, y=-1.067 x=-1.241, y=-1.344 x=-0.711, y=-0.72 x=-0.233, y=-0.53 x=-0.344, y=0.761 x=1.152, y=0.27 x=0.989, y=0.896 x=0.308, y=0.67 x=-1.851, y=-1.723 x=0.866, y=0.926 x=-0.586, y=0.32 x=-0.204, y=-0.76 x=1.617, y=1.863 x=-0.897, y=-1.095 x=1.375, y=0.119 x=0.027, y=0.911 x=-0.268, y=-0.958 x=-0.58, y=-0.581 x=1.263, y=0.222 x=0.021, y=0.109 x=0.228, y=-0.664 x=0.002, y=-0.26 x=-0.301, y=0.237 x=0.646, y=0.863 x=0.272, y=-1.038 x=-0.432, y=-0.686 x=-0.123, y=1.22 x=0.844, y=1.163 x=-1.237, y=-1.157 x=2, y=1.132 x=-0.836, y=0.309 x=-0.332, y=-0.738 x=0.17, y=0.198 x=-0.327, y=0.587 x=-1.737, y=-1.131 x=-0.054, y=0.877 x=0.317, y=0.75 x=-1.406, y=-1.905 x=0.04, y=0.092 x=1.456, y=0.878 x=0.373, y=0.985 x=-0.912, y=-0.551 x=-0.104, y=0.042 x=0.349, y=0.549 x=1.098, y=0.24 x=0.588, y=-0.011 x=-0.164, y=-0.854 x=0.235, y=-0.679 x=-0.713, y=0.139 x=0.252, y=-0.404 x=-0.029, y=-0.281 x=-0.988, y=-0.818 x=0.223, y=-0.99 x=-0.591, y=0.839 x=-0.36, y=-0.088 x=-0.006, y=-0.156 x=-0.054, y=-0.158 x=0.644, y=-0.111 x=-3, y=-4.018 x=-2.75, y=-6.388 x=-2.5, y=-5.262 x=-2.25, y=0.293 x=-2, y=1.587 x=-1.75, y=1.194 x=-1.5, y=1.829 x=-1.25, y=2.719 x=-1, y=-0.145 x=-0.75, y=0.779 x=-0.5, y=-0.837 x=-0.25, y=2.101 x=0, y=0.378 x=0.25, y=1.148 x=0.5, y=1.019 x=0.75, y=-3.978 x=1, y=-3.731 x=1.25, y=-0.191 x=1.5, y=-1.178 x=1.75, y=-2.55 x=2, y=-0.452 x=2.25, y=4.657 x=2.5, y=0.591 x=2.75, y=5.418 x=3, y=6.826 x=-4.221, y=-3.675 x=-4.749, y=-4.599 x=-4.378, y=-5.049 x=-4.908, y=-2.938 x=-3.111, y=-4.705 x=-4.7, y=-3.185 x=-4.948, y=-4.24 x=-5.087, y=-4.991 x=-3.755, y=-2.658 x=-4.866, y=-3.893 x=-3.202, y=-4.799 x=-3.536, y=-4.59 x=-4.643, y=-4.655 x=-4.223, y=-3.8 x=-4.771, y=-4.791 x=-5.162, y=-3.624 x=-2.644, y=-3.501 x=-5.565, y=-4.823 x=-3.561, y=-2.64 x=-4.529, y=-4.524 x=-5.6, y=-5.042 x=-3.048, y=-3.184 x=-4.283, y=-4.206 x=-5.239, y=-4.032 x=-2.871, y=-5.062 x=-2.117, y=-2.96 x=-3.794, y=-3.873 x=-3.245, y=-4.153 x=-4.242, y=-4.691 x=-3.935, y=-4.681 x=-2.991, y=-2.757 x=-4.498, y=-3.651 x=-4.482, y=-5.712 x=-3.324, y=-4.723 x=-5.062, y=-5.077 x=-4.34, y=-4.914 x=-4.945, y=-5.731 x=-4.523, y=-3.545 x=-2.907, y=-3.457 x=-4.38, y=-3.659 x=3.22, y=4.412 x=3.089, y=4.062 x=2.827, y=3.584 x=3.848, y=5.345 x=4.196, y=4.669 x=3.111, y=4.336 x=2.961, y=3.91 x=5.036, y=4.596 x=5.339, y=4.031 x=4.874, y=2.505 x=4.357, y=2.791 x=3.318, y=5.241 x=2.544, y=3.948 x=6.731, y=3.889 x=4.101, y=4.456 x=3.18, y=2.902 x=4.415, y=7.292 x=3.9, y=6.008 x=3.342, y=4.252 x=2.138, y=4.074 x=3.304, y=4.752 x=4.53, y=4.152 x=3.264, y=4.913 x=3.885, y=2.94 x=5.542, y=3.917 x=1.488, y=3.061 x=2.847, y=3.236 x=3.93, y=3.488 x=2.913, y=2.598 x=3.839, y=4.13 x=2.508, y=3.961 x=2.846, y=4.195 x=4.278, y=4.197 x=4.971, y=5.339 x=4.568, y=5.297 x=3.797, y=3.895 x=3.459, y=4.494 x=4.285, y=3.369 x=3.836, y=2.999 x=2.123, y=1.312 x=-0.678, y=-0.737 x=-0.734, y=0.333 x=-0.016, y=1.204 x=0.605, y=0.633 x=0.426, y=-0.995 x=-0.383, y=0.403 x=-0.064, y=0.253 x=-1.862, y=-0.361 x=0.298, y=0.52 x=-1.481, y=-1.025 x=0.719, y=0.808 x=1.151, y=-0.726 x=-0.082, y=-0.357 x=-0.401, y=0.791 x=-0.182, y=0.737 x=0.394, y=0.021 x=0.954, y=0.671 x=0.405, y=0.336 x=-1.617, y=-0.65 x=-0.669, y=-1.147 x=-2.452, y=1.586 x=1.709, y=-0.65 x=0.122, y=-0.171 x=1.066, y=-1.195 x=0.053, y=-0.57 x=-0.396, y=-0.235 x=0.819, y=-0.788 x=0.012, y=0.012 x=-2.723, y=0.397 x=1.545, y=0.484 x=-1.119, y=0.98 x=-0.22, y=-0.804 x=-1.564, y=-1.293 x=-1.811, y=-0.276 x=0.774, y=-1.055 x=0.575, y=1.214 x=-0.348, y=-1.078 x=0.015, y=-0.404 x=0.245, y=-1.466 x=0.436, y=1.71 x=0, y=0.983 0.983 x=1, y=0.95 0.95 x=2, y=0.967 0.967 x=3, y=0.917 0.917 x=0, y=0.209 x=1, y=-0.789 x=2, y=0.181 x=3, y=3.319 x=4, y=0.973 x=5, y=1.711 x=6, y=1.379 x=7, y=0.441 x=8, y=6.776 x=9, y=5.163 x=10, y=1.275 x=11, y=5.11 x=12, y=4.859 x=13, y=2.881 x=14, y=3.871 x=15, y=6.271 x=16, y=8.817 x=17, y=6.083 x=18, y=8.405 x=19, y=9.285 x=20, y=9.319 x=21, y=10.964 x=22, y=6.751 x=23, y=11.568 x=24, y=13.342 x=25, y=16.469 x=26, y=14.154 x=27, y=15.639 x=28, y=12.311 x=29, y=10.938 x=30, y=17.006 x=31, y=14.186 x=32, y=13.236 x=33, y=13.894 x=34, y=17.924 x=35, y=16.379 x=36, y=14.548 x=37, y=16.145 x=38, y=20.246 x=39, y=19.397 x=40, y=19.613 x=41, y=24.115 x=42, y=18.843 x=43, y=25.23 x=44, y=21.187 x=45, y=22.486 x=46, y=24.405 x=47, y=22.59 x=48, y=22.557 x=49, y=23.677 x=50, y=28.284 x=51, y=28.544 x=52, y=24.99 x=53, y=26.047 x=54, y=23.037 x=55, y=22.826 x=56, y=27.742 x=57, y=25.787 x=58, y=27.48 x=59, y=32.635 x=2.566, y=2 x=2.951, y=1 x=3.335, y=1 x=3.719, y=2 x=4.104, y=3 x=4.488, y=5 x=4.872, y=10 x=5.257, y=19 x=5.641, y=19 x=6.025, y=50 x=6.41, y=70 x=6.794, y=70 x=7.179, y=122 x=7.563, y=148 x=7.947, y=198 x=8.332, y=227 x=8.716, y=254 x=9.1, y=265 x=9.485, y=260 x=9.869, y=340 x=10.253, y=316 x=10.638, y=291 x=11.022, y=265 x=11.407, y=235 x=11.791, y=206 x=12.175, y=159 x=12.56, y=130 x=12.944, y=95 x=13.328, y=70 x=13.713, y=68 x=14.097, y=40 x=14.481, y=21 x=14.866, y=18 x=15.25, y=6 x=15.635, y=5 x=16.019, y=4 x=16.403, y=2 x=16.788, y=2 x=17.172, y=0 x=17.556, y=1 normal(mu = 10, sigma = 2), 4000 draws value test accuracy measured cubic fit measured forecast cluster 0 cluster 1 cluster 2 centroids measured truth Kalman

from statistics-ml

n_per_class = 60
c0x = normal(0, 0.6, n_per_class, seed=1)
c0y = normal(0, 0.6, n_per_class, seed=2)
c1x = normal(4, 0.6, n_per_class, seed=3)
c1y = normal(4, 0.6, n_per_class, seed=4)

Xc0 = hstack(reshape(c0x, n_per_class, 1), reshape(c0y, n_per_class, 1))
Xc1 = hstack(reshape(c1x, n_per_class, 1), reshape(c1y, n_per_class, 1))
Xc = vstack(Xc0, Xc1)             # (120, 2)


n = 2 * n_per_class
idx = 0 to n - 1
y = floor(idx / n_per_class)      # 0 for the first 60 rows, 1 for the last 60


function one_hot(labels, rows, k)
    m = zeros(rows, k)
    for i = 0 to rows - 1
        m[i, labels[i]] = 1
    end for
    return m
end function

Y = one_hot(y, n, 2)

net = mlp_classifier(2, [8], 2, seed=42)
trained = net.fit(Xc, Y, epochs=200, lr=0.1, loss="cross_entropy")

function accuracy(model, X, Ytrue, rows)
    logits = model.predict(X)
    correct = 0
    for i = 0 to rows - 1
        if argmax(logits[i, :]) == argmax(Ytrue[i, :]) then
            correct = correct + 1
        end if
    end for
    return correct / rows
end function

acc = accuracy(trained, Xc, Y, n)
print("training accuracy = {acc:.4f}")
output
training accuracy = 1.0000

from quickstart

How it works

normal(mu, sigma, ...) โ€” randn is this distribution's mu=0, sigma=1 special case.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.