/** * If method parameter n is non-null, then attempt to set distribution parameter <code>N</code> to * n if n is a nonnegative integer. If method parameter p is non-null, then set distribution * parameter <code>P</code> to method parameter p. * * @param n distribution parameter <code>N</code>, representing the number of categories. Must be * nonnegative. Can be set multiple times. * @param p distribution parameter <code>P</code>, representing the column vector of unnormalized * probabilities. */ public void setParams(Number n, MatrixLib p) { if (n != null) { if (n.intValue() < 0) { throw new IllegalArgumentException( "The number of trials 'n' for a Multinomial Distribution must be nonnegative"); } this.n = n.intValue(); this.hasN = true; } if (p != null) { if (p.numCols() != 1 || p.numRows() == 0) { throw new IllegalArgumentException("The vector p passed in is not a column vector"); } initializeProbabilityVector(p); this.hasP = true; } this.finiteSupport = null; }
/** * Precondition: p is a column vector * * <p>Sets instance variable p to a normalized array of probabilities Sets pCDF to the CDF of p * * @param p */ private void initializeProbabilityVector(MatrixLib p) { double sum = 0.0; for (int i = 0; i < p.numRows(); i++) { double ele = p.elementAt(i, 0); if (ele < 0) { throw new IllegalArgumentException( "Probability " + ele + " for element " + i + " is negative."); } sum += ele; } if (sum < 1e-9) { throw new IllegalArgumentException("Probabilities sum to approx zero"); } this.k = p.numRows(); this.p = new double[k]; this.pCDF = new double[k]; this.p[0] = p.elementAt(0, 0) / sum; this.pCDF[0] = this.p[0]; for (int i = 1; i < p.numRows(); i++) { this.p[i] = p.elementAt(i, 0) / sum; this.pCDF[i] = pCDF[i - 1] + this.p[i]; } }