Esempio n. 1
0
        /// <summary>
        /// Gets the Wing volatility
        /// </summary>
        /// <param name="volSurface">The vol surface.</param>
        /// <param name="time">The time.</param>
        /// <param name="strike">The strike.</param>
        /// <param name="wingParameterList"></param>
        /// <returns></returns>
        public static double GetWingValue(List <OrcWingParameters> wingParameterList, IInterpolation xInterp, double time, double strike)
        {
            double eps = 0.0001;
            IPoint pt1 = new Point2D(time, strike * (1 + eps));
            IPoint pt2 = new Point2D(time, strike * (1 - eps));

            double[] _years = new double[wingParameterList.Count];

            for (int idx = 0; idx < wingParameterList.Count; idx++)
            {
                _years[idx] = wingParameterList[idx].TimeToMaturity;
            }
            var volArray = new double[wingParameterList.Count];

            for (int idx = 0; idx < wingParameterList.Count; idx++)
            {
                volArray[idx] = OrcWingVol.Value(strike, wingParameterList[idx]);
            }
            double res = 0;

            if (volArray.Length > 1)
            {
                xInterp.Initialize(_years, volArray);
                res = xInterp.ValueAt(time, false);
            }
            else
            {
                res = volArray[0];
            }
            return(res);
        }
        public void TestAllInterpolations()
        {
            Random r = new Random(Environment.TickCount);

            foreach (string interp in _linearInterpolations)
            {
                TearDown();
                IInterpolation interpolation = InterpolationFactory.Create(interp);
                interpolation.Initialize(_times, _rates);
                Debug.WriteLine($"interpolationType : {interp}");
                for (int i = 1; i < 100; ++i)
                {
                    double time       = (i + r.Next(-10000, 10000) / 10000) / 10.0;
                    double interpRate = interpolation.ValueAt(time, true);
                    Debug.WriteLine($"interpolatedRate : {interpRate} Time: {time}");
                }
                int index = 0;
                foreach (double time in _times)
                {
                    if (time != 0.0)
                    {
                        double interpRate  = interpolation.ValueAt(time, true);
                        double interpValue = _rates[index];
                        Assert.AreEqual(interpRate, interpValue, 10 - 8);
                    }
                    index++;
                }
            }
            foreach (string interp in _logLinearInterpolations)
            {
                TearDown();
                double[] exp  = SetUp();
                double[] temp = new double[exp.Length];
                exp.CopyTo(temp, 0);
                IInterpolation interpolation = InterpolationFactory.Create(interp);
                interpolation.Initialize(_times, temp);
                Debug.WriteLine($"interpolationType : {interp}");
                for (int i = 1; i < 100; ++i)
                {
                    double time       = (i + r.Next(-10000, 10000) / 10000) / 10.0;
                    double interpRate = interpolation.ValueAt(time, true);
                    Debug.WriteLine($"interpolatedRate : {interpRate} Time: {time}");
                }
                int index = 0;
                foreach (double time in _times)
                {
                    if (time != 0.0)
                    {
                        double interpRate  = interpolation.ValueAt(time, true);
                        double interpValue = exp[index];
                        Assert.AreEqual(interpRate, interpValue, 10 - 8);
                    }
                    index++;
                }
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Initialize a class constructed by the default constructor.
 /// </summary>
 /// <remarks>
 /// The sequence of values for x must have been sorted.
 /// </remarks>
 /// <exception cref="ArgumentException">
 /// Thrown when the passed lists do not have the same size
 /// or do not provide enough points to interpolate.
 /// </exception>
 public void Initialize(double[] x, double[] y)
 {
     _curve = new LinearInterpolation();
     _curve.Initialize(x, y);
 }
        /// <summary>
        /// For any point, there should exist a function value. The point can be multi-dimensional.
        /// </summary>
        /// <param name="point"><c>IPoint</c> A point must have at least one dimension.
        /// <seealso cref="IPoint"/> The interface for a multi-dimensional point.</param>
        /// <returns>The <c>double</c> function value at the point</returns>
        public override double Value(IPoint point)
        {
            var curve      = (DiscreteSpace)GetDiscreteSpace();
            var basePoints = curve.GetClosestValues(point);

            if (basePoints.Count != 4)
            {
                throw new System.Exception("Incorrect number of base points.");
            }
            var x0     = basePoints[0].Coords;
            var y0     = basePoints[1].Coords; //yo
            var x1     = basePoints[2].Coords; //x1
            var y1     = basePoints[3].Coords; //y1
            var xArray = new double[2];

            xArray[0] = (double)x0[0];
            xArray[1] = (double)x1[0];
            var fxArray = new double[2];

            fxArray[0] = (double)x0[2];
            fxArray[1] = (double)x1[2];
            var yArray = new double[2];

            yArray[0] = (double)y0[0];
            yArray[1] = (double)y1[0];
            var fyArray = new double[2];

            fyArray[0] = (double)y0[2];
            fyArray[1] = (double)y1[2];
            var xyArray = new double[2];

            xyArray[0] = (double)x0[1];
            xyArray[1] = (double)y0[1];
            //_interpolation.Initialize(xArray, fxArray);
            //var xy = (double) point.Coords[0];
            //var xyValue = _interpolation.ValueAt(xy, true);
            //_interpolation.Initialize(yArray, fyArray);
            //var yy = (double)point.Coords[0];
            //var yyValue = _interpolation.ValueAt(yy, true);
            //_yInterpolation.Initialize(xyArray, new[] { xyValue, yyValue });
            //var result = _yInterpolation.ValueAt((double)point.Coords[1], true);
            Double xyValue;
            Double yyValue;

            if (xArray[0].Equals(xArray[1]))
            {
                xyValue = fxArray[0];
            }
            else
            {
                Interpolation.Initialize(xArray, fxArray);
                var xy = (double)point.Coords[0];
                xyValue = Interpolation.ValueAt(xy, true);
            }

            if (yArray[0].Equals(yArray[1]))
            {
                yyValue = fyArray[0];
            }
            else
            {
                Interpolation.Initialize(yArray, fyArray);
                var yy = (double)point.Coords[0];
                yyValue = Interpolation.ValueAt(yy, true);
            }
            var    yxArray = new[] { xyValue, yyValue };
            double result;

            if (xyArray[0] == xyArray[1])
            {
                result = fxArray[0];
            }
            else
            {
                YInterpolation.Initialize(xyArray, yxArray);
                result = YInterpolation.ValueAt((double)point.Coords[1], true);
            }
            return(result);//TODO will not work with splines.
        }