List of usage examples for java.lang Math exp
@HotSpotIntrinsicCandidate public static double exp(double a)
From source file:com.opengamma.analytics.financial.model.finitedifference.applications.BarrierOptionPricer.java
/** * Computes the price of a one-touch out barrier option in the Black-Scholes world by solving the BS PDE on a finite difference grid. If a barrier is hit at any time before expiry, * the option is cancelled (knocked-out) and a rebate (which is often zero) is paid <b>immediately</b>. If the barrier is not hit, then a normal European option payment is made. <p> * If the barrier is above the spot it is assumed to be an up-and-out barrier (otherwise it would expire immediately) otherwise it is a down-and-out barrier * As there are exact formulae for this case (see {@link BlackBarrierPriceFunction}), this is purely for testing purposes. * @param spot The current (i.e. option price time) of the underlying * @param barrierLevel The barrier level * @param strike The strike of the European option * @param expiry The expiry of the option * @param rate The interest rate.// w ww . j a v a 2s .c om * @param carry The cost-of-carry (i.e. the forward = spot*exp(costOfCarry*T) ) * @param vol The Black volatility. * @param isCall true for call * @param rebate The rebate amount. * @return The price. */ public double outBarrier(final double spot, final double barrierLevel, final double strike, final double expiry, final double rate, final double carry, final double vol, final boolean isCall, final double rebate) { final Function1D<Double, Double> intCon = ICP.getEuropeanPayoff(strike, isCall); final ConvectionDiffusionPDE1DStandardCoefficients pde = PDE.getBlackScholes(rate, rate - carry, vol); final boolean isUp = barrierLevel > spot; final double adj = 0.0; // _lambda == 0 ? ZETA * vol * Math.sqrt(expiry / (_nTNodes - 1)) : 0.0; double sMin; double sMax; BoundaryCondition lower; BoundaryCondition upper; if (isUp) { sMin = 0.0; sMax = barrierLevel * Math.exp(-adj); //bring the barrier DOWN slightly to adjust for discrete monitoring if (isCall) { lower = new DirichletBoundaryCondition(0.0, sMin); } else { final Function1D<Double, Double> lowerValue = new Function1D<Double, Double>() { @Override public Double evaluate(final Double tau) { return Math.exp(-rate * tau) * strike; } }; lower = new DirichletBoundaryCondition(lowerValue, sMin); } upper = new DirichletBoundaryCondition(rebate, sMax); } else { sMin = barrierLevel * Math.exp(adj); //bring the barrier UP slightly to adjust for discrete monitoring sMax = spot * Math.exp(_z * Math.sqrt(expiry)); lower = new DirichletBoundaryCondition(rebate, sMin); if (isCall) { final Function1D<Double, Double> upperValue = new Function1D<Double, Double>() { @Override public Double evaluate(final Double tau) { return Math.exp(-rate * tau) * (spot * Math.exp(carry * tau) - strike); } }; upper = new DirichletBoundaryCondition(upperValue, sMax); } else { upper = new DirichletBoundaryCondition(0.0, sMax); } } final MeshingFunction tMesh = new ExponentialMeshing(0, expiry, _nTNodes, _lambda); final MeshingFunction xMesh = new HyperbolicMeshing(sMin, sMax, spot, _nXNodes, _bunching); final PDEGrid1D grid = new PDEGrid1D(tMesh, xMesh); final PDE1DDataBundle<ConvectionDiffusionPDE1DCoefficients> pdeData = new PDE1DDataBundle<ConvectionDiffusionPDE1DCoefficients>( pde, intCon, lower, upper, grid); final PDEResults1D res = SOLVER.solve(pdeData); //for now just do linear interpolation on price. TODO replace this with something more robust final double[] xNodes = grid.getSpaceNodes(); final int index = SurfaceArrayUtils.getLowerBoundIndex(xNodes, spot); final double w = (xNodes[index + 1] - spot) / (xNodes[index + 1] - xNodes[index]); return w * res.getFunctionValue(index) + (1 - w) * res.getFunctionValue(index + 1); }
From source file:ArrayUtil.java
public static double sumexp(int[] d) { double r = 0; for (int i = 0; i < d.length; i++) r += Math.exp(d[i]); return r;//from w w w . j av a 2 s. c o m }
From source file:etomica.models.co2.PNCO2GCPM.java
/** * This returns the pairwise-additive portion of the GCPM potential for a * pair of atoms (dispersion + fixed-charge electrostatics) *//*from www . j a va2 s . c o m*/ public double getNonPolarizationEnergy(IMoleculeList molecules) { IAtomList water1Atoms = molecules.getMolecule(0).getChildList(); IAtomList water2Atoms = molecules.getMolecule(1).getChildList(); IVectorMutable C1r = water1Atoms.getAtom(0).getPosition(); IVectorMutable C2r = water2Atoms.getAtom(0).getPosition(); work.Ev1Mv2(C1r, C2r); shift.Ea1Tv1(-1, work); boundary.nearestImage(work); shift.PE(work); final boolean zeroShift = shift.squared() < 0.1; double r2 = work.squared(); if (r2 <= sigmaC * coreFac) { return Double.POSITIVE_INFINITY; } IVectorMutable O11r = water1Atoms.getAtom(1).getPosition(); IVectorMutable O12r = water1Atoms.getAtom(2).getPosition(); IVectorMutable O21r = water2Atoms.getAtom(1).getPosition(); IVectorMutable O22r = water2Atoms.getAtom(2).getPosition(); double sum = 0; if (zeroShift) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { r2 = water1Atoms.getAtom(i).getPosition().Mv1Squared(water2Atoms.getAtom(j).getPosition()); double r = Math.sqrt(r2); double rOverSigma = r / sigmaAll[i][j]; double sigma2OverR2 = 1 / (rOverSigma * rOverSigma); if (1 / sigma2OverR2 < coreFac) return Double.POSITIVE_INFINITY; double sixOverGamma = 6 / gamma; sum += epsilonAll[i][j] / (1 - sixOverGamma) * (sixOverGamma * Math.exp(gamma * (1 - rOverSigma)) - sigma2OverR2 * sigma2OverR2 * sigma2OverR2);//exp-6 potential(Udisp) } } } else { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { IVector r1 = water1Atoms.getAtom(i).getPosition(); shift.PE(r1); r2 = water2Atoms.getAtom(j).getPosition().Mv1Squared(shift); shift.ME(r1); double r = Math.sqrt(r2); double rOverSigma = r / sigmaAll[i][j]; double sigma2OverR2 = 1 / (rOverSigma * rOverSigma); if (1 / sigma2OverR2 < coreFac) return Double.POSITIVE_INFINITY; double sixOverGamma = 6 / gamma; sum += epsilonAll[i][j] / (1 - sixOverGamma) * (sixOverGamma * Math.exp(gamma * (1 - rOverSigma)) - sigma2OverR2 * sigma2OverR2 * sigma2OverR2);//exp-6 potential(Udisp) } } } if (zeroShift) { r2 = O11r.Mv1Squared(O21r); sum += chargeO * chargeO / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / (2 * tauO))); r2 = O11r.Mv1Squared(O22r); sum += chargeO * chargeO / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / (2 * tauO))); r2 = O12r.Mv1Squared(O21r); sum += chargeO * chargeO / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / (2 * tauO))); r2 = O12r.Mv1Squared(O22r); sum += chargeO * chargeO / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / (2 * tauO))); r2 = C1r.Mv1Squared(O21r); sum += chargeO * chargeC / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / sqrtCOtau)); r2 = C1r.Mv1Squared(O22r); sum += chargeO * chargeC / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / sqrtCOtau)); r2 = C2r.Mv1Squared(O11r); sum += chargeO * chargeC / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / sqrtCOtau)); r2 = C2r.Mv1Squared(O12r); sum += chargeO * chargeC / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / sqrtCOtau)); r2 = C1r.Mv1Squared(C2r); sum += chargeC * chargeC / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / (2 * tauC))); } else { shift.PE(O11r); r2 = O21r.Mv1Squared(shift); shift.ME(O11r); sum += chargeO * chargeO / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / (2 * tauO))); shift.PE(O11r); r2 = O22r.Mv1Squared(shift); shift.ME(O11r); sum += chargeO * chargeO / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / (2 * tauO))); shift.PE(O12r); r2 = O21r.Mv1Squared(shift); shift.ME(O12r); sum += chargeO * chargeO / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / (2 * tauO))); shift.PE(O12r); r2 = O22r.Mv1Squared(shift); shift.ME(O12r); sum += chargeO * chargeO / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / (2 * tauO))); shift.PE(C1r); r2 = O21r.Mv1Squared(shift); shift.ME(C1r); sum += chargeO * chargeC / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / sqrtCOtau)); shift.PE(C1r); r2 = O22r.Mv1Squared(shift); shift.ME(C1r); sum += chargeO * chargeC / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / sqrtCOtau)); shift.PE(O11r); r2 = C2r.Mv1Squared(shift); shift.ME(O11r); sum += chargeO * chargeC / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / sqrtCOtau)); shift.PE(O12r); r2 = C2r.Mv1Squared(shift); shift.ME(O12r); sum += chargeO * chargeC / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / sqrtCOtau)); shift.PE(C1r); r2 = C2r.Mv1Squared(shift); shift.ME(C1r); sum += chargeC * chargeC / Math.sqrt(r2) * (1 - org.apache.commons.math3.special.Erf.erfc(Math.sqrt(r2) / (2 * tauC))); } return sum; }
From source file:com.opengamma.analytics.financial.interestrate.swaption.provider.SwaptionBermudaFixedIborHullWhiteNumericalIntegrationMethod.java
/** * Computes the present value of the Physical delivery swaption. * @param swaption The swaption./*from w w w .j a v a2s . co m*/ * @param hullWhite The Hull-White parameters and the curves. * @return The present value. */ public MultipleCurrencyAmount presentValue(final SwaptionBermudaFixedIbor swaption, final HullWhiteOneFactorProviderInterface hullWhite) { ArgumentChecker.notNull(swaption, "Swaption"); ArgumentChecker.notNull(hullWhite, "Hull-White provider"); MulticurveProviderInterface multicurves = hullWhite.getMulticurveProvider(); HullWhiteOneFactorPiecewiseConstantParameters parameters = hullWhite.getHullWhiteParameters(); Currency ccy = swaption.getCurrency(); final int nbExpiry = swaption.getExpiryTime().length; Validate.isTrue(nbExpiry > 1, "At least two expiry dates required for this method"); double tmpdb; final double[] theta = new double[nbExpiry + 1]; // Extended expiry time (with 0). theta[0] = 0.0; System.arraycopy(swaption.getExpiryTime(), 0, theta, 1, nbExpiry); final AnnuityPaymentFixed[] cashflow = new AnnuityPaymentFixed[nbExpiry]; for (int loopexp = 0; loopexp < nbExpiry; loopexp++) { cashflow[loopexp] = swaption.getUnderlyingSwap()[loopexp].accept(CFEC, multicurves); } final int[] n = new int[nbExpiry]; final double[][][] alpha = new double[nbExpiry][][]; final double[][][] alpha2 = new double[nbExpiry][][]; // alpha^2 for (int loopexp = 0; loopexp < nbExpiry; loopexp++) { n[loopexp] = cashflow[loopexp].getNumberOfPayments(); alpha[loopexp] = new double[loopexp + 1][]; alpha2[loopexp] = new double[loopexp + 1][]; for (int k = 0; k <= loopexp; k++) { alpha[loopexp][k] = new double[n[loopexp]]; alpha2[loopexp][k] = new double[n[loopexp]]; for (int l = 0; l < alpha[loopexp][k].length; l++) { alpha[loopexp][k][l] = MODEL.alpha(parameters, theta[k], theta[k + 1], theta[k + 1], cashflow[loopexp].getNthPayment(l).getPaymentTime()); alpha2[loopexp][k][l] = alpha[loopexp][k][l] * alpha[loopexp][k][l]; } } } final int nbPoint2 = 2 * NB_POINT + 1; final int[] startInt = new int[nbExpiry - 1]; final int[] endInt = new int[nbExpiry - 1]; for (int i = 1; i < nbExpiry - 1; i++) { startInt[i] = 0; endInt[i] = nbPoint2 - 1; } startInt[0] = NB_POINT; endInt[0] = NB_POINT; final double[][] t = new double[nbExpiry][]; // payment time final double[][] dfS = new double[nbExpiry][]; // discount factor final double[] beta = new double[nbExpiry]; final double[][] h = new double[nbExpiry][]; final double[][] sa2 = new double[nbExpiry][]; for (int loopexp = 0; loopexp < nbExpiry; loopexp++) { beta[loopexp] = MODEL.beta(parameters, theta[loopexp], theta[loopexp + 1]); t[loopexp] = new double[n[loopexp]]; dfS[loopexp] = new double[n[loopexp]]; h[loopexp] = new double[n[loopexp]]; sa2[loopexp] = new double[n[loopexp]]; for (int loopcf = 0; loopcf < n[loopexp]; loopcf++) { t[loopexp][loopcf] = cashflow[loopexp].getNthPayment(loopcf).getPaymentTime(); dfS[loopexp][loopcf] = multicurves.getDiscountFactor(ccy, t[loopexp][loopcf]); h[loopexp][loopcf] = (1 - Math.exp(-parameters.getMeanReversion() * t[loopexp][loopcf])) / parameters.getMeanReversion(); tmpdb = 0.0; for (int k = 0; k <= loopexp; k++) { tmpdb += alpha2[loopexp][k][loopcf]; } sa2[loopexp][loopcf] = tmpdb; } } final double[] discountedCashFlowN = new double[n[nbExpiry - 1]]; for (int loopcf = 0; loopcf < n[nbExpiry - 1]; loopcf++) { discountedCashFlowN[loopcf] = dfS[nbExpiry - 1][loopcf] * cashflow[nbExpiry - 1].getNthPayment(loopcf).getAmount(); } final double lambda = MODEL.lambda(discountedCashFlowN, sa2[nbExpiry - 1], h[nbExpiry - 1]); final double[] betaSort = new double[nbExpiry]; System.arraycopy(beta, 0, betaSort, 0, nbExpiry); Arrays.sort(betaSort); final double minbeta = betaSort[0]; final double maxbeta = betaSort[nbExpiry - 1]; final double b = Math.min(10 * minbeta, maxbeta); final double epsilon = -2.0 / NB_POINT * NORMAL.getInverseCDF(1.0 / (200.0 * NB_POINT)) * b; // <- final double[] bX = new double[nbPoint2]; for (int looppt = 0; looppt < nbPoint2; looppt++) { bX[looppt] = -NB_POINT * epsilon + looppt * epsilon; } final double[] bX2 = new double[4 * NB_POINT + 1]; for (int looppt = 0; looppt < 4 * NB_POINT + 1; looppt++) { bX2[looppt] = -2 * NB_POINT * epsilon + looppt * epsilon; } final double[] htheta = new double[nbExpiry]; for (int loopexp = 0; loopexp < nbExpiry; loopexp++) { htheta[loopexp] = (1 - Math.exp(-parameters.getMeanReversion() * theta[loopexp + 1])) / parameters.getMeanReversion(); } final double[][] vZ = new double[nbExpiry - 1][nbPoint2]; for (int i = nbExpiry - 2; i >= 0; i--) { for (int looppt = 0; looppt < nbPoint2; looppt++) { vZ[i][looppt] = Math.exp(bX[looppt] * htheta[i]); } } final double[][] vW = new double[nbExpiry][]; // Swaption final double[][] vT = new double[nbExpiry - 1][]; // Swap final double omega = -Math.signum(cashflow[nbExpiry - 1].getNthPayment(0).getAmount()); final double[] kappaL = new double[nbPoint2]; for (int looppt = 0; looppt < nbPoint2; looppt++) { kappaL[looppt] = (lambda - bX[looppt]) / beta[nbExpiry - 1]; } final double[] sa2N1 = new double[n[nbExpiry - 1]]; for (int i = 0; i < n[nbExpiry - 1]; i++) { tmpdb = 0; for (int k = 0; k <= nbExpiry - 2; k++) { tmpdb += alpha2[nbExpiry - 1][k][i]; } sa2N1[i] = tmpdb; } vW[nbExpiry - 1] = new double[4 * NB_POINT + 1]; for (int j = 0; j < n[nbExpiry - 1]; j++) { for (int looppt = 0; looppt < nbPoint2; looppt++) { vW[nbExpiry - 1][NB_POINT + looppt] += discountedCashFlowN[j] * Math.exp(-sa2N1[j] / 2.0 - h[nbExpiry - 1][j] * bX[looppt]) * NORMAL.getCDF(omega * (kappaL[looppt] + alpha[nbExpiry - 1][nbExpiry - 1][j])); } } for (int looppt = 0; looppt < NB_POINT; looppt++) { vW[nbExpiry - 1][looppt] = vW[nbExpiry - 1][NB_POINT]; } for (int looppt = 0; looppt < NB_POINT; looppt++) { vW[nbExpiry - 1][3 * NB_POINT + 1 + looppt] = vW[nbExpiry - 1][3 * NB_POINT]; } final double c1sqrt2pi = 1.0 / Math.sqrt(2 * Math.PI); final double[][] pvcfT = new double[nbExpiry - 1][]; double[] vL; // Left side of intersection double[] vR; // Right side of intersection double[][] labc; double[][] rabc; final double[][] labcM = new double[3][4 * NB_POINT + 1]; final double[][] rabcM = new double[3][4 * NB_POINT + 1]; final double[] dabc = new double[3]; final int[] indSwap = new int[nbExpiry - 1]; // index of the intersection double xroot; final double[][] xN = new double[nbExpiry - 1][nbPoint2]; double ci; double coi; int is; final double[] ncdf0 = new double[nbPoint2]; final double[] ncdf1 = new double[nbPoint2]; final double[] ncdf2 = new double[nbPoint2]; final double[] ncdf0X = new double[nbPoint2 + 1]; final double[] ncdf1X = new double[nbPoint2 + 1]; final double[] ncdf2X = new double[nbPoint2 + 1]; double ncdf0x; double ncdf1x; double ncdf2x; double ncdfinit; // Main loop for the different expiry dates (except the last one) for (int i = nbExpiry - 2; i >= 0; i--) { vW[i] = new double[4 * NB_POINT + 1]; vT[i] = new double[4 * NB_POINT + 1]; // T: swap pvcfT[i] = new double[n[i]]; for (int j = 0; j < n[i]; j++) { pvcfT[i][j] = cashflow[i].getNthPayment(j).getAmount() * dfS[i][j]; for (int looppt = 0; looppt < 4 * NB_POINT + 1; looppt++) { vT[i][looppt] += pvcfT[i][j] * Math.exp(-sa2[i][j] / 2.0 - h[i][j] * bX2[looppt]); } } // Preparation for (int looppt = 0; looppt < nbPoint2; looppt++) { xN[i][looppt] = bX[looppt] / beta[i]; } ci = htheta[i] * beta[i]; coi = Math.exp(ci * ci / 2); // Left/Right if (omega < 0) { vL = vW[i + 1]; vR = vT[i]; } else { vR = vW[i + 1]; vL = vT[i]; } indSwap[i] = 0; while (vL[indSwap[i] + 1] >= vR[indSwap[i] + 1]) { indSwap[i]++; } // Parabola fit labc = parafit(epsilon / beta[i], vL); rabc = parafit(epsilon / beta[i], vR); for (int k = 0; k < 3; k++) { dabc[k] = labc[k][indSwap[i]] - rabc[k][indSwap[i]]; System.arraycopy(labc[k], 0, labcM[k], 0, indSwap[i] + 1); System.arraycopy(labc[k], indSwap[i], labcM[k], indSwap[i] + 1, labc[k].length - indSwap[i]); System.arraycopy(rabc[k], 0, rabcM[k], 0, indSwap[i] + 1); System.arraycopy(rabc[k], indSwap[i], rabcM[k], indSwap[i] + 1, rabc[k].length - indSwap[i]); } for (int looppt = 0; looppt < 4 * NB_POINT + 1; looppt++) { labcM[1][looppt] = labcM[1][looppt] + labcM[0][looppt] * 2 * ci; labcM[2][looppt] = labcM[2][looppt] + labcM[1][looppt] * ci - labcM[0][looppt] * ci * ci; rabcM[1][looppt] = rabcM[1][looppt] + rabcM[0][looppt] * 2 * ci; rabcM[2][looppt] = rabcM[2][looppt] + rabcM[1][looppt] * ci - rabcM[0][looppt] * ci * ci; } xroot = (-dabc[1] - Math.sqrt(dabc[1] * dabc[1] - 4 * dabc[0] * dabc[2])) / (2 * dabc[0]); ncdfinit = NORMAL.getCDF(xN[i][0]); for (int looppt = 0; looppt < nbPoint2; looppt++) { ncdf0[looppt] = NORMAL.getCDF(xN[i][looppt] - ci); ncdf1[looppt] = -c1sqrt2pi * Math.exp(-(xN[i][looppt] - ci) * (xN[i][looppt] - ci) / 2.0); ncdf2[looppt] = ncdf1[looppt] * (xN[i][looppt] - ci) + ncdf0[looppt]; } for (int j = startInt[i]; j <= endInt[i]; j++) { is = indSwap[i] - j + 1; // % all L if (j + 2 * NB_POINT <= indSwap[i]) { final double[][] xabc = new double[3][2 * NB_POINT]; for (int k = 0; k < 3; k++) { System.arraycopy(labcM[k], j, xabc[k], 0, 2 * NB_POINT); } for (int looppt = 0; looppt < 2 * NB_POINT; looppt++) { xabc[1][looppt] = xabc[1][looppt] + xabc[0][looppt] * 2 * xN[i][j]; xabc[2][looppt] = xabc[2][looppt] + xabc[1][looppt] * xN[i][j] - xabc[0][looppt] * xN[i][j] * xN[i][j]; } vW[i][j + NB_POINT] = 0; vW[i][j + NB_POINT] = vW[i][j + NB_POINT] + coi * ni2ncdf(ncdf2, ncdf1, ncdf0, xabc); } else if (j < indSwap[i]) { final double[][] xabc = new double[3][2 * NB_POINT + 1]; tmpdb = xroot - xN[i][j] - ci; ncdf0x = NORMAL.getCDF(tmpdb); ncdf1x = -Math.exp(-(tmpdb * tmpdb) / 2) * c1sqrt2pi; ncdf2x = ncdf1x * tmpdb + ncdf0x; for (int k = 0; k < 3; k++) { // System.arraycopy(rabcM[k], j, xabc[k], 0, 2 * _nbPoint + 1); // Swap System.arraycopy(labcM[k], j, xabc[k], 0, 2 * NB_POINT + 1); System.arraycopy(rabcM[k], indSwap[i] + 1, xabc[k], indSwap[i] + 1 - j, j + 2 * NB_POINT - indSwap[i]); } for (int looppt = 0; looppt < 2 * NB_POINT; looppt++) { xabc[1][looppt] = xabc[1][looppt] + xabc[0][looppt] * 2 * xN[i][j]; xabc[2][looppt] = xabc[2][looppt] + xabc[1][looppt] * xN[i][j] - xabc[0][looppt] * xN[i][j] * xN[i][j]; } System.arraycopy(ncdf0, 0, ncdf0X, 0, is); ncdf0X[is] = ncdf0x; System.arraycopy(ncdf0, is, ncdf0X, is + 1, ncdf0.length - is); System.arraycopy(ncdf1, 0, ncdf1X, 0, is); ncdf1X[is] = ncdf1x; System.arraycopy(ncdf1, is, ncdf1X, is + 1, ncdf1.length - is); System.arraycopy(ncdf2, 0, ncdf2X, 0, is); ncdf2X[is] = ncdf2x; System.arraycopy(ncdf2, is, ncdf2X, is + 1, ncdf2.length - is); vW[i][j + NB_POINT] = vL[j] * vZ[i][0] * ncdfinit; vW[i][j + NB_POINT] += coi * ni2ncdf(ncdf2X, ncdf1X, ncdf0X, xabc); vW[i][j + NB_POINT] += vR[j + 2 * NB_POINT] * vZ[i][vZ[i].length - 1] * ncdfinit; } else { final double[][] xabc = new double[3][2 * NB_POINT]; for (int k = 0; k < 3; k++) { System.arraycopy(rabcM[k], j + 1, xabc[k], 0, 2 * NB_POINT); // System.arraycopy(labcM[k], j + 1, xabc[k], 0, 2 * _nbPoint); // Swaption } for (int looppt = 0; looppt < 2 * NB_POINT; looppt++) { xabc[1][looppt] = xabc[1][looppt] + xabc[0][looppt] * 2 * xN[i][j]; xabc[2][looppt] = xabc[2][looppt] + xabc[1][looppt] * xN[i][j] - xabc[0][looppt] * xN[i][j] * xN[i][j]; } vW[i][j + NB_POINT] = vR[j] * vZ[i][0] * ncdfinit; vW[i][j + NB_POINT] += coi * ni2ncdf(ncdf2, ncdf1, ncdf0, xabc); vW[i][j + NB_POINT] += vR[j + 2 * NB_POINT] * vZ[i][vZ[i].length - 1] * ncdfinit; } } for (int j = 0; j < NB_POINT; j++) { // Flat extrapolation vW[i][j] = vW[i][NB_POINT]; vW[i][3 * NB_POINT + 1 + j] = vW[i][3 * NB_POINT]; } } // End main loop return MultipleCurrencyAmount.of(swaption.getUnderlyingSwap()[0].getFixedLeg().getCurrency(), vW[0][2 * NB_POINT] * (swaption.isLong() ? 1.0 : -1.0)); }
From source file:com.opengamma.analytics.financial.credit.creditdefaultswap.pricing.vanilla.isdanew.ISDACompliantCurve.java
/** * The discount factor or survival probability * @param t Time//ww w . j a v a 2 s . c o m * @return value */ public double getDiscountFactor(final double t) { ArgumentChecker.isTrue(t >= 0, "require t >= 0.0"); if (t == 0.0) { return 1.0; } final int index = Arrays.binarySearch(_t, t); if (index >= 0) { return _df[index]; } final int insertionPoint = -(1 + index); final double rt = getRT(t, insertionPoint); return Math.exp(-rt); }
From source file:dr.evomodel.branchmodel.lineagespecific.DirichletProcessOperator.java
private void doOp() throws MathException { for (int index = 0; index < realizationCount; index++) { int[] occupancy = new int[uniqueRealizationCount]; for (int i = 0; i < realizationCount; i++) { if (i != index) { int j = (int) categoriesParameter.getParameterValue(i); occupancy[j]++;// w ww . java 2 s .co m } // END: i check } // END: i loop double[] existingValues = new double[uniqueRealizationCount]; int counter = 0; int singletonIndex = -1; for (int i = 0; i < uniqueRealizationCount; i++) { if (occupancy[i] > 0) { occupancy[counter] = occupancy[i]; existingValues[counter++] = dpp.getUniqueParameter(i).getParameterValue(0); } else { singletonIndex = i; } //END: occupancy check } //END: i loop // Propose new value(s) double[] baseProposals = new double[realizationCount]; for (int i = 0; i < baseProposals.length; i++) { baseProposals[i] = dpp.baseModel.nextRandom()[0]; } // If a singleton if (singletonIndex > -1) { baseProposals[0] = uniqueParameters.getParameterValue(singletonIndex); } double[] logClusterProbs = new double[uniqueRealizationCount]; // draw existing int i; for (i = 0; i < counter; i++) { logClusterProbs[i] = Math.log(occupancy[i] / (realizationCount - 1 + intensity)); double value = allParameters.getParameterValue(index); double candidate = existingValues[i]; allParameters.setParameterValue(index, candidate); likelihood.makeDirty(); logClusterProbs[i] = logClusterProbs[i] + likelihood.getLikelihood(index).getLogLikelihood(); // logClusterProbs[i] = logClusterProbs[i] + likelihood .getLogLikelihood(); // System.out.println(likelihood.getLikelihood(index) .getLogLikelihood() + " " + likelihood .getLogLikelihood()); allParameters.setParameterValue(index, value); likelihood.makeDirty(); } // draw new for (; i < logClusterProbs.length; i++) { logClusterProbs[i] = Math.log((intensity) / (realizationCount - 1 + intensity)); // logClusterProbs[i] = Math.log(intensity / uniqueRealizationCount / (realizationCount - 1 + intensity)); double value = allParameters.getParameterValue(index); double candidate = baseProposals[i - counter]; allParameters.setParameterValue(index, candidate); likelihood.makeDirty(); logClusterProbs[i] = logClusterProbs[i] + likelihood.getLikelihood(index).getLogLikelihood(); // logClusterProbs[i] = logClusterProbs[i] + likelihood.getLogLikelihood(); // System.out.println(likelihood.getLikelihood(index) .getLogLikelihood() + " " + likelihood .getLogLikelihood()); allParameters.setParameterValue(index, value); likelihood.makeDirty(); } double smallestVal = logClusterProbs[0]; for (i = 1; i < uniqueRealizationCount; i++) { if (smallestVal > logClusterProbs[i]) { smallestVal = logClusterProbs[i]; } } double[] clusterProbs = new double[uniqueRealizationCount]; for (i = 0; i < clusterProbs.length; i++) { clusterProbs[i] = Math.exp(logClusterProbs[i] - smallestVal); } // dr.app.bss.Utils.printArray(clusterProbs); // System.exit(-1); // sample int sampledCluster = MathUtils.randomChoicePDF(clusterProbs); categoriesParameter.setParameterValue(index, sampledCluster); } //END: index loop }
From source file:com.opengamma.analytics.financial.interestrate.annuity.YieldSensitivityCalculator.java
/** * For a set of cash flows calculates the nth derivative of its PV with respect to its continuously compounded yield multiplied by the * factor (-1)^n which just keeps the sign positive when cash flows are positive * @param annuity Set of known cash flows * @param yield Continuously compounded constant interest rate * @param order The order of the derivative * @return nth order yield sensitivity (times (-1)^n) *//* w ww. j a v a 2s . c o m*/ public double calculateNthOrderSensitivityFromYield(final Annuity<? extends PaymentFixed> annuity, final double yield, final int order) { Validate.notNull(annuity, "annuity"); Validate.isTrue(order >= 0, "order must be positive"); double sum = 0; double t; double tPower; final int n = annuity.getNumberOfPayments(); PaymentFixed temp; for (int i = 0; i < n; i++) { temp = annuity.getNthPayment(i); t = temp.getPaymentTime(); tPower = Math.pow(t, order); sum += temp.getAmount() * tPower * Math.exp(-yield * t); } return sum; }
From source file:foam.zizim.android.BoskoiService.java
private void showNotification(String tickerText) { // This is what should be launched if the user selects our notification. Intent baseIntent = new Intent(this, IncidentsTab.class); baseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, baseIntent, 0); // choose the ticker text newBoskoiReportNotification = new Notification(R.drawable.favicon, tickerText, System.currentTimeMillis()); newBoskoiReportNotification.contentIntent = contentIntent; newBoskoiReportNotification.flags = Notification.FLAG_AUTO_CANCEL; newBoskoiReportNotification.defaults = Notification.DEFAULT_ALL; newBoskoiReportNotification.setLatestEventInfo(this, TAG, tickerText, contentIntent); if (ringtone) { // set the ringer Uri ringURI = Uri.fromFile(new File("/system/media/audio/ringtones/ringer.mp3")); newBoskoiReportNotification.sound = ringURI; }/*from w w w .ja v a2 s .c o m*/ if (vibrate) { double vibrateLength = 100 * Math.exp(0.53 * 20); long[] vibrate = new long[] { 100, 100, (long) vibrateLength }; newBoskoiReportNotification.vibrate = vibrate; if (flashLed) { int color = Color.BLUE; newBoskoiReportNotification.ledARGB = color; } newBoskoiReportNotification.ledOffMS = (int) vibrateLength; newBoskoiReportNotification.ledOnMS = (int) vibrateLength; newBoskoiReportNotification.flags = newBoskoiReportNotification.flags | Notification.FLAG_SHOW_LIGHTS; } mNotificationManager.notify(NOTIFICATION_ID, newBoskoiReportNotification); }
From source file:com.opengamma.analytics.financial.interestrate.future.method.BondFutureHullWhiteMethod.java
/** * Computes the future price from the curves used to price the underlying bonds and a Hull-White one factor model. * @param future The future security./* www . j a v a 2s .c o m*/ * @param hwData The curve and Hull-White parameters. * @param nbPoint The number of point in the numerical cross estimation. * @return The future price. */ public double price(final BondFuture future, final HullWhiteOneFactorPiecewiseConstantDataBundle hwData, final int nbPoint) { Validate.notNull(future, "Future"); Validate.notNull(hwData, "Hull-White data bundle"); final int nbBond = future.getDeliveryBasket().length; final YieldAndDiscountCurve bndCurve = hwData .getCurve(future.getDeliveryBasket()[0].getDiscountingCurveName()); final double expiry = future.getNoticeLastTime(); final double delivery = future.getDeliveryLastTime(); final double dfdelivery = bndCurve.getDiscountFactor(delivery); // Constructing non-homogeneous point series for the numerical estimations. final int nbPtWing = ((int) Math.floor(nbPoint / 20.)); // Number of point on each wing. final int nbPtCenter = nbPoint - 2 * nbPtWing; final double prob = 1.0 / (2.0 * nbPtCenter); final double xStart = NORMAL.getInverseCDF(prob); final double[] x = new double[nbPoint]; for (int loopwing = 0; loopwing < nbPtWing; loopwing++) { x[loopwing] = xStart * (1.0 + (nbPtWing - loopwing) / 2.0); x[nbPoint - 1 - loopwing] = -xStart * (1.0 + (nbPtWing - loopwing) / 2.0); } for (int loopcent = 0; loopcent < nbPtCenter; loopcent++) { x[nbPtWing + loopcent] = xStart + loopcent * (-2.0 * xStart) / (nbPtCenter - 1); } // Figures for each bond final double[][] cfTime = new double[nbBond][]; final double[][] df = new double[nbBond][]; final double[][] alpha = new double[nbBond][]; final double[][] beta = new double[nbBond][]; final double[][] cfaAdjusted = new double[nbBond][]; final double[] e = new double[nbBond]; final double[][] pv = new double[nbPoint][nbBond]; final AnnuityPaymentFixed[] cf = new AnnuityPaymentFixed[nbBond]; for (int loopbnd = 0; loopbnd < nbBond; loopbnd++) { cf[loopbnd] = future.getDeliveryBasket()[loopbnd].accept(CFEC, hwData); final int nbCf = cf[loopbnd].getNumberOfPayments(); cfTime[loopbnd] = new double[nbCf]; df[loopbnd] = new double[nbCf]; alpha[loopbnd] = new double[nbCf]; beta[loopbnd] = new double[nbCf]; cfaAdjusted[loopbnd] = new double[nbCf]; for (int loopcf = 0; loopcf < nbCf; loopcf++) { cfTime[loopbnd][loopcf] = cf[loopbnd].getNthPayment(loopcf).getPaymentTime(); df[loopbnd][loopcf] = bndCurve.getDiscountFactor(cfTime[loopbnd][loopcf]); alpha[loopbnd][loopcf] = MODEL.alpha(hwData.getHullWhiteParameter(), 0.0, expiry, delivery, cfTime[loopbnd][loopcf]); beta[loopbnd][loopcf] = MODEL.futuresConvexityFactor(hwData.getHullWhiteParameter(), expiry, cfTime[loopbnd][loopcf], delivery); cfaAdjusted[loopbnd][loopcf] = df[loopbnd][loopcf] / dfdelivery * beta[loopbnd][loopcf] * cf[loopbnd].getNthPayment(loopcf).getAmount() / future.getConversionFactor()[loopbnd]; for (int looppt = 0; looppt < nbPoint; looppt++) { pv[looppt][loopbnd] += cfaAdjusted[loopbnd][loopcf] * Math.exp(-alpha[loopbnd][loopcf] * alpha[loopbnd][loopcf] / 2.0 - alpha[loopbnd][loopcf] * x[looppt]); } } e[loopbnd] = future.getDeliveryBasket()[loopbnd].getAccruedInterest() / future.getConversionFactor()[loopbnd]; for (int looppt = 0; looppt < nbPoint; looppt++) { pv[looppt][loopbnd] -= e[loopbnd]; } } // Minimum: create a list of index of the CTD in each interval and a first estimate of the crossing point (x[]). final double[] pvMin = new double[nbPoint]; final int[] indMin = new int[nbPoint]; for (int looppt = 0; looppt < nbPoint; looppt++) { pvMin[looppt] = Double.POSITIVE_INFINITY; for (int loopbnd = 0; loopbnd < nbBond; loopbnd++) { if (pv[looppt][loopbnd] < pvMin[looppt]) { pvMin[looppt] = pv[looppt][loopbnd]; indMin[looppt] = loopbnd; } } } final ArrayList<Double> refx = new ArrayList<>(); final ArrayList<Integer> ctd = new ArrayList<>(); int lastInd = indMin[0]; ctd.add(indMin[0]); for (int looppt = 1; looppt < nbPoint; looppt++) { if (indMin[looppt] != lastInd) { ctd.add(indMin[looppt]); lastInd = indMin[looppt]; refx.add(x[looppt]); } } // Sum on each interval final int nbInt = ctd.size(); final double[] kappa = new double[nbInt - 1]; double price = 0.0; if (nbInt == 1) { for (int loopcf = 0; loopcf < cfaAdjusted[ctd.get(0)].length; loopcf++) { price += cfaAdjusted[ctd.get(0)][loopcf]; } price -= e[ctd.get(0)]; } else { // The intersections final BracketRoot bracketer = new BracketRoot(); final double accuracy = 1.0E-8; final RidderSingleRootFinder rootFinder = new RidderSingleRootFinder(accuracy); for (int loopint = 1; loopint < nbInt; loopint++) { final BondDifference cross = new BondDifference(cfaAdjusted[ctd.get(loopint - 1)], alpha[ctd.get(loopint - 1)], e[ctd.get(loopint - 1)], cfaAdjusted[ctd.get(loopint)], alpha[ctd.get(loopint)], e[ctd.get(loopint)]); final double[] range = bracketer.getBracketedPoints(cross, refx.get(loopint - 1) - 0.01, refx.get(loopint - 1) + 0.01); kappa[loopint - 1] = rootFinder.getRoot(cross, range[0], range[1]); } // From -infinity to first cross. for (int loopcf = 0; loopcf < cfaAdjusted[ctd.get(0)].length; loopcf++) { price += cfaAdjusted[ctd.get(0)][loopcf] * NORMAL.getCDF(kappa[0] + alpha[ctd.get(0)][loopcf]); } price -= e[ctd.get(0)] * NORMAL.getCDF(kappa[0]); // Between cross for (int loopint = 1; loopint < nbInt - 1; loopint++) { for (int loopcf = 0; loopcf < cfaAdjusted[ctd.get(loopint)].length; loopcf++) { price += cfaAdjusted[ctd.get(loopint)][loopcf] * (NORMAL.getCDF(kappa[loopint] + alpha[ctd.get(loopint)][loopcf]) - NORMAL.getCDF(kappa[loopint - 1] + alpha[ctd.get(loopint)][loopcf])); } price -= e[ctd.get(loopint)] * (NORMAL.getCDF(kappa[loopint]) - NORMAL.getCDF(kappa[loopint - 1])); } // From last cross to +infinity for (int loopcf = 0; loopcf < cfaAdjusted[ctd.get(nbInt - 1)].length; loopcf++) { price += cfaAdjusted[ctd.get(nbInt - 1)][loopcf] * (1.0 - NORMAL.getCDF(kappa[nbInt - 2] + alpha[ctd.get(nbInt - 1)][loopcf])); } price -= e[ctd.get(nbInt - 1)] * (1 - NORMAL.getCDF(kappa[nbInt - 2])); } return price; }
From source file:cc.clabs.stratosphere.mlp.contracts.CandidateEmitter.java
/** * Returns the value of the gaussian function * at x. C is a real constant. One can control * how steep the curve will fall down by choosing * lower values of C./*ww w . j a va 2s .com*/ * * @param x * @param C * @return */ private Double gaussian(Double x, Double C) { return Math.exp(-Math.pow(x, 2d) / (2d * Math.pow(2d * C, 2d))); }