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

net.finmath.marketdata.model.volatilities.CapletVolatilities Maven / Gradle / Ivy

Go to download

finmath lib is a Mathematical Finance Library in Java. It provides algorithms and methodologies related to mathematical finance.

There is a newer version: 6.0.19
Show newest version
/*
 * (c) Copyright Christian P. Fries, Germany. Contact: [email protected].
 *
 * Created on 20.05.2005
 */
package net.finmath.marketdata.model.volatilities;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import net.finmath.marketdata.model.AnalyticModelInterface;
import net.finmath.marketdata.model.curves.Curve;
import net.finmath.marketdata.model.curves.CurveInterface;
import net.finmath.marketdata.model.curves.DiscountCurveInterface;
import net.finmath.marketdata.model.curves.ForwardCurveInterface;

/**
 * A very simple container for Caplet volatilities.
 * 
 * It performs piecewise constant interpolation (discretization) in maturity dimension on iso-moneyness lines
 * and uses the default interpolation from the Curve class in strike dimension.
 * 
 * It allows to convert from several quoting conventions.
 * 
 * It needs a forward curve and a discount curve. The tenor length of the Caplet is inferred
 * from the forward curve.
 * 
 * @TODO: Need to add forward and discount curve to support implied vol.
 * @author Christian Fries
 */
public class CapletVolatilities extends AbstractVolatilitySurface {

	private Map	capletVolatilities = new HashMap();
	
	private transient Double[]	maturities;
	private transient Object		lazyInitLock = new Object();
	
	/**
	 * @param name The name of this volatility surface.
	 * @param referenceDate The reference date for this volatility surface, i.e., the date which defined t=0.
	 * @param forwardCurve The underlying forward curve.
	 * @param maturities The vector of maturities of the quotes.
	 * @param strikes The vector of strikes of the quotes.
	 * @param volatilities The vector of volatilities of the quotes.
	 * @param volatilityConvention The quoting convention of the volatilities provided.
	 * @param discountCurve The associated discount curve.
	 */
	public CapletVolatilities(String name, LocalDate referenceDate, ForwardCurveInterface forwardCurve,
			double[] maturities,
			double[] strikes,
			double[] volatilities,
			QuotingConvention volatilityConvention,
			DiscountCurveInterface discountCurve)  {
		super(name, referenceDate);
		this.forwardCurve = forwardCurve;
		this.discountCurve = discountCurve;
		this.quotingConvention = volatilityConvention;
		
		if(maturities.length != strikes.length || maturities.length != volatilities.length)
			throw new IllegalArgumentException("Length of vectors is not equal.");
		
		for(int i=0; i maturities.length-1) maturityGreaterEqualIndex = maturities.length-1;

			double maturityGreaterOfEqual	= maturities[maturityGreaterEqualIndex];

			// @TODO: Below we should trigger an exception if no forwardCurve is supplied but needed.
			// Interpolation / extrapolation is performed on iso-moneyness lines.
			double adjustedStrike = forwardCurve.getValue(model, maturityGreaterOfEqual) + (strike - forwardCurve.getValue(model, maturity));

			value			= capletVolatilities.get(maturityGreaterOfEqual).getValue(adjustedStrike);
		}

		return convertFromTo(model, maturity, strike, value, this.quotingConvention, quotingConvention);
	}

	public static AbstractVolatilitySurface fromFile(File inputFile) throws FileNotFoundException {
		// Read data
		BufferedReader		dataStream	= new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));		
		ArrayList	datasets	= new ArrayList();
		try {
			while(true) {
				String line = dataStream.readLine();
	
				// Check for end of file
				if(line == null)	break;
	
				// Ignore non caplet data
				if(!line.startsWith("caplet\t")) continue;

				datasets.add(line);
			}
			dataStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// @TODO: Name and reference date have to be set?!
		CapletVolatilities capletVolatilities = new CapletVolatilities(null, null);
		
		// Parse data
		for(int datasetIndex=0; datasetIndex




© 2015 - 2025 Weber Informatics LLC | Privacy Policy