2 # $Id: random.dem,v 1.12.2.2 2008/09/23 23:11:31 sfeam Exp $
6 # Lattice test for random numbers;
7 # If you can see any patterns in this plot, the random number generator
10 # Copyright (c) 1991, Jos van der Woude, jvdwoude@hut.nl
13 # - 6. 6. 2006 ds: added univariate and multivariate normal example
14 # - 10. 5. 2006 ds: added univariate and multivariate normal example
15 # - ?. ? 1991 jvdw: 1st version
21 set title "Lattice test for random numbers"
22 set xlabel "rand(n) ->"
23 set ylabel "rand(n + 1) ->"
24 set zlabel "rand(n + 2) ->"
30 set style function dots
33 pause -1 "Hit return to continue"
34 print "3D plot ahead, one moment please ..."
36 splot rand(0), rand(0), rand(0)
37 pause -1 "Hit return to continue"
40 print "Multivariate normal distribution"
42 print "The surface plot shows a two variable multivariate probability"
43 print "density function. On the x-y plane are some samples of the random"
44 print "vector and a contour plot illustrating the correlation, which in"
45 print "this case is zero, i.e. a circle. (Easier to view in map mode.)"
52 # A somewhat inelegant way of generating N random data points. A future
53 # non-pressing plot-command feature addition may address this issue.
56 set table "random.tmp"
57 plot invnorm(rand(0)),invnorm(rand(0))
61 tstring(n) = sprintf("%d random samples from a 2D Gaussian PDF with\nunit variance, zero mean and no dependence", n)
62 set title tstring(nsamp)
67 set cntrparam levels discrete 0.1
77 splot u,v,( 1/(2*pi) * exp(-0.5 * (u**2 + v**2)) ) with line lc rgb "black", \
78 "random.tmp" using 1:2:(-0.2) with points pointtype 7 lc rgb "black"
79 pause -1 "Hit return to continue"
85 print "Simple Monte Carlo simulation"
87 print "The first curve is a histogram where the binned frequency of occurence"
88 print "of a pseudo random variable distributed according to the normal"
89 print "(Gaussian) law is scaled such that the histogram converges to the"
90 print "normal probability density function with increasing number of samples"
91 print "used in the Monte Carlo simulation. The second curve is the normal"
92 print "probability density function with unit variance and zero mean."
98 scale = (binwidth/(xhigh-xlow))
99 # A somewhat inelegant way of generating N random data points. A future
100 # non-pressing plot-command feature addition may address this issue.
104 set table "random.tmp"
105 plot invnorm(rand(0)),(1.0*scale/nsamp)
111 tstring(n) = sprintf("Histogram of %d random samples from a univariate\nGaussian PDF with unit variance and zero mean", n)
112 set title tstring(nsamp)
117 bin(x) = (1.0/scale)*floor(x*scale)
118 plot "random.tmp" using (bin($1)):2 smooth frequency with steps \
119 title "scaled bin frequency", \
120 normal(x,0,1) with lines title "Gaussian p.d.f."
121 pause -1 "Hit return to continue"
124 print "Another Monte Carlo simulation"
126 print "This is similar to the previous simulation but uses multivariate"
127 print "zero mean, unit variance normal data by computing the distance "
128 print "each point is from the origin. That distribution is known to fit"
129 print "the Maxwell probability law, as shown."
132 # A somewhat inelegant way of generating N random data points. A future
133 # non-pressing plot-command feature addition may address this issue.
137 set isosamples 2,2 # Smallest possible
139 set table "random.tmp"
140 splot invnorm(rand(0)),invnorm(rand(0)),invnorm(rand(0))
146 if (oneplot) set multiplot layout 1,2
151 set xrange [rlow:rhigh]; set yrange [rlow:rhigh]; set zrange [rlow:rhigh]
152 set xtics axis nomirror; set ytics axis nomirror; set ztics axis nomirror;
158 set view 68, 28, 1.4, 0.9
159 tstring(n) = sprintf("Gaussian 3D cloud of %d random samples\n", n)
160 set title tstring(nsamp) offset graph 0.15, graph -0.33
161 splot "random.tmp" every :::::0 with dots
162 if (!oneplot) pause -1 "Hit return to continue"
165 unset xzeroaxis; unset yzeroaxis;
171 tstring(n) = sprintf("Histogram of distance from origin of\n%d multivariate unit variance samples", n)
172 set title tstring(nsamp) offset graph 0, graph 0.15
173 set key bmargin right vertical
177 scale = (binwidth/(xhigh-xlow))
180 bin(x) = (1.0/scale)*floor(x*scale)
181 plot "random.tmp" using (bin(sqrt($1**2+$2**2+$3**2))):(1.0*scale/nsamp) every :::::0 smooth frequency with steps \
182 title "scaled bin frequency", \
183 maxwell(x, 1/sqrt(2)) with lines title "Maxwell p.d.f."
185 if (oneplot) unset multiplot
187 pause -1 "Hit return to continue"
196 set tics out scale 0.5
197 set xtics 1.0 border rangelimited nomirror
198 set ytics 1.0 border rangelimited nomirror
202 set title 'Example of range-limited axes and tics'
205 plot 'random.tmp' using (1.5 + $1*cos(A)-2.*$2*sin(A)):(1.0 + $1*sin(A)+2.*$2*cos(A)) with dots
207 pause -1 "Hit return to continue"