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


Java EuropeanOption类代码示例

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


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

示例1: testProductImplementation

import net.finmath.montecarlo.assetderivativevaluation.products.EuropeanOption; //导入依赖的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

示例2: testEuropeanCall

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

	// Java DecimalFormat for our output format
	DecimalFormat numberFormatStrike	= new DecimalFormat("     0.00 ");
	DecimalFormat numberFormatValue		= new DecimalFormat(" 0.00E00");
	DecimalFormat numberFormatDeviation	= new DecimalFormat("  0.00E00; -0.00E00");

	// Test options with different strike
	System.out.println("Valuation of European Options");
	System.out.println(" Strike \t Monte-Carlo \t Analytic \t Deviation");

	double initialValue	= this.getInitialValue();
	double riskFreeRate	= this.getRiskFreeRate();
	double volatility	= this.getVolatility();

	double optionMaturity	= 5.0;

	double payoffUnit	= Math.exp(- riskFreeRate * optionMaturity);
	double forward		= initialValue / payoffUnit;
	double volBachelier = volatility / payoffUnit;

	for(double optionStrike = 0.75/payoffUnit; optionStrike < 1.25/payoffUnit; optionStrike += 0.05/payoffUnit) {

		// Create a product
		EuropeanOption		callOption	= new EuropeanOption(optionMaturity, optionStrike);
		// Value the product with Monte Carlo
		double valueMonteCarlo	= callOption.getValue(model);

		// Calculate the analytic value
		double valueAnalytic	= net.finmath.functions.AnalyticFormulas.bachelierOptionValue(forward, volBachelier, optionMaturity, optionStrike, payoffUnit);

		// Print result
		System.out.println(numberFormatStrike.format(optionStrike) + 
				"\t" + numberFormatValue.format(valueMonteCarlo) +
				"\t" + numberFormatValue.format(valueAnalytic) +
				"\t" + numberFormatDeviation.format(valueMonteCarlo-valueAnalytic));

		Assert.assertTrue(Math.abs(valueMonteCarlo-valueAnalytic) < 1E-02);
	}
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:50,代码来源:BachelierModelMonteCarloValuationTest.java

示例3: testEuropeanAsianBermudanOption

import net.finmath.montecarlo.assetderivativevaluation.products.EuropeanOption; //导入依赖的package包/类
/**
 * Evaluates different options (European, Asian, Bermudan) using the given model.
 * 
 * The options share the same maturity and strike for the at t=3.0.
 * Observations which can be made:
 * <ul>
 * <li>The Asian is cheaper than the European since averaging reduces the volatility.
 * <li>The European is cheaper than the Bermudan since exercises into the European is one (out of may) exercises strategies of the Bermudan.
 * </ul>
 * 
 * @throws CalculationException Thrown if s.th. went wrong during calculation (check getCause for details).
 */
@Test
public void testEuropeanAsianBermudanOption() throws CalculationException {
	/*
	 * Create the valuation model (see <code>getModel</code>)
	 */
	AssetModelMonteCarloSimulationInterface model = getModel();

	/*
	 * Common parameters
	 */
	double maturity = 3.0;
	double strike = 1.07;

	/*
	 * European Option
	 */
	EuropeanOption myEuropeanOption = new EuropeanOption(maturity,strike);
	double valueOfEuropeanOption = myEuropeanOption.getValue(model);

	/*
	 * Asian Option
	 */
	double[] averagingPoints = { 1.0, 1.5, 2.0, 2.5 , 3.0 };

	AsianOption myAsianOption = new AsianOption(maturity,strike, new TimeDiscretization(averagingPoints));
	double valueOfAsianOption = myAsianOption.getValue(model);

	/*
	 * Bermudan Option
	 */
	double[] exerciseDates	= { 1.0,  2.0,  3.0};
	double[] notionals		= { 1.20, 1.10, 1.0};
	double[] strikes		= { 1.03, 1.05, 1.07 };

	// Lower bound method
	BermudanOption myBermudanOptionLowerBound = new BermudanOption(exerciseDates, notionals, strikes, BermudanOption.ExerciseMethod.ESTIMATE_COND_EXPECTATION);
	double valueOfBermudanOptionLowerBound = myBermudanOptionLowerBound.getValue(model);

	// Upper bound method
	BermudanOption myBermudanOptionUpperBound = new BermudanOption(exerciseDates, notionals, strikes, BermudanOption.ExerciseMethod.UPPER_BOUND_METHOD);
	double valueOfBermudanOptionUpperBound = myBermudanOptionUpperBound.getValue(model);

	/*
	 * Output
	 */
	System.out.println("Value of Asian Option is \t"	+ valueOfAsianOption);
	System.out.println("Value of European Option is \t"	+ valueOfEuropeanOption);
	System.out.println("Value of Bermudan Option is \t"	+ "(" + valueOfBermudanOptionLowerBound + "," + valueOfBermudanOptionUpperBound + ")");

	Assert.assertTrue(valueOfAsianOption < valueOfEuropeanOption);
	Assert.assertTrue(valueOfBermudanOptionLowerBound < valueOfBermudanOptionUpperBound);
	Assert.assertTrue(valueOfEuropeanOption < valueOfBermudanOptionUpperBound);
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:66,代码来源:BachelierModelMonteCarloValuationTest.java

示例4: testEuropeanCallDelta

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

	// Java DecimalFormat for our output format
	DecimalFormat numberFormatStrike	= new DecimalFormat("     0.00 ");
	DecimalFormat numberFormatValue		= new DecimalFormat(" 0.00E00");
	DecimalFormat numberFormatDeviation	= new DecimalFormat("  0.00E00; -0.00E00");

	double initialValue	= this.getInitialValue();
	double riskFreeRate	= this.getRiskFreeRate();
	double volatility	= this.getVolatility();

	// Test options with different strike
	System.out.println("Calculation of Option Delta (European options with maturity 1.0):");
	System.out.println(" Strike \t MC Fin.Diff.\t MC Pathwise\t MC Likelihood\t Analytic \t Diff MC-FD \t Diff MC-PW \t Diff MC-LR");

	double optionMaturity	= 1.0;
	double payoffUnit	= Math.exp(- riskFreeRate * optionMaturity);
	double forward		= initialValue / payoffUnit;
	double volBachelier = volatility / payoffUnit;
	for(double optionStrike = 0.60; optionStrike < 1.50; optionStrike += 0.05) {

		// Create a product
		EuropeanOption		callOption	= new EuropeanOption(optionMaturity, optionStrike);

		// Value the product with Monte Carlo
		double shift = initialValue * 1E-6;

		Map<String,Object> dataUpShift = new HashMap<String,Object>();
		dataUpShift.put("initialValue", initialValue + shift);

		double valueUpShift	= (Double)(callOption.getValuesForModifiedData(model, dataUpShift).get("value"));

		Map<String,Object> dataDownShift = new HashMap<String,Object>();
		dataDownShift.put("initialValue", initialValue - shift);
		double valueDownShift	= (Double)(callOption.getValuesForModifiedData(model, dataDownShift).get("value"));

		// Calculate the finite difference of the monte-carlo value
		double delta = (valueUpShift-valueDownShift) / ( 2 * shift );

		// Calculate the finite difference of the analytic value
		double deltaFiniteDiffAnalytic	=
				(
						net.finmath.functions.AnalyticFormulas.bachelierOptionValue(forward+shift/payoffUnit, volBachelier, optionMaturity, optionStrike, payoffUnit)
						- net.finmath.functions.AnalyticFormulas.bachelierOptionValue(forward-shift/payoffUnit, volBachelier, optionMaturity, optionStrike, payoffUnit)
						)/(2*shift);

		// Calculate the analytic value
		//			double deltaAnalytic	= net.finmath.functions.AnalyticFormulas.blackScholesOptionDelta(initialValue, riskFreeRate, volatility, optionMaturity, optionStrike);


		// Print result
		System.out.println(numberFormatStrike.format(optionStrike) + 
				"\t" + numberFormatValue.format(delta) +
				"\t" + numberFormatValue.format(deltaFiniteDiffAnalytic) +
				"\t" + numberFormatDeviation.format((delta-deltaFiniteDiffAnalytic)));

		Assert.assertTrue(Math.abs(delta-deltaFiniteDiffAnalytic) < 1E-02);
	}
	System.out.println("__________________________________________________________________________________________\n");
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:70,代码来源:BachelierModelMonteCarloValuationTest.java

示例5: testEuropeanCallVega

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

		// Java DecimalFormat for our output format
		DecimalFormat numberFormatStrike	= new DecimalFormat("     0.00 ");
		DecimalFormat numberFormatValue		= new DecimalFormat(" 0.00E00");
		DecimalFormat numberFormatDeviation	= new DecimalFormat("  0.00E00; -0.00E00");

		double initialValue	= this.getInitialValue();
		double riskFreeRate	= this.getRiskFreeRate();
		double volatility	= this.getVolatility();

		// Test options with different strike
		System.out.println("Calculation of Option Vega (European options with maturity 1.0):");
		System.out.println(" Strike \t MC Fin.Diff.\t Analytic \t Diff MC-FD");

		double optionMaturity	= 5.0;
		double payoffUnit	= Math.exp(- riskFreeRate * optionMaturity);
		double forward		= initialValue / payoffUnit;
		for(double optionStrike = 0.60; optionStrike < 1.50; optionStrike += 0.05) {

			// Create a product
			EuropeanOption		callOption	= new EuropeanOption(optionMaturity, optionStrike);

			// Value the product with Monte Carlo
			double shift = volatility * 1E-5;

			Map<String,Object> dataUpShift = new HashMap<String,Object>();
			dataUpShift.put("volatility", volatility + shift);

			double valueUpShift	= (Double)(callOption.getValuesForModifiedData(model, dataUpShift).get("value"));

			Map<String,Object> dataDownShift = new HashMap<String,Object>();
			dataDownShift.put("volatility", volatility - shift);
			double valueDownShift	= (Double)(callOption.getValuesForModifiedData(model, dataDownShift).get("value"));

			// Calculate the finite difference of the monte-carlo value
			double vega = (valueUpShift-valueDownShift) / ( 2 * shift );

			double volBachelierUp = (volatility+shift) / payoffUnit;
			double volBachelierDown = (volatility-shift) / payoffUnit;
			// Calculate the finite difference of the analytic value
			double vegaFiniteDiffAnalytic	=
					(
							net.finmath.functions.AnalyticFormulas.bachelierOptionValue(forward, volBachelierUp, optionMaturity, optionStrike, payoffUnit)
							-
							net.finmath.functions.AnalyticFormulas.bachelierOptionValue(forward, volBachelierDown, optionMaturity, optionStrike, payoffUnit)
							)/(2*shift);

			// Calculate the analytic value
//			double vegaAnalytic	= net.finmath.functions.AnalyticFormulas.blackScholesOptionVega(initialValue, riskFreeRate, volatility, optionMaturity, optionStrike);

			// Print result
			System.out.println(numberFormatStrike.format(optionStrike) + 
					"\t" + numberFormatValue.format(vega) +
					"\t" + numberFormatValue.format(vegaFiniteDiffAnalytic) +
					"\t" + numberFormatDeviation.format(vega-vegaFiniteDiffAnalytic));

			Assert.assertTrue(Math.abs(vega-vegaFiniteDiffAnalytic) < 1E-01);
		}
		System.out.println("__________________________________________________________________________________________\n");
	}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:71,代码来源:BachelierModelMonteCarloValuationTest.java

示例6: testEuropeanCall

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

	// Java DecimalFormat for our output format
	DecimalFormat numberFormatStrike	= new DecimalFormat("     0.00 ");
	DecimalFormat numberFormatValue		= new DecimalFormat(" 0.00E00");
	DecimalFormat numberFormatDeviation	= new DecimalFormat("  0.00E00; -0.00E00");

	// Test options with different strike
	System.out.println("Valuation of European Options");
	System.out.println(" Strike \t Monte-Carlo \t Analytic \t Deviation");

	double initialValue	= this.getInitialValue();
	double riskFreeRate	= this.getRiskFreeRate();
	double volatility	= this.getVolatility();

	double optionMaturity	= 5.0;

	double payoffUnit	= Math.exp(- riskFreeRate * optionMaturity);
	double forward		= initialValue / payoffUnit;
	double volBachelier = volatility * Math.sqrt((Math.exp(2 * riskFreeRate * optionMaturity) - 1)/(2*riskFreeRate*optionMaturity));

	for(double optionStrike = 0.75/payoffUnit; optionStrike < 1.25/payoffUnit; optionStrike += 0.05/payoffUnit) {

		// Create a product
		EuropeanOption		callOption	= new EuropeanOption(optionMaturity, optionStrike);
		// Value the product with Monte Carlo
		double valueMonteCarlo	= callOption.getValue(model);

		// Calculate the analytic value
		double valueAnalytic	= net.finmath.functions.AnalyticFormulas.bachelierOptionValue(forward, volBachelier, optionMaturity, optionStrike, payoffUnit);

		// Print result
		System.out.println(numberFormatStrike.format(optionStrike) + 
				"\t" + numberFormatValue.format(valueMonteCarlo) +
				"\t" + numberFormatValue.format(valueAnalytic) +
				"\t" + numberFormatDeviation.format(valueMonteCarlo-valueAnalytic));

		Assert.assertTrue(Math.abs(valueMonteCarlo-valueAnalytic) < 1E-02);
	}
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:50,代码来源:InhomogenousBachelierModelMonteCarloValuationTest.java

示例7: testEuropeanCallDelta

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

	// Java DecimalFormat for our output format
	DecimalFormat numberFormatStrike	= new DecimalFormat("     0.00 ");
	DecimalFormat numberFormatValue		= new DecimalFormat(" 0.00E00");
	DecimalFormat numberFormatDeviation	= new DecimalFormat("  0.00E00; -0.00E00");

	double initialValue	= this.getInitialValue();
	double riskFreeRate	= this.getRiskFreeRate();
	double volatility	= this.getVolatility();

	// Test options with different strike
	System.out.println("Calculation of Option Delta (European options with maturity 1.0):");
	System.out.println(" Strike \t MC Fin.Diff.\t MC Pathwise\t MC Likelihood\t Analytic \t Diff MC-FD \t Diff MC-PW \t Diff MC-LR");

	double optionMaturity	= 1.0;
	double payoffUnit	= Math.exp(- riskFreeRate * optionMaturity);
	double forward		= initialValue / payoffUnit;
	double volBachelier = volatility * Math.sqrt((Math.exp(2 * riskFreeRate * optionMaturity) - 1)/(2*riskFreeRate*optionMaturity));
	for(double optionStrike = 0.60; optionStrike < 1.50; optionStrike += 0.05) {

		// Create a product
		EuropeanOption		callOption	= new EuropeanOption(optionMaturity, optionStrike);

		// Value the product with Monte Carlo
		double shift = initialValue * 1E-6;

		Map<String,Object> dataUpShift = new HashMap<String,Object>();
		dataUpShift.put("initialValue", initialValue + shift);

		double valueUpShift	= (Double)(callOption.getValuesForModifiedData(model, dataUpShift).get("value"));

		Map<String,Object> dataDownShift = new HashMap<String,Object>();
		dataDownShift.put("initialValue", initialValue - shift);
		double valueDownShift	= (Double)(callOption.getValuesForModifiedData(model, dataDownShift).get("value"));

		// Calculate the finite difference of the monte-carlo value
		double delta = (valueUpShift-valueDownShift) / ( 2 * shift );

		// Calculate the finite difference of the analytic value
		double deltaFiniteDiffAnalytic	=
				(
						net.finmath.functions.AnalyticFormulas.bachelierOptionValue(forward+shift/payoffUnit, volBachelier, optionMaturity, optionStrike, payoffUnit)
						- net.finmath.functions.AnalyticFormulas.bachelierOptionValue(forward-shift/payoffUnit, volBachelier, optionMaturity, optionStrike, payoffUnit)
						)/(2*shift);

		// Calculate the analytic value
		//			double deltaAnalytic	= net.finmath.functions.AnalyticFormulas.blackScholesOptionDelta(initialValue, riskFreeRate, volatility, optionMaturity, optionStrike);


		// Print result
		System.out.println(numberFormatStrike.format(optionStrike) + 
				"\t" + numberFormatValue.format(delta) +
				"\t" + numberFormatValue.format(deltaFiniteDiffAnalytic) +
				"\t" + numberFormatDeviation.format((delta-deltaFiniteDiffAnalytic)));

		Assert.assertTrue(Math.abs(delta-deltaFiniteDiffAnalytic) < 1E-02);
	}
	System.out.println("__________________________________________________________________________________________\n");
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:70,代码来源:InhomogenousBachelierModelMonteCarloValuationTest.java

示例8: testEuropeanCallVega

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

		// Java DecimalFormat for our output format
		DecimalFormat numberFormatStrike	= new DecimalFormat("     0.00 ");
		DecimalFormat numberFormatValue		= new DecimalFormat(" 0.00E00");
		DecimalFormat numberFormatDeviation	= new DecimalFormat("  0.00E00; -0.00E00");

		double initialValue	= this.getInitialValue();
		double riskFreeRate	= this.getRiskFreeRate();
		double volatility	= this.getVolatility();

		// Test options with different strike
		System.out.println("Calculation of Option Vega (European options with maturity 1.0):");
		System.out.println(" Strike \t MC Fin.Diff.\t Analytic \t Diff MC-FD");

		double optionMaturity	= 5.0;
		double payoffUnit	= Math.exp(- riskFreeRate * optionMaturity);
		double forward		= initialValue / payoffUnit;
		for(double optionStrike = 0.60; optionStrike < 1.50; optionStrike += 0.05) {

			// Create a product
			EuropeanOption		callOption	= new EuropeanOption(optionMaturity, optionStrike);

			// Value the product with Monte Carlo
			double shift = volatility * 1E-5;

			Map<String,Object> dataUpShift = new HashMap<String,Object>();
			dataUpShift.put("volatility", volatility + shift);

			double valueUpShift	= (Double)(callOption.getValuesForModifiedData(model, dataUpShift).get("value"));

			Map<String,Object> dataDownShift = new HashMap<String,Object>();
			dataDownShift.put("volatility", volatility - shift);
			double valueDownShift	= (Double)(callOption.getValuesForModifiedData(model, dataDownShift).get("value"));

			// Calculate the finite difference of the monte-carlo value
			double vega = (valueUpShift-valueDownShift) / ( 2 * shift );

			double volBachelierUp = (volatility+shift) * Math.sqrt((Math.exp(2 * riskFreeRate * optionMaturity) - 1)/(2*riskFreeRate*optionMaturity));
			double volBachelierDown = (volatility-shift) * Math.sqrt((Math.exp(2 * riskFreeRate * optionMaturity) - 1)/(2*riskFreeRate*optionMaturity));
			// Calculate the finite difference of the analytic value
			double vegaFiniteDiffAnalytic	=
					(
							net.finmath.functions.AnalyticFormulas.bachelierOptionValue(forward, volBachelierUp, optionMaturity, optionStrike, payoffUnit)
							-
							net.finmath.functions.AnalyticFormulas.bachelierOptionValue(forward, volBachelierDown, optionMaturity, optionStrike, payoffUnit)
							)/(2*shift);

			// Calculate the analytic value
//			double vegaAnalytic	= net.finmath.functions.AnalyticFormulas.blackScholesOptionVega(initialValue, riskFreeRate, volatility, optionMaturity, optionStrike);

			// Print result
			System.out.println(numberFormatStrike.format(optionStrike) + 
					"\t" + numberFormatValue.format(vega) +
					"\t" + numberFormatValue.format(vegaFiniteDiffAnalytic) +
					"\t" + numberFormatDeviation.format(vega-vegaFiniteDiffAnalytic));

			Assert.assertTrue(Math.abs(vega-vegaFiniteDiffAnalytic) < 1E-01);
		}
		System.out.println("__________________________________________________________________________________________\n");
	}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:71,代码来源:InhomogenousBachelierModelMonteCarloValuationTest.java

示例9: testEuropeanCall

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

	/*
	 * Cast the model to a MonteCarloBlackScholesModel - to get the parameters for analytic valuation
	 */
	MonteCarloBlackScholesModel blackScholesModel = (MonteCarloBlackScholesModel)model;

	// Java DecimalFormat for our output format
	DecimalFormat numberFormatStrike	= new DecimalFormat("     0.00 ");
	DecimalFormat numberFormatValue		= new DecimalFormat(" 0.00E00");
	DecimalFormat numberFormatDeviation	= new DecimalFormat("  0.00E00; -0.00E00");

	// Test options with different strike
	System.out.println("Valuation of European Options");
	System.out.println(" Strike \t Monte-Carlo \t Analytic \t Deviation");

	double initialValue	= blackScholesModel.getAssetValue(0.0, 0).get(0);
	// @TODO This needs to be changes to use random variables.
	double riskFreeRate	= blackScholesModel.getModel().getRiskFreeRate().getAverage();
	double volatility	= blackScholesModel.getModel().getVolatility().getAverage();

	double optionMaturity	= 1.0;
	for(double optionStrike = 0.60; optionStrike < 1.50; optionStrike += 0.05) {

		// Create a product
		EuropeanOption		callOption	= new EuropeanOption(optionMaturity, optionStrike);
		// Value the product with Monte Carlo
		double valueMonteCarlo	= callOption.getValue(model);

		// Calculate the analytic value
		double valueAnalytic	= net.finmath.functions.AnalyticFormulas.blackScholesOptionValue(initialValue, riskFreeRate, volatility, optionMaturity, optionStrike);

		// Print result
		System.out.println(numberFormatStrike.format(optionStrike) + 
				"\t" + numberFormatValue.format(valueMonteCarlo) +
				"\t" + numberFormatValue.format(valueAnalytic) +
				"\t" + numberFormatDeviation.format(valueMonteCarlo-valueAnalytic));

		Assert.assertTrue(Math.abs(valueMonteCarlo-valueAnalytic) < 1E-02);
	}
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:51,代码来源:BlackScholesMonteCarloValuationTest.java

示例10: testEuropeanAsianBermudanOption

import net.finmath.montecarlo.assetderivativevaluation.products.EuropeanOption; //导入依赖的package包/类
/**
 * Evaluates different options (European, Asian, Bermudan) using the given model.
 * 
 * The options share the same maturity and strike for the at t=3.0.
 * Observations which can be made:
 * <ul>
 * <li>The Asian is cheaper than the European since averaging reduces the volatility.
 * <li>The European is cheaper than the Bermudan since exercises into the European is one (out of may) exercises strategies of the Bermudan.
 * </ul>
 * 
 * @throws CalculationException Thrown if s.th. went wrong during calculation (check getCause for details).
 */
@Test
public void testEuropeanAsianBermudanOption() throws CalculationException {
	/*
	 * Create the valuation model (see <code>getModel</code>)
	 */
	AssetModelMonteCarloSimulationInterface model = getModel();

	/*
	 * Common parameters
	 */
	double maturity = 3.0;
	double strike = 1.07;

	/*
	 * European Option
	 */
	EuropeanOption myEuropeanOption = new EuropeanOption(maturity,strike);
	double valueOfEuropeanOption = myEuropeanOption.getValue(model);

	/*
	 * Asian Option
	 */
	double[] averagingPoints = { 1.0, 1.5, 2.0, 2.5 , 3.0 };

	AsianOption myAsianOption = new AsianOption(maturity,strike, new TimeDiscretization(averagingPoints));
	double valueOfAsianOption = myAsianOption.getValue(model);

	/*
	 * Bermudan Option
	 */
	double[] exerciseDates	= { 1.0,  2.0,  3.0};
	double[] notionals		= { 1.20, 1.10, 1.0};
	double[] strikes		= { 1.03, 1.05, 1.07 };

	// Lower bound method
	BermudanOption myBermudanOptionLowerBound = new BermudanOption(exerciseDates, notionals, strikes, BermudanOption.ExerciseMethod.ESTIMATE_COND_EXPECTATION);
	double valueOfBermudanOptionLowerBound = myBermudanOptionLowerBound.getValue(model);

	// Upper bound method
	BermudanOption myBermudanOptionUpperBound = new BermudanOption(exerciseDates, notionals, strikes, BermudanOption.ExerciseMethod.UPPER_BOUND_METHOD);
	double valueOfBermudanOptionUpperBound = myBermudanOptionUpperBound.getValue(model);

	/*
	 * Output
	 */
	System.out.println("Value of Asian Option is \t"	+ valueOfAsianOption);
	System.out.println("Value of European Option is \t"	+ valueOfEuropeanOption);
	System.out.println("Value of Bermudan Option is \t"	+ "(" + valueOfBermudanOptionLowerBound + "," + valueOfBermudanOptionUpperBound + ")");

	Assert.assertTrue(valueOfAsianOption < valueOfEuropeanOption);
	Assert.assertTrue(valueOfBermudanOptionLowerBound < valueOfBermudanOptionUpperBound+1E-2);
	Assert.assertTrue(valueOfEuropeanOption < valueOfBermudanOptionUpperBound);
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:66,代码来源:BlackScholesMonteCarloValuationTest.java

示例11: testEuropeanCallDelta

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

	/*
	 * Cast the model to a MonteCarloBlackScholesModel - to get the parameters for analytic valuation
	 */
	MonteCarloBlackScholesModel blackScholesModel = (MonteCarloBlackScholesModel)model;

	// Java DecimalFormat for our output format
	DecimalFormat numberFormatStrike	= new DecimalFormat("     0.00 ");
	DecimalFormat numberFormatValue		= new DecimalFormat(" 0.00E00");
	DecimalFormat numberFormatDeviation	= new DecimalFormat("  0.00E00; -0.00E00");

	double initialValue	= blackScholesModel.getAssetValue(0.0, 0).get(0);
	// @TODO This needs to be changes to use random variables.
	double riskFreeRate	= blackScholesModel.getModel().getRiskFreeRate().getAverage();
	double volatility	= blackScholesModel.getModel().getVolatility().getAverage();

	// Test options with different strike
	System.out.println("Calculation of Option Delta (European options with maturity 1.0):");
	System.out.println(" Strike \t MC Fin.Diff.\t MC Pathwise\t MC Likelihood\t Analytic \t Diff MC-FD \t Diff MC-PW \t Diff MC-LR");

	double optionMaturity	= 1.0;
	for(double optionStrike = 0.60; optionStrike < 1.50; optionStrike += 0.05) {

		// Create a product
		EuropeanOption		callOption	= new EuropeanOption(optionMaturity, optionStrike);

		// Value the product with Monte Carlo
		double shift = initialValue * 1E-6;

		Map<String,Object> dataUpShift = new HashMap<String,Object>();
		dataUpShift.put("initialValue", initialValue + shift);

		double valueUpShift	= (Double)(callOption.getValuesForModifiedData(model, dataUpShift).get("value"));

		Map<String,Object> dataDownShift = new HashMap<String,Object>();
		dataDownShift.put("initialValue", initialValue - shift);
		double valueDownShift	= (Double)(callOption.getValuesForModifiedData(model, dataDownShift).get("value"));

		// Calculate the finite difference of the monte-carlo value
		double delta = (valueUpShift-valueDownShift) / ( 2 * shift );

		// Calculate the finite difference of the analytic value
		double deltaFiniteDiffAnalytic	=
				(
						net.finmath.functions.AnalyticFormulas.blackScholesOptionValue(initialValue+shift, riskFreeRate, volatility, optionMaturity, optionStrike)
						- net.finmath.functions.AnalyticFormulas.blackScholesOptionValue(initialValue-shift, riskFreeRate, volatility, optionMaturity, optionStrike)
						)/(2*shift);

		// Calculate the analytic value
		double deltaAnalytic	= net.finmath.functions.AnalyticFormulas.blackScholesOptionDelta(initialValue, riskFreeRate, volatility, optionMaturity, optionStrike);


		// Print result
		System.out.println(numberFormatStrike.format(optionStrike) + 
				"\t" + numberFormatValue.format(delta) +
				"\t" + numberFormatValue.format(deltaAnalytic) +
				"\t" + numberFormatDeviation.format(delta-deltaAnalytic));

		Assert.assertTrue(Math.abs(delta-deltaAnalytic) < 1E-02);
	}
	System.out.println("__________________________________________________________________________________________\n");
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:73,代码来源:BlackScholesMonteCarloValuationTest.java

示例12: testEuropeanCallVega

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

	/*
	 * Cast the model to a MonteCarloBlackScholesModel - to get the parameters for analytic valuation
	 */
	MonteCarloBlackScholesModel blackScholesModel = (MonteCarloBlackScholesModel)model;

	// Java DecimalFormat for our output format
	DecimalFormat numberFormatStrike	= new DecimalFormat("     0.00 ");
	DecimalFormat numberFormatValue		= new DecimalFormat(" 0.00E00");
	DecimalFormat numberFormatDeviation	= new DecimalFormat("  0.00E00; -0.00E00");

	double initialValue	= blackScholesModel.getAssetValue(0.0, 0).get(0);
	// @TODO This needs to be changes to use random variables.
	double riskFreeRate	= blackScholesModel.getModel().getRiskFreeRate().getAverage();
	double volatility	= blackScholesModel.getModel().getVolatility().getAverage();

	// Test options with different strike
	System.out.println("Calculation of Option Vega (European options with maturity 1.0):");
	System.out.println(" Strike \t MC Fin.Diff.\t Analytic \t Diff MC-FD");

	double optionMaturity	= 5.0;
	for(double optionStrike = 0.60; optionStrike < 1.50; optionStrike += 0.05) {

		// Create a product
		EuropeanOption		callOption	= new EuropeanOption(optionMaturity, optionStrike);

		// Value the product with Monte Carlo
		double shift = volatility * 1E-6;

		Map<String,Object> dataUpShift = new HashMap<String,Object>();
		dataUpShift.put("volatility", volatility + shift);

		double valueUpShift	= (Double)(callOption.getValuesForModifiedData(model, dataUpShift).get("value"));

		Map<String,Object> dataDownShift = new HashMap<String,Object>();
		dataDownShift.put("volatility", volatility - shift);
		double valueDownShift	= (Double)(callOption.getValuesForModifiedData(model, dataDownShift).get("value"));

		// Calculate the finite difference of the monte-carlo value
		double vega = (valueUpShift-valueDownShift) / ( 2 * shift );

		// Calculate the finite difference of the analytic value
		double vegaFiniteDiffAnalytic	=
				(
						net.finmath.functions.AnalyticFormulas.blackScholesOptionValue(initialValue+shift, riskFreeRate, volatility, optionMaturity, optionStrike)
						- net.finmath.functions.AnalyticFormulas.blackScholesOptionValue(initialValue-shift, riskFreeRate, volatility, optionMaturity, optionStrike)
						)/(2*shift);

		// Calculate the analytic value
		double vegaAnalytic	= net.finmath.functions.AnalyticFormulas.blackScholesOptionVega(initialValue, riskFreeRate, volatility, optionMaturity, optionStrike);

		// Print result
		System.out.println(numberFormatStrike.format(optionStrike) + 
				"\t" + numberFormatValue.format(vega) +
				"\t" + numberFormatValue.format(vegaAnalytic) +
				"\t" + numberFormatDeviation.format(vega-vegaAnalytic));

		Assert.assertTrue(Math.abs(vega-vegaAnalytic) < 1E-01);
	}
	System.out.println("__________________________________________________________________________________________\n");
}
 
开发者ID:finmath,项目名称:finmath-lib,代码行数:72,代码来源:BlackScholesMonteCarloValuationTest.java

示例13: testHedge

import net.finmath.montecarlo.assetderivativevaluation.products.EuropeanOption; //导入依赖的package包/类
private static void testHedge() throws CalculationException {
		System.out.println("\n\nT E S T   o f   C a l i b r a t i o n   a n d   H e d g e \n");

		// Test parameters
		double initialValue		= 100;
		double riskFreeRate		= 0.05;
		double volatility		= 0.32;
		int numberOfPaths		= 1000;
		int numberOfTimeSteps	= 100;
		TimeDiscretization simulationTimeDiscretization = new TimeDiscretization(0.0, numberOfTimeSteps, 2.0 / numberOfTimeSteps);
		BrownianMotionInterface brownianMotion = new BrownianMotion(simulationTimeDiscretization, 1, numberOfPaths, 3141);
		AssetModelMonteCarloSimulationInterface model = new MonteCarloBlackScholesModel(simulationTimeDiscretization, numberOfPaths, initialValue, riskFreeRate, volatility);

		TimeDiscretization simulationTimeDiscretization2 = new TimeDiscretization(0.0, numberOfTimeSteps/100, 2.0 / numberOfTimeSteps * 100);
		AssetModelMonteCarloSimulationInterface model2 = new MonteCarloBlackScholesModel(simulationTimeDiscretization2, numberOfPaths, initialValue, riskFreeRate, volatility);

		// Calibration products
		double maturity = 2.0;

		int[]		testProductGroup			= { 6,		5,		2,		7,		1,		4,		3,		8		};
		double[]	testProductStrikes			= { 100,	102,	105,	108,	110,	112,	115,	118		};
		double[]	testProductTargetValues	= { 22.215,	21.329,	20.060,	18.859,	18.094,	17.358,	16.305,	15.312	};

		for(int testProductIndex = 0; testProductIndex<testProductStrikes.length; testProductIndex++) {
			int		group	=	testProductGroup[testProductIndex];
			double	strike	= testProductStrikes[testProductIndex];
			double	targetValue = testProductTargetValues[testProductIndex];
			System.out.println("\nGroup = " + group + "\tStrike = " + strike + "\n------------------------------------------------" + "\n");

			// Create hedge product
			AbstractAssetMonteCarloProduct product = new EuropeanOption(maturity, strike);

			// Hedge portfolio
			AbstractAssetMonteCarloProduct hedgePortfolio = new BlackScholesDeltaHedgedPortfolio(maturity, strike, riskFreeRate, volatility);
//			AbstractAssetMonteCarloProduct hedgePortfolio = new BlackScholesExchangeOptionDeltaHedgedPortfolio(maturity, strike, riskFreeRate, volatility);
//			AbstractAssetMonteCarloProduct hedgePortfolio = new FiniteDifferenceDeltaHedgedPortfolio(product, model2);

				// Print stuff about model
				System.out.println("Model............................: " + model.getClass().getSimpleName());
				System.out.println("Value of calibration product.....: " + product.getValue(model));

				// Check hedge
				RandomVariableInterface hedgePortfolioValues	= hedgePortfolio.getValue(maturity, model);
				RandomVariableInterface derivativeValues		= product.getValue(maturity, model);
				RandomVariableInterface underlyingValues		= model.getAssetValue(maturity, 0);
		
				RandomVariableInterface hedgePortfolioErrorValues	= hedgePortfolioValues.sub(derivativeValues);
				System.out.println("Hedge error (rms)................: " + Math.sqrt(hedgePortfolioErrorValues.squared().getAverage()));
				System.out.println("");
				
				// Output
				boolean printSamples = false;
				if(printSamples) {
					for(int pathIndex=0; pathIndex<hedgePortfolioValues.size(); pathIndex++) {
						System.out.print(underlyingValues.get(pathIndex) + "\t");
						System.out.print(hedgePortfolioValues.get(pathIndex) + "\t");
					}
				}
		}
	}
 
开发者ID:finmath,项目名称:finmath-experiments,代码行数:61,代码来源:DeltaHedgeSimulation.java

示例14: testEuropeanCall

import net.finmath.montecarlo.assetderivativevaluation.products.EuropeanOption; //导入依赖的package包/类
@Test
public void testEuropeanCall() throws CalculationException
{
	MonteCarloBlackScholesModel blackScholesModel = (MonteCarloBlackScholesModel)model;

	// Java DecimalFormat for our output format
	DecimalFormat numberFormatStrike	= new DecimalFormat("     0.00 ");
	DecimalFormat numberFormatValue		= new DecimalFormat(" 0.00E00");
	DecimalFormat numberFormatDeviation	= new DecimalFormat("  0.00E00; -0.00E00");

	// Test options with different strike
	System.out.println("Valuation of European Options");
	System.out.println(" Strike \t Monte-Carlo \t Analytic \t Deviation \t Monte-Carlo (alternative implementation)");

	double initialValue	= blackScholesModel.getAssetValue(0.0, 0).get(0);
	double riskFreeRate	= blackScholesModel.getModel().getRiskFreeRate();
	double volatility	= blackScholesModel.getModel().getVolatility();

	double optionMaturity	= 1.0;
	for(double optionStrike = 0.60; optionStrike < 1.50; optionStrike += 0.05) {

		// Create a product
		EuropeanOption		callOption	= new EuropeanOption(optionMaturity, optionStrike);
		// Value the product with Monte Carlo
		double valueMonteCarlo	= callOption.getValue(blackScholesModel);

		// Create a product
		EuropeanOption2	callOption2	= new EuropeanOption2(optionMaturity, optionStrike);
		// Value the product with Monte Carlo
		double valueMonteCarlo2	= callOption2.getValue(blackScholesModel);

		// Calculate the analytic value
		double valueAnalytic	= net.finmath.functions.AnalyticFormulas.blackScholesOptionValue(initialValue, riskFreeRate, volatility, optionMaturity, optionStrike);

		// Print result
		System.out.println(numberFormatStrike.format(optionStrike) + 
				"\t" + numberFormatValue.format(valueMonteCarlo) +
				"\t" + numberFormatValue.format(valueAnalytic) +
				"\t" + numberFormatDeviation.format(valueMonteCarlo-valueAnalytic) +
				"\t" + numberFormatValue.format(valueMonteCarlo2));

	}
}
 
开发者ID:finmath,项目名称:finmath-experiments,代码行数:44,代码来源:BlackScholesMonteCarloValuationTest.java

示例15: testEuropeanAsianBermudanOption

import net.finmath.montecarlo.assetderivativevaluation.products.EuropeanOption; //导入依赖的package包/类
/**
 * Evaluates different options (European, Asian, Bermudan) using the given model.
 * 
 * The options share the same maturity and strike for the at t=3.0.
 * Observations which can be made:
 * <ul>
 * <li>The Asian is cheaper than the European since averaging reduces the volatility.
 * <li>The European is cheaper than the Bermudan since exercises into the European is one (out of may) exercises strategies of the Bermudan.
 * </ul>
 */
@Test
public void testEuropeanAsianBermudanOption() throws CalculationException {
	/*
	 * Common parameters
	 */
	double maturity = 3.0;
	double strike = 1.06;

	/*
	 * European Option
	 */
	EuropeanOption myEuropeanOption = new EuropeanOption(maturity,strike);
	double valueOfEuropeanOption = myEuropeanOption.getValue(model);

	/*
	 * Asian Option
	 */
	double[] averagingPoints = { 1.0, 1.5, 2.0, 2.5 , 3.0 };

	AsianOption myAsianOption = new AsianOption(maturity,strike, new TimeDiscretization(averagingPoints));
	double valueOfAsianOption = myAsianOption.getValue(model);

	/*
	 * Bermudan Option
	 */
	double[] exerciseDates	= { 1.0,  2.0,  3.0};
	double[] notionals		= { 1.20, 1.10, 1.0};
	double[] strikes		= { 1.03, 1.05, 1.06 };

	// Lower bound method
	BermudanOption myBermudanOptionLowerBound = new BermudanOption(exerciseDates, notionals, strikes, BermudanOption.ExerciseMethod.ESTIMATE_COND_EXPECTATION);
	double valueOfBermudanOptionLowerBound = myBermudanOptionLowerBound.getValue(model);

	// Upper bound method
	BermudanOption myBermudanOptionUpperBound = new BermudanOption(exerciseDates, notionals, strikes, BermudanOption.ExerciseMethod.UPPER_BOUND_METHOD);
	double valueOfBermudanOptionUpperBound = myBermudanOptionUpperBound.getValue(model);

	/*
	 * Output
	 */
	System.out.println("Value of Asian Option is \t"	+ valueOfAsianOption);
	System.out.println("Value of European Option is \t"	+ valueOfEuropeanOption);
	System.out.println("Value of Bermudan Option is \t"	+ "(" + valueOfBermudanOptionLowerBound + "," + valueOfBermudanOptionUpperBound + ")");

	assertTrue(valueOfAsianOption < valueOfEuropeanOption);
	assertTrue(valueOfBermudanOptionLowerBound < valueOfBermudanOptionUpperBound);
	assertTrue(valueOfEuropeanOption < valueOfBermudanOptionUpperBound);
}
 
开发者ID:finmath,项目名称:finmath-experiments,代码行数:59,代码来源:BlackScholesMonteCarloValuationTest.java


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