List of usage examples for java.lang Math exp
@HotSpotIntrinsicCandidate public static double exp(double a)
From source file:endrov.nucAutoJH.FitGaussian.java
private static double[] fitGaussian2D_(EvPixels p, double sigmaInit, final double midxInit, final double midyInit) { //sigma00, sigma01, sigma11, mu_x, mu_y, c p = p.getReadOnly(EvPixelsType.DOUBLE); final double[] arrPixels = p.getArrayDouble(); final int w = p.getWidth(); final int h = p.getHeight(); int extent = (int) Math.round(3 * sigmaInit); extent = Math.max(extent, 2); final int sx = Math.max(0, (int) (midxInit - extent)); final int ex = Math.min(w, (int) (midxInit + extent + 1)); //+1 to the right? final int sy = Math.max(0, (int) (midyInit - extent)); final int ey = Math.min(h, (int) (midyInit + extent + 1)); double minIntensity = Double.MAX_VALUE; double maxIntensity = Double.MIN_VALUE; for (int y = sy; y < ey; y++) { int base = y * w; double dy2 = y - midyInit; dy2 = dy2 * dy2;//from ww w . j a v a2s . c o m for (int x = sx; x < ex; x++) { double dx2 = x - midxInit; dx2 = dx2 * dx2; double t = arrPixels[base + x]; //if(dx2+dy2<=extent*extent) { if (t < minIntensity) minIntensity = t; if (t > maxIntensity) maxIntensity = t; } } } //double[] weights=new double[]{1}; double[] startPoint = new double[] { sigmaInit, 0, sigmaInit, midxInit, midyInit, minIntensity, maxIntensity - minIntensity }; //double[] output=new double[startPoint.length]; try { MultivariateRealFunction func = new MultivariateRealFunction() { // opt.optimize( public double value(double[] arg) throws FunctionEvaluationException, IllegalArgumentException { double sigma00 = arg[0]; double sigma01 = arg[1]; double sigma11 = arg[2]; double mu0 = arg[3]; double mu1 = arg[4]; double C = arg[5]; double D = arg[6]; double sumError = 0; Matrix2d sigma = new Matrix2d(sigma00, sigma01, sigma01, sigma11); Matrix2d sigmaInv = new Matrix2d(); sigma.invert(sigmaInv); double sigmaDet = sigma.determinant(); double front = 1.0 / (2 * Math.PI * Math.sqrt(sigmaDet)); //System.out.println("front: "+front); //System.out.println("sigma inv "+sigmaInv); if (mu0 < sx || mu0 > ex) sumError += 1000000; if (mu1 < sy || mu1 > ey) sumError += 1000000; if (sigma00 < 1) sumError += 1000000; //if(sigma01<0) sumError+=1000000; if (sigma11 < 1) sumError += 1000000; if (D <= 0) sumError += 1000000; for (int y = sy; y < ey; y++) { int base = y * w; double dy2 = y - midyInit; dy2 = dy2 * dy2; for (int x = sx; x < ex; x++) { double dx2 = x - midxInit; dx2 = dx2 * dx2; double thisReal = arrPixels[base + x]; // if(dx2+dy2<=extent*extent) { // DoubleMatrix2D sigma=new DenseDoubleMatrix2D(new double[][]{{sigma00,sigma01},{sigma01,sigma11}}); //double sigmaDet=sigma00*sigma11-sigma01*sigma01; double dx0 = x - mu0; double dx1 = y - mu1; //http://en.wikipedia.org/wiki/Multivariate_normal_distribution Vector2d vX = new Vector2d(dx0, dx1); Vector2d op = new Vector2d(vX); sigmaInv.transform(op); double upper = -0.5 * op.dot(vX); double exp = Math.exp(upper); //System.out.println("front "+front+" "+exp+" C "+C+" thisreal"+thisReal+" upper "+upper); if (upper > -0.4) exp = 1; else exp = Math.max(0, 1 + upper + 0.4); /* if(exp<0.7) exp=0; else exp=1; */ double thisExpected = D * front * exp + C; double diff = thisExpected - thisReal; sumError += diff * diff; } } } //System.out.println(sigma00+"\t"+sigma01+"\t"+sigma11+"\tC"+C+"\tmu "+mu0+","+mu1+"\terr "+sumError); return sumError; // return new double[]{sumError}; } }; NelderMead opt = new NelderMead(); //LevenbergMarquardtOptimizer opt=new LevenbergMarquardtOptimizer(); opt.setMaxIterations(10000); RealPointValuePair pair = opt.optimize(func, GoalType.MINIMIZE, startPoint); int numit = opt.getIterations(); System.out.println("#it " + numit); System.out.println("err " + func.value(pair.getPointRef())); return pair.getPointRef(); // for(int i=0;i<startPoint.length;i++) // System.out.println("i: "+i+" "+output[i]); //, output, weights, startPoint); } /* catch (MaxIterationsExceededException e) { System.out.println("max it reached"); }*/ catch (Exception e) { e.printStackTrace(); } //Maybe this is a bad point? System.out.println("max it reached"); return startPoint; // return output; }
From source file:endrov.typeLineageAutoNucJH.FitGaussian.java
private static double[] fitGaussian2D_(EvPixels p, double sigmaInit, final double midxInit, final double midyInit) { //sigma00, sigma01, sigma11, mu_x, mu_y, c p = p.getReadOnly(EvPixelsType.DOUBLE); final double[] arrPixels = p.getArrayDouble(); final int w = p.getWidth(); final int h = p.getHeight(); int extent = (int) Math.round(3 * sigmaInit); extent = Math.max(extent, 2); final int sx = Math.max(0, (int) (midxInit - extent)); final int ex = Math.min(w, (int) (midxInit + extent + 1)); //+1 to the right? final int sy = Math.max(0, (int) (midyInit - extent)); final int ey = Math.min(h, (int) (midyInit + extent + 1)); double minIntensity = Double.MAX_VALUE; double maxIntensity = -Double.MAX_VALUE; for (int y = sy; y < ey; y++) { int base = y * w; double dy2 = y - midyInit; dy2 = dy2 * dy2;/*from w w w.j ava2 s . c o m*/ for (int x = sx; x < ex; x++) { double dx2 = x - midxInit; dx2 = dx2 * dx2; double t = arrPixels[base + x]; //if(dx2+dy2<=extent*extent) { if (t < minIntensity) minIntensity = t; if (t > maxIntensity) maxIntensity = t; } } } //double[] weights=new double[]{1}; double[] startPoint = new double[] { sigmaInit, 0, sigmaInit, midxInit, midyInit, minIntensity, maxIntensity - minIntensity }; //double[] output=new double[startPoint.length]; try { MultivariateRealFunction func = new MultivariateRealFunction() { // opt.optimize( public double value(double[] arg) throws FunctionEvaluationException, IllegalArgumentException { double sigma00 = arg[0]; double sigma01 = arg[1]; double sigma11 = arg[2]; double mu0 = arg[3]; double mu1 = arg[4]; double C = arg[5]; double D = arg[6]; double sumError = 0; Matrix2d sigma = new Matrix2d(sigma00, sigma01, sigma01, sigma11); Matrix2d sigmaInv = new Matrix2d(); sigma.invert(sigmaInv); double sigmaDet = sigma.determinant(); double front = 1.0 / (2 * Math.PI * Math.sqrt(sigmaDet)); //System.out.println("front: "+front); //System.out.println("sigma inv "+sigmaInv); if (mu0 < sx || mu0 > ex) sumError += 1000000; if (mu1 < sy || mu1 > ey) sumError += 1000000; if (sigma00 < 1) sumError += 1000000; //if(sigma01<0) sumError+=1000000; if (sigma11 < 1) sumError += 1000000; if (D <= 0) sumError += 1000000; for (int y = sy; y < ey; y++) { int base = y * w; double dy2 = y - midyInit; dy2 = dy2 * dy2; for (int x = sx; x < ex; x++) { double dx2 = x - midxInit; dx2 = dx2 * dx2; double thisReal = arrPixels[base + x]; // if(dx2+dy2<=extent*extent) { // DoubleMatrix2D sigma=new DenseDoubleMatrix2D(new double[][]{{sigma00,sigma01},{sigma01,sigma11}}); //double sigmaDet=sigma00*sigma11-sigma01*sigma01; double dx0 = x - mu0; double dx1 = y - mu1; //http://en.wikipedia.org/wiki/Multivariate_normal_distribution Vector2d vX = new Vector2d(dx0, dx1); Vector2d op = new Vector2d(vX); sigmaInv.transform(op); double upper = -0.5 * op.dot(vX); double exp = Math.exp(upper); //System.out.println("front "+front+" "+exp+" C "+C+" thisreal"+thisReal+" upper "+upper); if (upper > -0.4) exp = 1; else exp = Math.max(0, 1 + upper + 0.4); /* if(exp<0.7) exp=0; else exp=1; */ double thisExpected = D * front * exp + C; double diff = thisExpected - thisReal; sumError += diff * diff; } } } //System.out.println(sigma00+"\t"+sigma01+"\t"+sigma11+"\tC"+C+"\tmu "+mu0+","+mu1+"\terr "+sumError); return sumError; // return new double[]{sumError}; } }; NelderMead opt = new NelderMead(); //LevenbergMarquardtOptimizer opt=new LevenbergMarquardtOptimizer(); opt.setMaxIterations(10000); RealPointValuePair pair = opt.optimize(func, GoalType.MINIMIZE, startPoint); int numit = opt.getIterations(); System.out.println("#it " + numit); System.out.println("err " + func.value(pair.getPointRef())); return pair.getPointRef(); // for(int i=0;i<startPoint.length;i++) // System.out.println("i: "+i+" "+output[i]); //, output, weights, startPoint); } /* catch (MaxIterationsExceededException e) { System.out.println("max it reached"); }*/ catch (Exception e) { e.printStackTrace(); } //Maybe this is a bad point? System.out.println("max it reached"); return startPoint; // return output; }
From source file:eagle.security.userprofile.impl.UserProfileAnomalyKDEEvaluator.java
@Override public List<MLCallbackResult> detect(final String user, final String algorithm, UserActivityAggModel userActivity, UserProfileKDEModel aModel) { List<MLCallbackResult> mlPredictionOutputList = new ArrayList<MLCallbackResult>(); RealMatrix inputData = userActivity.matrix(); double[] probabilityEstimation = new double[inputData.getRowDimension()]; for (int i = 0; i < probabilityEstimation.length; i++) probabilityEstimation[i] = 1.0;// ww w.j a v a 2s .co m boolean[][] anomalyFeature = new boolean[inputData.getRowDimension()][inputData.getColumnDimension()]; for (int i = 0; i < anomalyFeature.length; i++) { for (int j = 0; j < anomalyFeature[i].length; j++) { anomalyFeature[i][j] = false; } } if (aModel == null) { LOG.info("No model available for this uer, returning"); return null; } Map<String, String> context = new HashMap<String, String>() { { put(UserProfileConstants.USER_TAG, user); put(UserProfileConstants.ALGORITHM_TAG, algorithm); } }; for (int i = 0; i < inputData.getRowDimension(); i++) { List<String> cmds = JavaConversions.seqAsJavaList(userActivity.cmdTypes()); if (inputData.getColumnDimension() != cmds.size()) { LOG.error("Test data is not with same dimension as training, aborting..."); return null; } else { UserCommandStatistics[] listStats = aModel.statistics(); for (int j = 0; j < inputData.getColumnDimension(); j++) { // LOG.info("mean for j=" + j + " is:" + listStats[j].getMean()); // LOG.info("stddev for j=" + j + " is:" + listStats[j].getStddev()); if (listStats[j].isLowVariant()) { // LOG.info(listStats[j].getCommandName() + " is low variant for user: " + user); if (inputData.getEntry(i, j) > listStats[j].getMean()) { probabilityEstimation[i] *= Double.NEGATIVE_INFINITY; anomalyFeature[i][j] = true; } } else { double stddev = listStats[j].getStddev(); //LOG.info("stddev: " + stddev); double mean = listStats[j].getMean(); //LOG.info("mean: " + mean); double sqrt2PI = Math.sqrt(2.0 * Math.PI); //LOG.info("sqrt2PI: " + sqrt2PI); double denominatorFirstPart = sqrt2PI * stddev; //LOG.info("denominatorFirstPart: " + denominatorFirstPart); double squareMeanNormal = Math.pow((inputData.getEntry(i, j) - mean), 2); //LOG.info("squareMeanNormal: " + squareMeanNormal); double twoPowStandardDev = Math.pow(stddev, 2); //LOG.info("twoPowStandardDev: " + twoPowStandardDev); double twoTimesTwoPowStandardDev = 2.0 * twoPowStandardDev; //LOG.info("twoTimesTwoPowStandardDev: " + twoTimesTwoPowStandardDev); double tempVal = ((1.00 / denominatorFirstPart) * (Math.exp(-(squareMeanNormal / twoTimesTwoPowStandardDev)))); probabilityEstimation[i] *= tempVal; //LOG.info("probabilityEstimation: " + probabilityEstimation[i]); if ((inputData.getEntry(i, j) - mean) > 2 * stddev) anomalyFeature[i][j] = true; } } } } for (int i = 0; i < probabilityEstimation.length; i++) { MLCallbackResult callBackResult = new MLCallbackResult(); callBackResult.setContext(context); //LOG.info("probability estimation for data @" + i + " is: " + probabilityEstimation[i]); if (probabilityEstimation[i] < aModel.maxProbabilityEstimate()) { callBackResult.setAnomaly(true); for (int col = 0; col < anomalyFeature[i].length; col++) { //LOG.info("feature anomaly? " + (featureVals[col] == true)); if (anomalyFeature[i][col] == true) { callBackResult.setFeature(aModel.statistics()[col].getCommandName()); } } } else { callBackResult.setAnomaly(false); } callBackResult.setTimestamp(userActivity.timestamp()); List<String> datapoints = new ArrayList<String>(); double[] rowVals = userActivity.matrix().getRow(i); for (double rowVal : rowVals) datapoints.add(rowVal + ""); callBackResult.setDatapoints(datapoints); callBackResult.setId(user); callBackResult.setAlgorithm(UserProfileConstants.KDE_ALGORITHM); mlPredictionOutputList.add(callBackResult); } return mlPredictionOutputList; }
From source file:eu.amidst.core.utils.Utils.java
/** * Normalizes an array of doubles./* ww w. j a v a 2 s . co m*/ * @param vals an {@code array} of {@code double}. * @return a normalized array of doubles. */ public static double[] logs2probs(double[] vals) { double max = vals[Utils.maxIndex(vals)]; double[] normalizedVals = new double[vals.length]; for (int i = 0; i < vals.length; i++) { normalizedVals[i] = Math.exp(vals[i] + max); } return Utils.normalize(normalizedVals); }
From source file:com.opengamma.analytics.financial.model.option.pricing.fourier.HestonCharacteristicExponent.java
@Override public ComplexNumber getValue(ComplexNumber u, double t) { // that u = 0 gives zero is true for any characteristic function, that u = -i gives zero is because this is already mean corrected if (u.getReal() == 0.0 && (u.getImaginary() == 0.0 || u.getImaginary() == -1.0)) { return ZERO; }/*from www. j a v a 2s . com*/ //non-stochastic vol limit if (_omega == 0.0 || mod(multiply(multiply(_omega / _kappa, u), add(I, u))) < 1e-6) { final ComplexNumber z = multiply(u, add(I, u)); if (_kappa * t < 1e-6) { return multiply(-_vol0 / 2 * t, z); } final double var = _theta * t + (_vol0 - _theta) * (1 - Math.exp(-_kappa * t)) / _kappa; return multiply(-var / 2, z); } final ComplexNumber c = getC(u, t); final ComplexNumber dv0 = multiply(_vol0, getD(u, t)); return add(c, dv0); }
From source file:fiji.plugin.trackmate.action.brownianmotion.WalkerMethodEstimator.java
private double probMSD(double msd, double k, double r) { double pmsd = 0; double thetaFactor = (2 * kB * temp * frameduration) / (3 * Math.PI * visk); double theta = thetaFactor / r; pmsd = (logK(k) + (k - 1) * (logK(k) + Math.log(msd)) + (-k * msd / theta)) - (k * Math.log(theta) + logGammaK(k)); //pmsd = (Math.log(k)+(k-1)*(Math.log(k)+Math.log(msd)) + (-k*msd/theta) ) - (k*Math.log(theta) + Gamma.logGamma(k)); pmsd = Math.exp(pmsd); return pmsd;// ww w .java 2s.com }
From source file:com.bmwcarit.barefoot.matcher.MatcherTest.java
private void assertCandidate(Tuple<MatcherCandidate, Double> candidate, Point sample) { Polyline polyline = map.get(candidate.one().point().edge().id()).geometry(); double f = spatial.intercept(polyline, sample); Point i = spatial.interpolate(polyline, f); double l = spatial.distance(i, sample); double sig2 = Math.pow(5d, 2); double sqrt_2pi_sig2 = Math.sqrt(2d * Math.PI * sig2); double p = 1 / sqrt_2pi_sig2 * Math.exp((-1) * l / (2 * sig2)); assertEquals(f, candidate.one().point().fraction(), 10E-6); assertEquals(p, candidate.two(), 10E-6); }
From source file:fingerprints.helper.BloomFilter.java
/** * Calculate the probability of a false positive given the specified number of inserted elements. * * Calculates the approximate probability of the contains() method returning true for an object that had not * previously been inserted into the bloom filter. This is known as the "false positive probability". * * @return The estimated false positive rate *//*w ww. j av a2 s. c om*/ public double getExpectedFalsePositiveProbability() { // (1 - e^(-k * n / m)) ^ k return Math.pow((1 - Math.exp(-k * (double) expectedPatterns / (double) bitSetSize)), k); }
From source file:com.opengamma.analytics.financial.model.option.pricing.analytic.JarrowRuddSkewnessKurtosisModel.java
private double getQ3(final double s, final double k, final double sigmaT, final double t, final double r, final double a, final double d2) { final double da = a * (d2 - sigmaT) / (k * sigmaT); final double df = Math.exp(-r * t); return -Math.pow(s * df, 3) * Math.pow(Math.exp(sigmaT * sigmaT - 1), 1.5) * df * da / 6.; }
From source file:ch.epfl.leb.sass.models.illuminations.internal.SquareUniformIllumination.java
/** * Returns the irradiance in the sample at the point (x, y, z). * // w ww . j a v a 2 s . c o m * @param x The x-position in the sample. * @param y The y-position in the sample. * @param z The z-position in the sample. */ @Override public double getIrradiance(double x, double y, double z) { // Compute the absorption, if any. double abs = Math.exp(-4 * Math.PI * electricField.getRefractiveIndex().getN(x, y, z).getImaginary() * z / electricField.getWavelength()); double irrad = this.power * abs / width / height; // TODO: Change this to an exception! assert (irrad != Double.NaN); return irrad; }