Ejemplo n.º 1
0
        /// <summary>
        /// This method allows to fit the implied volatility using different models.
        /// </summary>
        /// <param name="Hdataset"></param>
        /// <returns></returns>
        IFunction FitImplVolModel(CallPriceMarketData Hdataset)
        {
            int model = 1;

            switch (model)
            {
            case 0:
                PFunction2D.PFunction2D pf = new PFunction2D.PFunction2D(Hdataset.Maturity, Hdataset.Strike, Hdataset.Volatility);
                pf.Interpolation = DVPLUtils.EInterpolationType.LEAST_SQUARES;
                pf.Extrapolation = DVPLUtils.ExtrapolationType.USEMODEL;
                pf.Parse(null);
                return(pf);

            case 1:

                //define a model for fitting the implied volatility
                Fairmat.Statistics.LinearModel impVol = new Fairmat.Statistics.LinearModel(
                    new Fairmat.Statistics.Predictor[] {
                    delegate(Vector xx) { return(1); },
                    delegate(Vector xx) { return(xx[0]); },
                    delegate(Vector xx) { return(xx[1]); },
                    delegate(Vector xx) { return(System.Math.Pow(xx[0], 2)); },
                    delegate(Vector xx) { return(System.Math.Pow(xx[1], 2)); },
                    delegate(Vector xx) { return(xx[0] * xx[1]); },
                });

                // Unroll matrix and coordinate vectors in order to make it suitable
                // for the Quadratic model implementation.

                int    n     = Hdataset.Volatility.R * Hdataset.Volatility.C;
                Matrix xy    = new Matrix(n, 2);
                Vector z     = new Vector(n);
                int    count = 0;
                for (int x = 0; x < Hdataset.Volatility.R; x++)
                {
                    for (int y = 0; y < Hdataset.Volatility.C; y++)
                    {
                        if (Hdataset.Volatility[x, y] > 0.01)
                        {
                            xy[count, Range.All] = (new Vector()
                            {
                                Hdataset.Maturity[x], Hdataset.Strike[y]
                            }).T;
                            z[count]             = Hdataset.Volatility[x, y];
                            count++;
                        }
                    }
                }
                xy.Resize(count, xy.C);
                z.Resize(count);
                impVol.Estimate(xy, z);
                return(impVol);
            }

            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method allows to fit the implied volatility using different models.
        /// </summary>
        /// <param name="Hdataset"></param>
        /// <returns></returns>
        IFunction FitImplVolModel(CallPriceMarketData Hdataset)
        {
            int model = 1;
            switch (model)
            {
                case 0:
                    PFunction2D.PFunction2D pf = new PFunction2D.PFunction2D(Hdataset.Maturity, Hdataset.Strike, Hdataset.Volatility);
                    pf.Interpolation = DVPLUtils.EInterpolationType.LEAST_SQUARES;
                    pf.Extrapolation = DVPLUtils.ExtrapolationType.USEMODEL;
                    pf.Parse(null);
                    return pf;
                case 1:

                    //define a model for fitting the implied volatility
                    Fairmat.Statistics.LinearModel impVol = new Fairmat.Statistics.LinearModel(
                                                            new Fairmat.Statistics.Predictor[] {
                                                            delegate(Vector xx) { return 1; },
                                                            delegate(Vector xx) { return xx[0]; },
                                                            delegate(Vector xx) { return xx[1]; },
                                                            delegate(Vector xx) { return System.Math.Pow(xx[0], 2); },
                                                            delegate(Vector xx) { return System.Math.Pow(xx[1], 2); },
                                                            delegate(Vector xx) { return xx[0] * xx[1]; }, });

                    // Unroll matrix and coordinate vectors in order to make it suitable
                    // for the Quadratic model implementation.

                    int n = Hdataset.Volatility.R * Hdataset.Volatility.C;
                    Matrix xy = new Matrix(n, 2);
                    Vector z = new Vector(n);
                    int count = 0;
                    for (int x = 0; x < Hdataset.Volatility.R; x++)
                    {
                        for (int y = 0; y < Hdataset.Volatility.C; y++)
                        {
                            if (Hdataset.Volatility[x, y] > 0.01)
                            {
                                xy[count, Range.All] = (new Vector() { Hdataset.Maturity[x],Hdataset.Strike[y] }).T;
                                z[count] = Hdataset.Volatility[x, y];
                                count++;
                            }
                        }
                    }
                    xy.Resize(count, xy.C);
                    z.Resize(count);
                    impVol.Estimate(xy, z);
                    return impVol;
            }

            return null;
        }