25package umontreal.ssj.probdistmulti;
27import umontreal.ssj.util.Num;
56 public double prob(
int x[]) {
57 return prob_(n, p, x);
60 public double cdf(
int x[]) {
65 return getMean_(n, p);
69 return getCovariance_(n, p);
73 return getCorrelation_(n, p);
76 private static void verifParam(
int n,
double p[]) {
78 throw new IllegalArgumentException(
"n <= 0");
81 for (
int i = 0; i < p.length; i++) {
82 if ((p[i] < 0) || (p[i] > 1))
83 throw new IllegalArgumentException(
"p is not a probability vector");
87 if (Math.abs(sumPi - 1.0) > 1.0e-15)
88 throw new IllegalArgumentException(
"p is not a probability vector");
91 private static double prob_(
int n,
double p[],
int x[]) {
92 if (x.length != p.length)
93 throw new IllegalArgumentException(
"x and p must have the same dimension");
95 double sumXFact = 0.0;
99 for (
int i = 0; i < p.length; i++) {
101 sumXFact += Num.lnFactorial(x[i]);
102 sumPX += (x[i] * Math.log(p[i]));
108 return Math.exp(Num.lnFactorial(n) - sumXFact + sumPX);
118 public static double prob(
int n,
double p[],
int x[]) {
120 return prob_(n, p, x);
123 private static double cdf_(
int n,
double p[],
int x[]) {
128 if (x.length != p.length)
129 throw new IllegalArgumentException(
"x and p must have the same dimension");
131 int is[] =
new int[x.length];
132 for (
int i = 0; i < is.length; i++)
137 sum +=
prob(n, p, is);
143 while (j < x.length && is[j] == x[j])
160 public static double cdf(
int n,
double p[],
int x[]) {
162 return cdf_(n, p, x);
165 private static double[] getMean_(
int n,
double[] p) {
166 double mean[] =
new double[p.length];
168 for (
int i = 0; i < p.length; i++)
178 public static double[]
getMean(
int n,
double[] p) {
181 return getMean_(n, p);
184 private static double[][] getCovariance_(
int n,
double[] p) {
185 double cov[][] =
new double[p.length][p.length];
187 for (
int i = 0; i < p.length; i++) {
188 for (
int j = 0; j < p.length; j++)
189 cov[i][j] = -n * p[i] * p[j];
191 cov[i][i] = n * p[i] * (1.0 - p[i]);
202 return getCovariance_(n, p);
205 private static double[][] getCorrelation_(
int n,
double[] p) {
206 double corr[][] =
new double[p.length][p.length];
208 for (
int i = 0; i < p.length; i++) {
209 for (
int j = 0; j < p.length; j++)
210 corr[i][j] = -Math.sqrt(p[i] * p[j] / ((1.0 - p[i]) * (1.0 - p[j])));
222 return getCorrelation_(n, p);
240 public static double[]
getMLE(
int x[][],
int m,
int d,
int n) {
241 double parameters[] =
new double[d];
242 double xBar[] =
new double[d];
246 throw new IllegalArgumentException(
"m <= 0");
248 throw new IllegalArgumentException(
"d <= 0");
250 for (
int i = 0; i < d; i++)
253 for (
int v = 0; v < m; v++)
254 for (
int c = 0; c < d; c++)
257 for (
int i = 0; i < d; i++) {
258 xBar[i] = xBar[i] / (double) n;
262 throw new IllegalArgumentException(
"n is not correct");
264 for (
int i = 0; i < d; i++)
265 parameters[i] = xBar[i] / (
double) n;
291 throw new IllegalArgumentException(
"n <= 0");
293 throw new IllegalArgumentException(
"p.length < 2");
296 this.dimension = p.length;
297 this.p =
new double[dimension];
298 for (
int i = 0; i < dimension; i++) {
299 if ((p[i] < 0) || (p[i] > 1))
300 throw new IllegalArgumentException(
"p is not a probability vector");
306 if (Math.abs(sumP - 1.0) > 1.0e-15)
307 throw new IllegalArgumentException(
"p is not a probability vector");
Classes implementing multi-dimensional discrete distributions over the integers should inherit from t...
double cdf(int x[])
Computes the cumulative probability function of the distribution evaluated at x, assuming the lowest...
double[][] getCorrelation()
Returns the correlation matrix of the distribution, defined as.
static double[] getMean(int n, double[] p)
Computes the mean of the multinomial distribution with parameters and ( ,…, ).
int getN()
Returns the parameter of this object.
static double[][] getCovariance(int n, double[] p)
Computes the covariance matrix of the multinomial distribution with parameters and ( ,...
static double[] getMLE(int x[][], int m, int d, int n)
Estimates and returns the parameters [ ,…, ] of the multinomial distribution using the maximum likeli...
double[][] getCovariance()
Returns the variance-covariance matrix of the distribution, defined as .
static double prob(int n, double p[], int x[])
Computes the probability mass function ( fMultinomial ) of the multinomial distribution with paramete...
double prob(int x[])
Returns the probability mass function , which should be a real number in .
static double cdf(int n, double p[], int x[])
Computes the function of the multinomial distribution with parameters and ( ,…, ) evaluated at .
double[] getP()
Returns the parameters ( ,…, ) of this object.
double[] getMean()
Returns the mean vector of the distribution, defined as .
void setParams(int n, double p[])
Sets the parameters and ( ,…, ) of this object.
MultinomialDist(int n, double p[])
Creates a MultinomialDist object with parameters and ( ,…, ) such that .
static double[][] getCorrelation(int n, double[] p)
Computes the correlation matrix of the multinomial distribution with parameters and ( ,...