25package umontreal.ssj.randvarmulti;
27import cern.colt.matrix.DoubleMatrix2D;
28import cern.colt.matrix.linalg.SingularValueDecomposition;
29import umontreal.ssj.probdist.NormalDist;
30import umontreal.ssj.randvar.NormalGen;
31import umontreal.ssj.rng.RandomStream;
32import cern.colt.matrix.impl.DenseDoubleMatrix2D;
59 private double[] lambda;
61 private static SingularValueDecomposition getSvd(DoubleMatrix2D sigma) {
62 return (
new SingularValueDecomposition(sigma));
65 private DoubleMatrix2D decompPCA(SingularValueDecomposition svd) {
66 DoubleMatrix2D D = svd.getS();
68 for (
int i = 0; i < D.rows(); ++i) {
69 lambda[i] = D.getQuick(i, i);
70 D.setQuick(i, i, Math.sqrt(D.getQuick(i, i)));
72 DoubleMatrix2D P = svd.getV();
73 return P.zMult(D,
null);
76 private void initL() {
77 if (mu.length != sigma.rows() || mu.length != sigma.columns())
78 throw new IllegalArgumentException(
"Incompatible mean vector and covariance matrix");
79 lambda =
new double[mu.length];
80 sqrtSigma = decompPCA(getSvd(sigma));
88 super(gen1, mu, sigma);
109 super(gen1, mu, sigma);
119 public static DoubleMatrix2D
decompPCA(
double[][] sigma) {
120 return decompPCA(
new DenseDoubleMatrix2D(sigma));
129 public static DoubleMatrix2D
decompPCA(DoubleMatrix2D sigma) {
134 SingularValueDecomposition sv = getSvd(sigma);
136 DoubleMatrix2D D = sv.getS();
138 for (
int i = 0; i < D.rows(); ++i)
139 D.setQuick(i, i, Math.sqrt(D.getQuick(i, i)));
140 DoubleMatrix2D P = sv.getV();
142 return P.zMult(D,
null);
152 return sqrtSigma.copy();
158 public static double[]
getLambda(DoubleMatrix2D sigma) {
159 SingularValueDecomposition sv = getSvd(sigma);
160 DoubleMatrix2D D = sv.getS();
161 double[] lambd =
new double[D.rows()];
162 for (
int i = 0; i < D.rows(); ++i)
163 lambd[i] = D.getQuick(i, i);
182 if (sigma.rows() != mu.length || sigma.columns() != mu.length)
183 throw new IllegalArgumentException(
"Invalid dimensions of covariance matrix");
184 this.sigma.assign(sigma);
185 this.sqrtSigma = decompPCA(getSvd(sigma));
215 throw new NullPointerException(
"gen1 is null");
218 if (dist.
getMu() != 0.0)
219 throw new IllegalArgumentException(
"mu != 0");
221 throw new IllegalArgumentException(
"sigma != 1");
223 if (mu.length != sigma.rows() || mu.length != sigma.columns())
224 throw new IllegalArgumentException(
"Incompatible mean vector and covariance matrix dimensions");
225 double[] temp =
new double[mu.length];
226 DoubleMatrix2D sqrtSigma = decompPCA(sigma);
227 for (
int i = 0; i < temp.length; i++) {
228 temp[i] = gen1.nextDouble();
229 if (temp[i] == Double.NEGATIVE_INFINITY)
231 if (temp[i] == Double.POSITIVE_INFINITY)
234 for (
int i = 0; i < temp.length; i++) {
236 for (
int c = 0; c < temp.length; c++)
237 p[i] += sqrtSigma.getQuick(i, c) * temp[c];
247 nextPoint(gen1, mu,
new DenseDoubleMatrix2D(sigma), p);
259 for (
int i = 0; i < n; i++) {
260 temp[i] = gen1.nextDouble();
261 if (temp[i] == Double.NEGATIVE_INFINITY)
263 if (temp[i] == Double.POSITIVE_INFINITY)
266 for (
int i = 0; i < n; i++) {
268 for (
int c = 0; c < n; c++)
269 p[i] += sqrtSigma.getQuick(i, c) * temp[c];
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a (page 80)).
double getMu()
Returns the parameter .
double getSigma()
Returns the parameter .
This class implements methods for generating random variates from the normal distribution .
MultinormalGen(NormalGen gen1, int d)
Constructs a generator with the standard multinormal distribution (with and.
static double[] getLambda(DoubleMatrix2D sigma)
Computes and returns the eigenvalues of sigma in decreasing order.
void nextPoint(double[] p)
Generates a point from this multinormal distribution.
DoubleMatrix2D getPCADecompSigma()
Returns the matrix of this object.
MultinormalPCAGen(NormalGen gen1, double[] mu, double[][] sigma)
Equivalent to MultinormalPCAGen(gen1, mu, new DenseDoubleMatrix2D(sigma)).
double[] getLambda()
Returns the eigenvalues of in decreasing order.
MultinormalPCAGen(NormalGen gen1, double[] mu, DoubleMatrix2D sigma)
Constructs a multinormal generator with mean vector mu and covariance matrix sigma.
static DoubleMatrix2D decompPCA(double[][] sigma)
Computes the decomposition sigma = .
void setSigma(DoubleMatrix2D sigma)
Sets the covariance matrix of this multinormal generator to sigma (and recomputes ).
static void nextPoint(NormalGen gen1, double[] mu, DoubleMatrix2D sigma, double[] p)
Generates a -dimensional vector from the multinormal distribution with mean vector mu and covariance ...
static void nextPoint(NormalGen gen1, double[] mu, double[][] sigma, double[] p)
Equivalent to nextPoint(gen1, mu, new DenseDoubleMatrix2D(sigma), p).
static DoubleMatrix2D decompPCA(DoubleMatrix2D sigma)
Computes the decomposition sigma = .