SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
KernelDensityGen.java
1/*
2 * Class: KernelDensityGen
3 * Description: random variate generators for distributions obtained via
4 kernel density estimation methods
5 * Environment: Java
6 * Software: SSJ
7 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
8 * Organization: DIRO, Universite de Montreal
9 * @author
10 * @since
11 *
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 */
26package umontreal.ssj.randvar;
27
28import umontreal.ssj.probdist.*;
29import umontreal.ssj.rng.RandomStream;
30
139public class KernelDensityGen extends RandomVariateGen {
140
141 protected RandomVariateGen kernelGen;
142 protected double bandwidth;
143 protected boolean positive; // If we want positive reflection.
144
151 public KernelDensityGen(RandomStream s, EmpiricalDist dist, RandomVariateGen kGen, double h) {
152 super(s, dist);
153 if (h < 0.0)
154 throw new IllegalArgumentException("h < 0");
155 if (kGen == null)
156 throw new IllegalArgumentException("kGen == null");
157 kernelGen = kGen;
158 bandwidth = h;
159 }
160
169 this(s, dist, kGen, 0.77639 * getBaseBandwidth(dist));
170 }
171
175
180 public static double getBaseBandwidth(EmpiricalDist dist) {
181 double r = dist.getInterQuartileRange() / 1.34;
182 double sigma = dist.getSampleStandardDeviation();
183 if (sigma < r)
184 r = sigma;
185 return (1.36374 * r / Math.exp(0.2 * Math.log(dist.getN())));
186 }
187
191 public void setBandwidth(double h) {
192 if (h < 0)
193 throw new IllegalArgumentException("h < 0");
194 bandwidth = h;
195 }
196
204 public void setPositiveReflection(boolean reflect) {
205 positive = reflect;
206 }
207
208 public double nextDouble() {
209 double x = (dist.inverseF(stream.nextDouble()) + bandwidth * kernelGen.nextDouble());
210 if (positive)
211 return Math.abs(x);
212 else
213 return x;
214 }
215}
216
Extends DiscreteDistribution to an empirical distribution function, based on the observations (sorte...
static double getBaseBandwidth(EmpiricalDist dist)
Computes and returns the value of in ( bandwidth0 ).
void setPositiveReflection(boolean reflect)
After this method is called with true, the generator will produce only positive values,...
double nextDouble()
Generates a random number from the continuous distribution contained in this object.
KernelDensityGen(RandomStream s, EmpiricalDist dist, NormalGen kGen)
This constructor uses a gaussian kernel and the default bandwidth.
KernelDensityGen(RandomStream s, EmpiricalDist dist, RandomVariateGen kGen, double h)
Creates a new generator for a kernel density estimated from the observations given by the empirical d...
void setBandwidth(double h)
Sets the bandwidth to h.
This class implements methods for generating random variates from the normal distribution .
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...