List of usage examples for org.apache.commons.math3.linear ArrayRealVector ArrayRealVector
public ArrayRealVector(double[] v1, double[] v2)
From source file:edu.stanford.cfuller.colocalization3d.correction.PositionCorrector.java
/** * Applies an existing correction to the positions of a set of objects, using a specified reference * and correction channel.//from www . j a v a2s. com * * @param imageObjects A Vector containing all the ImageObjects to be corrected. * @param c The Correction object to be used, which could have been generated with determineCorrection, or loaded from disk. * @return A RealVector with one entry per ImageObject, containing the corrected scalar distance between the object in the reference channel and the channel being corrected. */ public RealVector applyCorrection(Correction c, java.util.List<ImageObject> imageObjects) { int referenceChannel = this.parameters.getIntValueForKey(REF_CH_PARAM); int channelToCorrect = this.parameters.getIntValueForKey(CORR_CH_PARAM); RealVector diffs = new ArrayRealVector(imageObjects.size(), 0.0); RealVector averageVectorDiffs = null; for (int i = 0; i < imageObjects.size(); i++) { diffs.setEntry(i, imageObjects.get(i).getScalarDifferenceBetweenChannels(referenceChannel, channelToCorrect, this.pixelToDistanceConversions)); if (i == 0) { averageVectorDiffs = imageObjects.get(i) .getVectorDifferenceBetweenChannels(referenceChannel, channelToCorrect).copy(); } else { averageVectorDiffs = averageVectorDiffs.add(imageObjects.get(i) .getVectorDifferenceBetweenChannels(referenceChannel, channelToCorrect).map(new Abs())); } } averageVectorDiffs.mapDivideToSelf(imageObjects.size()); averageVectorDiffs = averageVectorDiffs.ebeMultiply(this.pixelToDistanceConversions); java.util.logging.Logger.getLogger("edu.stanford.cfuller.colocalization3d") .info("mean separation uncorrected = " + diffs.getL1Norm() / diffs.getDimension()); java.util.logging.Logger.getLogger("edu.stanford.cfuller.colocalization3d") .info("mean separation components uncorrected = " + averageVectorDiffs.toString()); RealVector newDiffs = new ArrayRealVector(imageObjects.size(), 0.0); if (this.parameters.getBooleanValueForKey("correct_images")) { for (int i = 0; i < imageObjects.size(); i++) { try { newDiffs.setEntry(i, this.correctSingleObjectVectorDifference(c, imageObjects.get(i), referenceChannel, channelToCorrect).getNorm()); imageObjects.get(i).setCorrectionSuccessful(true); } catch (UnableToCorrectException e) { newDiffs.setEntry(i, -1.0 * Double.MAX_VALUE); imageObjects.get(i).setCorrectionSuccessful(false); } } java.util.logging.Logger.getLogger("edu.stanford.cfuller.colocalization3d") .info("mean separation corrected = " + newDiffs.getL1Norm() / newDiffs.getDimension()); } else { newDiffs = diffs; } return newDiffs; }
From source file:gamlss.distributions.ST3.java
/** First derivative dldtau = dl/dtau, where l - log-likelihood function. * @param y - vector of values of response variable * @return a vector of First derivative dldtau = dl/dtau *///from w w w . j a v a2 s . c o m public final ArrayRealVector dldt(final ArrayRealVector y) { dldt = new double[size]; for (int i = 0; i < size; i++) { //dldta <- -0.5*log(1+dsq1/tau)+(w1*dsq1-1)/(2*tau) //dldt <- dldt+0.5*digamma((tau+1)/2)-0.5*digamma(tau/2) final double temp = -0.5 * (FastMath.log(1 + dsq[i] / tauV.getEntry(i))) + (w[i] * dsq[i] - 1) / (2 * tauV.getEntry(i)); dldt[i] = temp + 0.5 * (Gamma.digamma((tauV.getEntry(i) + 1) / 2)) - 0.5 * (Gamma.digamma(tauV.getEntry(i) / 2)); } s = null; dsq = null; w = null; ym = null; return new ArrayRealVector(dldt, false); }
From source file:gamlss.distributions.ST1.java
/** First derivative dlds = dl/dsigma, where l - log-likelihood function. * @param y - vector of values of response variable * @return a vector of First derivative dlds = dl/dsigma *//*from w w w. ja v a 2s . c o m*/ public final ArrayRealVector dlds(final ArrayRealVector y) { dlds = new double[size]; for (int i = 0; i < size; i++) { //dldd <- -(dt(w,df=tau)/pt(w,df=tau))*nu*z/sigma //+ ((lam*(z^2))-1)/sigma tdDist.setDegreesOfFreedom(tauV.getEntry(i)); dlds[i] = -(tdDist.density(w[i]) / tdDist.cumulativeProbability(w[i])) * nuV.getEntry(i) * z[i] / sigmaV.getEntry(i) + ((lam[i] * (z[i] * z[i])) - 1) / sigmaV.getEntry(i); } z = null; w = null; lam = null; return new ArrayRealVector(dlds, false); }
From source file:gamlss.algorithm.AdditiveFit.java
/** * Calculates rss = weighted.mean(residuals^2, w). * @param resid - residuals/* w w w .j a va2s . com*/ * @param weights - vector of weights * @return weighted.mean(residuals^2, w) */ private double calculateRss(final ArrayRealVector resid, final ArrayRealVector weights) { //rss <- weighted.mean(residuals^2, w) size = resid.getDimension(); tempArr = new double[size]; for (int i = 0; i < size; i++) { tempArr[i] = resid.getEntry(i) * resid.getEntry(i); } size = 0; return mean.evaluate(new ArrayRealVector(tempArr, false).getDataRef(), weights.toArray()); }
From source file:gamlss.distributions.JSUo.java
/** Second derivative d2ldm2= (d^2l)/(dmu^2), * where l - log-likelihood function./*from ww w . j a v a 2s .co m*/ * @param y - vector of values of response variable * @return a vector of second derivative d2ldm2= (d^2l)/(dmu^2) */ private ArrayRealVector d2ldm2(final ArrayRealVector y) { double[] out = new double[size]; for (int i = 0; i < size; i++) { //d2ldm2 <- -dldm*dldm out[i] = -dldm[i] * dldm[i]; if (out[i] > -1e-15) { out[i] = -1e-15; } } muV = null; sigmaV = null; nuV = null; tauV = null; return new ArrayRealVector(out, false); }
From source file:gamlss.distributions.TF2.java
/** Second derivative d2lds2= (d^2l)/(dsigma^2), * where l - log-likelihood function.//from ww w. ja v a 2 s. co m * @param y - vector of values of response variable * @return a vector of second derivative d2lds2= (d^2l)/(dsigma^2) */ private ArrayRealVector d2lds2(final ArrayRealVector y) { double[] out = new double[size]; for (int i = 0; i < size; i++) { //s2 <- sigma1^2 final double s2 = sigma1.getEntry(i) * sigma1.getEntry(i); //d2ldd2 <- -(ds1dd^2)*((2*nu)/((nu+3)*s2)) out[i] = -(ds1dd[i] * ds1dd[i]) * ((2.0 * nuV.getEntry(i)) / ((nuV.getEntry(i) + 3) * s2)); } sigma1 = null; ds1dd = null; muV = null; sigmaV = null; nuV = null; return new ArrayRealVector(out, false); }
From source file:gamlss.utilities.MatrixFunctions.java
/** * Get main diagonal of the matrix./* www .j av a 2 s .co m*/ * @param m - matrix * @return main diagonal as vector */ public static ArrayRealVector getMainDiagonal(final BlockRealMatrix m) { double[] tempArr = new double[m.getRowDimension()]; for (int i = 0; i < m.getColumnDimension(); i++) { for (int j = 0; j < m.getColumnDimension(); j++) { if (i == j) { tempArr[i] = m.getEntry(i, j); } } } return new ArrayRealVector(tempArr, false); }
From source file:gamlss.distributions.ST4.java
/** First derivative dldn = dl/dnu, where l - log-likelihood function. * @param y - vector of values of response variable * @return a vector of First derivative dldn = dl/dnu *///from w ww . jav a 2 s. c o m public final ArrayRealVector dldn(final ArrayRealVector y) { dldn = new double[size]; double lk1 = 0; double lk2 = 0; for (int i = 0; i < size; i++) { if (nuV.getEntry(i) < 1000000) { //lk1 <- lgamma(0.5) + lgamma(nu/2) //- lgamma((nu+1)/2) + 0.5*log(nu lk1 = Gamma.logGamma(0.5) + Gamma.logGamma(nuV.getEntry(i) / 2) - Gamma.logGamma((nuV.getEntry(i) + 1) / 2) + 0.5 * FastMath.log(nuV.getEntry(i)); } else { lk1 = 0.5 * FastMath.log(2.0 * FastMath.PI); } //lk2 <- ifelse(tau < 1000000, lk2, 0.5*log(2*pi)) if (tauV.getEntry(i) < 1000000) { //lk2 <- lgamma(0.5) + lgamma(tau/2) //- lgamma((tau+1)/2) + 0.5*log(tau) lk2 = Gamma.logGamma(0.5) + Gamma.logGamma(tauV.getEntry(i) / 2) - Gamma.logGamma((tauV.getEntry(i) + 1) / 2) + 0.5 * FastMath.log(tauV.getEntry(i)); } else { lk2 = 0.5 * FastMath.log(2.0 * FastMath.PI); } //lk <- lk2 - lk1 //k <- exp(lk) final double k = FastMath.exp(lk2 - lk1); //dldva <- -0.5*log(1+dsq/nu)+(w1*dsq)/(2*nu) //dldv <- ifelse(y < mu, dldva , 0) if (y.getEntry(i) < muV.getEntry(i)) { dldn[i] = -0.5 * FastMath.log(1 + dsq[i] / nuV.getEntry(i)) + (w[i] * dsq[i]) / (2.0 * nuV.getEntry(i)); } else { dldn[i] = 0.0; } //dldv <- dldv+0.5*(digamma((nu+1)/2) //-digamma(nu/2)-(1/nu))*(1/(1+k)) dldn[i] = dldn[i] + 0.5 * (Gamma.digamma((nuV.getEntry(i) + 1) / 2.0) - Gamma.digamma(nuV.getEntry(i) / 2.0) - (1 / nuV.getEntry(i))) * (1.0 / (1 + k)); } dsq = null; w = null; return new ArrayRealVector(dldn, false); }
From source file:gamlss.distributions.PE.java
/** Second derivative d2ldm2= (d^2l)/(dmu^2), * where l - log-likelihood function./*from ww w. ja v a 2 s . c om*/ * @param y - vector of values of response variable * @return a vector of second derivative d2ldm2= (d^2l)/(dmu^2) */ private ArrayRealVector d2ldm2(final ArrayRealVector y) { double[] out = new double[size]; for (int i = 0; i < size; i++) { //d2ldm2 <- if (any(nu<1.05)) -dldm*dldm else d2ldm2 if (nuV.getEntry(i) < 1.05) { out[i] = -dldm[i] * dldm[i]; } else { //d2ldm2 <- -(nu*nu*gamma(2-(1/nu))*gamma(3/nu)) ///((sigma*gamma(1/nu))^2) out[i] = -(nuV.getEntry(i) * nuV.getEntry(i) * FastMath.exp(Gamma.logGamma(2 - (1 / nuV.getEntry(i)))) * FastMath.exp(Gamma.logGamma(3 / nuV.getEntry(i)))) / ((sigmaV.getEntry(i) * FastMath.exp(Gamma.logGamma(1 / nuV.getEntry(i)))) * (sigmaV.getEntry(i) * FastMath.exp(Gamma.logGamma(1 / nuV.getEntry(i))))); } } muV = null; sigmaV = null; nuV = null; return new ArrayRealVector(out, false); }
From source file:gamlss.distributions.GT.java
/** First derivative dldtau = dl/dtau, where l - log-likelihood function. * @param y - vector of values of response variable * @return a vector of First derivative dldtau = dl/dtau *///from w ww . jav a 2 s . com public final ArrayRealVector dldt(final ArrayRealVector y) { dldt = new double[size]; for (int i = 0; i < size; i++) { // dldt <- -(tau*w*zt*log(abs(z))) + log(1+(zt/nu)) dldt[i] = -(tauV.getEntry(i) * w[i] * zt[i] * FastMath.log(FastMath.abs(z[i]))) + FastMath.log(1 + (zt[i] / nuV.getEntry(i))); // dldt <- dldt + digamma(1/tau)-digamma(nu+(1/tau))+log(nu)+tau // dldt <- dldt/(tau^2) dldt[i] = (dldt[i] + Gamma.digamma(1 / tauV.getEntry(i)) - Gamma.digamma(nuV.getEntry(i) + (1 / tauV.getEntry(i))) + FastMath.log(nuV.getEntry(i)) + tauV.getEntry(i)) / (tauV.getEntry(i) * tauV.getEntry(i)); } w = null; z = null; zt = null; return new ArrayRealVector(dldt, false); }