Initial release of Maemo 5 port of gnuplot
[gnuplot] / demo / random.dem
1 #
2 # $Id: random.dem,v 1.12.2.2 2008/09/23 23:11:31 sfeam Exp $
3 #
4 # random.dem
5 #
6 # Lattice test for random numbers;
7 # If you can see any patterns in this plot, the random number generator
8 # is not very good.
9 #
10 # Copyright (c) 1991, Jos van der Woude, jvdwoude@hut.nl
11
12 # History:
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
16
17 unset key
18 set xrange [0: 1]
19 set yrange [0: 1]
20 set zrange [0: 1]
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) ->"
25 set format x "%3.2f"
26 set format y "%3.2f"
27 set format z "%3.2f"
28 set tics
29 set sample 1000
30 set style function dots
31 set parametric
32 plot rand(0), rand(0)
33 pause -1 "Hit return to continue"
34 print "3D plot ahead, one moment please ..."
35 set sample 50
36 splot rand(0), rand(0), rand(0)
37 pause -1 "Hit return to continue"
38
39 print ""
40 print "Multivariate normal distribution"
41 print ""
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.)"
46 print ""
47 nsamp = 50
48 unset xlabel
49 unset ylabel
50 unset zlabel
51 set parametric
52 # A somewhat inelegant way of generating N random data points.  A future
53 # non-pressing plot-command feature addition may address this issue.
54 set samples nsamp
55 set format "%8.5g"
56 set table "random.tmp"
57 plot invnorm(rand(0)),invnorm(rand(0))
58 unset table
59 unset format
60 #
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)
63 unset key
64 set hidden3d
65 set contour
66 set view 68, 28, 1, 1
67 set cntrparam levels discrete 0.1
68 unset clabel
69 set xrange [-3:3]
70 set yrange [-3:3]
71 set zrange [-0.2:0.2]
72 set ztics 0,0.05
73 set urange [-3:3]
74 set vrange [-3:3]
75 set ticslevel 0
76 set isosamples 30
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"
80 unset contour
81
82 load "stat.inc"
83
84 print ""
85 print "Simple Monte Carlo simulation"
86 print ""
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."
93 print ""
94 nsamp = 5000
95 binwidth = 20
96 xlow = -3.0
97 xhigh = 3.0
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.
101 set parametric
102 set samples nsamp
103 set format "%8.5g"
104 set table "random.tmp"
105 plot invnorm(rand(0)),(1.0*scale/nsamp)
106 unset table
107 unset format
108 #
109 unset parametric
110 set samples 200
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)
113 set key
114 set grid
115 set xrange [-3:3]
116 set yrange [0:0.45]
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"
122
123 print ""
124 print "Another Monte Carlo simulation"
125 print ""
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."
130 print ""
131 reset
132 # A somewhat inelegant way of generating N random data points.  A future
133 # non-pressing plot-command feature addition may address this issue.
134 nsamp = 3000
135 set parametric
136 set samples nsamp
137 set isosamples 2,2 # Smallest possible
138 set format "%8.5g"
139 set table "random.tmp"
140 splot invnorm(rand(0)),invnorm(rand(0)),invnorm(rand(0))
141 unset table
142 unset format
143 #
144 oneplot = 1
145 #
146 if (oneplot) set multiplot layout 1,2
147 #
148 unset key
149 rlow = -4.0
150 rhigh = 4.0
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;
153 set border 0
154 set xyplane at 0
155 set xzeroaxis lt -1
156 set yzeroaxis lt -1
157 set zzeroaxis lt -1
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"
163
164 unset parametric
165 unset xzeroaxis; unset yzeroaxis;
166 set border
167 set grid
168 set samples 200
169 set size 0.47,0.72
170 set origin 0.44,0.18
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
174 xlow = 0.0
175 xhigh = 4.5
176 binwidth = 20
177 scale = (binwidth/(xhigh-xlow))
178 set xrange [0:xhigh]
179 set yrange [0:0.65]
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."
184 #
185 if (oneplot) unset multiplot
186 #
187 pause -1 "Hit return to continue"
188
189 unset key
190 unset grid
191 set xrange [-8:8]
192 set yrange [-8:8]
193 set size ratio 1.0
194 set zeroaxis
195 set border 3
196 set tics out scale 0.5
197 set xtics 1.0 border rangelimited nomirror
198 set ytics 1.0 border rangelimited nomirror
199 set tics scale 0.5
200 set format xy "%.0f"
201
202 set title 'Example of range-limited axes and tics'
203
204 A = pi/9.
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
206
207 pause -1 "Hit return to continue"
208
209