📄 stocc.htm
字号:
<br>
If you want to call the same stochastic functions from more than one thread
in a multi-threaded program then you have to make these functions re-entrant. There are two ways to do
this:<br>
<br>
Method 1. Find all static local variables in the non-uniform random variate
generating functions in stoc1.cpp and stoc3.cpp. Make these variables non-static and make the setup in these
functions non-conditional. Don't use <code>StochasticLib2</code> and
stoc2.cpp.<br>
<br>
Method 2. Find all static local variables in the non-uniform random variate
generating functions in stoc1.cpp, stoc2.cpp and stoc3.cpp. Make all these variables
non-static members of the respective class. Those variables that need to be
initialized to <code>-1</code> (names ending in <code>_last</code>) must be so in the constructor.<br>
<br>
Whether you use method 1 or 2, you have to give each thread its own instance
of the class and make sure they don't have the same seed. A difference of 1
in the seeds is sufficient.</li>
<li>See <a href="randomc.htm">randomc.htm</a> for more portability issues.</li>
</ol>
<h2>Function descriptions</h2>
<dl>
<dt>int Bernoulli(double p)</dt>
<dd>Bernoulli distribution with probability parameter p.<br>
Returns 1 with probability p, or 0 with probability 1-p.<br>
Error conditions:<br>
Gives error message if p < 0 or p > 1.</dd>
<dt>double Normal(double m, double s)</dt>
<dd>Normal distribution with mean m and standard deviation s.<br>
This distribution simulates the sum of many random factors.<br>
Definition:<br>
f(x) = (2*pi*s<SUP>2</SUP>)<sup>-0.5</sup>*exp(-0.5*s<sup>-2</sup>*(x-m)<sup>2</sup>)<br>
Error conditions:<br>
None.</dd>
<dt>long int Poisson (double L)</dt>
<dd>Poisson distribution with mean L.<br>
This is the distribution of the number of events in a given time span or a
given geographical area when these events are randomly scattered in time or
space.<br>
Definition:<br>
f(x) = L<sup>x</sup>/x! * exp(-L)<br>
Error conditions:<br>
Gives error message if L < 0 or L > 2*10<sup>9</sup>.</dd>
<dt>long int Binomial (long int n, double p)</dt>
<dd>Binomial distribution with parameters n and p.<br>
This is the distribution of the number of red balls you get when drawing n
balls <i>with replacement</i> from an urn where p is the fraction of red
balls in the urn.<br>
Definition:<br>
f(x) = B(n,x) * p<sup>x </sup>* (1-p)<sup>n-x</sup>,<br>
where the binomial coefficient B(a,b) = a! / (b! * (a-b)!).<br>
Error conditions:<br>
Gives error message if n < 0 or p < 0 or p > 1.</dd>
<dt>long int Hypergeometric (long int n, long int m, long int N)</dt>
<dd>Hypergeometric distribution with parameters n, m, N. (Note the order of
the parameters).<br>
This is the distribution of the number of red balls you get when drawing n
balls <i>without replacement</i> from an urn where the urn contains N balls,
where m balls are red and N-m balls are white.<br>
Definition:<br>
f(x) = B(m,x) * B(N-m,n-x) / B(N,n),<br>
where the binomial coefficient B(a,b) = a! / (b! * (a-b)!).<br>
Error conditions:<br>
Gives error message if any parameter is negative or n > N or m > N.</dd>
<dt>long int WalleniusNCHyp(long int n, long int m, long int N, double
odds)</dt>
<dd>The Wallenius noncentral hypergeometric distribution is the same as the hypergeometric distribution, but with bias. The bias can be seen
as an odds ratio. A bias > 1 will favor the red balls, a bias < 1 will
favor the white balls.<br>
When bias = 1 we have the hypergeometric distribution.<br>
Error conditions:<br>
Gives error message if any parameter is negative or n > N or m > N.</dd>
<dt>long int FishersNCHyp(long int n, long int m, long int N, double
odds)</dt>
<dd>The Fisher's noncentral hypergeometric distribution is a conditional binomial
distribution resembling the Wallenius noncentral hypergeometric distribution. See the
file <A HREF="distrib.pdf">distrib.pdf</A> for a definition. Execution may
be slow and inexact when N is high and bias is far from 1.<br>
Error conditions:<br>
Gives error message if any parameter is negative or n > N or m > N.</dd>
<dt>void Multinomial (long int * destination, double * source, long int n, int colors)<br>
void Multinomial (long int * destination, long int * source, long int n, int colors)</dt>
<dd>Multivariate binomial distribution.<br>
This is the distribution you get when drawing n balls from an urn <i>with
replacement</i>, where there can be any number of colors. This is the same
as the binomial distribution when <code>colors</code> = 2.<br>
The number of balls of each color is returned in <code>destination</code>,
which must be an array with <code>colors</code> places. <code>source</code>
contains the number or fraction of balls of each color in the urn. <code>source</code>
must be a <code>double</code> or <code>long int</code> array with <code>colors</code> places.<br>
The sum of the values in <code>source</code> does not have to be 1, but it
must be positive. The probability that a ball has color <code>i</code>
is <code>source[i]</code> divided by the sum of all values in <code>source</code>.<br>
Error conditions:<br>
Gives an error message if any parameter is negative or if the sum of the
values in <code>source</code> is zero. The behavior is unpredictable if <code>source</code>
or <code>destination</code> has less than <code>colors</code> places.</dd>
<dt>void MultiHypergeo (long int * destination, long int * source, long int n, int colors)</dt>
<dd>Multivariate hypergeometric distribution.<br>
This is the distribution you get when drawing n balls from an urn <i>without
replacement</i>, where there can be any number of colors. This is the same
as the hypergeometric distribution when <code>colors</code> = 2.<br>
The number of balls of each color is returned in <code>destination</code>,
which must be an array with <code>colors</code> places. <code>source</code>
contains the number of balls of each color in the urn. <code>source</code>
must be an array with <code>colors</code> places.<br>
Error conditions:<br>
Gives an error message if any parameter is negative or if the sum of the
values in <code>source</code> is less than n. The behavior is unpredictable
if <code>source</code> or <code>destination</code> has less than <code>colors</code>
places.</dd>
<dt>void MultiWalleniusNCHyp(long int * destination, long int * source, double * weights, long int n, int
colors)</dt>
<dd>Multivariate Wallenius noncentral hypergeometric distribution. This is the
distribution you get when drawing colored balls from un urn <i>without
replacement</i>, <i>with bias</i>. <code>weights</code>
is an array with <code>colors</code> places containing the odds for each
color. The probability of drawing a particular ball is proportional to its
weight. The other parameters are the same as above.<BR>
This function may be inexact, but uses an approximation with an accuracy that
is better than 1% in almost all cases.<br>
Error conditions:<br>
Gives an error message if any parameter is negative or if the total number
of balls with nonzero weight is less than n. The behavior is unpredictable
if any of the arrays has less than <code>colors</code>
places.</dd>
<dt>void MultiFishersNCHyp(long int * destination, long int * source, double * weights, long int n, int
colors)</dt>
<dd>The multivariate Fisher's noncentral hypergeometric distribution is a conditional
binomial distribution resembling the multivariate Wallenius noncentral hypergeometric
distribution. See the file <A HREF="distrib.pdf">distrib.pdf</A> for a
definition.<br>
This function may be inexact, but uses an approximation with an accuracy that
is better than 1% in most cases. The precision can be tuned at the expense
of higher calculation times.<br>
Error conditions:<br>
Gives an error message if any parameter is negative or if the total number
of balls with nonzero weight is less than n. The behavior is unpredictable
if any of the arrays has less than <code>colors</code>
places.</dd>
<dt>void Shuffle(int * list, int min, int n)</dt>
<dd>This function makes a list of the n numbers from <code>min</code>
to <code>min+n-1</code> in random order.<br>
The result is returned in <code>list</code>, which must be an array with n elements.<br>
The array index goes from <code> 0</code> to <code>n-1</code>.<br>
If you want to shuffle something else than integers then use the integers in
<code> list</code> as an index into a table of the items you want to shuffle.<br>
Error conditions: none. The behavior is unpredictable if the size of the
array <code>list</code> is less than <code>n</code>.</dd>
<dt>static double LnFac(long int n)</dt>
<dd>Log factorial. Mainly used internally.<br>
Definition:<br>
LnFac(n) = log<sub>e</sub>(n!).<br>
This function uses a table when n < 1024 and Stirling's approximation for
larger n. The table is generated by the constructor.<br>
Error conditions:<br>
Gives an error message if n < 0.</dd>
</dl>
<h2>Theory</h2>
<p>These distributions are defined in the file <A HREF="distrib.pdf">distrib.pdf</A>.
The methods used for generating variates with these distributions are described
in the file <A HREF="sampmet.pdf">sampmet.pdf</A>.</p>
<p>A theoretical description of the univariate and multivariate Wallenius and
Fisher's noncentral hypergeometric distributions, including calculation methods and
sampling methods, is given in <A HREF="http://www.agner.org/random/theory/nchyp.pdf">nchyp.pdf</A>.</p>
<p>Examples showing how to simulate biological evolution using this class
library can be found in <a href="evolc.zip">evolc.zip</a>.</p>
<H2>General public license</H2>
<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -