/usr/share/doc/pyxplot/html/sect0035.html is in pyxplot-doc 0.9.2-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="generator" content="plasTeX" />
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<title>PyXPlot Users' Guide: Random data generation</title>
<link href="chap-progDataTypes.html" title="Programming: Pyxplot’s data types" rel="next" />
<link href="sec-histogram.html" title="Histograms" rel="prev" />
<link href="ch-numerics.html" title="Working with data" rel="up" />
<link rel="stylesheet" href="styles/styles.css" />
</head>
<body>
<div class="navigation">
<table cellspacing="2" cellpadding="0" width="100%">
<tr>
<td><a href="sec-histogram.html" title="Histograms"><img alt="Previous: Histograms" border="0" src="icons/previous.gif" width="32" height="32" /></a></td>
<td><a href="ch-numerics.html" title="Working with data"><img alt="Up: Working with data" border="0" src="icons/up.gif" width="32" height="32" /></a></td>
<td><a href="chap-progDataTypes.html" title="Programming: Pyxplot’s data types"><img alt="Next: Programming: Pyxplot’s data types" border="0" src="icons/next.gif" width="32" height="32" /></a></td>
<td class="navtitle" align="center">PyXPlot Users' Guide</td>
<td><a href="index.html" title="Table of Contents"><img border="0" alt="" src="icons/contents.gif" width="32" height="32" /></a></td>
<td><a href="sect0288.html" title="Index"><img border="0" alt="" src="icons/index.gif" width="32" height="32" /></a></td>
<td><img border="0" alt="" src="icons/blank.gif" width="32" height="32" /></td>
</tr>
</table>
</div>
<div class="breadcrumbs">
<span>
<span>
<a href="index.html">PyXPlot Users' Guide</a> <b>:</b>
</span>
</span><span>
<span>
<a href="sect0001.html">Introduction to PyXPlot</a> <b>:</b>
</span>
</span><span>
<span>
<a href="ch-numerics.html">Working with data</a> <b>:</b>
</span>
</span><span>
<span>
<b class="current">Random data generation</b>
</span>
</span>
<hr />
</div>
<div><h1 id="a0000000036">5.10 Random data generation</h1>
<p>Pyxplot has functions for generating random numbers from a variety of common probability distributions. These functions are in the <tt class="tt">random</tt> module: </p><ul class="itemize">
<li><p><tt class="tt">random.random()</tt> – returns a random real number between 0 and 1. <a name="a0000000593" id="a0000000593"></a> </p></li><li><p><tt class="tt">random.binomial(<img src="images/img-0269.png" alt="$p,n$" style="vertical-align:-4px;
width:29px;
height:12px" class="math gen" />)</tt> – returns a random sample from a binomial distribution with <img src="images/img-0025.png" alt="$n$" style="vertical-align:0px;
width:11px;
height:8px" class="math gen" /> independent trials and a success probability <img src="images/img-0270.png" alt="$p$" style="vertical-align:-4px;
width:10px;
height:12px" class="math gen" />. <a name="a0000000594" id="a0000000594"></a> </p></li><li><p><tt class="tt">random.chisq(<img src="images/img-0271.png" alt="$\nu $" style="vertical-align:0px;
width:9px;
height:8px" class="math gen" />)</tt> – returns a random sample from a <img src="images/img-0272.png" alt="$\chi $" style="vertical-align:-4px;
width:11px;
height:12px" class="math gen" />-squared distribution with <img src="images/img-0271.png" alt="$\nu $" style="vertical-align:0px;
width:9px;
height:8px" class="math gen" /> degrees of freedom. <a name="a0000000595" id="a0000000595"></a> </p></li><li><p><tt class="tt">random.gaussian(<img src="images/img-0273.png" alt="$\sigma $" style="vertical-align:0px;
width:11px;
height:8px" class="math gen" />)</tt> – returns a random sample from a Gaussian (normal) distribution of standard deviation <img src="images/img-0273.png" alt="$\sigma $" style="vertical-align:0px;
width:11px;
height:8px" class="math gen" /> and centred on zero. <a name="a0000000596" id="a0000000596"></a> </p></li><li><p><tt class="tt">random.lognormal(<img src="images/img-0274.png" alt="$\zeta ,\sigma $" style="vertical-align:-4px;
width:28px;
height:17px" class="math gen" />)</tt> – returns a random sample from the log normal distribution centred on <img src="images/img-0275.png" alt="$\zeta $" style="vertical-align:-4px;
width:9px;
height:17px" class="math gen" />, and of width <img src="images/img-0273.png" alt="$\sigma $" style="vertical-align:0px;
width:11px;
height:8px" class="math gen" />. <a name="a0000000597" id="a0000000597"></a> </p></li><li><p><tt class="tt">random.poisson(<img src="images/img-0025.png" alt="$n$" style="vertical-align:0px;
width:11px;
height:8px" class="math gen" />)</tt> – returns a random integer from a Poisson distribution with mean <img src="images/img-0025.png" alt="$n$" style="vertical-align:0px;
width:11px;
height:8px" class="math gen" />. <a name="a0000000598" id="a0000000598"></a> </p></li><li><p><tt class="tt">random.tdist(<img src="images/img-0271.png" alt="$\nu $" style="vertical-align:0px;
width:9px;
height:8px" class="math gen" />)</tt> – returns a random sample from a <img src="images/img-0065.png" alt="$t$" style="vertical-align:0px;
width:6px;
height:12px" class="math gen" />-distribution with <img src="images/img-0271.png" alt="$\nu $" style="vertical-align:0px;
width:9px;
height:8px" class="math gen" /> degrees of freedom. <a name="a0000000599" id="a0000000599"></a> </p></li>
</ul><p>These functions all rely on a common underlying random number generator<a href="#a0000000600" class="footnote"><sup class="footnotemark">1</sup></a>, whose seed may be set using the <tt class="tt">set seed</tt> command<a name="a0000000601" id="a0000000601"></a>, which should be followed by any integer. The sequence of random samples generated is always the same after setting any particular seed. </p><p>When Pyxplot starts, the seed is implicitly set to zero. <b class="bf">This means that Pyxplot always produces the same series of random numbers when restarted.</b> This series can be reproduced by typing: </p><pre>
set seed 0
</pre><p>For applications where this repeatability is undesirable, the following command may help, using the system clock as a random seed: </p><pre>
set seed time.now().toUnix()
</pre><p>This gives a different sequence of random numbers each second. However, the user is advised to consider carefully whether this is sufficient for the particular application being implemented. </p><p> <span class="upshape"><span class="mdseries"><span class="rm">Using random numbers to estimate the value of <img src="images/img-0140.png" alt="$\pi $" style="vertical-align:0px;
width:11px;
height:8px" class="math gen" />.</span></span></span></p><div>
<table cellspacing="0" class="tabular">
<tr>
<td style="border-top-style:solid; border-left:1px solid black; border-right:1px solid black; border-top-color:black; border-top-width:1px; text-align:left"><p> Pyxplot’s functions for generating random numbers are most commonly used for adding noise to artificially-generated data. In this example, however, we use them to implement a rather inefficient algorithm for estimating the value of the mathematical constant <img src="images/img-0140.png" alt="$\pi $" style="vertical-align:0px;
width:11px;
height:8px" class="math gen" />. The algorithm works by spreading randomly-placed samples in the square <img src="images/img-0276.png" alt="$\left\{ -1<x<1;\; -1<y<1\right\} $" style="vertical-align:-5px;
width:210px;
height:19px" class="math gen" />. The number of these which lie within the circle of unit radius about the origin are then counted. Since the square has an area of <img src="images/img-0277.png" alt="$4\, \mathrm{unit}^2$" style="vertical-align:0px;
width:51px;
height:16px" class="math gen" /> and the circle an area of <img src="images/img-0278.png" alt="$\pi \, \mathrm{unit}^2$" style="vertical-align:0px;
width:53px;
height:16px" class="math gen" />, the fraction of the points which lie within the unit circle equals the ratio of these two areas: <img src="images/img-0279.png" alt="$\pi /4$" style="vertical-align:-5px;
width:29px;
height:18px" class="math gen" />. </p></td>
</tr><tr>
<td style="text-align:left; border-right:1px solid black; border-left:1px solid black"><p>The following script performs this calculation using <img src="images/img-0280.png" alt="$N=5000$" style="vertical-align:0px;
width:76px;
height:13px" class="math gen" /> randomly placed samples. Firstly, the positions of the random samples are generated using the <tt class="tt">random()</tt> function, and written to a file called <tt class="tt">random.dat</tt> using the <tt class="tt">tabulate</tt> command. Then, the <tt class="tt">foreach datum</tt> command – which will be introduced in Section <a></a> – is used to loop over these, counting how many lie within the unit circle. </p></td>
</tr><tr>
<td style="text-align:left; border-right:1px solid black; border-left:1px solid black"><p><tt class="ttfamily">Nsamples = 500</tt><br /></p></td>
</tr><tr>
<td style="text-align:left; border-right:1px solid black; border-left:1px solid black"><p><tt class="ttfamily">rand() = random.random()</tt><br /></p></td>
</tr><tr>
<td style="text-align:left; border-right:1px solid black; border-left:1px solid black"><p><tt class="ttfamily">set samp Nsamples</tt><br /><tt class="ttfamily">set output "pi_estimation.dat"</tt><br /><tt class="ttfamily">tabulate 1-2*rand():1-2*rand() using 0:2:3</tt><br /></p></td>
</tr><tr>
<td style="text-align:left; border-right:1px solid black; border-left:1px solid black"><p><tt class="ttfamily">n=0</tt><br /><tt class="ttfamily">foreach datum i,j in "pi_estimation.dat" u 2:3</tt><br /><tt class="ttfamily"> {</tt><br /><tt class="ttfamily"> n = n + (hypot(i,j)<img src="images/img-0046.png" alt="$<$" style="vertical-align:0px;
width:12px;
height:11px" class="math gen" />1)</tt><br /><tt class="ttfamily"> }</tt><br /><tt class="ttfamily">print "pi=%s"%(n / Nsamples * 4)</tt> </p></td>
</tr><tr>
<td style="text-align:left; border-right:1px solid black; border-left:1px solid black"><p>On the author’s machine, this script returns a value of <img src="images/img-0281.png" alt="$3.1352$" style="vertical-align:0px;
width:50px;
height:13px" class="math gen" /> when executed using the random samples which are returned immediately after starting Pyxplot. This method of estimating <img src="images/img-0140.png" alt="$\pi $" style="vertical-align:0px;
width:11px;
height:8px" class="math gen" /> is well modelled as a Poisson process, and the uncertainty in this result can be estimated from the Poisson distribution to be <img src="images/img-0282.png" alt="$1/\sqrt {N}$" style="vertical-align:-5px;
width:49px;
height:22px" class="math gen" />. In this case, the uncertainty is <img src="images/img-0283.png" alt="$0.01$" style="vertical-align:0px;
width:31px;
height:12px" class="math gen" />, in close agreement with the deviation of the returned value of <img src="images/img-0281.png" alt="$3.1352$" style="vertical-align:0px;
width:50px;
height:13px" class="math gen" /> from more accurate measures of <img src="images/img-0140.png" alt="$\pi $" style="vertical-align:0px;
width:11px;
height:8px" class="math gen" />. </p></td>
</tr><tr>
<td style="text-align:left; border-right:1px solid black; border-left:1px solid black"><p>With a little modification, this script can be adapted to produce a diagram of the datapoints used in its calculation. Below is a modified version of the second half of the script, which loops over the data points stored in the data file <tt class="tt">random.dat</tt>. It uses Pyxplot’s vector graphics commands, which will be introduced in Chapter <a href="ch-vector_graphics.html">3</a>, to produce such a diagram: </p></td>
</tr><tr>
<td style="text-align:left; border-right:1px solid black; border-left:1px solid black"><p><small class="footnotesize"><tt class="ttfamily">set multiplot ; set nodisplay</tt><br /></small></p></td>
</tr><tr>
<td style="text-align:left; border-right:1px solid black; border-left:1px solid black"><p><small class="footnotesize"><tt class="ttfamily"># Draw a unit circle and a unit square</tt><br /><tt class="ttfamily">title = "ex_pi_estimation" ; load "fig_init.ppl"</tt><br /><tt class="ttfamily">box from -width/2,-width/2 to width/2,width/2</tt><br /><tt class="ttfamily">circle at 0,0 radius width/2 with lt 2</tt><br /></small></p></td>
</tr><tr>
<td style="text-align:left; border-right:1px solid black; border-left:1px solid black"><p><small class="footnotesize"><tt class="ttfamily"># Now plot the positions of these random data points andi</tt><br /><tt class="ttfamily"># count how many lie within a unit circle</tt><br /><tt class="ttfamily">n=0</tt><br /><tt class="ttfamily">foreach datum i,j in "pi_estimation.dat" using 2:3</tt><br /><tt class="ttfamily"> {</tt><br /><tt class="ttfamily"> point at width/2*i , width/2*j with ps 0.1</tt><br /><tt class="ttfamily"> n = n + (hypot(i,j)<img src="images/img-0046.png" alt="$<$" style="vertical-align:0px;
width:12px;
height:11px" class="math gen" />1)</tt><br /><tt class="ttfamily"> }</tt><br /><tt class="ttfamily">set display ; refresh</tt><br /><tt class="ttfamily">print "pi=%.4f"%(n / Nsamples * 4)</tt> </small> </p></td>
</tr><tr>
<td style="text-align:left; border-right:1px solid black; border-left:1px solid black"><p>The graphical output from this script is shown below. The number of datapoints has been reduced to <tt class="tt">Nsamples</tt><img src="images/img-0284.png" alt="$=500$" style="vertical-align:0px;
width:45px;
height:13px" class="math gen" /> for clarity: </p></td>
</tr><tr>
<td style="border-bottom-style:solid; border-bottom-width:1px; border-left:1px solid black; border-right:1px solid black; text-align:left; border-bottom-color:black"><center> <img src="images/img-0286.png" alt="\includegraphics{examples/eps/ex_pi_estimation}" style="width:333px; height:333px" />
</center></td>
</tr>
</table>
</div></div>
<div id="footnotes">
<p><b>Footnotes</b></p>
<ol>
<li id="a0000000600">The gsl library’s default random number generator, <tt class="tt">gsl_rng_default</tt> is used. As of version 1.15, this maps to <tt class="tt">gsl_rng_mt19937</tt> with a default seed of zero. The various probability distributions above are sampled using the functions <tt class="tt">gsl_ran_binomial</tt> and similar.</li>
</ol>
</div>
<div class="navigation">
<table cellspacing="2" cellpadding="0" width="100%">
<tr>
<td><a href="sec-histogram.html" title="Histograms"><img alt="Previous: Histograms" border="0" src="icons/previous.gif" width="32" height="32" /></a></td>
<td><a href="ch-numerics.html" title="Working with data"><img alt="Up: Working with data" border="0" src="icons/up.gif" width="32" height="32" /></a></td>
<td><a href="chap-progDataTypes.html" title="Programming: Pyxplot’s data types"><img alt="Next: Programming: Pyxplot’s data types" border="0" src="icons/next.gif" width="32" height="32" /></a></td>
<td class="navtitle" align="center">PyXPlot Users' Guide</td>
<td><a href="index.html" title="Table of Contents"><img border="0" alt="" src="icons/contents.gif" width="32" height="32" /></a></td>
<td><a href="sect0288.html" title="Index"><img border="0" alt="" src="icons/index.gif" width="32" height="32" /></a></td>
<td><img border="0" alt="" src="icons/blank.gif" width="32" height="32" /></td>
</tr>
</table>
</div>
<script language="javascript" src="icons/imgadjust.js" type="text/javascript"></script>
</body>
</html>
|