예제 #1
0
        public void TestDiscreteCurve1ClosestValues()
        {
            var curve   = new DiscreteCurve(_pointCoords, _pointValues);
            var point   = new Point1D(0.3);
            var closest = curve.GetClosestValues(point);

            Assert.AreEqual(closest[0].Coords[0], 0.22);
            Assert.AreEqual(closest[0].FunctionValue, 0.066000000000000003d);
            Assert.AreEqual(closest[1].Coords[0], 0.5);
            Assert.AreEqual(closest[1].FunctionValue, 0.067000000000000004d);
        }
예제 #2
0
        public void TestInterpolatedCubicCurve()
        {
            IInterpolation     interp      = new CubicSplineInterpolation();
            DiscreteCurve      curve       = new DiscreteCurve(_pointCoords, _pointValues);
            IInterpolatedSpace interpCurve = new InterpolatedCurve(curve, interp, true);

            foreach (double point in _testPointArray)
            {
                IPoint p = new Point1D(point);
                Console.WriteLine(interpCurve.Value(p));
            }
        }
예제 #3
0
        public void TestDiscreteCurve1()
        {
            IDiscreteSpace curve = new DiscreteCurve(_pointCoords, _pointValues);

            for (int i = 0; i < curve.GetPointList().Count; i++)
            {
                Debug.Print("X Coord {0} Y Coord {1}", curve.GetPointList()[i].Coords[0], curve.GetPointList()[i].Coords[1]);
                Assert.AreEqual(curve.GetPointList()[i].FunctionValue, _pointValues[i]);
                Assert.AreEqual(curve.GetPointList()[i].Coords[1], _pointValues[i]);
                Assert.AreEqual(curve.GetPointList()[i].Coords[0], _pointCoords[i]);
            }
        }
예제 #4
0
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="pt">The pt.</param>
        /// <param name="divCurve">The div curve.</param>
        /// <returns></returns>
        public static double Value(IPoint pt, DiscreteCurve divCurve)
        {
            List <IPoint> pts = divCurve.GetPointList();

            foreach (IPoint item in pts)
            {
                if (item == pt)
                {
                    return(pt.FunctionValue);
                }
            }
            return(0);
        }
예제 #5
0
        /// <summary>
        /// Gets the rate lin365.
        /// </summary>
        /// <param name="yearFraction1">The year fraction1.</param>
        /// <param name="yearFraction2">The year fraction2.</param>
        /// <param name="ratedays">The ratedays.</param>
        /// <param name="rateamts">The rateamts.</param>
        /// <param name="daybasis">The daybasis.</param>
        /// <returns></returns>
        public static double GetRateCCLin365(double yearFraction1, double yearFraction2, int[] ratedays, double[] rateamts)
        {
            DiscreteCurve rc        = CreateCurve(ratedays, rateamts, 365);
            var           rateCurve = new InterpolatedCurve(rc, InterpolationFactory.Create("LinearInterpolation"), false);
            IPoint        pt1       = new Point1D(yearFraction1);
            IPoint        pt2       = new Point1D(yearFraction2);
            double        rt1       = rateCurve.Value(pt1);
            double        rt2       = rateCurve.Value(pt2);

            if (yearFraction1 != yearFraction2)
            {
                return((rt2 * yearFraction2 - rt1 * yearFraction1) / (yearFraction2 - yearFraction1));
            }
            return(0);
        }
예제 #6
0
        public void TestPiecewiseRateInterpolatedCurve()
        {
            var                interp      = new PiecewiseConstantZeroRateInterpolation();//PWL requires the first point to be after the first 2 in the curve..
            DiscreteCurve      curve       = new DiscreteCurve(_pointCoords, _pointValues);
            IInterpolatedSpace interpCurve = new InterpolatedCurve(curve, interp, true);

            foreach (double point in _testPointArray)
            {
                IPoint p    = new Point1D(point);
                double val  = interpCurve.Value(p);
                double rate = val;
                Console.WriteLine(val);
                Console.WriteLine(rate);
            }
        }
예제 #7
0
        void Optimize(BasicSpecification basicSpecification)
        {
            if (specification == null)
            {
                specification = new Specification(basicSpecification);
            }

            // TODO: make sure the new curve resembles the old one, even when SegmentCount and/or SegmentTemplate are changed
            if (basicSpecification.SegmentCount != specification.BasicSpecification.SegmentCount || basicSpecification.SegmentTemplate != specification.BasicSpecification.SegmentTemplate)
            {
                specification = new Specification(basicSpecification);
            }

            specification = new Specification(basicSpecification, specification.Position);

            try
            {
                Stopwatch stopwatch = new Stopwatch();

                stopwatch.Restart();
                specification = optimizer.Normalize(specification);
                stopwatch.Stop();

                Console.WriteLine("normalization: {0} s", stopwatch.Elapsed.TotalSeconds);

                stopwatch.Restart();
                Kurve.Curves.Curve curve = new DiscreteCurve(optimizer.GetCurve(specification), (int)basicSpecification.CurveLength);
                stopwatch.Stop();

                Console.WriteLine("discrete curve: {0} s", stopwatch.Elapsed.TotalSeconds);

                Application.Invoke
                (
                    delegate(object sender, EventArgs e)
                {
                    if (CurveChanged != null)
                    {
                        CurveChanged(basicSpecification, curve);
                    }
                }
                );
            }
            catch (InvalidOperationException exception)
            {
                Terminal.Write(exception.Message, ConsoleColor.Red);
                Terminal.WriteLine();
            }
        }
예제 #8
0
 public void TestAllInterpolatedCurves()
 {
     foreach (string interp in _linearInterpolations)
     {
         TearDown();
         IInterpolation     interpolation = InterpolationFactory.Create(interp);
         DiscreteCurve      curve         = new DiscreteCurve(_times, _rates);
         IInterpolatedSpace interpCurve   = new InterpolatedCurve(curve, interpolation, true);
         Debug.WriteLine($"interpolationType : {interp}");
         foreach (double point in _testPointArray)
         {
             IPoint p = new Point1D(point);
             Console.WriteLine(interpCurve.Value(p));
         }
     }
     foreach (string interp in _specialInterpolations)
     {
         TearDown();
         var interpolation = (WingModelInterpolation)InterpolationFactory.Create(interp);
         interpolation.Forward = 1.0;
         interpolation.Spot    = 1.0;
         var curve = new DiscreteCurve(Strikes, Vols);
         IInterpolatedSpace interpCurve = new InterpolatedCurve(curve, interpolation, true);
         Debug.WriteLine($"interpolationType : {interp}");
         foreach (double point in _testPointArray)
         {
             IPoint p = new Point1D(point);
             Console.WriteLine(interpCurve.Value(p));
         }
     }
     foreach (string interp in _logLinearInterpolations)
     {
         TearDown();
         double[]           exp           = SetUp();
         IInterpolation     interpolation = InterpolationFactory.Create(interp);
         DiscreteCurve      curve         = new DiscreteCurve(_times, exp);
         IInterpolatedSpace interpCurve   = new InterpolatedCurve(curve, interpolation, true);
         Debug.WriteLine($"interpolationType : {interp}");
         foreach (double point in _testPointArray)
         {
             IPoint p = new Point1D(point);
             Console.WriteLine(interpCurve.Value(p));
         }
     }
 }
예제 #9
0
        /// <summary>
        /// Anns the yield.
        /// </summary>
        /// <param name="baseDate">The base date.</param>
        /// <param name="targetDate">The target date.</param>
        /// <param name="divCurve">The div curve.</param>
        /// <param name="ratetimes">The rate times.</param>
        /// <param name="rateamts">The rate amounts.</param>
        /// <param name="im">the im?</param>
        /// <param name="cf">The cf?</param>
        /// <returns></returns>
        public static double GetPVDivs(double yearFraction1, double yearFraction2, int[] divdays, double[] divamts, int[] ratedays, double[] rateamts, string im, CompoundingFrequencyEnum cf, int daybasis)
        {
            DiscreteCurve divCurve  = CreateCurve(divdays, divamts, daybasis);
            DiscreteCurve rc        = CreateCurve(ratedays, rateamts, daybasis);
            List <IPoint> points    = divCurve.GetPointList();
            double        sum       = 0;
            var           rateCurve = new InterpolatedCurve(rc, InterpolationFactory.Create(im), false);

            foreach (IPoint pt in points)
            {
                decimal t    = Convert.ToDecimal(pt.GetX());
                decimal rate = Convert.ToDecimal(rateCurve.Value(pt));
                double  df   = Convert.ToDouble(GetDiscountFactor(t, rate, cf));
                if ((pt.GetX() <= yearFraction2) & (pt.GetX() > yearFraction1))
                {
                    sum += pt.FunctionValue * df;
                }
            }
            return(sum);
        }
예제 #10
0
        public void TestDiscreteCurve3()
        {
            double[] p = { .1, .23, .4, .55, .73, .88 };

            double[] results = { .99, .98, .97, .94, .93, .88 };
            var      points  = new List <IPoint>();

            for (int i = 0; i < p.Length; i++)
            {
                var pi = new Point1D(p[i], results[i]);
                points.Add(pi);
            }

            IDiscreteSpace curve = new DiscreteCurve(points);

            for (int i = 0; i < curve.GetPointList().Count; i++)
            {
                Assert.AreEqual(curve.GetCoordinateArray(1)[i], curve.GetPointList()[i].Coords[0]);
            }
        }
예제 #11
0
        public void TestDiscreteCurveFromPointList()
        {
            var index     = 0;
            var pointList = new List <IPoint>();

            foreach (var pt in _pointCoords)
            {
                var point = new Point1D(pt, _pointValues[index]);
                pointList.Add(point);
                index++;
            }
            IDiscreteSpace curve = new DiscreteCurve(pointList);

            for (int i = 0; i < curve.GetPointList().Count; i++)
            {
                Debug.Print("X Coord {0} Y Coord {1}", curve.GetPointList()[i].Coords[0], curve.GetPointList()[i].Coords[1]);
                Assert.AreEqual(curve.GetPointList()[i].FunctionValue, _pointValues[i]);
                Assert.AreEqual(curve.GetPointList()[i].Coords[1], _pointValues[i]);
                Assert.AreEqual(curve.GetPointList()[i].Coords[0], _pointCoords[i]);
            }
        }
예제 #12
0
        /// <summary>
        /// Anns the yield.
        /// </summary>
        /// <param name="baseDate">The base date.</param>
        /// <param name="targetDate">The target date.</param>
        /// <param name="divCurve">The div curve.</param>
        /// <param name="ratetimes">The rate times.</param>
        /// <param name="rateamts">The rate amounts.</param>
        /// <param name="im">the im?</param>
        /// <param name="cf">The cf?</param>
        /// <returns></returns>
        public static double GetPVDivs(DateTime baseDate, DateTime targetDate, DiscreteCurve divCurve, double[] ratetimes, double[] rateamts, string im, CompoundingFrequencyEnum cf)
        {
            List <IPoint> points = divCurve.GetPointList();
            double        sum    = 0;

            var rateCurve = new InterpolatedCurve(new DiscreteCurve(ratetimes, rateamts), InterpolationFactory.Create(im), false);

            int    t0       = (targetDate - baseDate).Days;
            double maturity = t0 / 365.0;

            foreach (IPoint pt in points)
            {
                decimal t    = Convert.ToDecimal(pt.GetX());
                decimal rate = Convert.ToDecimal(rateCurve.Value(pt));
                double  df   = Convert.ToDouble(GetDiscountFactor(t, rate, cf));
                if ((pt.GetX() <= maturity) & (pt.GetX() > 0))
                {
                    sum += pt.FunctionValue * df;
                }
            }
            return(sum);
        }
예제 #13
0
 /// <summary>
 /// Anns the yield.
 /// </summary>
 /// <param name="baseDate">The base date.</param>
 /// <param name="targetDate">The target date.</param>
 /// <param name="divCurve">The div curve.</param>
 /// <param name="ratetimes">The rate times.</param>
 /// <param name="rateamts">The rate amounts.</param>
 /// <param name="im">the im?</param>
 /// <param name="cf">The cf?</param>
 /// <returns></returns>
 public static double GetPVDivs(DateTime baseDate, DateTime targetDate, DiscreteCurve divCurve, double[] ratetimes, double[] rateamts, string im, string cf)
 {
     return(GetPVDivs(baseDate, targetDate, divCurve, ratetimes, rateamts, im,
                      EnumParse.ToCompoundingFrequencyEnum(cf)));
 }
예제 #14
0
        ///<summary>
        ///</summary>
        ///<param name="termCurve"></param>
        ///<param name="baseDate"></param>
        ///<param name="dayCounter"></param>
        ///<returns></returns>
        public static DiscreteCurve Converter(TermCurve termCurve, DateTime baseDate, IDayCounter dayCounter)
        {
            var curve = new DiscreteCurve(PointCurveHelper(termCurve, baseDate, dayCounter));

            return(curve);
        }