List of usage examples for java.lang Math exp
@HotSpotIntrinsicCandidate public static double exp(double a)
From source file:beast.evolution.coalescent.DemographicFunction.java
default double getInterval(double U, int lineageCount, double timeOfLastCoalescent, double earliestTimeOfFinalCoalescent) { if (timeOfLastCoalescent > earliestTimeOfFinalCoalescent) { throw new IllegalArgumentException("Given maximum height is smaller than given final coalescent time"); }/*from w ww .j a v a 2 s .c o m*/ final double fullIntegral = getIntegral(timeOfLastCoalescent, earliestTimeOfFinalCoalescent); final double normalisation = 1 - Math.exp(-CombinatoricsUtils.binomialCoefficientDouble(lineageCount, 2) * fullIntegral); final double intensity = getIntensity(timeOfLastCoalescent); double tmp = -Math.log(1 - U * normalisation) / CombinatoricsUtils.binomialCoefficientDouble(lineageCount, 2) + intensity; return getInverseIntensity(tmp) - timeOfLastCoalescent; }
From source file:com.datumbox.framework.statistics.distributions.ContinuousDistributions.java
/** * Calculates the probability from 0 to X under Exponential Distribution * /*from w ww . ja v a2 s. c om*/ * @param x * @param lamda * @return * @throws IllegalArgumentException */ public static double ExponentialCdf(double x, double lamda) throws IllegalArgumentException { if (x < 0 || lamda <= 0) { throw new IllegalArgumentException(); } double probability = 1.0 - Math.exp(-lamda * x); return probability; }
From source file:com.opengamma.analytics.financial.model.option.pricing.tree.NormalBinomialTreeBuilderTest.java
@Test public void testPriceFlat() { final RecombiningBinomialTree<BinomialTreeNode<Double>> assetPriceTree = BUILDER.buildAssetTree(T, DATA, 200);// w w w. j a va2 s.c o m RecombiningBinomialTree<BinomialTreeNode<Double>> optionPriceTree = BUILDER.buildOptionPriceTree(OPTION, DATA, assetPriceTree); EuropeanVanillaOption o = new EuropeanVanillaOption(FORWARD, T, true); final BlackFunctionData data = new BlackFunctionData(FORWARD, YIELD_CURVE.getDiscountFactor(T), 0); double impVol = BLACK_IMPLIED_VOL.getImpliedVolatility(data, o, optionPriceTree.getNode(0, 0).getValue()); //double impVol = BlackImpliedVolFormula.impliedVol(optionPriceTree.getNode(0, 0).getValue(), FORWARD, FORWARD, YIELD_CURVE.getDiscountFactor(T), T, true); assertEquals(ATM_VOL, impVol, 1e-3); for (int i = 0; i < 10; i++) { final double m = -1.5 + 3.0 * i / 10.0; final double strike = FORWARD * Math.exp(ATM_VOL * Math.sqrt(T) * m); final OptionDefinition option = new EuropeanVanillaOptionDefinition(strike, OPTION.getExpiry(), OPTION.isCall()); o = new EuropeanVanillaOption(strike, T, OPTION.isCall()); optionPriceTree = BUILDER.buildOptionPriceTree(option, DATA, assetPriceTree); impVol = BLACK_IMPLIED_VOL.getImpliedVolatility(data, o, optionPriceTree.getNode(0, 0).getValue()); //impVol = BlackImpliedVolFormula.impliedVol(optionPriceTree.getNode(0, 0).getValue(), FORWARD, strike, YIELD_CURVE.getDiscountFactor(T), T, true); // System.out.println(strike+"\t"+impVol); assertEquals(ATM_VOL, impVol, 1e-3); } }
From source file:com.itemanalysis.psychometrics.irt.model.Irm4PL.java
/** * Computes the gradientAt (vector of first partial derivatives) with respect to the item parameters. * This method uses item parameters passed to teh method. It does NOT use item parameters stored in the * object.// w w w . ja v a2 s. com * * Note: The second argument (int k) is not actually used by this class. It is here to satisfy the interface. * * @param theta person ability estimate. * @param iparam array of item parameters. * @param k response category * @param D scaling constant that is either 1 or 1.7 * @return an array of first partial derivatives (i.e. the gradientAt). */ public double[] gradient(double theta, double[] iparam, int k, double D) { double[] deriv = new double[numberOfParameters]; double a = iparam[0]; double b = iparam[1]; double c = iparam[2]; double x = iparam[3]; double w = D * (theta - b); double z = Math.exp(D * a * (theta - b)); double z2 = z * z; double d = 1 + z; double d2 = d * d; double xmc = x - c; deriv[0] = xmc * z * w / d - xmc * z2 * w / d2; deriv[1] = -(xmc * z * D * a / d - xmc * z2 * D * a / d2); deriv[2] = 1.0 - z / d; deriv[3] = z / d; if (k == 0) { deriv[0] = -deriv[0]; deriv[1] = -deriv[1]; deriv[2] = -deriv[2]; deriv[3] = -deriv[3]; } return deriv; }
From source file:net.sf.jtmt.clustering.SimulatedAnnealingClusterer.java
/** * Cluster.//ww w. j a v a 2 s . c o m * * @param collection the collection * @return the list */ public List<Cluster> cluster(DocumentCollection collection) { // 1) Get initial set of clusters... int numDocs = collection.size(); int numClusters = (int) Math.floor(Math.sqrt(numDocs)); List<Cluster> clusters = new ArrayList<Cluster>(); for (int i = 0; i < numClusters; i++) { clusters.add(new Cluster("C" + i)); } // ...and set initial temperature parameter T. double temperature = initialTemperature; // Randomly assign documents to the k clusters. if (randomizeDocs) { collection.shuffle(); } for (int i = 0; i < numDocs; i++) { int targetCluster = i % numClusters; clusters.get(targetCluster).addDocument(collection.getDocumentNameAt(i), collection.getDocument(collection.getDocumentNameAt(i))); } log.debug("..Initial clusters: " + clusters.toString()); // 2) Repeat until temperature is reduced to the minimum. while (temperature > finalTemperature) { double previousAverageRadius = 0.0D; List<Cluster> prevClusters = new ArrayList<Cluster>(); // 2.1) Run loop NUM_LOOP times. for (int loop = 0; loop < numberOfLoops; loop++) { // 2.1.1) Find a new set of clusters by altering the membership of some // documents. // pick two clusters at random List<Integer> randomClusterIds = getRandomClusterIds(clusters); // pick two documents out of the clusters at random List<String> randomDocumentNames = getRandomDocumentNames(collection, randomClusterIds, clusters); // exchange the two random documents among the random clusters. clusters.get(randomClusterIds.get(0)).removeDocument(randomDocumentNames.get(0)); clusters.get(randomClusterIds.get(0)).addDocument(randomDocumentNames.get(1), collection.getDocument(randomDocumentNames.get(1))); clusters.get(randomClusterIds.get(1)).removeDocument(randomDocumentNames.get(1)); clusters.get(randomClusterIds.get(1)).addDocument(randomDocumentNames.get(0), collection.getDocument(randomDocumentNames.get(0))); // 2.1.2) Compare the difference between the values of the new and old // set of clusters. If there is an improvement, accept the new // set of clusters, otherwise accept the new set of clusters with // probability p. log.debug("..Intermediate clusters: " + clusters.toString()); double averageRadius = getAverageRadius(clusters); if (averageRadius > previousAverageRadius) { // possible downhill move, calculate the probability of it being // accepted double probability = Math.exp((previousAverageRadius - averageRadius) / temperature); if (probability < downhillProbabilityCutoff) { // go back to the cluster before the changes clusters.clear(); clusters.addAll(prevClusters); continue; } } prevClusters.clear(); prevClusters.addAll(clusters); previousAverageRadius = averageRadius; } // 2.2) Reduce the temperature based on the cooling schedule. temperature = temperature / 10; } // 3) Return the final set of clusters. return clusters; }
From source file:com.opengamma.analytics.financial.model.option.pricing.analytic.JarrowRuddSkewnessKurtosisModel.java
private double getQ4(final double s, final double k, final double sigmaT, final double t, final double r, final double a, final double d2) { final double sigmaTSq = sigmaT * sigmaT; final double x = d2 - sigmaT; final double da2 = a * (x * x - sigmaT * x - 1) / (k * k * sigmaTSq); final double df = Math.exp(-r * t); return Math.pow(s * df, 4) * Math.pow(Math.exp(sigmaTSq) - 1, 2) * df * da2 / 24.; }
From source file:lanchester.MultiArena3.java
public void step() { boolean aboveFloor = true; double currentCycle = 0.; int numFoes = forces.size(); if (isMyTurn == null) { isMyTurn = new boolean[numFoes][numFoes]; stanceArray = new int[numFoes][numFoes]; currentFloor = new double[numFoes][numFoes]; currentCeiling = new double[numFoes][numFoes]; for (int i1 = 0; i1 < numFoes; i1++) { int ind1 = forceMap.get(forces.get(i1)); for (int i2 = 0; i2 < numFoes; i2++) { int ind2 = forceMap.get(forces.get(i2)); isMyTurn[i1][i2] = true; if (i1 == i2) { stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE; currentFloor[i1][i2] = 0.; currentCeiling[i1][i2] = 100.; } else { stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2)); setFloor(i1, i2);/*from w ww.j av a 2 s . c o m*/ setCeiling(i1, i2); } } } } Array2DRowRealMatrix mat = getMat(); EigenDecomposition eigen = new EigenDecomposition(mat); double det = eigen.getDeterminant(); double[] eVals = eigen.getRealEigenvalues(); if (eigen.hasComplexEigenvalues()) { System.out.println("Complex eigenvalues"); for (int i1 = 0; i1 < forces.size(); i1++) { MultiForce f = forces.get(i1); System.out.println(f.getName() + " has " + f.getNumber() + " forces remaining"); } } double[] initialNums = getInitialNumbers(forces); Array2DRowRealMatrix eVectors = (Array2DRowRealMatrix) eigen.getV(); LUDecomposition lu = new LUDecomposition(eVectors); double det2 = lu.getDeterminant(); double[] coeffs = new double[numFoes]; for (int i1 = 0; i1 < numFoes; i1++) { Array2DRowRealMatrix tmpMat = (Array2DRowRealMatrix) eVectors.copy(); tmpMat.setColumn(i1, initialNums); LUDecomposition tmpLU = new LUDecomposition(tmpMat); double tmpDet = tmpLU.getDeterminant(); coeffs[i1] = tmpDet / det2; } aboveFloor = true; boolean belowCeiling = true; int cntr = 0; int numGone; do { timeStep = determineTimeStep(); MultiTimeStep currentStep = new MultiTimeStep(numFoes); currentTime += timeStep; currentCycle += timeStep; currentStep.setTime(currentTime); numGone = 0; for (int i1 = 0; i1 < numFoes; i1++) { double updatedForce = 0.; if (stillAlive[i1]) { for (int i2 = 0; i2 < numFoes; i2++) { updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * currentCycle); } if (updatedForce < 1.) { updatedForce = lb; stillAlive[i1] = false; numGone++; } } else { numGone++; updatedForce = lb; } forces.get(i1).updateForce(updatedForce); currentStep.setForceNumber(updatedForce, i1); } history.add(currentStep); aboveFloor = checkAboveFloors(); belowCeiling = checkBelowCeilings(); cntr++; } while (aboveFloor && belowCeiling && cntr < 2000 && (numFoes - numGone) > 1); for (int i1 = 0; i1 < numFoes; i1++) { for (int i2 = 0; i2 < numFoes; i2++) { if (i1 == i2) { stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE; currentFloor[i1][i2] = 0.; } else { stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2)); setFloor(i1, i2); } } } // eVectors. // this.currentTime++; // Truncator truncator = new Truncator(); if (numFoes - numGone == 1) { loneSurvivor = true; // System.out.println("time = " + time); } }
From source file:com.opengamma.analytics.financial.interestrate.annuity.ZSpreadCalculator.java
public Map<String, List<DoublesPair>> calculateZSpreadSensitivityToCurve( final Annuity<? extends Payment> annuity, final YieldCurveBundle curves, final double zSpread) { Validate.notNull(annuity, "annuity"); Validate.notNull(curves, "curves"); final double dPricedZ = calculatePriceSensitivityToZSpread(annuity, curves, zSpread); Validate.isTrue(dPricedZ != 0.0, "Price Sensitivity To ZSpread is zero"); final Map<String, List<DoublesPair>> temp = PV_SENSITIVITY_CALCULATOR.visit(annuity, curves); final Map<String, List<DoublesPair>> result = new HashMap<String, List<DoublesPair>>(); for (final String name : temp.keySet()) { final List<DoublesPair> unadjusted = temp.get(name); final ArrayList<DoublesPair> adjusted = new ArrayList<DoublesPair>(unadjusted.size()); for (final DoublesPair pair : unadjusted) { final DoublesPair newPair = new DoublesPair(pair.first, -pair.second * Math.exp(-zSpread * pair.first) / dPricedZ); adjusted.add(newPair);//w w w. j a v a 2 s . co m } result.put(name, adjusted); } return result; }
From source file:com.opengamma.analytics.math.ComplexMathUtils.java
public static ComplexNumber pow(final ComplexNumber z1, final ComplexNumber z2) { ArgumentChecker.notNull(z1, "z1"); ArgumentChecker.notNull(z2, "z2"); final double mod = mod(z1); final double arg = arg(z1); final double mult = Math.pow(mod, z2.getReal()) * Math.exp(-z2.getImaginary() * arg); final double theta = z2.getReal() * arg + z2.getImaginary() * Math.log(mod); return new ComplexNumber(mult * Math.cos(theta), mult * Math.sin(theta)); }
From source file:com.opengamma.analytics.financial.model.finitedifference.applications.TwoStateMarkovChainPricer.java
private double probState1(final double t) { final double sum = _chainDB.getLambda12() + _chainDB.getLambda21(); return _chainDB.getSteadyStateProb() + (_chainDB.getP0() - _chainDB.getSteadyStateProb()) * Math.exp(-sum * t); }