com.opengamma.strata.pricer.capfloor.DirectIborCapletFloorletFlatVolatilityDefinition Maven / Gradle / Ivy
/*
* Copyright (C) 2019 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.pricer.capfloor;
import static com.opengamma.strata.market.ValueType.BLACK_VOLATILITY;
import static com.opengamma.strata.market.ValueType.NORMAL_VOLATILITY;
import static com.opengamma.strata.market.ValueType.YEAR_FRACTION;
import java.io.Serializable;
import java.util.Map;
import java.util.NoSuchElementException;
import org.joda.beans.Bean;
import org.joda.beans.ImmutableBean;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaBean;
import org.joda.beans.MetaProperty;
import org.joda.beans.gen.BeanDefinition;
import org.joda.beans.gen.PropertyDefinition;
import org.joda.beans.impl.direct.DirectFieldsBeanBuilder;
import org.joda.beans.impl.direct.DirectMetaBean;
import org.joda.beans.impl.direct.DirectMetaProperty;
import org.joda.beans.impl.direct.DirectMetaPropertyMap;
import com.opengamma.strata.basics.date.DayCount;
import com.opengamma.strata.basics.index.IborIndex;
import com.opengamma.strata.collect.ArgChecker;
import com.opengamma.strata.collect.array.DoubleArray;
import com.opengamma.strata.collect.array.DoubleMatrix;
import com.opengamma.strata.market.curve.CurveMetadata;
import com.opengamma.strata.market.curve.Curves;
import com.opengamma.strata.market.curve.DefaultCurveMetadata;
import com.opengamma.strata.market.curve.interpolator.CurveExtrapolator;
import com.opengamma.strata.market.curve.interpolator.CurveExtrapolators;
import com.opengamma.strata.market.curve.interpolator.CurveInterpolator;
import com.opengamma.strata.market.surface.SurfaceMetadata;
import com.opengamma.strata.math.impl.interpolation.PenaltyMatrixGenerator;
import com.opengamma.strata.pricer.option.RawOptionData;
/**
* Definition of caplet volatilities calibration.
*
* This definition is used with {@link DirectIborCapletFloorletFlatVolatilityCalibrator}.
* The volatilities of the constituent caplets in the market caps are "model parameters"
* and calibrated to the market data under a certain penalty constraint.
* The resulting volatilities object will be a set of caplet volatilities on the expiry dimension
* interpolated by {@link CurveInterpolator}.
*
* The penalty defined in this class is based on the finite difference approximation of the second order derivatives
* along time dimension. See {@link PenaltyMatrixGenerator} for detail.
*/
@BeanDefinition
public final class DirectIborCapletFloorletFlatVolatilityDefinition
implements IborCapletFloorletVolatilityDefinition, ImmutableBean, Serializable {
/**
* The name of the volatilities.
*/
@PropertyDefinition(validate = "notNull", overrideGet = true)
private final IborCapletFloorletVolatilitiesName name;
/**
* The Ibor index for which the data is valid.
*/
@PropertyDefinition(validate = "notNull", overrideGet = true)
private final IborIndex index;
/**
* The day count to measure the time.
*/
@PropertyDefinition(validate = "notNull", overrideGet = true)
private final DayCount dayCount;
/**
* Penalty intensity parameter.
*/
@PropertyDefinition(validate = "ArgChecker.notNegative")
private final double lambda;
/**
* The interpolator for the caplet volatilities.
*/
@PropertyDefinition(validate = "notNull")
private final CurveInterpolator interpolator;
/**
* The extrapolator for the caplet volatilities on the left.
*/
@PropertyDefinition(validate = "notNull")
private final CurveExtrapolator extrapolatorLeft;
/**
* The extrapolator for the caplet volatilities on the right.
*/
@PropertyDefinition(validate = "notNull")
private final CurveExtrapolator extrapolatorRight;
//-------------------------------------------------------------------------
/**
* Obtains an instance with flat extrapolators.
*
* @param name the name of the volatilities
* @param index the Ibor index
* @param dayCount the day count to use
* @param lambda the penalty intensity parameter
* @param interpolator the interpolator
* @return the instance
*/
public static DirectIborCapletFloorletFlatVolatilityDefinition of(
IborCapletFloorletVolatilitiesName name,
IborIndex index,
DayCount dayCount,
double lambda,
CurveInterpolator interpolator) {
return of(
name,
index,
dayCount,
lambda,
interpolator,
CurveExtrapolators.FLAT,
CurveExtrapolators.FLAT);
}
/**
* Obtains an instance.
*
* @param name the name of the volatilities
* @param index the Ibor index
* @param dayCount the day count to use
* @param lambda the penalty intensity parameter
* @param interpolator the surface interpolator
* @param extrapolatorLeft the extrapolator left
* @param extrapolatorRight the extrapolator right
* @return the instance
*/
public static DirectIborCapletFloorletFlatVolatilityDefinition of(
IborCapletFloorletVolatilitiesName name,
IborIndex index,
DayCount dayCount,
double lambda,
CurveInterpolator interpolator,
CurveExtrapolator extrapolatorLeft,
CurveExtrapolator extrapolatorRight) {
return new DirectIborCapletFloorletFlatVolatilityDefinition(
name,
index,
dayCount,
lambda,
interpolator,
extrapolatorLeft,
extrapolatorRight);
}
//-------------------------------------------------------------------------
@Override
public SurfaceMetadata createMetadata(RawOptionData capFloorData) {
throw new IllegalArgumentException("Surface metadata is not defined for this definition. Use createCurveMetadata.");
}
/**
* Creates curve metadata.
*
* @param capFloorData the data
* @return the curve metadata
*/
public CurveMetadata createCurveMetadata(RawOptionData capFloorData) {
CurveMetadata metadata;
if (capFloorData.getDataType().equals(BLACK_VOLATILITY)) {
metadata = Curves.blackVolatilityByExpiry(name.getName(), dayCount);
} else if (capFloorData.getDataType().equals(NORMAL_VOLATILITY)) {
metadata = DefaultCurveMetadata.builder()
.curveName(name.getName())
.xValueType(YEAR_FRACTION)
.yValueType(NORMAL_VOLATILITY)
.dayCount(dayCount)
.build();
} else {
throw new IllegalArgumentException("Data type not supported");
}
return metadata;
}
/**
* Computes penalty matrix.
*
* The penalty matrix is based on the second order finite difference differentiation in {@link PenaltyMatrixGenerator}.
* The number of node points must be greater than 2 in order to compute the second order derivative.
*
* @param expiries the expiries
* @return the penalty matrix
*/
public DoubleMatrix computePenaltyMatrix(DoubleArray expiries) {
ArgChecker.isTrue(expiries.size() > 2, "Need at least 3 points for a curvature estimate");
DoubleMatrix penalty = PenaltyMatrixGenerator.getPenaltyMatrix(expiries.toArray(), 2);
return penalty.multipliedBy(lambda);
}
//------------------------- AUTOGENERATED START -------------------------
/**
* The meta-bean for {@code DirectIborCapletFloorletFlatVolatilityDefinition}.
* @return the meta-bean, not null
*/
public static DirectIborCapletFloorletFlatVolatilityDefinition.Meta meta() {
return DirectIborCapletFloorletFlatVolatilityDefinition.Meta.INSTANCE;
}
static {
MetaBean.register(DirectIborCapletFloorletFlatVolatilityDefinition.Meta.INSTANCE);
}
/**
* The serialization version id.
*/
private static final long serialVersionUID = 1L;
/**
* Returns a builder used to create an instance of the bean.
* @return the builder, not null
*/
public static DirectIborCapletFloorletFlatVolatilityDefinition.Builder builder() {
return new DirectIborCapletFloorletFlatVolatilityDefinition.Builder();
}
private DirectIborCapletFloorletFlatVolatilityDefinition(
IborCapletFloorletVolatilitiesName name,
IborIndex index,
DayCount dayCount,
double lambda,
CurveInterpolator interpolator,
CurveExtrapolator extrapolatorLeft,
CurveExtrapolator extrapolatorRight) {
JodaBeanUtils.notNull(name, "name");
JodaBeanUtils.notNull(index, "index");
JodaBeanUtils.notNull(dayCount, "dayCount");
ArgChecker.notNegative(lambda, "lambda");
JodaBeanUtils.notNull(interpolator, "interpolator");
JodaBeanUtils.notNull(extrapolatorLeft, "extrapolatorLeft");
JodaBeanUtils.notNull(extrapolatorRight, "extrapolatorRight");
this.name = name;
this.index = index;
this.dayCount = dayCount;
this.lambda = lambda;
this.interpolator = interpolator;
this.extrapolatorLeft = extrapolatorLeft;
this.extrapolatorRight = extrapolatorRight;
}
@Override
public DirectIborCapletFloorletFlatVolatilityDefinition.Meta metaBean() {
return DirectIborCapletFloorletFlatVolatilityDefinition.Meta.INSTANCE;
}
//-----------------------------------------------------------------------
/**
* Gets the name of the volatilities.
* @return the value of the property, not null
*/
@Override
public IborCapletFloorletVolatilitiesName getName() {
return name;
}
//-----------------------------------------------------------------------
/**
* Gets the Ibor index for which the data is valid.
* @return the value of the property, not null
*/
@Override
public IborIndex getIndex() {
return index;
}
//-----------------------------------------------------------------------
/**
* Gets the day count to measure the time.
* @return the value of the property, not null
*/
@Override
public DayCount getDayCount() {
return dayCount;
}
//-----------------------------------------------------------------------
/**
* Gets penalty intensity parameter.
* @return the value of the property
*/
public double getLambda() {
return lambda;
}
//-----------------------------------------------------------------------
/**
* Gets the interpolator for the caplet volatilities.
* @return the value of the property, not null
*/
public CurveInterpolator getInterpolator() {
return interpolator;
}
//-----------------------------------------------------------------------
/**
* Gets the extrapolator for the caplet volatilities on the left.
* @return the value of the property, not null
*/
public CurveExtrapolator getExtrapolatorLeft() {
return extrapolatorLeft;
}
//-----------------------------------------------------------------------
/**
* Gets the extrapolator for the caplet volatilities on the right.
* @return the value of the property, not null
*/
public CurveExtrapolator getExtrapolatorRight() {
return extrapolatorRight;
}
//-----------------------------------------------------------------------
/**
* Returns a builder that allows this bean to be mutated.
* @return the mutable builder, not null
*/
public Builder toBuilder() {
return new Builder(this);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj != null && obj.getClass() == this.getClass()) {
DirectIborCapletFloorletFlatVolatilityDefinition other = (DirectIborCapletFloorletFlatVolatilityDefinition) obj;
return JodaBeanUtils.equal(name, other.name) &&
JodaBeanUtils.equal(index, other.index) &&
JodaBeanUtils.equal(dayCount, other.dayCount) &&
JodaBeanUtils.equal(lambda, other.lambda) &&
JodaBeanUtils.equal(interpolator, other.interpolator) &&
JodaBeanUtils.equal(extrapolatorLeft, other.extrapolatorLeft) &&
JodaBeanUtils.equal(extrapolatorRight, other.extrapolatorRight);
}
return false;
}
@Override
public int hashCode() {
int hash = getClass().hashCode();
hash = hash * 31 + JodaBeanUtils.hashCode(name);
hash = hash * 31 + JodaBeanUtils.hashCode(index);
hash = hash * 31 + JodaBeanUtils.hashCode(dayCount);
hash = hash * 31 + JodaBeanUtils.hashCode(lambda);
hash = hash * 31 + JodaBeanUtils.hashCode(interpolator);
hash = hash * 31 + JodaBeanUtils.hashCode(extrapolatorLeft);
hash = hash * 31 + JodaBeanUtils.hashCode(extrapolatorRight);
return hash;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(256);
buf.append("DirectIborCapletFloorletFlatVolatilityDefinition{");
buf.append("name").append('=').append(JodaBeanUtils.toString(name)).append(',').append(' ');
buf.append("index").append('=').append(JodaBeanUtils.toString(index)).append(',').append(' ');
buf.append("dayCount").append('=').append(JodaBeanUtils.toString(dayCount)).append(',').append(' ');
buf.append("lambda").append('=').append(JodaBeanUtils.toString(lambda)).append(',').append(' ');
buf.append("interpolator").append('=').append(JodaBeanUtils.toString(interpolator)).append(',').append(' ');
buf.append("extrapolatorLeft").append('=').append(JodaBeanUtils.toString(extrapolatorLeft)).append(',').append(' ');
buf.append("extrapolatorRight").append('=').append(JodaBeanUtils.toString(extrapolatorRight));
buf.append('}');
return buf.toString();
}
//-----------------------------------------------------------------------
/**
* The meta-bean for {@code DirectIborCapletFloorletFlatVolatilityDefinition}.
*/
public static final class Meta extends DirectMetaBean {
/**
* The singleton instance of the meta-bean.
*/
static final Meta INSTANCE = new Meta();
/**
* The meta-property for the {@code name} property.
*/
private final MetaProperty name = DirectMetaProperty.ofImmutable(
this, "name", DirectIborCapletFloorletFlatVolatilityDefinition.class, IborCapletFloorletVolatilitiesName.class);
/**
* The meta-property for the {@code index} property.
*/
private final MetaProperty index = DirectMetaProperty.ofImmutable(
this, "index", DirectIborCapletFloorletFlatVolatilityDefinition.class, IborIndex.class);
/**
* The meta-property for the {@code dayCount} property.
*/
private final MetaProperty dayCount = DirectMetaProperty.ofImmutable(
this, "dayCount", DirectIborCapletFloorletFlatVolatilityDefinition.class, DayCount.class);
/**
* The meta-property for the {@code lambda} property.
*/
private final MetaProperty lambda = DirectMetaProperty.ofImmutable(
this, "lambda", DirectIborCapletFloorletFlatVolatilityDefinition.class, Double.TYPE);
/**
* The meta-property for the {@code interpolator} property.
*/
private final MetaProperty interpolator = DirectMetaProperty.ofImmutable(
this, "interpolator", DirectIborCapletFloorletFlatVolatilityDefinition.class, CurveInterpolator.class);
/**
* The meta-property for the {@code extrapolatorLeft} property.
*/
private final MetaProperty extrapolatorLeft = DirectMetaProperty.ofImmutable(
this, "extrapolatorLeft", DirectIborCapletFloorletFlatVolatilityDefinition.class, CurveExtrapolator.class);
/**
* The meta-property for the {@code extrapolatorRight} property.
*/
private final MetaProperty extrapolatorRight = DirectMetaProperty.ofImmutable(
this, "extrapolatorRight", DirectIborCapletFloorletFlatVolatilityDefinition.class, CurveExtrapolator.class);
/**
* The meta-properties.
*/
private final Map> metaPropertyMap$ = new DirectMetaPropertyMap(
this, null,
"name",
"index",
"dayCount",
"lambda",
"interpolator",
"extrapolatorLeft",
"extrapolatorRight");
/**
* Restricted constructor.
*/
private Meta() {
}
@Override
protected MetaProperty> metaPropertyGet(String propertyName) {
switch (propertyName.hashCode()) {
case 3373707: // name
return name;
case 100346066: // index
return index;
case 1905311443: // dayCount
return dayCount;
case -1110092857: // lambda
return lambda;
case 2096253127: // interpolator
return interpolator;
case 1271703994: // extrapolatorLeft
return extrapolatorLeft;
case 773779145: // extrapolatorRight
return extrapolatorRight;
}
return super.metaPropertyGet(propertyName);
}
@Override
public DirectIborCapletFloorletFlatVolatilityDefinition.Builder builder() {
return new DirectIborCapletFloorletFlatVolatilityDefinition.Builder();
}
@Override
public Class extends DirectIborCapletFloorletFlatVolatilityDefinition> beanType() {
return DirectIborCapletFloorletFlatVolatilityDefinition.class;
}
@Override
public Map> metaPropertyMap() {
return metaPropertyMap$;
}
//-----------------------------------------------------------------------
/**
* The meta-property for the {@code name} property.
* @return the meta-property, not null
*/
public MetaProperty name() {
return name;
}
/**
* The meta-property for the {@code index} property.
* @return the meta-property, not null
*/
public MetaProperty index() {
return index;
}
/**
* The meta-property for the {@code dayCount} property.
* @return the meta-property, not null
*/
public MetaProperty dayCount() {
return dayCount;
}
/**
* The meta-property for the {@code lambda} property.
* @return the meta-property, not null
*/
public MetaProperty lambda() {
return lambda;
}
/**
* The meta-property for the {@code interpolator} property.
* @return the meta-property, not null
*/
public MetaProperty interpolator() {
return interpolator;
}
/**
* The meta-property for the {@code extrapolatorLeft} property.
* @return the meta-property, not null
*/
public MetaProperty extrapolatorLeft() {
return extrapolatorLeft;
}
/**
* The meta-property for the {@code extrapolatorRight} property.
* @return the meta-property, not null
*/
public MetaProperty extrapolatorRight() {
return extrapolatorRight;
}
//-----------------------------------------------------------------------
@Override
protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
switch (propertyName.hashCode()) {
case 3373707: // name
return ((DirectIborCapletFloorletFlatVolatilityDefinition) bean).getName();
case 100346066: // index
return ((DirectIborCapletFloorletFlatVolatilityDefinition) bean).getIndex();
case 1905311443: // dayCount
return ((DirectIborCapletFloorletFlatVolatilityDefinition) bean).getDayCount();
case -1110092857: // lambda
return ((DirectIborCapletFloorletFlatVolatilityDefinition) bean).getLambda();
case 2096253127: // interpolator
return ((DirectIborCapletFloorletFlatVolatilityDefinition) bean).getInterpolator();
case 1271703994: // extrapolatorLeft
return ((DirectIborCapletFloorletFlatVolatilityDefinition) bean).getExtrapolatorLeft();
case 773779145: // extrapolatorRight
return ((DirectIborCapletFloorletFlatVolatilityDefinition) bean).getExtrapolatorRight();
}
return super.propertyGet(bean, propertyName, quiet);
}
@Override
protected void propertySet(Bean bean, String propertyName, Object newValue, boolean quiet) {
metaProperty(propertyName);
if (quiet) {
return;
}
throw new UnsupportedOperationException("Property cannot be written: " + propertyName);
}
}
//-----------------------------------------------------------------------
/**
* The bean-builder for {@code DirectIborCapletFloorletFlatVolatilityDefinition}.
*/
public static final class Builder extends DirectFieldsBeanBuilder {
private IborCapletFloorletVolatilitiesName name;
private IborIndex index;
private DayCount dayCount;
private double lambda;
private CurveInterpolator interpolator;
private CurveExtrapolator extrapolatorLeft;
private CurveExtrapolator extrapolatorRight;
/**
* Restricted constructor.
*/
private Builder() {
}
/**
* Restricted copy constructor.
* @param beanToCopy the bean to copy from, not null
*/
private Builder(DirectIborCapletFloorletFlatVolatilityDefinition beanToCopy) {
this.name = beanToCopy.getName();
this.index = beanToCopy.getIndex();
this.dayCount = beanToCopy.getDayCount();
this.lambda = beanToCopy.getLambda();
this.interpolator = beanToCopy.getInterpolator();
this.extrapolatorLeft = beanToCopy.getExtrapolatorLeft();
this.extrapolatorRight = beanToCopy.getExtrapolatorRight();
}
//-----------------------------------------------------------------------
@Override
public Object get(String propertyName) {
switch (propertyName.hashCode()) {
case 3373707: // name
return name;
case 100346066: // index
return index;
case 1905311443: // dayCount
return dayCount;
case -1110092857: // lambda
return lambda;
case 2096253127: // interpolator
return interpolator;
case 1271703994: // extrapolatorLeft
return extrapolatorLeft;
case 773779145: // extrapolatorRight
return extrapolatorRight;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
}
@Override
public Builder set(String propertyName, Object newValue) {
switch (propertyName.hashCode()) {
case 3373707: // name
this.name = (IborCapletFloorletVolatilitiesName) newValue;
break;
case 100346066: // index
this.index = (IborIndex) newValue;
break;
case 1905311443: // dayCount
this.dayCount = (DayCount) newValue;
break;
case -1110092857: // lambda
this.lambda = (Double) newValue;
break;
case 2096253127: // interpolator
this.interpolator = (CurveInterpolator) newValue;
break;
case 1271703994: // extrapolatorLeft
this.extrapolatorLeft = (CurveExtrapolator) newValue;
break;
case 773779145: // extrapolatorRight
this.extrapolatorRight = (CurveExtrapolator) newValue;
break;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
return this;
}
@Override
public Builder set(MetaProperty> property, Object value) {
super.set(property, value);
return this;
}
@Override
public DirectIborCapletFloorletFlatVolatilityDefinition build() {
return new DirectIborCapletFloorletFlatVolatilityDefinition(
name,
index,
dayCount,
lambda,
interpolator,
extrapolatorLeft,
extrapolatorRight);
}
//-----------------------------------------------------------------------
/**
* Sets the name of the volatilities.
* @param name the new value, not null
* @return this, for chaining, not null
*/
public Builder name(IborCapletFloorletVolatilitiesName name) {
JodaBeanUtils.notNull(name, "name");
this.name = name;
return this;
}
/**
* Sets the Ibor index for which the data is valid.
* @param index the new value, not null
* @return this, for chaining, not null
*/
public Builder index(IborIndex index) {
JodaBeanUtils.notNull(index, "index");
this.index = index;
return this;
}
/**
* Sets the day count to measure the time.
* @param dayCount the new value, not null
* @return this, for chaining, not null
*/
public Builder dayCount(DayCount dayCount) {
JodaBeanUtils.notNull(dayCount, "dayCount");
this.dayCount = dayCount;
return this;
}
/**
* Sets penalty intensity parameter.
* @param lambda the new value
* @return this, for chaining, not null
*/
public Builder lambda(double lambda) {
ArgChecker.notNegative(lambda, "lambda");
this.lambda = lambda;
return this;
}
/**
* Sets the interpolator for the caplet volatilities.
* @param interpolator the new value, not null
* @return this, for chaining, not null
*/
public Builder interpolator(CurveInterpolator interpolator) {
JodaBeanUtils.notNull(interpolator, "interpolator");
this.interpolator = interpolator;
return this;
}
/**
* Sets the extrapolator for the caplet volatilities on the left.
* @param extrapolatorLeft the new value, not null
* @return this, for chaining, not null
*/
public Builder extrapolatorLeft(CurveExtrapolator extrapolatorLeft) {
JodaBeanUtils.notNull(extrapolatorLeft, "extrapolatorLeft");
this.extrapolatorLeft = extrapolatorLeft;
return this;
}
/**
* Sets the extrapolator for the caplet volatilities on the right.
* @param extrapolatorRight the new value, not null
* @return this, for chaining, not null
*/
public Builder extrapolatorRight(CurveExtrapolator extrapolatorRight) {
JodaBeanUtils.notNull(extrapolatorRight, "extrapolatorRight");
this.extrapolatorRight = extrapolatorRight;
return this;
}
//-----------------------------------------------------------------------
@Override
public String toString() {
StringBuilder buf = new StringBuilder(256);
buf.append("DirectIborCapletFloorletFlatVolatilityDefinition.Builder{");
buf.append("name").append('=').append(JodaBeanUtils.toString(name)).append(',').append(' ');
buf.append("index").append('=').append(JodaBeanUtils.toString(index)).append(',').append(' ');
buf.append("dayCount").append('=').append(JodaBeanUtils.toString(dayCount)).append(',').append(' ');
buf.append("lambda").append('=').append(JodaBeanUtils.toString(lambda)).append(',').append(' ');
buf.append("interpolator").append('=').append(JodaBeanUtils.toString(interpolator)).append(',').append(' ');
buf.append("extrapolatorLeft").append('=').append(JodaBeanUtils.toString(extrapolatorLeft)).append(',').append(' ');
buf.append("extrapolatorRight").append('=').append(JodaBeanUtils.toString(extrapolatorRight));
buf.append('}');
return buf.toString();
}
}
//-------------------------- AUTOGENERATED END --------------------------
}