25package umontreal.ssj.probdistmulti;
27import umontreal.ssj.util.Num;
28import umontreal.ssj.util.RootFinder;
29import umontreal.ssj.functions.MathFunction;
52 protected double Fl[];
58 public Function(
int k,
int m,
int ups[],
double Fl[]) {
62 this.Fl =
new double[Fl.length];
63 System.arraycopy(Fl, 0, this.Fl, 0, Fl.length);
64 this.ups =
new int[ups.length];
65 System.arraycopy(ups, 0, this.ups, 0, ups.length);
68 for (
int i = 0; i < ups.length; i++)
72 public double evaluate(
double gamma) {
74 for (
int l = 0; l < M; l++)
75 sum += (Fl[l] / (gamma + (
double) l));
76 return (sum - Math.log1p(sumUps / (k * gamma)));
80 private static class FuncInv
extends Function implements
MathFunction {
82 public FuncInv(
int k,
int m,
int ups[],
double Fl[]) {
86 public double evaluate(
double nu) {
88 for (
int l = 0; l < M; l++)
89 sum += Fl[l] / (1.0 + nu * l);
90 return (sum * nu - Math.log1p(sumUps * nu / k));
104 return prob_(n, p, x);
112 return getMean_(n, p);
116 return getCovariance_(n, p);
120 return getCorrelation_(n, p);
123 private static void verifParam(
double n,
double p[]) {
127 throw new IllegalArgumentException(
"n <= 0");
129 for (
int i = 0; i < p.length; i++) {
130 if ((p[i] < 0) || (p[i] >= 1))
131 throw new IllegalArgumentException(
"p is not a probability vector");
136 throw new IllegalArgumentException(
"p is not a probability vector");
139 private static double prob_(
double n,
double p[],
int x[]) {
143 double sumLnXiFact = 0.0;
144 double sumXiLnPi = 0.0;
146 if (x.length != p.length)
147 throw new IllegalArgumentException(
"x and p must have the same size");
149 for (
int i = 0; i < p.length; i++) {
152 sumLnXiFact += Num.lnFactorial(x[i]);
153 sumXiLnPi += x[i] * Math.log(p[i]);
157 return Math.exp(Num.lnGamma(n + sumXi) - (Num.lnGamma(n) + sumLnXiFact) + n * Math.log(p0) + sumXiLnPi);
168 public static double prob(
double n,
double p[],
int x[]) {
170 return prob_(n, p, x);
173 private static double cdf_(
double n,
double p[],
int x[]) {
174 throw new UnsupportedOperationException(
"cdf not implemented");
182 public static double cdf(
double n,
double p[],
int x[]) {
185 return cdf_(n, p, x);
188 private static double[] getMean_(
double n,
double p[]) {
191 double mean[] =
new double[p.length];
193 for (
int i = 0; i < p.length; i++)
197 for (
int i = 0; i < p.length; i++)
198 mean[i] = n * p[i] / p0;
207 public static double[]
getMean(
double n,
double p[]) {
210 return getMean_(n, p);
213 private static double[][] getCovariance_(
double n,
double p[]) {
216 double cov[][] =
new double[p.length][p.length];
218 for (
int i = 0; i < p.length; i++)
222 for (
int i = 0; i < p.length; i++) {
223 for (
int j = 0; j < p.length; j++)
224 cov[i][j] = n * p[i] * p[j] / (p0 * p0);
226 cov[i][i] = n * p[i] * (p[i] + p0) / (p0 * p0);
239 return getCovariance_(n, p);
242 private static double[][] getCorrelation_(
double n,
double[] p) {
243 double corr[][] =
new double[p.length][p.length];
247 for (
int i = 0; i < p.length; i++)
251 for (
int i = 0; i < p.length; i++) {
252 for (
int j = 0; j < p.length; j++)
253 corr[i][j] = Math.sqrt(p[i] * p[j] / ((p0 + p[i]) * (p0 + p[j])));
265 return getCorrelation_(n, p);
295 public static double[]
getMLE(
int x[][],
int m,
int d) {
296 int ups[] =
new int[m];
297 double mean[] =
new double[d];
304 for (i = 0; i < d; i++)
309 for (j = 0; j < m; j++) {
311 for (i = 0; i < d; i++) {
316 for (i = 0; i < d; i++)
328 for (j = 1; j < m; j++)
332 if (M >= Integer.MAX_VALUE)
333 throw new IllegalArgumentException(
"n/p_i too large");
335 double Fl[] =
new double[M];
336 for (l = 0; l < M; l++) {
338 for (j = 0; j < m; j++)
342 Fl[l] = (double) prop / (
double) m;
352 double parameters[] =
new double[d + 1];
353 Function f =
new Function(m, (
int) M, ups, Fl);
356 double lambda[] =
new double[d];
357 double sumLambda = 0.0;
358 for (i = 0; i < d; i++) {
359 lambda[i] = mean[i] / parameters[0];
360 sumLambda += lambda[i];
363 for (i = 0; i < d; i++) {
364 parameters[i + 1] = lambda[i] / (1.0 + sumLambda);
365 if (parameters[i + 1] > 1.0)
366 throw new IllegalArgumentException(
"p_i > 1");
388 int ups[] =
new int[m];
389 double mean[] =
new double[d];
395 for (i = 0; i < d; i++)
400 for (j = 0; j < m; j++) {
402 for (i = 0; i < d; i++) {
407 for (i = 0; i < d; i++)
412 for (j = 1; j < m; j++)
416 if (M >= Integer.MAX_VALUE)
417 throw new IllegalArgumentException(
"n/p_i too large");
419 double Fl[] =
new double[M];
420 for (l = 0; l < M; l++) {
422 for (j = 0; j < m; j++)
426 Fl[l] = (double) prop / (
double) m;
429 FuncInv f =
new FuncInv(m, M, ups, Fl);
453 throw new IllegalArgumentException(
"n <= 0");
456 this.dimension = p.length;
457 this.p =
new double[dimension];
460 for (
int i = 0; i < dimension; i++) {
461 if ((p[i] < 0) || (p[i] >= 1))
462 throw new IllegalArgumentException(
"p is not a probability vector");
469 throw new IllegalArgumentException(
"p is not a probability vector");
Classes implementing multi-dimensional discrete distributions over the integers should inherit from t...
double getGamma()
Returns the parameter of this object.
static double cdf(double n, double p[], int x[])
Computes the cumulative probability function of the negative multinomial distribution with parameter...
double[] getP()
Returns the parameters ( , …, ) of this object.
NegativeMultinomialDist(double n, double p[])
Creates a NegativeMultinomialDist object with parameters and ( , …, ) such that ,...
static double[][] getCorrelation(double n, double[] p)
Computes the correlation matrix of the negative multinomial distribution with parameters and ( ,...
static double[][] getCovariance(double n, double p[])
Computes the covariance matrix of the negative multinomial distribution with parameters and ( ,...
static double[] getMean(double n, double p[])
Computes the mean of the negative multinomial distribution with parameters and ( ,...
static double getMLEninv(int x[][], int m, int d)
Estimates and returns the parameter of the negative multinomial distribution using the maximum likel...
static double prob(double n, double p[], int x[])
Computes the probability mass function ( fNegativeMultinomial ) of the negative multinomial distribut...
void setParams(double n, double p[])
Sets the parameters and ( , …, ) of this object.
double prob(int x[])
Returns the probability mass function , which should be a real number in .
double[][] getCorrelation()
Returns the correlation matrix of the distribution, defined as.
double[] getMean()
Returns the mean vector of the distribution, defined as .
static double[] getMLE(int x[][], int m, int d)
Estimates and returns the parameters [ ,.
double[][] getCovariance()
Returns the variance-covariance matrix of the distribution, defined as .
This class provides numerical methods to solve non-linear equations.
static double brentDekker(double a, double b, MathFunction f, double tol)
Computes a root of the function in f using the Brent-Dekker method.
This interface should be implemented by classes which represent univariate mathematical functions.