25package umontreal.ssj.probdistmulti;
27import cern.colt.matrix.DoubleMatrix2D;
28import cern.colt.matrix.impl.DenseDoubleMatrix2D;
29import cern.colt.matrix.linalg.Algebra;
48 protected double[] mu;
49 protected DoubleMatrix2D sigma;
50 protected DoubleMatrix2D invSigma;
52 protected static Algebra algebra =
new Algebra();
54 public MultiNormalDist(
double[] mu,
double[][] sigma) {
62 invSigma = algebra.inverse(sigma);
64 double[] temp =
new double[mu.length];
65 for (
int i = 0; i < mu.length; i++) {
67 for (
int j = 0; j < mu.length; j++)
68 sum += ((x[j] - mu[j]) * invSigma.getQuick(j, i));
73 for (
int i = 0; i < mu.length; i++)
74 sum += temp[i] * (x[i] - mu[i]);
76 return (Math.exp(-0.5 * sum) / Math.sqrt(Math.pow(2 * Math.PI, mu.length) * algebra.det(sigma)));
84 return sigma.toArray();
88 return getCorrelation_(mu, sigma.toArray());
99 public static double density(
double[] mu,
double[][] sigma,
double[] x) {
104 if (sigma.length != sigma[0].length)
105 throw new IllegalArgumentException(
"sigma must be a square matrix");
106 if (mu.length != sigma.length)
107 throw new IllegalArgumentException(
"mu and sigma must have the same dimension");
109 sig =
new DenseDoubleMatrix2D(sigma);
110 inv = algebra.inverse(sig);
112 double[] temp =
new double[mu.length];
113 for (
int i = 0; i < mu.length; i++) {
115 for (
int j = 0; j < mu.length; j++)
116 sum += ((x[j] - mu[j]) * inv.getQuick(j, i));
121 for (
int i = 0; i < mu.length; i++)
122 sum += temp[i] * (x[i] - mu[i]);
124 return (Math.exp(-0.5 * sum) / Math.sqrt(Math.pow(2 * Math.PI, mu.length) * algebra.det(sig)));
140 public static double[]
getMean(
double[] mu,
double[][] sigma) {
141 if (sigma.length != sigma[0].length)
142 throw new IllegalArgumentException(
"sigma must be a square matrix");
143 if (mu.length != sigma.length)
144 throw new IllegalArgumentException(
"mu and sigma must have the same dimension");
154 if (sigma.length != sigma[0].length)
155 throw new IllegalArgumentException(
"sigma must be a square matrix");
156 if (mu.length != sigma.length)
157 throw new IllegalArgumentException(
"mu and sigma must have the same dimension");
162 private static double[][] getCorrelation_(
double[] mu,
double[][] sigma) {
163 double corr[][] =
new double[mu.length][mu.length];
165 for (
int i = 0; i < mu.length; i++) {
166 for (
int j = 0; j < mu.length; j++)
167 corr[i][j] = -sigma[i][j] / Math.sqrt(sigma[i][i] * sigma[j][j]);
178 if (sigma.length != sigma[0].length)
179 throw new IllegalArgumentException(
"sigma must be a square matrix");
180 if (mu.length != sigma.length)
181 throw new IllegalArgumentException(
"mu and sigma must have the same dimension");
183 return getCorrelation_(mu, sigma);
198 public static double[]
getMLEMu(
double[][] x,
int n,
int d) {
200 throw new IllegalArgumentException(
"n <= 0");
202 throw new IllegalArgumentException(
"d <= 0");
204 double[] parameters =
new double[d];
205 for (
int i = 0; i < parameters.length; i++)
208 for (
int i = 0; i < n; i++)
209 for (
int j = 0; j < d; j++)
210 parameters[j] += x[i][j];
212 for (
int i = 0; i < parameters.length; i++)
213 parameters[i] = parameters[i] / (
double) n;
229 public static double[][]
getMLESigma(
double[][] x,
int n,
int d) {
233 throw new IllegalArgumentException(
"n <= 0");
235 throw new IllegalArgumentException(
"d <= 0");
238 double[][] parameters =
new double[d][d];
239 for (
int i = 0; i < parameters.length; i++)
240 for (
int j = 0; j < parameters.length; j++)
241 parameters[i][j] = 0.0;
243 for (
int i = 0; i < parameters.length; i++) {
244 for (
int j = 0; j < parameters.length; j++) {
246 for (
int t = 0; t < n; t++)
247 sum += (x[t][i] - mean[i]) * (x[t][j] - mean[j]);
248 parameters[i][j] = sum / (double) n;
275 return sigma.toArray();
284 if (sigma.length != sigma[0].length)
285 throw new IllegalArgumentException(
"sigma must be a square matrix");
286 if (mu.length != sigma.length)
287 throw new IllegalArgumentException(
"mu and sigma must have the same dimension");
289 this.mu =
new double[mu.length];
290 this.dimension = mu.length;
291 System.arraycopy(mu, 0, this.mu, 0, mu.length);
292 this.sigma =
new DenseDoubleMatrix2D(sigma);
Classes implementing continuous multi-dimensional distributions should inherit from this class.
double[][] getCorrelation()
Returns the correlation matrix of the distribution, defined as.
void setParams(double[] mu, double[][] sigma)
Sets the parameters and.
static double[][] getCorrelation(double[] mu, double[][] sigma)
Computes the correlation matrix of the multinormal distribution with parameters and ).
static double[][] getCovariance(double[] mu, double[][] sigma)
Computes the covariance matrix of the multinormal distribution with parameters and .
double[][] getSigma()
Returns the parameter of this object.
static double[][] getMLESigma(double[][] x, int n, int d)
Estimates the parameters of the multinormal distribution using the maximum likelihood method.
double density(double[] x)
Returns , the probability density of evaluated at the point , where .
static double density(double[] mu, double[][] sigma, double[] x)
Computes the density ( fMultinormal ) of the multinormal distribution with parameters.
static double[] getMean(double[] mu, double[][] sigma)
Returns the mean of the multinormal distribution with parameters and.
double[] getMean()
Returns the mean vector of the distribution, defined as .
static double[] getMLEMu(double[][] x, int n, int d)
Estimates the parameters of the multinormal distribution using the maximum likelihood method.
double getMu(int i)
Returns the -th component of the parameter.
int getDimension()
Returns the dimension of the distribution.
double[] getMu()
Returns the parameter of this object.
double[][] getCovariance()
Returns the variance-covariance matrix of the distribution, defined as .