当前位置: 首页>>代码示例>>Java>>正文


Java CalculationException类代码示例

本文整理汇总了Java中net.finmath.exception.CalculationException的典型用法代码示例。如果您正苦于以下问题:Java CalculationException类的具体用法?Java CalculationException怎么用?Java CalculationException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


CalculationException类属于net.finmath.exception包,在下文中一共展示了CalculationException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getValue

import net.finmath.exception.CalculationException; //导入依赖的package包/类
/**
 * This method returns the value random variable of the product within the specified model, evaluated at a given evalutationTime.
 * Note: For a lattice this is often the value conditional to evalutationTime, for a Monte-Carlo simulation this is the (sum of) value discounted to evaluation time.
 * Cashflows prior evaluationTime are not considered.
 * 
 * @param evaluationTime The time on which this products value should be observed.
 * @param model The model used to price the product.
 * @return The random variable representing the value of the product discounted to evaluation time
 * @throws net.finmath.exception.CalculationException Thrown if the valuation fails, specific cause may be available via the <code>cause()</code> method.
 */
@Override
public RandomVariableInterface getValue(double evaluationTime, AssetModelMonteCarloSimulationInterface model) throws CalculationException {
	// Calculate average
	RandomVariableInterface average = model.getRandomVariableForConstant(0.0);
	for(double time : timesForAveraging) {
		RandomVariableInterface underlying	= model.getAssetValue(time, underlyingIndex);
		average = average.add(underlying);
	}
	average = average.div(timesForAveraging.getNumberOfTimes());
	
	// The payoff: values = max(underlying - strike, 0)
	RandomVariableInterface values = average.sub(strike).floor(0.0);

	// Discounting...
	RandomVariableInterface numeraireAtMaturity		= model.getNumeraire(maturity);
	RandomVariableInterface monteCarloWeights		= model.getMonteCarloWeights(maturity);
	values = values.div(numeraireAtMaturity).mult(monteCarloWeights);

	// ...to evaluation time.
	RandomVariableInterface	numeraireAtEvalTime					= model.getNumeraire(evaluationTime);
	RandomVariableInterface	monteCarloProbabilitiesAtEvalTime	= model.getMonteCarloWeights(evaluationTime);
	values = values.mult(numeraireAtEvalTime).div(monteCarloProbabilitiesAtEvalTime);

	return values;
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:36,代码来源:AsianOption.java

示例2: testModelRandomVariable

import net.finmath.exception.CalculationException; //导入依赖的package包/类
/**
 * @throws CalculationException Thrown if s.th. went wrong during calculation (check getCause for details).
 */
@Test
public void testModelRandomVariable() throws CalculationException {
	/*
	 * Create the valuation model (see <code>getModel</code>)
	 */
	AssetModelMonteCarloSimulationInterface model = getModel();

	RandomVariableInterface stockAtTimeOne = model.getAssetValue(1.0, 0);

	System.out.println("The first 100 realizations of the " + stockAtTimeOne.size() + " realizations of S(1) are:");
	System.out.println("Path\tValue");
	for(int i=0; i<100;i++) {
		System.out.println(i + "\t" + stockAtTimeOne.get(i));
	}
	
	double spot = stockAtTimeOne.div(model.getNumeraire(1.0)).mult(model.getNumeraire(model.getTime(0))).getAverage();
	System.out.println("Expectation of S(1)/N(1)*N(0) = " + spot + " (expected " + initialValue + ")");
	Assert.assertEquals(initialValue, spot, 2E-3);
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:23,代码来源:BachelierModelMonteCarloValuationTest.java

示例3: testProductImplementation

import net.finmath.exception.CalculationException; //导入依赖的package包/类
@Test
public void testProductImplementation() throws CalculationException {
	// Create a model
	AbstractModel model = new BlackScholesModel(initialValue, riskFreeRate, volatility);

	// Create a time discretization
	TimeDiscretizationInterface timeDiscretization = new TimeDiscretization(0.0 /* initial */, numberOfTimeSteps, deltaT);

	// Create a corresponding MC process
	AbstractProcess process = new ProcessEulerScheme(new BrownianMotion(timeDiscretization, 1 /* numberOfFactors */, numberOfPaths, seed));

	// Using the process (Euler scheme), create an MC simulation of a Black-Scholes model
	AssetModelMonteCarloSimulationInterface monteCarloBlackScholesModel = new MonteCarloAssetModel(model, process);

	/*
	 * Value a call option (using the product implementation)
	 */
	EuropeanOption europeanOption = new EuropeanOption(optionMaturity, optionStrike);
	double value = europeanOption.getValue(monteCarloBlackScholesModel);
	double valueAnalytic = AnalyticFormulas.blackScholesOptionValue(initialValue, riskFreeRate, volatility, optionMaturity, optionStrike);

	System.out.println("value using Monte-Carlo.......: " + value);
	System.out.println("value using analytic formula..: " + valueAnalytic);
	
	Assert.assertEquals(valueAnalytic, value, 0.005);
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:27,代码来源:MonteCarloBlackScholesModelTest.java

示例4: testModelProperties

import net.finmath.exception.CalculationException; //导入依赖的package包/类
/**
 * Test some properties of the model
 * 
 * @throws CalculationException Thrown if s.th. went wrong during calculation (check getCause for details).
 */
@Test
public void testModelProperties() throws CalculationException {
	/*
	 * Create the valuation model (see <code>getModel</code>)
	 */
	AssetModelMonteCarloSimulationInterface model = getModel();

	TimeDiscretizationInterface modelTimeDiscretization = model.getTimeDiscretization();

	System.out.println("Time \tAverage \t\tVariance");
	for(double time : modelTimeDiscretization) {
		RandomVariableInterface assetValue = model.getAssetValue(time, 0);

		double average	= assetValue.getAverage();
		double variance	= assetValue.getVariance();
		double error	= assetValue.getStandardError();

		DecimalFormat formater2Digits = new DecimalFormat("0.00");
		DecimalFormat formater4Digits = new DecimalFormat("0.0000");
		System.out.println(formater2Digits.format(time) + " \t" + formater4Digits.format(average) + "\t+/- " + formater4Digits.format(error) + "\t" + formater4Digits.format(variance));
	}
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:28,代码来源:InhomogenousBachelierModelMonteCarloValuationTest.java

示例5: testMultiPeriodFloater

import net.finmath.exception.CalculationException; //导入依赖的package包/类
@Test
public void testMultiPeriodFloater() throws CalculationException {
	
	double tolerance = 2E-3;
	ArrayList<AbstractProductComponent> periods = new ArrayList<AbstractProductComponent>();
	for(int iPeriod = 0; iPeriod<10; iPeriod++) {
		double periodStart	= 2.0 + 0.5 * iPeriod;
		double periodEnd	= 2.0 + 0.5 * (iPeriod+1);
		double periodLength	= periodEnd-periodStart;

		AbstractIndex index = new LIBORIndex(0.0, periodLength);
		Period period = new Period(periodStart, periodEnd, periodStart, periodEnd, new Notional(1.0), index, periodLength, true, true, false);
		periods.add(period);
	}
	AbstractProductComponent floater = new ProductCollection(periods);
	double value = floater.getValue(liborMarketModel);
	
	double toleranceThisTest = tolerance/Math.sqrt(((double)liborMarketModel.getNumberOfPaths())/100000.0);

	NumberFormat formatDec6 = new DecimalFormat("0.000000");
	System.out.println(
			formatDec6.format(value) + "\t" +
					formatDec6.format(toleranceThisTest));

	Assert.assertEquals("Deviation", 0.0, value, toleranceThisTest);
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:27,代码来源:LIBORIndexTest.java

示例6: getValue

import net.finmath.exception.CalculationException; //导入依赖的package包/类
/**
    * This method returns the value random variable of the product within the specified model, evaluated at a given evalutationTime.
    * Note: For a lattice this is often the value conditional to evalutationTime, for a Monte-Carlo simulation this is the (sum of) value discounted to evaluation time.
    * Cashflows prior evaluationTime are not considered.
    * 
    * @param evaluationTime The time on which this products value should be observed.
    * @param model The model used to price the product.
    * @return The random variable representing the value of the product discounted to evaluation time
    * @throws net.finmath.exception.CalculationException Thrown if the valuation fails, specific cause may be available via the <code>cause()</code> method.
    */
   @Override
   public RandomVariableInterface getValue(double evaluationTime, LIBORModelMonteCarloSimulationInterface model) throws CalculationException {
	
	// Get random variables
       RandomVariableInterface	numeraire				= model.getNumeraire(maturity);
       RandomVariableInterface	monteCarloProbabilities	= model.getMonteCarloWeights(maturity);

       // Calculate numeraire relative value
       RandomVariableInterface values = model.getRandomVariableForConstant(1.0);
       values = values.div(numeraire).mult(monteCarloProbabilities);
       
       // Convert back to values
       RandomVariableInterface	numeraireAtEvaluationTime				= model.getNumeraire(evaluationTime);
       RandomVariableInterface	monteCarloProbabilitiesAtEvaluationTime	= model.getMonteCarloWeights(evaluationTime);
       values = values.mult(numeraireAtEvaluationTime).div(monteCarloProbabilitiesAtEvaluationTime);

	// Return values
	return values;	
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:30,代码来源:Bond.java

示例7: testModelRandomVariable

import net.finmath.exception.CalculationException; //导入依赖的package包/类
/**
 * @throws CalculationException Thrown if s.th. went wrong during calculation (check getCause for details).
 */
@Test
public void testModelRandomVariable() throws CalculationException {
	/*
	 * Create the valuation model (see <code>getModel</code>)
	 */
	AssetModelMonteCarloSimulationInterface model = getModel();

	RandomVariableInterface stockAtTimeOne = model.getAssetValue(1.0, 0);

	System.out.println("The first 100 realizations of the " + stockAtTimeOne.size() + " realizations of S(1) are:");
	System.out.println("Path\tValue");
	for(int i=0; i<100;i++) {
		System.out.println(i + "\t" + stockAtTimeOne.get(i));
	}
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:19,代码来源:BlackScholesMonteCarloValuationTest.java

示例8: getValue

import net.finmath.exception.CalculationException; //导入依赖的package包/类
/**
 * This method returns the value random variable of the product within the specified model, evaluated at a given evalutationTime.
 * Note: For a lattice this is often the value conditional to evalutationTime, for a Monte-Carlo simulation this is the (sum of) value discounted to evaluation time.
 * cash-flows prior evaluationTime are not considered.
 * 
 * @param evaluationTime The time on which this products value should be observed.
 * @param model The model used to price the product.
 * @return The random variable representing the value of the product discounted to evaluation time
 * @throws net.finmath.exception.CalculationException Thrown if the valuation fails, specific cause may be available via the <code>cause()</code> method. 
 */
@Override
public RandomVariableInterface getValue(double evaluationTime, LIBORModelMonteCarloSimulationInterface model) throws CalculationException {        

	// Note: We use > here. To distinguish an end of day valuation use hour of day for cash flows and evaluation date.
	if(evaluationTime > flowDate) return model.getRandomVariableForConstant(0.0);

	RandomVariableInterface values = model.getRandomVariableForConstant(flowAmount);
	if(isPayer) values = values.mult(-1.0);

	// Rebase to evaluationTime
	if(flowDate != evaluationTime) {
		// Get random variables
		RandomVariableInterface	numeraire				= model.getNumeraire(flowDate);
		RandomVariableInterface	numeraireAtEval			= model.getNumeraire(evaluationTime);
		//        RandomVariableInterface	monteCarloProbabilities	= model.getMonteCarloWeights(getPaymentDate());
		values = values.div(numeraire).mult(numeraireAtEval);
	}

	// Return values
	return values;	
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:32,代码来源:Cashflow.java

示例9: getValue

import net.finmath.exception.CalculationException; //导入依赖的package包/类
@Override
public RandomVariableInterface getValue(double evaluationTime, LIBORModelMonteCarloSimulationInterface model) throws CalculationException {

	if(inceptionTime > evaluationTime) return new RandomVariable(0.0);
	if(accrualPeriod <= 0) return new RandomVariable(Double.MAX_VALUE);
	
	// Initialize the value of the account to 1.0
	RandomVariableInterface value = new RandomVariable(initialValue);

	// Loop over accrual periods
	for(double time=inceptionTime; time<evaluationTime; time += accrualPeriod) {	
		// Get the forward fixed at the beginning of the period
		RandomVariableInterface	forwardRate				= model.getLIBOR(time, time, time+accrualPeriod);
		double					currentAccrualPeriod	= Math.min(accrualPeriod , evaluationTime-time);
		
		// Accrue the value using the current forward rate
		value = value.accrue(forwardRate, currentAccrualPeriod);
	}
	
	return value;
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:22,代码来源:MoneyMarketAccount.java

示例10: createCalibrationItem

import net.finmath.exception.CalculationException; //导入依赖的package包/类
private CalibrationItem createCalibrationItem(double exerciseDate, double swapPeriodLength, int numberOfPeriods, double moneyness, double targetVolatility, ForwardCurveInterface forwardCurve, DiscountCurveInterface discountCurve) throws CalculationException {

		double[]	fixingDates			= new double[numberOfPeriods];
		double[]	paymentDates		= new double[numberOfPeriods];
		double[]	swapTenor			= new double[numberOfPeriods + 1];

		for (int periodStartIndex = 0; periodStartIndex < numberOfPeriods; periodStartIndex++) {
			fixingDates[periodStartIndex] = exerciseDate + periodStartIndex * swapPeriodLength;
			paymentDates[periodStartIndex] = exerciseDate + (periodStartIndex + 1) * swapPeriodLength;
			swapTenor[periodStartIndex] = exerciseDate + periodStartIndex * swapPeriodLength;
		}
		swapTenor[numberOfPeriods] = exerciseDate + numberOfPeriods * swapPeriodLength;

		// Swaptions swap rate
		double swaprate = moneyness + getParSwaprate(forwardCurve, discountCurve, swapTenor);

		// Set swap rates for each period
		double[] swaprates = new double[numberOfPeriods];
		Arrays.fill(swaprates, swaprate);

		/*
		 * We use Monte-Carlo calibration on implied volatility.
		 * Alternatively you may change here to Monte-Carlo valuation on price or
		 * use an analytic approximation formula, etc.
		 */
		SwaptionSimple swaptionMonteCarlo = new SwaptionSimple(swaprate, swapTenor, SwaptionSimple.ValueUnit.VOLATILITY);
//		double targetValuePrice = AnalyticFormulas.blackModelSwaptionValue(swaprate, targetVolatility, fixingDates[0], swaprate, getSwapAnnuity(discountCurve, swapTenor));
		return new CalibrationItem(swaptionMonteCarlo, targetVolatility, 1.0);
	}
 
开发者ID:finmath,项目名称:finmath-lib-cuda-extensions,代码行数:30,代码来源:LIBORMarketModelCalibrationTest.java

示例11: testProductImplementation

import net.finmath.exception.CalculationException; //导入依赖的package包/类
@Test
public void testProductImplementation() throws CalculationException {
	long millisStart = System.currentTimeMillis();

	// Create a model
	AbstractModel model = new BlackScholesModel(initialValue, riskFreeRate, volatility);

	// Create a corresponding MC process
	AbstractProcess process = new ProcessEulerScheme(brownian);

	// Link model and process for delegation
	process.setModel(model);
	model.setProcess(process);

	/*
	 * Value a call option - directly
	 */
	TimeDiscretizationInterface timeDiscretization = brownian.getTimeDiscretization();
	
	RandomVariableInterface asset = process.getProcessValue(timeDiscretization.getTimeIndex(optionMaturity), assetIndex);
	RandomVariableInterface numeraireAtPayment = model.getNumeraire(optionMaturity);
	RandomVariableInterface numeraireAtEval = model.getNumeraire(0.0);
	
	RandomVariableInterface payoff = asset.sub(optionStrike).floor(0.0);
	double value = payoff.div(numeraireAtPayment).mult(numeraireAtEval).getAverage();

	double valueAnalytic = AnalyticFormulas.blackScholesOptionValue(initialValue, riskFreeRate, volatility, optionMaturity, optionStrike);

	System.out.print("   value Monte-Carlo = " + formatterReal4.format(value));
	System.out.print("\t value analytic    = " + formatterReal4.format(valueAnalytic));

	long millisEnd = System.currentTimeMillis();

	System.out.println("\t calculation time = " + formatterReal2.format((millisEnd - millisStart)/1000.0) + " sec.");
	System.out.println("");

	Assert.assertEquals(valueAnalytic, value, 0.005);
}
 
开发者ID:finmath,项目名称:finmath-lib-cuda-extensions,代码行数:39,代码来源:MonteCarloBlackScholesModelTest.java

示例12: testSingleCurveModel

import net.finmath.exception.CalculationException; //导入依赖的package包/类
@Test
public void testSingleCurveModel() throws CalculationException {
	System.out.println("Runnning tests with a single curve LIBOR Market Model");

	LIBORModelMonteCarloSimulationInterface liborMarketModel = createSingleCurveLIBORMarketModel(numberOfPaths, numberOfFactors, correlationDecayParam);

	testModel(liborMarketModel, false);
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:9,代码来源:SwaptionAnalyticApproximationTest.java

示例13: createCalibrationItem

import net.finmath.exception.CalculationException; //导入依赖的package包/类
private CalibrationItem createCalibrationItem(double weight, double exerciseDate, double swapPeriodLength, int numberOfPeriods, double moneyness, double targetVolatility, String targetVolatilityType, ForwardCurveInterface forwardCurve, DiscountCurveInterface discountCurve) throws CalculationException {

		double[]	fixingDates			= new double[numberOfPeriods];
		double[]	paymentDates		= new double[numberOfPeriods];
		double[]	swapTenor			= new double[numberOfPeriods + 1];

		for (int periodStartIndex = 0; periodStartIndex < numberOfPeriods; periodStartIndex++) {
			fixingDates[periodStartIndex] = exerciseDate + periodStartIndex * swapPeriodLength;
			paymentDates[periodStartIndex] = exerciseDate + (periodStartIndex + 1) * swapPeriodLength;
			swapTenor[periodStartIndex] = exerciseDate + periodStartIndex * swapPeriodLength;
		}
		swapTenor[numberOfPeriods] = exerciseDate + numberOfPeriods * swapPeriodLength;

		// Swaptions swap rate
		double swaprate = moneyness + getParSwaprate(forwardCurve, discountCurve, swapTenor);

		// Set swap rates for each period
		double[] swaprates = new double[numberOfPeriods];
		Arrays.fill(swaprates, swaprate);

		/*
		 * We use Monte-Carlo calibration on implied volatility.
		 * Alternatively you may change here to Monte-Carlo valuation on price or
		 * use an analytic approximation formula, etc.
		 */
		SwaptionSimple swaptionMonteCarlo = new SwaptionSimple(swaprate, swapTenor, SwaptionSimple.ValueUnit.valueOf(targetVolatilityType));
		//		double targetValuePrice = AnalyticFormulas.blackModelSwaptionValue(swaprate, targetVolatility, fixingDates[0], swaprate, getSwapAnnuity(discountCurve, swapTenor));
		return new CalibrationItem(swaptionMonteCarlo, targetVolatility, weight);
	}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:30,代码来源:LIBORMarketModelCalibrationTest.java

示例14: getParSwaprate

import net.finmath.exception.CalculationException; //导入依赖的package包/类
private static double getParSwaprate(LIBORModelMonteCarloSimulationInterface liborMarketModel, double[] swapTenor, String tenorCode) throws CalculationException {
	DiscountCurveInterface modelCurve = new DiscountCurveFromForwardCurve(liborMarketModel.getModel().getForwardRateCurve());
	ForwardCurveInterface forwardCurve = new ForwardCurveFromDiscountCurve(modelCurve.getName(), liborMarketModel.getModel().getForwardRateCurve().getReferenceDate(), tenorCode);
	return net.finmath.marketdata.products.Swap.getForwardSwapRate(new TimeDiscretization(swapTenor), new TimeDiscretization(swapTenor),
			forwardCurve,
			modelCurve);
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:8,代码来源:HullWhiteModelTest.java

示例15: testMultiCurveModel

import net.finmath.exception.CalculationException; //导入依赖的package包/类
@Test
public void testMultiCurveModel() throws CalculationException {
	System.out.println("Runnning tests with a multi curve LIBOR Market Model");

	LIBORModelMonteCarloSimulationInterface liborMarketModel = createMultiCurveLIBORMarketModel(numberOfPaths, numberOfFactors, correlationDecayParam);

	testModel(liborMarketModel, true);
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:9,代码来源:SwaptionAnalyticApproximationTest.java


注:本文中的net.finmath.exception.CalculationException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。