com.opengamma.strata.pricer.rate.SimpleIborIndexRates Maven / Gradle / Ivy
/*
* Copyright (C) 2016 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.pricer.rate;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import org.joda.beans.Bean;
import org.joda.beans.BeanBuilder;
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.ImmutableConstructor;
import org.joda.beans.gen.PropertyDefinition;
import org.joda.beans.impl.direct.DirectMetaBean;
import org.joda.beans.impl.direct.DirectMetaProperty;
import org.joda.beans.impl.direct.DirectMetaPropertyMap;
import org.joda.beans.impl.direct.DirectPrivateBeanBuilder;
import com.opengamma.strata.basics.currency.Currency;
import com.opengamma.strata.basics.date.DayCount;
import com.opengamma.strata.basics.index.IborIndex;
import com.opengamma.strata.basics.index.IborIndexObservation;
import com.opengamma.strata.collect.ArgChecker;
import com.opengamma.strata.collect.Messages;
import com.opengamma.strata.collect.array.DoubleArray;
import com.opengamma.strata.collect.timeseries.LocalDateDoubleTimeSeries;
import com.opengamma.strata.data.MarketDataName;
import com.opengamma.strata.market.ValueType;
import com.opengamma.strata.market.curve.Curve;
import com.opengamma.strata.market.curve.CurveInfoType;
import com.opengamma.strata.market.curve.Curves;
import com.opengamma.strata.market.curve.InterpolatedNodalCurve;
import com.opengamma.strata.market.param.CurrencyParameterSensitivities;
import com.opengamma.strata.market.param.CurrencyParameterSensitivity;
import com.opengamma.strata.market.param.ParameterMetadata;
import com.opengamma.strata.market.param.ParameterPerturbation;
import com.opengamma.strata.market.param.UnitParameterSensitivity;
import com.opengamma.strata.market.sensitivity.PointSensitivityBuilder;
/**
* An Ibor index curve providing rates directly from a forward rates curve.
*
* This provides historic and forward rates for a single {@link IborIndex}, such as 'GBP-LIBOR-3M'.
*
* This implementation is based on an underlying curve that is stored with fixing and direct forward rates.
*/
@BeanDefinition(builderScope = "private")
public final class SimpleIborIndexRates
implements IborIndexRates, ImmutableBean, Serializable {
/**
* The index that the rates are for.
*/
@PropertyDefinition(validate = "notNull", overrideGet = true)
private final IborIndex index;
/**
* The valuation date.
*/
@PropertyDefinition(validate = "notNull", overrideGet = true)
private final LocalDate valuationDate;
/**
* The underlying forward curve.
*/
@PropertyDefinition(validate = "notNull")
private final Curve curve;
/**
* The time-series of fixings, defaulted to an empty time-series.
* This includes the known historical fixings and may be empty.
*/
@PropertyDefinition(validate = "notNull", overrideGet = true)
private final LocalDateDoubleTimeSeries fixings;
/**
* The day count convention of the curve.
*/
private final transient DayCount dayCount; // cached, not a property
/**
* Obtains an instance from a curve, with an empty time-series of fixings.
*
* The curve is specified by an instance of {@link Curve}, such as {@link InterpolatedNodalCurve}.
* The curve must have x-values of {@linkplain ValueType#YEAR_FRACTION year fractions} with
* the day count specified. The y-values must be {@linkplain ValueType#FORWARD_RATE forward rates}.
* A suitable metadata instance for the curve can be created by {@link Curves#forwardRates(String, DayCount)}.
* In the curve the Ibor rates are indexed by the maturity date.
*
* @param index the index
* @param valuationDate the valuation date for which the curve is valid
* @param curve the curve of forward rates
* @return the rates view
*/
public static SimpleIborIndexRates of(IborIndex index, LocalDate valuationDate, Curve curve) {
return new SimpleIborIndexRates(index, valuationDate, curve, LocalDateDoubleTimeSeries.empty());
}
/**
* Obtains an instance from a curve and time-series of fixing.
*
* The curve is specified by an instance of {@link Curve}, such as {@link InterpolatedNodalCurve}.
* The curve must have x-values of {@linkplain ValueType#YEAR_FRACTION year fractions} with
* the day count specified. The y-values must be {@linkplain ValueType#FORWARD_RATE forward rates}.
* In the curve the Ibor rates are indexed by the maturity date.
*
* @param index the index
* @param valuationDate the valuation date for which the curve is valid
* @param curve the curve of forward rates
* @param fixings the time-series of fixings
* @return the rates view
*/
public static SimpleIborIndexRates of(
IborIndex index,
LocalDate valuationDate,
Curve curve,
LocalDateDoubleTimeSeries fixings) {
return new SimpleIborIndexRates(index, valuationDate, curve, fixings);
}
@ImmutableConstructor
private SimpleIborIndexRates(
IborIndex index,
LocalDate valuationDate,
Curve curve,
LocalDateDoubleTimeSeries fixings) {
ArgChecker.notNull(index, "index");
ArgChecker.notNull(valuationDate, "valuationDate");
ArgChecker.notNull(curve, "curve");
ArgChecker.notNull(fixings, "fixings");
curve.getMetadata().getXValueType().checkEquals(
ValueType.YEAR_FRACTION, "Incorrect x-value type for ibor curve");
curve.getMetadata().getYValueType().checkEquals(
ValueType.FORWARD_RATE, "Incorrect y-value type for ibor curve");
DayCount dayCount = curve.getMetadata().findInfo(CurveInfoType.DAY_COUNT)
.orElseThrow(() -> new IllegalArgumentException("Incorrect curve metadata, missing DayCount"));
this.valuationDate = valuationDate;
this.index = index;
this.curve = curve;
this.fixings = fixings;
this.dayCount = dayCount;
}
// ensure standard constructor is invoked
private Object readResolve() {
return new SimpleIborIndexRates(index, valuationDate, curve, fixings);
}
//-------------------------------------------------------------------------
@Override
public Optional findData(MarketDataName name) {
if (curve.getName().equals(name)) {
return Optional.of(name.getMarketDataType().cast(curve));
}
return Optional.empty();
}
@Override
public int getParameterCount() {
return curve.getParameterCount();
}
@Override
public double getParameter(int parameterIndex) {
return curve.getParameter(parameterIndex);
}
@Override
public ParameterMetadata getParameterMetadata(int parameterIndex) {
return curve.getParameterMetadata(parameterIndex);
}
@Override
public OptionalInt findParameterIndex(ParameterMetadata metadata) {
return curve.findParameterIndex(metadata);
}
@Override
public SimpleIborIndexRates withParameter(int parameterIndex, double newValue) {
return withCurve(curve.withParameter(parameterIndex, newValue));
}
@Override
public SimpleIborIndexRates withPerturbation(ParameterPerturbation perturbation) {
return withCurve(curve.withPerturbation(perturbation));
}
//-------------------------------------------------------------------------
@Override
public double rate(IborIndexObservation observation) {
if (!observation.getFixingDate().isAfter(getValuationDate())) {
return historicRate(observation);
}
return rateIgnoringFixings(observation);
}
// historic rate
private double historicRate(IborIndexObservation observation) {
LocalDate fixingDate = observation.getFixingDate();
OptionalDouble fixedRate = fixings.get(fixingDate);
if (fixedRate.isPresent()) {
return fixedRate.getAsDouble();
} else if (fixingDate.isBefore(getValuationDate())) { // the fixing is required
if (fixings.isEmpty()) {
throw new IllegalArgumentException(
Messages.format("Unable to get fixing for {} on date {}, no time-series supplied", index, fixingDate));
}
throw new IllegalArgumentException(Messages.format("Unable to get fixing for {} on date {}", index, fixingDate));
} else {
return rateIgnoringFixings(observation);
}
}
@Override
public double rateIgnoringFixings(IborIndexObservation observation) {
double relativeYearFraction = relativeYearFraction(observation.getMaturityDate());
return curve.yValue(relativeYearFraction);
}
//-------------------------------------------------------------------------
@Override
public PointSensitivityBuilder ratePointSensitivity(IborIndexObservation observation) {
LocalDate fixingDate = observation.getFixingDate();
LocalDate valuationDate = getValuationDate();
if (fixingDate.isBefore(valuationDate) ||
(fixingDate.equals(valuationDate) && fixings.get(fixingDate).isPresent())) {
return PointSensitivityBuilder.none();
}
return IborRateSensitivity.of(observation, 1d);
}
@Override
public PointSensitivityBuilder rateIgnoringFixingsPointSensitivity(IborIndexObservation observation) {
return IborRateSensitivity.of(observation, 1d);
}
//-------------------------------------------------------------------------
@Override
public CurrencyParameterSensitivities parameterSensitivity(IborRateSensitivity pointSensitivity) {
LocalDate maturityDate = pointSensitivity.getObservation().getMaturityDate();
double relativeYearFraction = relativeYearFraction(maturityDate);
UnitParameterSensitivity unitSensitivity = curve.yValueParameterSensitivity(relativeYearFraction);
CurrencyParameterSensitivity sensitivity =
unitSensitivity.multipliedBy(pointSensitivity.getCurrency(), pointSensitivity.getSensitivity());
return CurrencyParameterSensitivities.of(sensitivity);
}
@Override
public CurrencyParameterSensitivities createParameterSensitivity(Currency currency, DoubleArray sensitivities) {
return CurrencyParameterSensitivities.of(curve.createParameterSensitivity(currency, sensitivities));
}
//-------------------------------------------------------------------------
/**
* Returns a new instance with a different curve.
*
* @param curve the new curve
* @return the new instance
*/
public SimpleIborIndexRates withCurve(Curve curve) {
return new SimpleIborIndexRates(index, valuationDate, curve, fixings);
}
// calculate the relative time between the valuation date and the specified date using the day count of the curve
private double relativeYearFraction(LocalDate date) {
return dayCount.relativeYearFraction(valuationDate, date);
}
//------------------------- AUTOGENERATED START -------------------------
/**
* The meta-bean for {@code SimpleIborIndexRates}.
* @return the meta-bean, not null
*/
public static SimpleIborIndexRates.Meta meta() {
return SimpleIborIndexRates.Meta.INSTANCE;
}
static {
MetaBean.register(SimpleIborIndexRates.Meta.INSTANCE);
}
/**
* The serialization version id.
*/
private static final long serialVersionUID = 1L;
@Override
public SimpleIborIndexRates.Meta metaBean() {
return SimpleIborIndexRates.Meta.INSTANCE;
}
//-----------------------------------------------------------------------
/**
* Gets the index that the rates are for.
* @return the value of the property, not null
*/
@Override
public IborIndex getIndex() {
return index;
}
//-----------------------------------------------------------------------
/**
* Gets the valuation date.
* @return the value of the property, not null
*/
@Override
public LocalDate getValuationDate() {
return valuationDate;
}
//-----------------------------------------------------------------------
/**
* Gets the underlying forward curve.
* @return the value of the property, not null
*/
public Curve getCurve() {
return curve;
}
//-----------------------------------------------------------------------
/**
* Gets the time-series of fixings, defaulted to an empty time-series.
* This includes the known historical fixings and may be empty.
* @return the value of the property, not null
*/
@Override
public LocalDateDoubleTimeSeries getFixings() {
return fixings;
}
//-----------------------------------------------------------------------
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj != null && obj.getClass() == this.getClass()) {
SimpleIborIndexRates other = (SimpleIborIndexRates) obj;
return JodaBeanUtils.equal(index, other.index) &&
JodaBeanUtils.equal(valuationDate, other.valuationDate) &&
JodaBeanUtils.equal(curve, other.curve) &&
JodaBeanUtils.equal(fixings, other.fixings);
}
return false;
}
@Override
public int hashCode() {
int hash = getClass().hashCode();
hash = hash * 31 + JodaBeanUtils.hashCode(index);
hash = hash * 31 + JodaBeanUtils.hashCode(valuationDate);
hash = hash * 31 + JodaBeanUtils.hashCode(curve);
hash = hash * 31 + JodaBeanUtils.hashCode(fixings);
return hash;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(160);
buf.append("SimpleIborIndexRates{");
buf.append("index").append('=').append(JodaBeanUtils.toString(index)).append(',').append(' ');
buf.append("valuationDate").append('=').append(JodaBeanUtils.toString(valuationDate)).append(',').append(' ');
buf.append("curve").append('=').append(JodaBeanUtils.toString(curve)).append(',').append(' ');
buf.append("fixings").append('=').append(JodaBeanUtils.toString(fixings));
buf.append('}');
return buf.toString();
}
//-----------------------------------------------------------------------
/**
* The meta-bean for {@code SimpleIborIndexRates}.
*/
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 index} property.
*/
private final MetaProperty index = DirectMetaProperty.ofImmutable(
this, "index", SimpleIborIndexRates.class, IborIndex.class);
/**
* The meta-property for the {@code valuationDate} property.
*/
private final MetaProperty valuationDate = DirectMetaProperty.ofImmutable(
this, "valuationDate", SimpleIborIndexRates.class, LocalDate.class);
/**
* The meta-property for the {@code curve} property.
*/
private final MetaProperty curve = DirectMetaProperty.ofImmutable(
this, "curve", SimpleIborIndexRates.class, Curve.class);
/**
* The meta-property for the {@code fixings} property.
*/
private final MetaProperty fixings = DirectMetaProperty.ofImmutable(
this, "fixings", SimpleIborIndexRates.class, LocalDateDoubleTimeSeries.class);
/**
* The meta-properties.
*/
private final Map> metaPropertyMap$ = new DirectMetaPropertyMap(
this, null,
"index",
"valuationDate",
"curve",
"fixings");
/**
* Restricted constructor.
*/
private Meta() {
}
@Override
protected MetaProperty> metaPropertyGet(String propertyName) {
switch (propertyName.hashCode()) {
case 100346066: // index
return index;
case 113107279: // valuationDate
return valuationDate;
case 95027439: // curve
return curve;
case -843784602: // fixings
return fixings;
}
return super.metaPropertyGet(propertyName);
}
@Override
public BeanBuilder extends SimpleIborIndexRates> builder() {
return new SimpleIborIndexRates.Builder();
}
@Override
public Class extends SimpleIborIndexRates> beanType() {
return SimpleIborIndexRates.class;
}
@Override
public Map> metaPropertyMap() {
return metaPropertyMap$;
}
//-----------------------------------------------------------------------
/**
* 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 valuationDate} property.
* @return the meta-property, not null
*/
public MetaProperty valuationDate() {
return valuationDate;
}
/**
* The meta-property for the {@code curve} property.
* @return the meta-property, not null
*/
public MetaProperty curve() {
return curve;
}
/**
* The meta-property for the {@code fixings} property.
* @return the meta-property, not null
*/
public MetaProperty fixings() {
return fixings;
}
//-----------------------------------------------------------------------
@Override
protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
switch (propertyName.hashCode()) {
case 100346066: // index
return ((SimpleIborIndexRates) bean).getIndex();
case 113107279: // valuationDate
return ((SimpleIborIndexRates) bean).getValuationDate();
case 95027439: // curve
return ((SimpleIborIndexRates) bean).getCurve();
case -843784602: // fixings
return ((SimpleIborIndexRates) bean).getFixings();
}
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 SimpleIborIndexRates}.
*/
private static final class Builder extends DirectPrivateBeanBuilder {
private IborIndex index;
private LocalDate valuationDate;
private Curve curve;
private LocalDateDoubleTimeSeries fixings;
/**
* Restricted constructor.
*/
private Builder() {
}
//-----------------------------------------------------------------------
@Override
public Object get(String propertyName) {
switch (propertyName.hashCode()) {
case 100346066: // index
return index;
case 113107279: // valuationDate
return valuationDate;
case 95027439: // curve
return curve;
case -843784602: // fixings
return fixings;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
}
@Override
public Builder set(String propertyName, Object newValue) {
switch (propertyName.hashCode()) {
case 100346066: // index
this.index = (IborIndex) newValue;
break;
case 113107279: // valuationDate
this.valuationDate = (LocalDate) newValue;
break;
case 95027439: // curve
this.curve = (Curve) newValue;
break;
case -843784602: // fixings
this.fixings = (LocalDateDoubleTimeSeries) newValue;
break;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
return this;
}
@Override
public SimpleIborIndexRates build() {
return new SimpleIborIndexRates(
index,
valuationDate,
curve,
fixings);
}
//-----------------------------------------------------------------------
@Override
public String toString() {
StringBuilder buf = new StringBuilder(160);
buf.append("SimpleIborIndexRates.Builder{");
buf.append("index").append('=').append(JodaBeanUtils.toString(index)).append(',').append(' ');
buf.append("valuationDate").append('=').append(JodaBeanUtils.toString(valuationDate)).append(',').append(' ');
buf.append("curve").append('=').append(JodaBeanUtils.toString(curve)).append(',').append(' ');
buf.append("fixings").append('=').append(JodaBeanUtils.toString(fixings));
buf.append('}');
return buf.toString();
}
}
//-------------------------- AUTOGENERATED END --------------------------
}