List of usage examples for org.apache.commons.math3.linear ArrayRealVector ArrayRealVector
public ArrayRealVector(double[] v1, double[] v2)
From source file:edu.stanford.cfuller.imageanalysistools.fitting.BisquareLinearFit.java
/** * Calculates the standardized adjusted residuals (according to the same definition used by MATLAB) of the data points for fitting. * * @param indVarValues The values of the independent variable used for the fitting. * @param depVarValues The values of the dependent variable used for the fitting. * @param leverages the leverages of the independent variables, as compted by {@link #calculateLeverages(RealVector)} * @param fitParams the results of a (possibly weighted) least squares fit to the data, containing one or two components: a slope and an optional y-intercept. * @return a RealVector containing an adjusted residual value for each data point *///from w ww.j a v a 2 s . c o m protected RealVector calculateStandardizedAdjustedResiduals(RealVector indVarValues, RealVector depVarValues, RealVector leverages, RealVector fitParams) { RealVector predictedValues = indVarValues.mapMultiply(fitParams.getEntry(0)); RealVector denom = leverages.mapMultiply(-1.0).mapAddToSelf(1 + this.CLOSE_TO_ZERO) .mapToSelf(new org.apache.commons.math3.analysis.function.Sqrt()); if (!this.noIntercept) { predictedValues = predictedValues.mapAdd(fitParams.getEntry(1)); } double stddev = 0; double mean = 0; for (int i = 0; i < depVarValues.getDimension(); i++) { mean += depVarValues.getEntry(i); } mean /= depVarValues.getDimension(); stddev = depVarValues.mapSubtract(mean).getNorm() * (depVarValues.getDimension() * 1.0 / (depVarValues.getDimension() - 1)); RealVector residuals = depVarValues.subtract(predictedValues).ebeDivide(denom); RealVector absDev = residuals.map(new org.apache.commons.math3.analysis.function.Abs()); int smallerDim = 2; if (this.noIntercept) { smallerDim = 1; } double[] resArray = residuals.map(new org.apache.commons.math3.analysis.function.Abs()).toArray(); java.util.Arrays.sort(resArray); RealVector partialRes = new ArrayRealVector(absDev.getDimension() - smallerDim + 1, 0.0); for (int i = smallerDim - 1; i < resArray.length; i++) { partialRes.setEntry(i - smallerDim + 1, resArray[i]); } double resMAD = 0; if (partialRes.getDimension() % 2 == 0) { resMAD = LocalBackgroundEstimationFilter.quickFindKth(partialRes.getDimension() / 2, partialRes) + LocalBackgroundEstimationFilter.quickFindKth(partialRes.getDimension() / 2 - 1, partialRes); resMAD /= 2.0; } else { resMAD = LocalBackgroundEstimationFilter.quickFindKth((partialRes.getDimension() - 1) / 2, partialRes); } resMAD /= 0.6745; if (resMAD < stddev * CLOSE_TO_ZERO) { resMAD = stddev * CLOSE_TO_ZERO; } return residuals.mapDivide(DEFAULT_TUNING_CONST * resMAD); }
From source file:gamlss.algorithm.CGAlgorithm.java
/** * Calculates inverse of dr vector values (1/dr) * @param dr - vector of 1/(link function of the linear pridictoor) values * @return 1/dr//from w w w .jav a 2 s.c o m */ private ArrayRealVector drInverse(ArrayRealVector dr) { double[] out = new double[dr.getDimension()]; for (int i = 0; i < out.length; i++) { out[i] = 1 / dr.getEntry(i); } return new ArrayRealVector(out, false); }
From source file:gamlss.distributions.NO.java
/** Second derivative d2lds2= (d^2l)/(dsigma^2). * @return a vector of second derivative d2lds2= (d^2l)/(dsigma^2) *///from w ww. j a v a 2s . com private ArrayRealVector d2lds2() { //d2ldd2 = function(sigma) -(2/(sigma^2)), double[] out = new double[size]; for (int i = 0; i < size; i++) { //out[i] = -(2/FastMath.pow(sigma.getEntry(i), 2)); out[i] = -(2 / tempV2.getEntry(i)); } return new ArrayRealVector(out, false); }
From source file:gamlss.algorithm.GlimFitCG.java
private ArrayRealVector etaMean(ArrayRealVector etaMU, ArrayRealVector etaOldMU) { double[] out = new double[etaMU.getDimension()]; for (int i = 0; i < out.length; i++) { out[i] = (etaMU.getEntry(i) + etaOldMU.getEntry(i)) / 2; }//from ww w .j a va 2 s . c om return new ArrayRealVector(out, false); }
From source file:gamlss.distributions.JSUo.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 . j a v a2 s . c o m public final ArrayRealVector dlds(final ArrayRealVector y) { dlds = new double[size]; for (int i = 0; i < size; i++) { //dldd <- (-1/(sigma*(z*z+1)))+((r*tau*z)/(sigma*(z*z+1)^(0.5))) dlds[i] = (-1 / (sigmaV.getEntry(i) * (z[i] * z[i] + 1))) + ((r[i] * tauV.getEntry(i) * z[i]) / (sigmaV.getEntry(i) * FastMath.sqrt(z[i] * z[i] + 1))); } r = null; z = null; return new ArrayRealVector(dlds, false); }
From source file:gamlss.distributions.TF.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 *//* w ww .java 2 s. c o m*/ public final ArrayRealVector dldn(final ArrayRealVector y) { double[] dldn = new double[size]; v2 = new double[size]; v3 = new double[size]; for (int i = 0; i < size; i++) { //dsq3 <- 1+(dsq/nu) final double dsq3 = 1 + (dsq[i] / nuV.getEntry(i)); //v2 <- nu/2 v2[i] = nuV.getEntry(i) / 2.0; //v3 <- (nu+1)/2 v3[i] = (nuV.getEntry(i) + 1) / 2.0; //dldv <- -log(dsq3)+(omega*dsq-1)/nu +digamma(v3)-digamma(v2) dldn[i] = -FastMath.log(dsq3) + (omega[i] * dsq[i] - 1) / nuV.getEntry(i) + Gamma.digamma(v3[i]) - Gamma.digamma(v2[i]); //dldv <- dldv/2 dldn[i] = dldn[i] / 2.0; } s2 = null; dsq = null; omega = null; return new ArrayRealVector(dldn, false); }
From source file:gamlss.distributions.ST3.java
/** First derivative dldm = dl/dmu, where l - log-likelihood function. * @param y - vector of values of response variable * @return a vector of first derivative dldm = dl/dmu *//* w ww . ja va2 s . c o m*/ public final ArrayRealVector dldm(final ArrayRealVector y) { dldm = new double[size]; for (int i = 0; i < size; i++) { //dldm <- ifelse(y < mu, (w1*(y-mu))/(s1^2),(w2*(y-mu))/(s2^2)) dldm[i] = (w[i] * ym[i]) / (s[i] * s[i]); } s = null; dsq = null; w = null; ym = null; return new ArrayRealVector(dldm, false); }
From source file:gamlss.distributions.SST.java
/** First derivative dldm = dl/dmu, where l - log-likelihood function. * @param y - vector of values of response variable * @return a vector of first derivative dldm = dl/dmu *///from ww w.j av a2 s . c o m public final ArrayRealVector dldm(final ArrayRealVector y) { //dldm <- ST3()$dldm(y, mu1, sigma1, nu, tau) st3.setDistributionParameter(DistributionSettings.MU, new ArrayRealVector(mu1, false)); st3.setDistributionParameter(DistributionSettings.SIGMA, new ArrayRealVector(sigma1, false)); st3.setDistributionParameter(DistributionSettings.NU, nuV); st3.setDistributionParameter(DistributionSettings.TAU, tauV); tempV = st3.firstDerivative(DistributionSettings.MU, y); dldm = tempV.getDataRef(); m1 = null; m2 = null; s1 = null; mu1 = null; sigma1 = null; return tempV; }
From source file:gamlss.distributions.ST4.java
/** First derivative dldm = dl/dmu, where l - log-likelihood function. * @param y - vector of values of response variable * @return a vector of first derivative dldm = dl/dmu *///from w w w . j a v a 2s . c om public final ArrayRealVector dldm(final ArrayRealVector y) { dldm = new double[size]; for (int i = 0; i < size; i++) { //dldm <- ifelse(y < mu, (w1*(y-mu))/(s^2) , (w2*(y-mu))/(s^2)) dldm[i] = (w[i] * (y.getEntry(i) - muV.getEntry(i))) / (sigmaV.getEntry(i) * sigmaV.getEntry(i)); } dsq = null; w = null; return new ArrayRealVector(dldm, false); }
From source file:gamlss.utilities.MatrixFunctions.java
/** * Calculates inverse of vector values./*from w ww . j ava2s. c om*/ * @param v - vector * @return 1/v */ public static ArrayRealVector inverse(final ArrayRealVector v) { double[] tempArr = new double[v.getDimension()]; for (int i = 0; i < tempArr.length; i++) { tempArr[i] = 1 / v.getEntry(i); } return new ArrayRealVector(tempArr, false); }