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

net.finmath.plots.PlotProcess2D 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. Contact: [email protected].
 *
 * Created on 21 May 2018
 */

package net.finmath.plots;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.function.DoubleFunction;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import net.finmath.plots.jfreechart.JFreeChartUtilities;
import net.finmath.stochastic.RandomVariable;
import net.finmath.time.TimeDiscretization;

/**
 * Small convenient wrapper for JFreeChart line plot of a stochastic process.
 *
 * @author Christian Fries
 */
public class PlotProcess2D implements Plot {

	private final TimeDiscretization timeDiscretization;
	private final Named> process;
	private final int maxNumberOfPaths;

	private String title = "";
	private String xAxisLabel = "x";
	private String yAxisLabel = "y";
	private NumberFormat xAxisNumberFormat = new DecimalFormat("#.##");
	private NumberFormat yAxisNumberFormat = new DecimalFormat("#.##");
	private Boolean isLegendVisible = false;

	private transient JFrame frame;
	private transient JFreeChart chart;
	private Color[] colors;

	/**
	 * Plot the first (maxNumberOfPaths) paths of a time discrete stochastic process.
	 *
	 * @param timeDiscretization The time discretization to be used for the x-axis.
	 * @param process The stochastic process to be plotted against the y-axsis (the first n paths are plotted).
	 * @param maxNumberOfPaths Maximum number of path (n) to be plotted.
	 */
	public PlotProcess2D(final TimeDiscretization timeDiscretization, final Named> process, final int maxNumberOfPaths) {
		super();
		this.timeDiscretization = timeDiscretization;
		this.process = process;
		this.maxNumberOfPaths = maxNumberOfPaths;
	}

	/**
	 * Plot the first (maxNumberOfPaths) paths of a time discrete stochastic process.
	 *
	 * @param timeDiscretization The time discretization to be used for the x-axis.
	 * @param process The stochastic process to be plotted against the y-axsis (the first n paths are plotted).
	 * @param maxNumberOfPaths Maximum number of path (n) to be plotted.
	 */
	public PlotProcess2D(final TimeDiscretization timeDiscretization, final DoubleFunction process, final int maxNumberOfPaths) {
		super();
		this.timeDiscretization = timeDiscretization;
		this.process = new Named>("", process);
		this.maxNumberOfPaths = maxNumberOfPaths;
	}

	/**
	 * Plot the first (maxNumberOfPaths) paths of a time discrete stochastic process.
	 *
	 * @param timeDiscretization The time discretization to be used for the x-axis.
	 * @param process The stochastic process to be plotted against the y-axsis (the first n paths are plotted).
	 * @param maxNumberOfPaths Maximum number of path (n) to be plotted.
	 */
	public PlotProcess2D(final TimeDiscretization timeDiscretization, final DoubleToRandomVariableFunction process, final int maxNumberOfPaths) {
		super();
		this.timeDiscretization = timeDiscretization;
		this.process = new Named>("", t -> { try{ return process.apply(t);} catch(final Exception e) { return null; }});
		this.maxNumberOfPaths = maxNumberOfPaths;
	}

	/**
	 * Plot the first 100 paths of a time discrete stochastic process.
	 *
	 * @param timeDiscretization The time discretization to be used for the x-axis.
	 * @param process The stochastic process to be plotted against the y-axsis.
	 */
	public PlotProcess2D(final TimeDiscretization timeDiscretization, final Named> process) {
		this(timeDiscretization, process, 100);
	}

	private void init() {
		final ArrayList seriesList = new ArrayList();
		for(final double time : timeDiscretization) {
			final RandomVariable randomVariable = process.get().apply(time);
			int numberOfPath = randomVariable.isDeterministic() ? maxNumberOfPaths : Math.min(randomVariable.size(), maxNumberOfPaths);
			for(int pathIndex=0; pathIndex




© 2015 - 2025 Weber Informatics LLC | Privacy Policy