SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
MixtureGen.java
1/*
2 * Class: MixtureGen
3 * Description: random variate generators for a mixture of distributions
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.randvar;
26
27import umontreal.ssj.probdist.DiscreteDistribution;
28import umontreal.ssj.rng.RandomStream;
29import umontreal.ssj.probdist.Distribution;
30
52public class MixtureGen extends RandomVariateGen {
53
57 protected Distribution[] dists;
58
63 protected DiscreteDistribution weightsDist; // it is used to select the distribution
64
69 protected double[] weights;
70
84 super(s, null);
85 if (dists.length != weights.length)
86 throw new IllegalArgumentException("The arrays dists and weigths must have the same length");
87
88 this.dists = dists;
89 this.weights = weights;
90
91 initWeightDistribution();
92 }
93
98 private void initWeightDistribution() {
99 int[] idx = new int[weights.length];
100 for (int i = 0; i < idx.length; i++)
101 idx[i] = i;
102 weightsDist = new DiscreteDistribution(idx, weights, idx.length);
103 }
104
108
115 @Override
117 throw new UnsupportedOperationException("Use getDistributions method instead");
118 }
119
124 return dists;
125 }
126
131 public double[] getWeights() {
132 return weights;
133 }
134
138
139 @Override
140 public double nextDouble() {
141 // select distribution from mixture
142 int idx = (int) Math.round(weightsDist.inverseF(stream.nextDouble()));
143
144 // generate random variate from selected distribution
145 return dists[idx].inverseF(stream.nextDouble());
146 }
147}
This class implements discrete distributions over a finite set of real numbers (also over integers as...
Distribution[] getDistributions()
Returns the distributions of this mixture.
DiscreteDistribution weightsDist
The discrete distribution that is used to select randomly a distribution from the mixture when genera...
double nextDouble()
Generates a random number from the continuous distribution contained in this object.
MixtureGen(RandomStream s, Distribution[] dists, double[] weights)
Creates a new mixture distribution generator defined by the given.
double[] getWeights()
Returns the probability associated with each distribution of the mixture.
double[] weights
The probability of each distribution that compose this mixture, see variable dists .
Distribution getDistribution()
This method is not supported, it will throw an UnsupportedOperationException.
Distribution[] dists
The different distributions that compose this mixture.
This interface should be implemented by all classes supporting discrete and continuous distributions.
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...