This example shows simple random walk implemented by Merseene Twister in gsl-ocaml, a binding of GNU Scientific Library (GSL).
In [1]:
#require "gsl" ;;
#use "archimedes_iocaml.ml" ;;
In [2]:
let random_walk ~rng ~sigma n =
Array.init n (fun _ -> Gsl_randist.gaussian ~sigma rng)
|> Array.fold_left (fun (ws, w) z -> let w' = w +. z in (w' :: ws, w')) ([], 0.0)
|> fst
|> List.rev
Out[2]:
In [3]:
let rng = Gsl_rng.make Gsl_rng.MT19937 ;; (* Mersenne Twister *)
let vp = A.init ~w:760. ~h:300. ["iocaml"] in
A.Axes.box vp ;
A.set_color vp A.Color.red ;
A.List.y ~style:`Lines vp (random_walk ~rng ~sigma:0.01 1000) ;
A.set_color vp A.Color.blue ;
A.List.y ~style:`Lines vp (random_walk ~rng ~sigma:0.01 1000) ;
A.set_color vp A.Color.green ;
A.List.y ~style:`Lines vp (random_walk ~rng ~sigma:0.01 1000) ;
A.set_color vp A.Color.magenta ;
A.List.y ~style:`Lines vp (random_walk ~rng ~sigma:0.01 1000) ;
A.set_color vp A.Color.cyan ;
A.List.y ~style:`Lines vp (random_walk ~rng ~sigma:0.01 1000) ;
A.close vp
Out[3]:
Out[3]:
In [ ]: