Beispiel #1
0
    protected override void PostProcessData()
    {
        // adapt DF from DateDf to PostProcessInterpo according to <DoInterpOn>
        IEnumerable <double> adaptedDF = from c in DateDf
                                         select interpAdapter.FromDfToInterp(c.Value, c.Key);

        // Inizialize PostProcesInterpo
        PostProcessInterpo.Ini(DateDf.Keys, adaptedDF.ToArray());
    }
Beispiel #2
0
    private delegate double SwapRate(SwapStyle S);  // used in function to calculate swap rate

    // used in Solve() function on which best fit works
    private void function_fvec(double[] x, double[] fi, object obj)
    {
        // fi is output should be vector of zeros (i.e. swap calculated - swap value from input)
        int N = x.Count(); // size of x (set of guess)

        // I update PreProcessedDate with new set of guess(x[]), for all Key (dates) in IniGuessData,
        for (int i = 0; i < N; i++)
        {
            PreProcessedData[IniGuessData.ElementAt(i).Key] = x[i];
        }

        // set up interpolator with updated data
        PostProcessInterpo.Ini(PreProcessedData.Keys.ToArray(), PreProcessedData.Values.ToArray());

        // Lambda expression: calculate Par Rate given a SwapStyle building block
        SwapRate SwapCalc = BB =>
        {
            // fixed leg data
            double[] yfFixLeg = BB.scheduleLeg1.GetYFVect(BB.swapLeg1.DayCount); // fixed is leg 1

            // dfs array of fixed lag
            double[] dfDates = Date.GetSerialValue(BB.scheduleLeg1.payDates); // serial date of fixed lag (each dates we should find df)
            // Vector<double> k = PostProcessInterpo.Curve(new Vector<double>(dfDates, 0));

            double[] dfFixLeg = PostProcessInterpo.Curve(dfDates); // get interpolated value (i.e. log df or log r or...
            // transform interpolated value back to discount factor
            for (int i = 0; i < yfFixLeg.Length; i++)
            {
                dfFixLeg[i] = interpAdapter.FromInterpToDf(dfFixLeg[i], dfDates[i]);
            }

            // Interpolation Methods for Curve Construction PATRICK S. HAGAN & GRAEME WEST Applied Mathematical Finance,Vol. 13, No. 2, 89–129, June 2006
            // Formula 2) page 4
            return(Formula.ParRate(yfFixLeg, dfFixLeg)); // Calculate par rate
        };

        // iterate building block and calculate difference between starting data and recalculated data: best fit if each fi[i]==0
        for (int i = 0; i < N; i++)
        {
            // SwapCalc((SwapStyle)SwapStyleArray[i]) is recalculated data
            // SwapStyleArray[i].rateValue is starting data to match
            fi[i] = (SwapCalc((SwapStyle)OnlyGivenSwap[i]) - OnlyGivenSwap[i].rateValue) * 10000; // best fit if fi[i] ==0!, for each i
        }
    }