List of usage examples for org.apache.commons.math.distribution BetaDistributionImpl density
@Override public double density(double x)
From source file:change_point_detection.BetaDistributionChangePoint.java
public int/*estimated change point*/ detectChange() throws Exception { int estimatedChangePoint = -1; int N = this.dynamicWindow.size(); this.cushion = Math.max(100, (int) Math.floor(Math.pow(N, gamma))); //mean conf. should not fall below 0.3 if (N > (2 * this.cushion) && calculateMean(0, N - 1) <= 0.3) return N - 1; double threshold = -Math.log(this.sensitivity); double w = 0; int kAtMaxW = -1; for (int k = this.cushion; k <= N - this.cushion; k++) { if (calculateMean(k, N - 1) <= 0.95 * calculateMean(0, k - 1)) { double skn = 0; /* estimate pre and post change parameters */ double alphaPreChange = calcBetaDistAlpha(0, k - 1); double betaPreChange = calculateBetaDistBeta(alphaPreChange, 0, k - 1); double alphaPostChange = calcBetaDistAlpha(k, N - 1); double betaPostChange = calculateBetaDistBeta(alphaPostChange, k, N - 1); BetaDistributionImpl preBetaDist = new BetaDistributionImpl(alphaPreChange, betaPreChange); BetaDistributionImpl postBetaDist = new BetaDistributionImpl(alphaPostChange, betaPostChange); for (int i = k; i < N; i++) { try { skn += Math.log(postBetaDist.density(this.dynamicWindow.get(i).doubleValue()) / preBetaDist.density(this.dynamicWindow.get(i).doubleValue())); } catch (Exception e) { e.printStackTrace(); System.out.println("continuing..."); skn = 0;//from w w w .j av a 2 s .c o m break; } } if (skn > w) { w = skn; kAtMaxW = k; } } } if (w >= threshold && kAtMaxW != -1) { System.out.println("\nChangePoint Found!"); estimatedChangePoint = kAtMaxW; System.out.println("Estimated change point is " + estimatedChangePoint); } //force change point if confidence falls down terribly if (estimatedChangePoint == -1 && N >= 100 && calculateMean(0, N - 1) < 0.3) estimatedChangePoint = N - 1; return estimatedChangePoint; }
From source file:change_point_detection.CpdDP.java
public int/*estimated change point*/ detectChange() throws Exception { int estimatedChangePoint = -1; int N = this.dynamicWindow.size(); this.cushion = Math.max(100, (int) Math.floor(Math.pow(N, gamma))); //mean conf. should not fall below 0.3 double preChangeMean, postChangeMean, wholeMean; wholeMean = calculateMean(0, N - 1); if ((N > (2 * this.cushion) && wholeMean <= 0.3) || this.dynamicWindow.size() > this.dim) return N - 1; double threshold = -Math.log(this.sensitivity); double w = 0; int kAtMaxW = -1; for (int k = this.cushion; k <= N - this.cushion; k++) { double skn = 0; int prevN = this.trackN[k]; preChangeMean = prevN == -1 ? calculateMean(0, k - 1) : this.cusumElementArr[prevN][k].getPreChangeMean(); postChangeMean = calculateMean(k, N - 1); if (postChangeMean <= (1 - this.secAlgMarSlack) * preChangeMean) {//signal from secondary if (prevN == -1) { //calculate from scratch /* estimate pre and post change parameters */ double alphaPreChange = calcBetaDistAlpha(0, k - 1, preChangeMean); double betaPreChange = calculateBetaDistBeta(alphaPreChange, 0, k - 1, preChangeMean); double alphaPostChange = calcBetaDistAlpha(k, N - 1, postChangeMean); double betaPostChange = calculateBetaDistBeta(alphaPostChange, k, N - 1, postChangeMean); BetaDistributionImpl preBetaDist = new BetaDistributionImpl(alphaPreChange, betaPreChange); BetaDistributionImpl postBetaDist = new BetaDistributionImpl(alphaPostChange, betaPostChange); for (int i = k; i < N; i++) { try { skn += Math.log(postBetaDist.density(this.dynamicWindow.get(i).doubleValue()) / preBetaDist.density(this.dynamicWindow.get(i).doubleValue())); } catch (Exception e) { e.printStackTrace(); System.out.println("continuing..."); skn = 0;//from w w w. j a v a 2 s .c o m break; } } this.cusumElementArr[N - 1][k] = new CusumElement(preBetaDist, postBetaDist, preChangeMean, skn); } else {//warning and calculate recursively double alphaPostChange2 = calcBetaDistAlpha(k, N - 1, postChangeMean); double betaPostChange2 = calculateBetaDistBeta(alphaPostChange2, k, N - 1, postChangeMean); BetaDistributionImpl postBetaDist2 = new BetaDistributionImpl(alphaPostChange2, betaPostChange2); skn += this.cusumElementArr[prevN][k].getCusumScore(); for (int i = prevN + 1; i < N; i++) { try { skn += Math.log(postBetaDist2.density(this.dynamicWindow.get(i).doubleValue()) / this.cusumElementArr[prevN][k].getPreChangeDist() .density(this.dynamicWindow.get(i).doubleValue())); } catch (Exception e) { e.printStackTrace(); System.out.println("continuing..."); skn = 0; break; } } this.cusumElementArr[N - 1][k] = new CusumElement( this.cusumElementArr[prevN][k].getPreChangeDist(), postBetaDist2, preChangeMean, skn); } this.trackN[k] = N - 1; } if (skn > w) { w = skn; kAtMaxW = k; } } if (w >= threshold && kAtMaxW != -1) { System.out.println("\nChangePoint Found!"); estimatedChangePoint = kAtMaxW; System.out.println("Estimated change point is " + estimatedChangePoint + ", detected at point: " + N); } //force change point if confidence falls down terribly if (estimatedChangePoint == -1 && N >= 100 && wholeMean < 0.3) estimatedChangePoint = N - 1; return estimatedChangePoint; }