📄 nr02doc.htm
字号:
<p>SymGen is a modification of PosGen for unimodal distributions symmetric about theorigin, such as the standard normal. </p><h3><a NAME="asymgen"></a>AsymGen:</h3><p>A general random number generator for unimodal distributions following the method usedby PosGen. The constructor takes one argument: the location of the mode of thedistribution. </p><h3><a NAME="discretegen"></a>DiscreteGen:</h3><p>This is for generating random numbers taking just a finite number of values. There aretwo alternative forms of the constructor: </p><pre> DiscreteGen D(n,prob); DiscreteGen D(n,prob,val);</pre><p>where <tt>n</tt> is an integer giving the number of values, <tt>prob</tt> a Real arrayof length <tt>n</tt> giving the probabilities and <tt>val</tt> a Real array of length <tt>n</tt>giving the set of values that are generated. If <tt>val</tt> is omitted the values are <tt>0,1,...,n-1</tt>.</p><p>The method requires two uniform random numbers for each number it produces. This methodis described by Kronmal and Peterson, <i>American Statistician</i>, 1979, Vol 33, No 4,pp214-218. </p><h3><a NAME="sum"></a>SumRandom:</h3><p>This is for building a random number generator as a linear or multiplicativecombination of existing random number generators. Suppose <tt>RV1</tt>, <tt>RV2</tt>, <tt>RV3</tt>,<tt>RV4</tt> are random number generators defined with constructors given above and <tt>r1</tt>,<tt>r2</tt>, <tt>r0</tt> are Reals and <tt>i1</tt>, <tt>i3</tt> are integers. </p><p>Then the generator <tt>S</tt> defined by something like </p><pre> SumRandom S = RV1(i1)*r1 - RV2*r2 + RV3(i3)*RV4 + r0;</pre><p>has the obvious meaning. <tt>RV1(i1)</tt> means that the sum of <tt>i1</tt> independentvalues from <tt>RV1</tt> should be used. Note that <tt>RV1*RV1</tt> means the product oftwo independent numbers generated from <tt>RV1</tt>. Remember that <tt>SumRandom</tt> isslow if the number of terms or copies is large. I support the four arithmetic operators <tt>+</tt>,<tt>-</tt>, <tt>*</tt> and <tt>/</tt> but cannot calculate the means and variances if youdivide by a random variable. </p><p>Use <tt>SumRandom</tt> to quickly set up simple combinations of the existinggenerators. But if the combination is going to be used extensively, then it is probablybetter to write a new class to do this. </p><p>Example: <i>normal</i> with mean = 10, standard deviation = 5: </p><pre> Normal N; SumRandom Z = 10 + 5 * N; for (int i=0; i<100; i++) cout << Z.Next() << "\n";</pre><p>Example: <i>F</i> distribution with <i>m</i> and <i>n</i> degrees of freedom: </p><pre> int m, n; ... put values in m and n ChiSq Num(m); ChiSq Den(n); SumRandom F = (double)n/(double)m * Num / Den; for (int i=0; i<100; i++) cout << F.Next() << "\n";</pre><h3><a NAME="mixed"></a>MixedRandom:</h3><p>This is for mixtures of distributions. Suppose <tt>rv1</tt>, <tt>rv2</tt>, <tt>rv3</tt>are random number generators and <tt>p1</tt>, <tt>p2</tt>, <tt>p3</tt> are Reals summingto 1. Then the generator <tt>M</tt> defined by </p><pre> MixedRandom M = rv1(p1) + rv2(p2) + rv3(p3);</pre><p>produces a random number generator with selects its next random number from <tt>rv1</tt>with probability <tt>p1</tt>, <tt>rv2</tt> with probability <tt>p2</tt>, <tt>rv3</tt> withprobability <tt>p3</tt>. </p><p>Alternatively one can use the constructor </p><pre> MixedRandom M(n, prob, rv);</pre><p>where <tt>n</tt> is the number of distributions in the mixture, <tt>prob</tt> the Realarray of probabilities, <tt>rv</tt> an array of pointers to random variables. </p><p>Normal with outliers: </p><pre> Normal N; Cauchy C; MixedRandom Z = N(0.9) + C(0.1); for (int i=0; i<100; i++) cout << Z.Next() << "\n";</pre><p>or: </p><pre> Normal N; MixedRandom Z = N(0.9) + (10*N)(0.1); for (int i=0; i<100; i++) cout << Z.Next() << "\n";</pre><h3><a NAME="permutation"></a>RandomPermutation:</h3><p>To draw <tt>M</tt> numbers without replacement from <tt>start, start+1, ..., start+N-1</tt>use </p><pre> RandomPermutation RP; RP.Next(N, M, p, start);</pre><p>where <tt>p</tt> is an <tt>int*</tt> pointing to an array of length <tt>M</tt> orlonger. Results are returned to that array. </p><pre> RP.Next(N, p, start);</pre><p>assumes <tt>M = N</tt>. The parameter, <tt>start</tt>has a default value of 0. </p><p>The method is rather inefficient if <tt>N</tt> is very large and <tt>M</tt> is muchsmaller. </p><h3><a NAME="combination"></a>RandomCombination:</h3><p>To draw <tt>M</tt> numbers without replacement from <tt>start, start+1, ..., start+N-1</tt>and then sort use </p><pre> RandomCombination RC; RC.Next(N, M, p, start);</pre><p>where <tt>p</tt> is an <tt>int*</tt> pointing to an array of length <tt>M</tt> orlonger. Results are returned to that array. </p><pre> RC.Next(N, p, start);</pre><p>assumes <tt>M = N</tt>. The parameter, <tt>start</tt>has a default value of 0. </p><p>The method is rather inefficient if <tt>N</tt> is large. A better approach for large <tt>N</tt>would be to generate the sorted combination directly. This would also provide a better wayof doing permutations with large <tt>N</tt>, small <tt>M</tt>.</p><h3><a name="VariPoisson"></a>VariPoisson</h3><p>Use this class if you want to generate a Poisson random variable but you want to change the parameter frequently, so using the Poisson class would be inefficient. There is one member function</p><pre> int VariPoisson::iNext(Real mu);</pre><p>which returns a new Poisson random number with mean <TT>mu</TT>. To generate 100 Poisson random numbers with means 1,2,...,100 use the following program</p><pre> VariPoisson VP; for (int i = 1; i <= 100; ++i) { Real mu = i; cout << VP.iNext(mu) << end; }</pre><p>The constructor is slow so put it outside any loop. The individual calls to <TT>iNext</TT> should be quite fast. The method is approximate for <TT>mu >= 300.</TT> The constructor is not in any class hierarchy and <TT>iNext</TT> is not virtual. This class is somewhat beta-ish and may change in a future release of <i>newran</i>.</p><h3><a name="VariBinomial"></a>VariBinomial</h3><p>Use this class if you want to generate a Binomial random variable but you want to change the parameters of the frequently, so using the Binomial class would be inefficient. There is one member function</p><pre> int VariBinomial::iNext(int n, Real p);</pre><p>which returns a new Binomial random number with number of trials <tt>n</tt> and probability of success <tt>p</tt>. To generate 100 Binomial random numbers with <tt>n</tt> = 1,2,...,100 and <tt>p</tt> = 0.5 use the following program</p><pre> VariBinomial VB; for (int n = 1; n <= 100; ++n) { Real p = 0.5; cout << VB.iNext(n, p) << end; }</pre><p>The constructor is slow so put it outside any loop. The individual calls to <TT>iNext</TT> should be quite fast. The method is approximate if both <TT>n*p > 200</tt> and <tt>n*(1-p) > 200.</TT> The constructor is not in any class hierarchy and <TT>iNext</TT> is not virtual. This class is somewhat beta-ish and may change in a future release of <i>newran</i>.</p><h3><a name="VariLogNormal"></a>VariLogNormal</h3><p>Use this class if you want to generate a log normal random variable and you want to change the parameters of the frequently. There is one member function</p><pre> Real VariLogNormal::Next(Real mean, Real sd);</pre><p>which returns a new log normal random number with mean <tt>mean</tt> and standard deviation <tt>sd</tt>. Note that <tt>mean</tt> and <tt>sd</tt> are the mean and standard deviation of the log normal distribution and not of the underlying normal distribution. To generate 100 log normal random numbers with <tt>mean</tt> = 1,2,...,100 and <tt>sd</tt> = 1.0 use the following program</p><pre> VariLogNormal VLN; for (int i = 1; i <= 100; ++i) { Real mean = i; Real sd = 1.0; cout << VLN.Next(mean, sd) << end; }</pre><p>The constructor is not in any class hierarchy and <TT>Next</TT> is not virtual. This class is somewhat beta-ish and may change in a future release of <i>newran</i>.</p><h3><a NAME="extreal"></a>ExtReal</h3><p>A class consisting of a Real and an enumeration, <tt>EXT_REAL_CODE</tt>, taking thefollowing values: <ul> <li>Finite</li> <li>PlusInfinity</li> <li>MinusInfinity</li> <li>Indefinite</li> <li>Missing</li></ul><p>The arithmetic functions <tt>+</tt>, <tt>-</tt>, <tt>*</tt>, <tt>/</tt> are defined inthe obvious ways, as is <tt><<</tt> for printing. The constructor can take either aReal or a value of <tt>EXT_REAL_CODE</tt> as an argument. If there is no argument theobject is given the value <i>Missing</i>. Member function <tt>IsReal()</tt> returns <i>true</i>if the enumeration value is <i>Finite</i> and in this case value of the Real can be foundwith <tt>Value()</tt>. The enumeration value can be found with member function <tt>Code()</tt>.</p><p><i>ExtReal</i> is used at the type for values returned from the <i>Mean</i> and <i>Variance</i>member functions since these values may be infinite, indefinite or missing. </p><p> </p><h2><a NAME="supporting"></a>Descriptions of the supporting classes:</h2><h3>ChiSq1:</h3><p>Non-central chi-squared with one degree of freedom. Used as part of ChiSq. </p><h3>Gamma1:</h3><p>This generates random numbers from a gamma distribution with shape parameter <tt>alpha< 1</tt>. Because the density is infinite at <i>x</i> = 0 a power transform isrequired. The constructor takes <tt>alpha</tt> as an argument. </p><h3>Gamma2:</h3><p>Gamma distribution for the shape parameter, <tt>alpha</tt>, greater than 1. Theconstructor takes <tt>alpha</tt> as the argument. </p><h3>Poisson1:</h3><p>Poisson distribution; derived from AsymGen. The constructor takes the mean as theargument. Used by Poisson for values of the mean greater than 10. </p><h3>Poisson2:</h3><p>Poisson distribution with mean less than or equal to 10. Uses DiscreteGen. Constructortakes the mean as its argument. </p><h3>Binomial1:</h3><p>Binomial distribution; derived from AsymGen. Used by Binomial for <tt>n >= 40</tt>.Constructor takes <i>n</i> and <i>p</i> as arguments. </p><h3>Binomial2:</h3><p>Binomial distribution with <tt>n < 40</tt>. Uses DiscreteGen. Constructor takes <i>n</i>and <i>p</i> as arguments. </p><h3>AddedRandom, SubtractedRandom, MultipliedRandom, ShiftedRandom,ReverseShiftedRandom, ScaledRandom, RepeatedRandom, SelectedRandom, AddedSelectedRandom:</h3><p>These are used by SumRandom and MixedRandom. </p><p> </p><h2><a NAME="generating"></a>Generating numbers from otherdistributions:</h2><table BORDER="0" CELLSPACING="4" CELLPADDING="4"> <tr> <th ALIGN="LEFT" VALIGN="TOP"><b>Distribution type</b></th> <th ALIGN="LEFT" VALIGN="TOP"><b>Method</b></th> <th ALIGN="LEFT" VALIGN="TOP"><b>Example</b></th> </tr> <tr> <td VALIGN="TOP">Continuous finite unimodal density (no parameters, can calculate density)</td> <td VALIGN="TOP">Use <a HREF="#posgenx">PosGenX</a>, <a HREF="#symgenx">SymGenX</a> or <a HREF="#asymgenx">AsymGenX</a>.</td> <td VALIGN="TOP"></td> </tr> <tr> <td VALIGN="TOP">Continuous finite unimodal density (with parameters, can calculate density)</td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -