public double logPdf(double x) { if (x < 0) return Double.NEGATIVE_INFINITY; double r = -1 * (mean * mean) / (mean - stdev * stdev); double p = mean / (stdev * stdev); return Math.log(Math.pow(1 - p, x)) + Math.log(Math.pow(p, r)) + GammaFunction.lnGamma(r + x) - GammaFunction.lnGamma(r) - GammaFunction.lnGamma(x + 1); }
private void computeNormalizationConstant() { logNormalizationConstant = 0; try { logNormalizationConstant = df / 2.0 * Math.log(new Matrix(scaleMatrix).determinant()); } catch (IllegalDimension illegalDimension) { illegalDimension.printStackTrace(); } logNormalizationConstant -= df * dim / 2.0 * Math.log(2); logNormalizationConstant -= dim * (dim - 1) / 4.0 * Math.log(Math.PI); for (int i = 1; i <= dim; i++) { logNormalizationConstant -= GammaFunction.lnGamma((df + 1 - i) / 2.0); } }
public double getLogLik2Group(int group1, int group2) { double L = 0.0; int ngroup1 = 0; for (int i = 0; i < assignments.getDimension(); i++) { if ((int) assignments.getParameterValue(i) == group1) { ngroup1++; } } int ngroup2 = 0; for (int i = 0; i < assignments.getDimension(); i++) { if ((int) assignments.getParameterValue(i) == group2) { ngroup2++; } } int ngroup = (ngroup1 + ngroup2); if (ngroup != 0) { double[][] group = new double[ngroup][2]; double mean[] = new double[2]; int count = 0; for (int i = 0; i < assignments.getDimension(); i++) { if ((int) assignments.getParameterValue(i) == group1) { group[count][0] = modelLikelihood.getData()[i][0]; group[count][1] = modelLikelihood.getData()[i][1]; mean[0] += group[count][0]; mean[1] += group[count][1]; count += 1; } } for (int i = 0; i < assignments.getDimension(); i++) { if ((int) assignments.getParameterValue(i) == group2) { group[count][0] = modelLikelihood.getData()[i][0]; group[count][1] = modelLikelihood.getData()[i][1]; mean[0] += group[count][0]; mean[1] += group[count][1]; count += 1; } } mean[0] /= ngroup; mean[1] /= ngroup; double kn = k0 + ngroup; double vn = v0 + ngroup; double[][] sumdif = new double[2][2]; for (int i = 0; i < ngroup; i++) { sumdif[0][0] += (group[i][0] - mean[0]) * (group[i][0] - mean[0]); sumdif[0][1] += (group[i][0] - mean[0]) * (group[i][1] - mean[1]); sumdif[1][0] += (group[i][0] - mean[0]) * (group[i][1] - mean[1]); sumdif[1][1] += (group[i][1] - mean[1]) * (group[i][1] - mean[1]); } double[][] TnInv = new double[2][2]; TnInv[0][0] = T0Inv[0][0] + ngroup * (k0 / kn) * (mean[0] - m[0]) * (mean[0] - m[0]) + sumdif[0][0]; TnInv[0][1] = T0Inv[0][1] + ngroup * (k0 / kn) * (mean[1] - m[1]) * (mean[0] - m[0]) + sumdif[0][1]; TnInv[1][0] = T0Inv[1][0] + ngroup * (k0 / kn) * (mean[0] - m[0]) * (mean[1] - m[1]) + sumdif[1][0]; TnInv[1][1] = T0Inv[1][1] + ngroup * (k0 / kn) * (mean[1] - m[1]) * (mean[1] - m[1]) + sumdif[1][1]; double logDetTn = -Math.log(TnInv[0][0] * TnInv[1][1] - TnInv[0][1] * TnInv[1][0]); L += -(ngroup) * Math.log(Math.PI); L += Math.log(k0) - Math.log(kn); L += (vn / 2) * logDetTn - (v0 / 2) * logDetT0; L += GammaFunction.lnGamma(vn / 2) + GammaFunction.lnGamma((vn / 2) - 0.5); L += -GammaFunction.lnGamma(v0 / 2) - GammaFunction.lnGamma((v0 / 2) - 0.5); } return L; }