CrossOvernightFloatingStream.java
package org.drip.sample.ois;
import java.util.*;
import org.drip.analytics.date.JulianDate;
import org.drip.analytics.daycount.Convention;
import org.drip.analytics.support.*;
import org.drip.function.r1tor1.*;
import org.drip.market.definition.OvernightIndex;
import org.drip.market.otc.*;
import org.drip.numerical.common.*;
import org.drip.param.creator.*;
import org.drip.param.market.*;
import org.drip.param.period.*;
import org.drip.param.valuation.*;
import org.drip.product.creator.*;
import org.drip.product.definition.CalibratableComponent;
import org.drip.product.rates.*;
import org.drip.service.env.EnvManager;
import org.drip.spline.basis.PolynomialFunctionSetParams;
import org.drip.spline.params.*;
import org.drip.spline.stretch.*;
import org.drip.state.creator.*;
import org.drip.state.discount.*;
import org.drip.state.estimator.LatentStateStretchBuilder;
import org.drip.state.identifier.*;
import org.drip.state.inference.*;
/*
* -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*/
/*!
* Copyright (C) 2018 Lakshmi Krishnamurthy
* Copyright (C) 2017 Lakshmi Krishnamurthy
* Copyright (C) 2016 Lakshmi Krishnamurthy
* Copyright (C) 2015 Lakshmi Krishnamurthy
* Copyright (C) 2014 Lakshmi Krishnamurthy
*
* This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
* libraries targeting analysts and developers
* https://lakshmidrip.github.io/DRIP/
*
* DRIP is composed of four main libraries:
*
* - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
* - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
* - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
* - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
*
* - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
* Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
* Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
* Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
* Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
*
* - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
* Incorporator, Holdings Constraint, and Transaction Costs.
*
* - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
*
* - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* CrossOvernightStream demonstrates the construction, customization, and valuation of Cross-Currency
* Overnight Floating Streams.
*
* @author Lakshmi Krishnamurthy
*/
public class CrossOvernightFloatingStream {
private static final FixFloatComponent OTCOISFixFloat (
final JulianDate dtSpot,
final String strCurrency,
final String strMaturityTenor,
final double dblCoupon)
{
FixedFloatSwapConvention ffConv = OvernightFixedFloatContainer.FundConventionFromJurisdiction (
strCurrency
);
return ffConv.createFixFloatComponent (
dtSpot,
strMaturityTenor,
dblCoupon,
0.,
1.
);
}
/*
* Construct the Array of Deposit Instruments from the given set of parameters
*
* USE WITH CARE: This sample ignores errors and does not handle exceptions.
*/
private static final SingleStreamComponent[] DepositInstrumentsFromMaturityDays (
final JulianDate dtEffective,
final String strCurrency,
final int[] aiDay)
throws Exception
{
SingleStreamComponent[] aDeposit = new SingleStreamComponent[aiDay.length];
for (int i = 0; i < aiDay.length; ++i)
aDeposit[i] = SingleStreamComponentBuilder.Deposit (
dtEffective,
dtEffective.addBusDays (
aiDay[i],
strCurrency
),
OvernightLabel.Create (
strCurrency
)
);
return aDeposit;
}
/*
* Construct the Array of Swap Instruments from the given set of parameters
*
* USE WITH CARE: This sample ignores errors and does not handle exceptions.
*/
private static final FixFloatComponent[] OISFromMaturityTenor (
final JulianDate dtSpot,
final String strCurrency,
final String[] astrMaturityTenor,
final double[] adblCoupon)
throws Exception
{
FixFloatComponent[] aOIS = new FixFloatComponent[astrMaturityTenor.length];
for (int i = 0; i < astrMaturityTenor.length; ++i)
aOIS[i] = OTCOISFixFloat (
dtSpot,
strCurrency,
astrMaturityTenor[i],
adblCoupon[i]
);
return aOIS;
}
private static final MergedDiscountForwardCurve CustomOISCurveBuilderSample (
final JulianDate dtSpot,
final String strCurrency)
throws Exception
{
/*
* Construct the Array of Deposit Instruments and their Quotes from the given set of parameters
*/
SingleStreamComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
dtSpot,
strCurrency,
new int[] {
1, 2, 3, 7, 14, 21, 30, 60
}
);
double[] adblDepositQuote = new double[] {
0.01200, 0.01200, 0.01200, 0.01450, 0.01550, 0.01600, 0.01660, 0.01850, // Cash
};
/*
* Construct the Deposit Instrument Set Stretch Builder
*/
LatentStateStretchSpec depositStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
"DEPOSIT",
aDepositComp,
"ForwardRate",
adblDepositQuote
);
/*
* Construct the Array of EDF Instruments and their Quotes from the given set of parameters
*/
SingleStreamComponent[] aEDFComp = SingleStreamComponentBuilder.ForwardRateFuturesPack (
dtSpot,
4,
strCurrency
);
double[] adblEDFQuote = new double[] {
0.01612, 0.01580, 0.01589, 0.01598
};
/*
* Construct the Cash Instrument Set Stretch Builder
*/
LatentStateStretchSpec edfStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
"EDF",
aEDFComp,
"ForwardRate",
adblEDFQuote
);
/*
* Construct the Array of OIS Instruments and their Quotes from the given set of parameters
*/
double[] adblOISQuote = new double[] {
0.02604, // 4Y
0.02808, // 5Y
0.02983, // 6Y
0.03136, // 7Y
0.03268, // 8Y
0.03383, // 9Y
0.03488 // 10Y
};
CalibratableComponent[] aOISComp = OISFromMaturityTenor (
dtSpot,
strCurrency,
new java.lang.String[] {
"4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y"
},
adblOISQuote
);
/*
* Construct the OIS Instrument Set Stretch Builder
*/
LatentStateStretchSpec oisStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
"SWAP",
aOISComp,
"SwapRate",
adblOISQuote
);
LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {
depositStretch,
edfStretch,
oisStretch
};
/*
* Set up the Linear Curve Calibrator using the following parameters:
* - Cubic Exponential Mixture Basis Spline Set
* - Ck = 2, Segment Curvature Penalty = 2
* - Quadratic Rational Shape Controller
* - Natural Boundary Setting
*/
LinearLatentStateCalibrator lcc = new LinearLatentStateCalibrator (
new SegmentCustomBuilderControl (
MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
new PolynomialFunctionSetParams (4),
SegmentInelasticDesignControl.Create (
2,
2
),
new ResponseScalingShapeControl (
true,
new QuadraticRationalShapeControl (0.)
),
null
),
BoundarySettings.NaturalStandard(),
MultiSegmentSequence.CALIBRATE,
null,
null
);
/*
* Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the array
* of Cash and Swap Stretches.
*/
return ScenarioDiscountCurveBuilder.ShapePreservingDFBuild (
strCurrency,
lcc,
aStretchSpec,
new ValuationParams (
dtSpot,
dtSpot,
strCurrency
),
null,
null,
null,
1.
);
}
private static final LatentStateFixingsContainer SetFlatOvernightFixings (
final JulianDate dtStart,
final JulianDate dtEnd,
final JulianDate dtValue,
final ForwardLabel fri,
final double dblFlatFixing,
final double dblNotional)
throws Exception
{
LatentStateFixingsContainer lsfc = new LatentStateFixingsContainer();
double dblAccount = 1.;
int iPrevDate = dtStart.julian();
JulianDate dt = dtStart.addDays (1);
while (dt.julian() <= dtEnd.julian()) {
lsfc.add (
dt,
fri,
dblFlatFixing
);
if (dt.julian() <= dtValue.julian()) {
double dblAccrualFraction = Convention.YearFraction (
iPrevDate,
dt.julian(),
"Act/360",
false,
null,
"USD"
);
dblAccount *= (1. + dblFlatFixing * dblAccrualFraction);
}
iPrevDate = dt.julian();
dt = dt.addBusDays (
1,
"USD"
);
}
System.out.println ("\tManual Calc Float Accrued (Geometric Compounding): " + (dblAccount - 1.) * dblNotional);
System.out.println ("\tManual Calc Float Accrued (Arithmetic Compounding): " +
((dtValue.julian() - dtStart.julian()) * dblNotional * dblFlatFixing / 360.));
return lsfc;
}
public static final Map<String, Double> CompoundingRun (
final ForwardLabel fri)
throws Exception
{
double dblOISVol = 0.3;
double dblUSDFundingVol = 0.3;
double dblUSDFundingUSDOISCorrelation = 0.3;
String strCurrency = fri.currency();
JulianDate dtToday = org.drip.analytics.date.DateUtil.Today().addTenorAndAdjust (
"0D",
strCurrency
);
MergedDiscountForwardCurve dc = CustomOISCurveBuilderSample (
dtToday,
strCurrency
);
JulianDate dtCustomOISStart = dtToday.subtractTenor ("2M");
JulianDate dtCustomOISMaturity = dtToday.addTenor ("4M");
CompositePeriodSetting cpsFloating = new CompositePeriodSetting (
360,
"ON",
strCurrency,
null,
-1.,
null,
null,
null,
null
);
ComposableFloatingUnitSetting cfusFloating = new ComposableFloatingUnitSetting (
"ON",
CompositePeriodBuilder.EDGE_DATE_SEQUENCE_OVERNIGHT,
null,
fri,
CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
0.
);
List<Integer> lsFloatingStreamEdgeDate = CompositePeriodBuilder.OvernightEdgeDates (
dtCustomOISStart,
dtCustomOISStart.addTenorAndAdjust (
"6M",
strCurrency
),
strCurrency
);
Stream floatStream = new Stream (
CompositePeriodBuilder.FloatingCompositeUnit (
lsFloatingStreamEdgeDate,
cpsFloating,
cfusFloating
)
);
CurveSurfaceQuoteContainer mktParams = MarketParamsBuilder.Create (
dc,
null,
null,
null,
null,
null,
SetFlatOvernightFixings (
dtCustomOISStart,
dtCustomOISMaturity,
dtToday,
fri,
0.003,
-1.
)
);
ValuationParams valParams = new ValuationParams (
dtToday,
dtToday,
strCurrency
);
FundingLabel fundingLabelUSD = FundingLabel.Standard ("USD");
mktParams.setForwardVolatility (
ScenarioDeterministicVolatilityBuilder.FlatForward (
dtToday.julian(),
VolatilityLabel.Standard (fri),
fri.currency(),
dblOISVol
)
);
mktParams.setFundingVolatility (
ScenarioDeterministicVolatilityBuilder.FlatForward (
dtToday.julian(),
VolatilityLabel.Standard (fundingLabelUSD),
"USD",
dblUSDFundingVol
)
);
mktParams.setForwardFundingCorrelation (
fri,
fundingLabelUSD,
new FlatUnivariate (dblUSDFundingUSDOISCorrelation)
);
return floatStream.value (
valParams,
null,
mktParams,
null
);
}
public static final void main (
final String[] astrArgs)
throws Exception
{
/*
* Initialize the Credit Analytics Library
*/
EnvManager.InitEnv ("");
String strCurrency = "USD";
Map<String, Double> mapArithmeticOutput = CompoundingRun (
OvernightLabel.Create (
strCurrency
)
);
Map<String, Double> mapGeometricOutput = CompoundingRun (
ForwardLabel.Create (
new OvernightIndex (
strCurrency + "OIS",
"OIS",
strCurrency,
"Act/360",
strCurrency,
"ON",
0,
CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
),
"ON"
)
);
System.out.println ("\n\t-----------------------------------");
System.out.println ("\t GEOMETRIC | ARITHMETIC | CHECK");
System.out.println ("\t-----------------------------------\n");
for (Map.Entry<String, Double> meGeometric : mapGeometricOutput.entrySet()) {
String strKey = meGeometric.getKey();
double dblGeometricMeasure = meGeometric.getValue();
double dblArithmeticMeasure = mapArithmeticOutput.get (strKey);
String strMatch = NumberUtil.WithinTolerance (
dblGeometricMeasure,
dblArithmeticMeasure,
1.e-08,
1.e-04
) ?
"MATCH " :
"DIFFER";
System.out.println ("\t" +
FormatUtil.FormatDouble (dblGeometricMeasure, 1, 8, 1.) + " | " +
FormatUtil.FormatDouble (dblArithmeticMeasure, 1, 8, 1.) + " | " +
strMatch + " <= " + strKey
);
}
}
}