/usr/share/doc/pyxplot/html/sect0030.html is in pyxplot-doc 0.8.4-3.
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 | <!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="sect0031.html" title="Programming and Flow Control" 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="sect0031.html" title="Programming and Flow Control"><img alt="Next: Programming and Flow Control" 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="sect0255.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="a0000000031">5.10 Random Data Generation</h1>
<p>PyXPlot has a range of mathematical functions which draw random samples from a variety of probability distributions. These are: </p><ul class="itemize">
<li><p><tt class="tt">random()</tt> – returns a random real number between 0 and 1. <a name="a0000000558" id="a0000000558"></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-0014.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="a0000000559" id="a0000000559"></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="a0000000560" id="a0000000560"></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 upon zero. <a name="a0000000561" id="a0000000561"></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="a0000000562" id="a0000000562"></a> </p></li><li><p><tt class="tt">random_poisson(<img src="images/img-0014.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-0014.png" alt="$n$" style="vertical-align:0px;
width:11px;
height:8px" class="math gen" />. <a name="a0000000563" id="a0000000563"></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-0056.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="a0000000564" id="a0000000564"></a> </p></li>
</ul><p>These functions all rely upon a common underlying random number generator, whose seed may be set using the <tt class="tt">set seed</tt> command<a name="a0000000565" id="a0000000565"></a>, which should be followed by any integer. </p><p> <span class="upshape"><span class="mdseries"><span class="rm">Using random numbers to estimate the value of <img src="images/img-0136.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-0136.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 href="ex-meansd.html">6.5</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="tt">Nsamples = 5000 </tt><br /><tt class="tt"></tt><br /><tt class="tt">set samp Nsamples </tt><br /><tt class="tt">set output "random.dat" </tt><br /><tt class="tt">tabulate 1-2*random():1-2*random() using 0:2:3 </tt><br /><tt class="tt"></tt><br /><tt class="tt">n=0 </tt><br /><tt class="tt">foreach datum i,j in ’random.dat’ using 2:3 { </tt><br /><tt class="tt">n = n + (hypot(i,j)<1) </tt><br /><tt class="tt">} </tt><br /><tt class="tt"></tt><br /><tt class="tt">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-0136.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-0136.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 datapoints stored in the datafile <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><tt class="tt">set multiplot ; set nodisplay</tt><br /><tt class="tt"># Draw a unit circle and a unit square</tt><br /><tt class="tt">box from -width/2,-width/2 to width/2,width/2</tt><br /><tt class="tt">circle at 0,0 radius width/2 with lt 2</tt><br /><tt class="tt"></tt><br /><tt class="tt"># Now plot the positions of the random data points</tt><br /><tt class="tt">n=0</tt><br /><tt class="tt">foreach datum i,j in ’random.dat’ using 2:3</tt><br /><tt class="tt">{</tt><br /><tt class="tt">point at width/2*i , width/2*j with ps 0.1</tt><br /><tt class="tt">n = n + (hypot(i,j)<1)</tt><br /><tt class="tt">}</tt><br /><tt class="tt">set display ; refresh</tt><br /><tt class="tt"></tt><br /><tt class="tt">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>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 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="sect0031.html" title="Programming and Flow Control"><img alt="Next: Programming and Flow Control" 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="sect0255.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>
|