Skip to content

Commit

Permalink
Improved output. Impelenting against an interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
cfries committed Jun 27, 2024
1 parent fa5d97b commit 990bc43
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.finmath.montecarlo.BrownianMotion;
import net.finmath.montecarlo.BrownianMotionFromMersenneRandomNumbers;
import net.finmath.montecarlo.assetderivativevaluation.models.BlackScholesModel;
import net.finmath.montecarlo.assetderivativevaluation.products.AssetMonteCarloProduct;
import net.finmath.montecarlo.assetderivativevaluation.products.EuropeanOption;
import net.finmath.montecarlo.model.ProcessModel;
import net.finmath.montecarlo.process.EulerSchemeFromProcessModel;
Expand Down Expand Up @@ -65,20 +66,23 @@ public void testDirectValuation() throws CalculationException {

final RandomVariable asset = process.getProcessValue(timeDiscretization.getTimeIndex(optionMaturity), assetIndex);
final RandomVariable numeraireAtPayment = model.getNumeraire(process, optionMaturity);
final RandomVariable numeraireAtEval = model.getNumeraire(process, 0.0);
final RandomVariable numeraireAtEval = model.getNumeraire(process, initialTime);

final RandomVariable payoff = asset.sub(optionStrike).floor(0.0);
final double value = payoff.div(numeraireAtPayment).mult(numeraireAtEval).getAverage();

final 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);

System.out.println("Implementation using model " + model.getClass().getSimpleName() + " directly.");
System.out.println("\tvalue using Monte-Carlo.......: " + value);
System.out.println("\tvalue using analytic formula..: " + valueAnalytic);

Assert.assertEquals(valueAnalytic, value, 0.005);
}

@Test
public void testProductImplementation() throws CalculationException {

/*
* Model
*/
Expand Down Expand Up @@ -107,19 +111,20 @@ public void testProductImplementation() throws CalculationException {
*/

// Create product
final EuropeanOption europeanOption = new EuropeanOption(optionMaturity, optionStrike);
final AssetMonteCarloProduct europeanOption = new EuropeanOption(optionMaturity, optionStrike);

// Value product using model
final double value = europeanOption.getValue(monteCarloBlackScholesModel);
final double value = europeanOption.getValue(initialTime, monteCarloBlackScholesModel).expectation().doubleValue();

/*
* Analytic value using Black-Scholes formula
*/

final 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);
System.out.println("\nImplementation using model " + model.getClass().getSimpleName() + " with product " + europeanOption.getClass().getSimpleName());
System.out.println("\tvalue using Monte-Carlo.......: " + value);
System.out.println("\tvalue using analytic formula..: " + valueAnalytic);

Assert.assertEquals(valueAnalytic, value, 0.005);
}
Expand Down

0 comments on commit 990bc43

Please sign in to comment.