SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
DirichletGen.java
1/*
2 * Class: DirichletGen
3 * Description: multivariate generator for a Dirichlet distribution
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.randvarmulti;
26
27import umontreal.ssj.probdist.GammaDist;
28import umontreal.ssj.randvar.RandomVariateGen;
29import umontreal.ssj.randvar.GammaAcceptanceRejectionGen;
30import umontreal.ssj.rng.RandomStream;
31
57public class DirichletGen extends RandomMultivariateGen {
58 private GammaAcceptanceRejectionGen[] ggens;
59 private double[] alphas;
60
61 // use log gamma if all alphas are smaller than this threshold
62 private static final double ALPHA_THRESHOLD = 0.1;
63
64 // determine if the Dirichlet multivariate must be generated using the
65 // log gamma, when the shape parameter alphas are all small.
66 private boolean useLogGamma = false;
67
78 public DirichletGen(RandomStream stream, double[] alphas) {
79 if (stream == null)
80 throw new NullPointerException("stream is null");
81 this.stream = stream;
82 dimension = alphas.length;
83 ggens = new GammaAcceptanceRejectionGen[alphas.length];
84 for (int k = 0; k < alphas.length; k++)
85 ggens[k] = new GammaAcceptanceRejectionGen(stream, new GammaDist(alphas[k], 1.0 / 2.0));
86
87 this.alphas = new double[alphas.length];
88 System.arraycopy(alphas, 0, this.alphas, 0, alphas.length);
89 useLogGamma = canUseLogGamma(alphas);
90 }
91
101 public double getAlpha(int i) {
102 return alphas[i];
103 }
104
115 public static void nextPoint(RandomStream stream, double[] alphas, double[] p) {
116 if (canUseLogGamma(alphas)) {
117 nextPointUsingLog(stream, alphas, p);
118 return;
119 }
120
121 double total = 0;
122 for (int i = 0; i < alphas.length; i++) {
123 p[i] = GammaAcceptanceRejectionGen.nextDouble(stream, stream, alphas[i], 1.0 / 2.0);
124 total += p[i];
125 }
126 for (int i = 0; i < alphas.length; i++)
127 p[i] /= total;
128 }
129
135 public void nextPoint(double[] p) {
136 if (useLogGamma) {
137 nextPointUsingLog(stream, alphas, p);
138 return;
139 }
140
141 int n = ggens.length;
142 double total = 0;
143 for (int i = 0; i < n; i++) {
144 p[i] = ggens[i].nextDouble();
145 total += p[i];
146 }
147 for (int i = 0; i < n; i++)
148 p[i] /= total;
149 }
150
155 private static void nextPointUsingLog(RandomStream stream, double[] alphas, double[] p) {
156 double total = 0;
157 double[] log = new double[p.length]; // contains the log value
158
159 // get the log value of the gammas
160 for (int i = 0; i < alphas.length; i++) {
161 log[i] = GammaAcceptanceRejectionGen.nextDoubleLog(stream, stream, alphas[i], 1.0 / 2.0);
162 }
163
164 // find the ratio for each p
165 for (int i = 0; i < alphas.length; i++) {
166 total = 0;
167 for (int j = 0; j < alphas.length; j++)
168 total += Math.exp(log[j] - log[i]);
169 p[i] = 1.0 / total;
170 }
171 }
172
177 private static boolean canUseLogGamma(double[] alphas) {
178 for (int i = 0; i < alphas.length; i++)
179 if (alphas[i] >= ALPHA_THRESHOLD)
180 return false;
181 return true;
182 }
183
184}
Extends the class ContinuousDistribution for the gamma distribution tjoh95a  (page 337) with shape pa...
This class implements gamma random variate generators using a method that combines acceptance-rejecti...
double nextDoubleLog()
Returns the natural log value of a new gamma variate.
static double nextDouble(RandomStream s, RandomStream aux, double alpha, double lambda)
Generates a new gamma variate with parameters &#160;alpha and &#160;lambda, using main stream s and auxilia...
void nextPoint(double[] p)
Generates a point from the Dirichlet distribution.
static void nextPoint(RandomStream stream, double[] alphas, double[] p)
Generates a new point from the Dirichlet distribution with parameters alphas, using the stream stream...
double getAlpha(int i)
Returns the parameter for this Dirichlet generator.
DirichletGen(RandomStream stream, double[] alphas)
Constructs a new Dirichlet generator with parameters &#160;alphas[i], for , and the stream stream.
This class is the multivariate counterpart of.
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...