net.finmath.marketdata.model.volatilities.CapletVolatilities Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of finmath-lib Show documentation
Show all versions of finmath-lib Show documentation
finmath lib is a Mathematical Finance Library in Java.
It provides algorithms and methodologies related to mathematical finance.
/*
* (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.AnalyticModel;
import net.finmath.marketdata.model.curves.Curve;
import net.finmath.marketdata.model.curves.CurveInterpolation;
import net.finmath.marketdata.model.curves.DiscountCurve;
import net.finmath.marketdata.model.curves.ForwardCurve;
/**
* 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 CurveFromInterpolationPoints 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
* @version 1.0
*/
public class CapletVolatilities extends AbstractVolatilitySurface {
private final 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(final String name, final LocalDate referenceDate, final ForwardCurve forwardCurve,
final double[] maturities,
final double[] strikes,
final double[] volatilities,
final QuotingConvention volatilityConvention,
final DiscountCurve discountCurve) {
super(name, referenceDate, forwardCurve, discountCurve, volatilityConvention, null);
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;
}
final 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.
final double adjustedStrike = getForwardCurve().getValue(model, maturityGreaterOfEqual) + (strike - getForwardCurve().getValue(model, maturity));
value = capletVolatilities.get(maturityGreaterOfEqual).getValue(adjustedStrike);
}
return convertFromTo(model, maturity, strike, value, this.getQuotingConvention(), quotingConvention);
}
public static AbstractVolatilitySurface fromFile(final File inputFile) throws FileNotFoundException {
// Read data
final ArrayList datasets = new ArrayList<>();
try(BufferedReader dataStream = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)))) {
String line;
while((line = dataStream.readLine()) != null) {
// Ignore non caplet data
if(!line.startsWith("caplet\t")) {
continue;
}
datasets.add(line);
}
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// @TODO Name and reference date have to be set?!
final CapletVolatilities capletVolatilities = new CapletVolatilities(null, null);
// Parse data
for(int datasetIndex=0; datasetIndex