/// <summary>
        /// Calibrates a list of curve groups, each containing one or more curves.
        /// <para>
        /// The calibration is defined using a list of <seealso cref="RatesCurveGroupDefinition"/>.
        /// Observable market data and existing known data are also needed to complete the calibration.
        /// </para>
        /// <para>
        /// A curve must only exist in one group.
        ///
        /// </para>
        /// </summary>
        /// <param name="allGroupsDefn">  the curve group definitions </param>
        /// <param name="knownData">  the starting data for the calibration </param>
        /// <param name="marketData">  the market data required to build a trade for the instrument </param>
        /// <param name="refData">  the reference data, used to resolve the trades </param>
        /// <returns> the rates provider resulting from the calibration </returns>
        public ImmutableRatesProvider calibrate(IList <RatesCurveGroupDefinition> allGroupsDefn, ImmutableRatesProvider knownData, MarketData marketData, ReferenceData refData)
        {
            // this method effectively takes one CurveGroupDefinition
            // the list is a split of the definition, not multiple independent definitions

            if (!knownData.ValuationDate.Equals(marketData.ValuationDate))
            {
                throw new System.ArgumentException(Messages.format("Valuation dates do not match: {} and {}", knownData.ValuationDate, marketData.ValuationDate));
            }
            // perform calibration one group at a time, building up the result by mutating these variables
            ImmutableRatesProvider             providerCombined           = knownData;
            ImmutableList <CurveParameterSize> orderPrev                  = ImmutableList.of();
            ImmutableMap <CurveName, JacobianCalibrationMatrix> jacobians = ImmutableMap.of();

            foreach (RatesCurveGroupDefinition groupDefn in allGroupsDefn)
            {
                if (groupDefn.Entries.Empty)
                {
                    continue;
                }
                RatesCurveGroupDefinition groupDefnBound = groupDefn.bindTimeSeries(knownData.ValuationDate, knownData.TimeSeries);
                // combine all data in the group into flat lists
                ImmutableList <ResolvedTrade>      trades            = groupDefnBound.resolvedTrades(marketData, refData);
                ImmutableList <double>             initialGuesses    = groupDefnBound.initialGuesses(marketData);
                ImmutableList <CurveParameterSize> orderGroup        = toOrder(groupDefnBound);
                ImmutableList <CurveParameterSize> orderPrevAndGroup = ImmutableList.builder <CurveParameterSize>().addAll(orderPrev).addAll(orderGroup).build();

                // calibrate
                RatesProviderGenerator providerGenerator     = ImmutableRatesProviderGenerator.of(providerCombined, groupDefnBound, refData);
                DoubleArray            calibratedGroupParams = calibrateGroup(providerGenerator, trades, initialGuesses, orderGroup);
                ImmutableRatesProvider calibratedProvider    = providerGenerator.generate(calibratedGroupParams);

                // use calibration to build Jacobian matrices
                if (groupDefnBound.ComputeJacobian)
                {
                    jacobians = updateJacobiansForGroup(calibratedProvider, trades, orderGroup, orderPrev, orderPrevAndGroup, jacobians);
                }
                ImmutableMap <CurveName, DoubleArray> sensitivityToMarketQuote = ImmutableMap.of();
                if (groupDefnBound.ComputePvSensitivityToMarketQuote)
                {
                    ImmutableRatesProvider providerWithJacobian = providerGenerator.generate(calibratedGroupParams, jacobians);
                    sensitivityToMarketQuote = sensitivityToMarketQuoteForGroup(providerWithJacobian, trades, orderGroup);
                }
                orderPrev = orderPrevAndGroup;

                // use Jacobians to build output curves
                providerCombined = providerGenerator.generate(calibratedGroupParams, jacobians, sensitivityToMarketQuote);
            }
            // return the calibrated provider
            return(providerCombined);
        }
Beispiel #2
0
        //-------------------------------------------------------------------------
        public override DoubleArray apply(DoubleArray x)
        {
            // create child provider from matrix
            ImmutableRatesProvider childProvider = providerGenerator.generate(x);

            // calculate value for each trade using the child provider
            return(DoubleArray.of(trades.Count, i => measures.value(trades[i], childProvider)));
        }
        //-------------------------------------------------------------------------
        public override DoubleMatrix apply(DoubleArray x)
        {
            // create child provider from matrix
            ImmutableRatesProvider provider = providerGenerator.generate(x);
            // calculate derivative for each trade using the child provider
            int size = trades.Count;

            return(DoubleMatrix.ofArrayObjects(size, size, i => measures.derivative(trades[i], provider, curveOrder)));
        }