예제 #1
0
        /// <summary>
        /// get the k^th order penalty matrix, P. This is defined as P = (D^T)*D , where D is the k^th order difference
        /// matrix (see getDifferenceMatrix), so the scalar amount (x^T)*P*x = |Dx|^2  is greater the more k^th order
        /// differences there are in the vector, x. This can then act as a penalty on x in some optimisation routine where
        /// x is the vector of (fit) parameters. </summary>
        /// <param name="m"> Length of the  vector. </param>
        /// <param name="k"> Difference order. Require m > k </param>
        /// <returns> The k^th order penalty matrix, P </returns>
        public static DoubleMatrix getPenaltyMatrix(int m, int k)
        {
            ArgChecker.notNegativeOrZero(m, "m");
            ArgChecker.notNegative(k, "k");
            ArgChecker.isTrue(k < m, "Difference order too high, require m > k, but have: m = {} and k = {}", m, k);
            if (k == 0)
            {
                return(DoubleMatrix.identity(m));
            }
            DoubleMatrix d  = getDifferenceMatrix(m, k);
            DoubleMatrix dt = MA.getTranspose(d);

            return((DoubleMatrix)MA.multiply(dt, d));
        }
예제 #2
0
        private DoubleMatrix getDiffMatrix(int m, int k)
        {
            ArgChecker.isTrue(k < m, "difference order too high");

//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] data = new double[m][m];
            double[][] data = RectangularArrays.ReturnRectangularDoubleArray(m, m);
            if (m == 0)
            {
                return(DoubleMatrix.copyOf(data));
            }

            int[] coeff = new int[k + 1];

            int sign = 1;

            for (int i = k; i >= 0; i--)
            {
                coeff[i] = (int)(sign * binomialCoefficient(k, i));
                sign    *= -1;
            }

            for (int i = k; i < m; i++)
            {
                for (int j = 0; j < k + 1; j++)
                {
                    data[i][j + i - k] = coeff[j];
                }
            }
            DoubleMatrix d = DoubleMatrix.copyOf(data);

            DoubleMatrix dt = _algebra.getTranspose(d);

            return((DoubleMatrix)_algebra.multiply(dt, d));
        }
        internal override DoubleArray computedBucketedCs01(ResolvedCdsTrade trade, IList <ResolvedCdsTrade> bucketCds, CreditRatesProvider ratesProvider, ReferenceData refData)
        {
            checkCdsBucket(trade, bucketCds);
            ResolvedCds product       = trade.Product;
            Currency    currency      = product.Currency;
            StandardId  legalEntityId = product.LegalEntityId;
            LocalDate   valuationDate = ratesProvider.ValuationDate;

            int         nBucket          = bucketCds.Count;
            DoubleArray impSp            = impliedSpread(bucketCds, ratesProvider, refData);
            NodalCurve  creditCurveBase  = Calibrator.calibrate(bucketCds, impSp, DoubleArray.filled(nBucket), CurveName.of("baseImpliedCreditCurve"), valuationDate, ratesProvider.discountFactors(currency), ratesProvider.recoveryRates(legalEntityId), refData);
            IsdaCreditDiscountFactors df = IsdaCreditDiscountFactors.of(currency, valuationDate, creditCurveBase);
            CreditRatesProvider       ratesProviderBase = ratesProvider.toImmutableCreditRatesProvider().toBuilder().creditCurves(ImmutableMap.of(Pair.of(legalEntityId, currency), LegalEntitySurvivalProbabilities.of(legalEntityId, df))).build();

            double[][]         res     = new double[nBucket][];
            PointSensitivities pointPv = Pricer.presentValueOnSettleSensitivity(trade, ratesProviderBase, refData);
            DoubleArray        vLambda = ratesProviderBase.singleCreditCurveParameterSensitivity(pointPv, legalEntityId, currency).Sensitivity;

            for (int i = 0; i < nBucket; i++)
            {
                PointSensitivities pointSp = Pricer.parSpreadSensitivity(bucketCds[i], ratesProviderBase, refData);
                res[i] = ratesProviderBase.singleCreditCurveParameterSensitivity(pointSp, legalEntityId, currency).Sensitivity.toArray();
            }
            DoubleMatrix          jacT  = MATRIX_ALGEBRA.getTranspose(DoubleMatrix.ofUnsafe(res));
            LUDecompositionResult luRes = DECOMPOSITION.apply(jacT);
            DoubleArray           vS    = luRes.solve(vLambda);

            return(vS);
        }
예제 #4
0
 /// <summary>
 /// Constructor. </summary>
 /// <param name="lArray"> The matrix L as an array of doubles. </param>
 public CholeskyDecompositionOpenGammaResult(double[][] lArray)
 {
     _lArray      = lArray;
     _l           = DoubleMatrix.copyOf(_lArray);
     _lT          = ALGEBRA.getTranspose(_l);
     _determinant = 1.0;
     for (int loopdiag = 0; loopdiag < _lArray.Length; ++loopdiag)
     {
         _determinant *= _lArray[loopdiag][loopdiag] * _lArray[loopdiag][loopdiag];
     }
 }