Qu

random_walk

Builtin index · Statistics & ML

result = random_walk(n_steps, [drift=0], [volatility=1], [seed=])

Input: n_steps, drift, volatility, seed · Output: result

What it is

Generates a discretized Wiener-process path: n_steps (a positive integer) increments, each drawn ~ Normal(drift, volatility), cumulatively summed, with an optional integer seed=.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = random_walk(n_steps, [drift=0], [volatility=1], [seed=])
n_steps —not documented yet
drift optional (default 0)not documented yet
volatility optional (default 1)not documented yet
seed optionalnot documented yet
result vector
Returns a length-n_steps + 1 vector starting at 0, where Var(x[n]) = n * volatility^2.

Examples

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

seed(42)                             # reseed the shared stream

u = rand(3, seed = 1)                # an explicit seed still wins

print("rand draws: {round(u, 3)}")
print("chi2pdf(2, 3):  {round(chi2pdf(2, 3), 4)}")
print("normpdf(0):     {round(normpdf(0), 4)}")

walk = random_walk(20, drift = 0.1, volatility = 1.0, seed = 3)
print("random walk, last value: {round(walk[19], 3)}")
output
rand draws: [0.746, 0.971, 0.444]
chi2pdf(2, 3):  0.2076
normpdf(0):     0.3989
random walk, last value: -3.552

from statistics-ml

How it works

random_walk(n_steps, [drift=0], [volatility=1], [seed=]) — a discretized Wiener-process-style path: x[0] = 0, x[i] = x[i-1] + drift + volatility * randn(). Reuses the same Rng (.normal()) and seed= convention as randn/kmeans for deterministic reproducibility.

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