// Calculate the total rate sensitivity internal PointSensitivityBuilder calculateRateSensitivity() { double factor = pastCompositionFactor() * valuationCompositionFactor() / accrualFactorTotal; ObjDoublePair <PointSensitivityBuilder> compositionFactorAndSensitivityNonCutoff = this.compositionFactorAndSensitivityNonCutoff(); ObjDoublePair <PointSensitivityBuilder> compositionFactorAndSensitivityCutoff = this.compositionFactorAndSensitivityCutoff(); PointSensitivityBuilder combinedPointSensitivity = compositionFactorAndSensitivityNonCutoff.First.multipliedBy(compositionFactorAndSensitivityCutoff.Second * factor); combinedPointSensitivity = combinedPointSensitivity.combinedWith(compositionFactorAndSensitivityCutoff.First.multipliedBy(compositionFactorAndSensitivityNonCutoff.Second * factor)); return(combinedPointSensitivity); }
// Composition - forward part in non-cutoff period; past/valuation date case dealt with in previous methods internal ObjDoublePair <PointSensitivityBuilder> compositionFactorAndSensitivityNonCutoff() { if (!nextFixing.isAfter(lastFixingNonCutoff)) { OvernightIndexObservation obs = computation.observeOn(nextFixing); LocalDate startDate = obs.EffectiveDate; LocalDate endDate = computation.calculateMaturityFromFixing(lastFixingNonCutoff); double accrualFactor = dayCount.yearFraction(startDate, endDate); double rate = rates.periodRate(obs, endDate); PointSensitivityBuilder rateSensitivity = rates.periodRatePointSensitivity(obs, endDate); rateSensitivity = rateSensitivity.multipliedBy(accrualFactor); return(ObjDoublePair.of(rateSensitivity, 1.0d + accrualFactor * rate)); } return(ObjDoublePair.of(PointSensitivityBuilder.none(), 1.0d)); }
// constructor that sorts (artificial boolean flag) private InterpolatedNodalSurface(SurfaceMetadata metadata, DoubleArray xValues, DoubleArray yValues, DoubleArray zValues, SurfaceInterpolator interpolator, bool sort) { validateInputs(metadata, xValues, yValues, zValues, interpolator); // sort inputs IDictionary <DoublesPair, ObjDoublePair <ParameterMetadata> > sorted = new SortedDictionary <DoublesPair, ObjDoublePair <ParameterMetadata> >(); for (int i = 0; i < xValues.size(); i++) { ParameterMetadata pm = metadata.getParameterMetadata(i); sorted[DoublesPair.of(xValues.get(i), yValues.get(i))] = ObjDoublePair.of(pm, zValues.get(i)); } double[] sortedX = new double[sorted.Count]; double[] sortedY = new double[sorted.Count]; double[] sortedZ = new double[sorted.Count]; ParameterMetadata[] sortedPm = new ParameterMetadata[sorted.Count]; int pos = 0; foreach (KeyValuePair <DoublesPair, ObjDoublePair <ParameterMetadata> > entry in sorted.SetOfKeyValuePairs()) { sortedX[pos] = entry.Key.First; sortedY[pos] = entry.Key.Second; sortedZ[pos] = entry.Value.Second; sortedPm[pos] = entry.Value.First; pos++; } // assign SurfaceMetadata sortedMetadata = metadata.withParameterMetadata(Arrays.asList(sortedPm)); this.metadata = sortedMetadata; this.xValues = DoubleArray.ofUnsafe(sortedX); this.yValues = DoubleArray.ofUnsafe(sortedY); this.zValues = DoubleArray.ofUnsafe(sortedZ); IDictionary <DoublesPair, double> pairs = new Dictionary <DoublesPair, double>(); for (int i = 0; i < xValues.size(); i++) { pairs[DoublesPair.of(xValues.get(i), yValues.get(i))] = zValues.get(i); } this.interpolator = interpolator; this.boundInterpolator = interpolator.bind(this.xValues, this.yValues, this.zValues); this.parameterMetadata = IntStream.range(0, ParameterCount).mapToObj(i => sortedMetadata.getParameterMetadata(i)).collect(toImmutableList()); }
// Composition - forward part in the cutoff period; past/valuation date case dealt with in previous methods internal ObjDoublePair <PointSensitivityBuilder> compositionFactorAndSensitivityCutoff() { OvernightIndexObservation obs = computation.observeOn(lastFixingNonCutoff); if (!nextFixing.isAfter(lastFixingNonCutoff)) { double rate = rates.rate(obs); double compositionFactor = 1.0d; double compositionFactorDerivative = 0.0; for (int i = 0; i < cutoffOffset - 1; i++) { compositionFactor *= 1.0d + accrualFactorCutoff[i] * rate; compositionFactorDerivative += accrualFactorCutoff[i] / (1.0d + accrualFactorCutoff[i] * rate); } compositionFactorDerivative *= compositionFactor; PointSensitivityBuilder rateSensitivity = cutoffOffset <= 1 ? PointSensitivityBuilder.none() : rates.ratePointSensitivity(obs); rateSensitivity = rateSensitivity.multipliedBy(compositionFactorDerivative); return(ObjDoublePair.of(rateSensitivity, compositionFactor)); } return(ObjDoublePair.of(PointSensitivityBuilder.none(), 1.0d)); }