All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.finmath.plots.demo.Plot2DDemo2 Maven / Gradle / Ivy

Go to download

finmath lib plot extensions provide convenient plotting methods by providing consistent wrappers to plot libraries (like JFreeChart or JavaFX).

The newest version!
/*
 * (c) Copyright Christian P. Fries, Germany. All rights reserved. Contact: [email protected].
 *
 * Created on 21 May 2018
 */

package net.finmath.plots.demo;

import java.util.Arrays;
import java.util.function.DoubleUnaryOperator;

import net.finmath.functions.AnalyticFormulas;
import net.finmath.plots.Named;
import net.finmath.plots.Plot;
import net.finmath.plots.Plot2D;

/**
 * Plots the value of an option under the Black-Scholes model as a function of strike and time-to-maturity.
 *
 * @author Christian Fries
 */
public class Plot2DDemo2 {

	/**
	 * Run the demo.
	 * Plots the value of an option under the Black-Scholes model as a function of strike and time-to-maturity.
	 *
	 * @param args Not used.
	 * @throws Exception Exception from the graphics backend.
	 */
	public static void main(final String[] args) throws Exception {

		final double initialStockValue = 100;
		final double riskFreeRate = 0.04;
		final double volatility = 0.40;

		final DoubleUnaryOperator function = (strike) -> {
			final double optionMaturity = 1.0;
			final double optionStrike = strike;

			final double z = AnalyticFormulas.blackScholesOptionValue(initialStockValue, riskFreeRate, volatility, optionMaturity, optionStrike);

			return z;
		};

		final DoubleUnaryOperator function2 = (strike) -> {
			final double optionStrike = strike;
			final double optionMaturity = 2.0;

			final double z = AnalyticFormulas.blackScholesOptionValue(initialStockValue, riskFreeRate, volatility, optionMaturity, optionStrike);

			return z;
		};

		final Plot plot = new Plot2D(0.0, 300.0, 100, Arrays.asList(
				new Named("Maturity 1", function),
				new Named("Maturity 2", function2)));

		plot.setTitle("Black-Scholes Model European Option Value").setXAxisLabel("strike").setYAxisLabel("value").setIsLegendVisible(true);
		plot.show();
		// Plots will stay open and the program will not stop. Call plot.close() to end the app.
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy