예제 #1
0
        public double OisAnnuity(OisSchedule schedule, InterpMethod interpolation)
        {
            double output = 0.0;
            double discFactor;

            for (int i = 0; i < schedule.AdjEndDates.Count; i++)
            {
                discFactor = DiscFactor(schedule.AsOf, schedule.AdjEndDates[i], schedule.DayCount, interpolation);
                output    += schedule.Coverages[i] * discFactor;
            }
            return(output);
        }
예제 #2
0
 public CalibrationSpec(double precision, double scaling, double diffStep, InterpMethod interpolation, int maxIterations, double startingValues, int m, bool useAd, bool inheritDiscSize, double stepSizeOfInheritance, int[] calibrationOrder = null)
 {
     Precision      = precision;
     Scaling        = scaling;
     DiffStep       = diffStep;
     Interpolation  = interpolation;
     MaxIterations  = maxIterations;
     StartingValues = startingValues;
     M                     = m;
     UseAd                 = useAd;
     CalibrationOrder      = calibrationOrder;
     InheritDiscShape      = inheritDiscSize;
     StepSizeOfInheritance = stepSizeOfInheritance;
 }
예제 #3
0
        public static RGB Interpolate(this IImage <RGB> img, InterpMethod method, double x, double y)
        {
            switch (method)
            {
            case InterpMethod.NearestNeighbour:
                return(InterpolateNearest(img, x, y));

            case InterpMethod.Bilinear:
                return(InteroplateBilinear(img, x, y, (a, b, ratio) => a * (1.0 - ratio) + b * ratio));

            default:
                throw new NotImplementedException();
            }
        }
예제 #4
0
 public static double Curve_GetValue(string baseHandle, DateTime date, InterpMethod interpolation)
 {
     if (ObjectMap.DiscCurves.ContainsKey(baseHandle))
     {
         return(ObjectMap.DiscCurves[baseHandle].Interp(date, interpolation));
     }
     else if (ObjectMap.FwdCurves.ContainsKey(baseHandle))
     {
         return(ObjectMap.FwdCurves[baseHandle].Interp(date, interpolation));
     }
     else
     {
         throw new InvalidOperationException("Curve " + baseHandle + " does not exist in the ObjectMap.");
     }
 }
예제 #5
0
        public FwdCurveRepresentation(Curve curve, CurveTenor tenor, DateTime asOf, DayCount dayCount, DayRule dayRule, InterpMethod interpolation)
        {
            Dates      = curve.Dates;
            _zcbValues = curve.Values;
            Dimension  = curve.Dimension;
            Tenor      = tenor;
            AsOf       = asOf;

            _fwdDayCount   = dayCount;
            _fwdDayRule    = dayRule;
            _interpolation = interpolation;

            ConstructZcbCurveFromDatesAndValues();
            ConstructFwdRates();
        }
예제 #6
0
        public ADouble OisRateAD(OisSwap swap, InterpMethod interpolation)
        {
            ADouble floatContribution = 0.0;
            ADouble annuity           = OisAnnuityAD(swap.FixedSchedule, interpolation);

            DateTime asOf = swap.FloatSchedule.AsOf;

            for (int i = 0; i < swap.FloatSchedule.AdjEndDates.Count; i++)
            {
                DateTime startDate      = swap.FloatSchedule.AdjStartDates[i];
                DateTime endDate        = swap.FloatSchedule.AdjEndDates[i];
                ADouble  compoundedRate = OisCompoundedRateAD(asOf, startDate, endDate, swap.FloatSchedule.DayRule, swap.FloatSchedule.DayCount, interpolation);
                ADouble  discFactor     = DiscFactor(asOf, endDate, swap.FixedSchedule.DayCount, interpolation);
                ADouble  coverage       = DateHandling.Cvg(startDate, endDate, swap.FloatSchedule.DayCount);
                floatContribution = floatContribution + discFactor * compoundedRate * coverage;
            }
            return(floatContribution / annuity);
        }
예제 #7
0
        public FwdCurveRepresentation(Curve_AD curve, CurveTenor tenor, DateTime asOf, DayCount dayCount, DayRule dayRule, InterpMethod interpolation)
        {
            Dates     = curve.Dates;
            Dimension = curve.Dimension;
            Tenor     = tenor;
            AsOf      = asOf;

            _fwdDayCount   = dayCount;
            _fwdDayRule    = dayRule;
            _interpolation = interpolation;

            for (int i = 0; i < Dimension; i++)
            {
                Values.Add(curve.Values[i].Value);
            }

            ConstructZcbCurveFromDatesAndValues();
            ConstructFwdRates();
        }
예제 #8
0
        public static IImage <double> Resize(this IImage <double> img, InterpMethod method, int newHeight, int newWidth)
        {
            double heightDiff = (double)img.Height / (double)newHeight;
            double widthDiff  = (double)img.Width / (double)newWidth;

            var resizedImage = new Image(newWidth, newHeight);

            Parallel.For(0, newHeight, y =>
            {
                var relativeY    = heightDiff * y;
                double relativeX = 0.0;
                for (int x = 0; x < newWidth; x++)
                {
                    relativeX         += widthDiff;
                    resizedImage[y, x] = img.Interpolate(method, relativeX, relativeY);
                }
            });

            return(resizedImage);
        }
예제 #9
0
        public static string Calibration_CalibrationSettings_Make(string baseHandle, double precision, double scaling, double diffStep, string interpolation, int maxIterations, double startingValues, int bfgs_m, bool useAd, bool inheritDiscSize, double stepSizeOfInheritance, object[] calibrationOrder = null)
        {
            InterpMethod interp = StrToEnum.InterpolationConvert(interpolation);

            if (calibrationOrder[0] is ExcelMissing)
            {
                calibrationOrder = null;
                CalibrationFunctions.CalibrationSpec_Make(baseHandle, precision, scaling, diffStep, interp, maxIterations, startingValues, bfgs_m, useAd, inheritDiscSize, stepSizeOfInheritance);
            }
            else
            {
                // Need to do this, since object[] cannot be cast to int[]...
                int[] intCalibrationOrder = new int[calibrationOrder.Length];
                for (int i = 0; i < calibrationOrder.Length; i++)
                {
                    intCalibrationOrder[i] = Convert.ToInt32(calibrationOrder[i]);
                }

                CalibrationFunctions.CalibrationSpec_Make(baseHandle, precision, scaling, diffStep, interp, maxIterations, startingValues, bfgs_m, useAd, inheritDiscSize, stepSizeOfInheritance, intCalibrationOrder);
            }

            return(baseHandle);
        }
예제 #10
0
 public static double FwdCurve_GetValue(string baseName, DateTime date, InterpMethod interpolation)
 {
     return(ObjectMap.FwdCurves[baseName].Interp(date, interpolation));
 }
예제 #11
0
        public static void LinearRateModel_Make(string baseName, string fwdCurveCollectionName, string discCurveName, InterpMethod interpolation)
        {
            FwdCurveContainer fwdCurves = ObjectMap.FwdCurveCollections[fwdCurveCollectionName];
            Curve             discCurve = ObjectMap.DiscCurves[discCurveName];
            LinearRateModel   model     = new LinearRateModel(discCurve, fwdCurves, interpolation);

            ObjectMap.LinearRateModels[baseName] = model;
        }
예제 #12
0
        public static double Curve_GetFwdRate(string curveHandle, DateTime asOf, DateTime date, CurveTenor tenor, DayCount dayCount, DayRule dayRule, InterpMethod interpolation)
        {
            DateTime endDate = DateHandling.AddTenorAdjust(date, EnumToStr.CurveTenor(tenor), dayRule);

            if (ObjectMap.DiscCurves.ContainsKey(curveHandle))
            {
                Curve curve = ObjectMap.DiscCurves[curveHandle];
                return(curve.FwdRate(asOf, date, endDate, dayRule, dayCount, interpolation));
            }
            else if (ObjectMap.FwdCurves.ContainsKey(curveHandle))
            {
                Curve curve = ObjectMap.FwdCurves[curveHandle];
                return(curve.FwdRate(asOf, date, endDate, dayRule, dayCount, interpolation));
            }
            else
            {
                throw new InvalidOperationException(curveHandle + " does not exist in the objectMap.");
            }
        }
예제 #13
0
        public static void FwdCurveRepresentation_MakeFromDiscCurve(string baseName, string discCurveHandle, CurveTenor tenor, DateTime asOf, DayCount dayCount, DayRule dayRule, InterpMethod interpolation)
        {
            Curve discCurve = ObjectMap.DiscCurves[discCurveHandle];

            ObjectMap.FwdCurveRepresentations[baseName] = new FwdCurveRepresentation(discCurve, tenor, asOf, dayCount, dayRule, interpolation);
        }
예제 #14
0
        public static void CalibrationSpec_Make(string baseName, double precision, double scaling, double diffStep, InterpMethod interpolation, int maxIterations, double startingValues, int bfgs_m, bool useAd, bool inheritDiscSize, double stepSizeOfInheritance, int[] calibrationOrder = null)
        {
            CalibrationSpec spec = new CalibrationSpec(precision, scaling, diffStep, interpolation, maxIterations, startingValues, bfgs_m, useAd, inheritDiscSize, stepSizeOfInheritance, calibrationOrder);

            ObjectMap.CalibrationSettings[baseName] = spec;
        }
예제 #15
0
        public static ADouble InterpolateCurve(List <DateTime> xDates, DateTime date, List <ADouble> yArr, InterpMethod method)
        {
            double[] xArr = xDates.Select(i => i.ToOADate()).ToArray();
            double   x    = date.ToOADate();

            return(InterpolateCurve(xArr, x, yArr, method));
        }
예제 #16
0
        public ADouble FwdRate(DateTime asOf, DateTime startDate, DateTime endDate, DayRule dayRule, DayCount dayCount, InterpMethod interpolation)
        {
            ADouble ps  = DiscFactor(asOf, startDate, dayCount, interpolation);
            ADouble pe  = DiscFactor(asOf, endDate, dayCount, interpolation);
            ADouble cvg = DateHandling.Cvg(startDate, endDate, dayCount);

            return((ps / pe - 1.0) / cvg);
        }
예제 #17
0
        public double OisCompoundedRateAD(DateTime asOf, DateTime startDate, DateTime endDate, DayRule dayRule, DayCount dayCount, InterpMethod interpolation)
        {
            double   CompoundedRate  = 1;
            double   CompoundedRate2 = 1;
            DateTime RollDate        = startDate;

            while (RollDate.Date < endDate.Date)
            {
                DateTime NextBusinessDay = DateHandling.AddTenorAdjust(RollDate, "1B", DayRule.F);
                double   Rate            = ZeroRate(NextBusinessDay, interpolation);
                double   fwdOisRate      = FwdRate(asOf, RollDate, NextBusinessDay, DayRule.F, dayCount, interpolation);

                double disc1 = DiscFactor(asOf, RollDate, dayCount, interpolation);
                double disc2 = DiscFactor(asOf, NextBusinessDay, dayCount, interpolation);

                double Days     = NextBusinessDay.Subtract(RollDate).TotalDays;
                double shortCvg = DateHandling.Cvg(RollDate, NextBusinessDay, dayCount);
                RollDate         = NextBusinessDay;
                CompoundedRate  *= (1 + fwdOisRate * shortCvg);
                CompoundedRate2 *= disc1 / disc2;
            }
            double coverage = DateHandling.Cvg(startDate, endDate, dayCount);

            return((CompoundedRate2 - 1) / coverage);
        }
예제 #18
0
 internal static extern void ValueAnimation_SetInterpolationMethod(IntPtr handle, InterpMethod method);
예제 #19
0
 public static double Interpolate(double[] xArr, double x, double[] yArr, InterpMethod method)
 {
     return(Interpolate(xArr.ToList <double>(), x, yArr.ToList <double>(), method));
 }
예제 #20
0
 public ADouble AnnuityAD(SwapSchedule Schedule, InterpMethod Method)
 {
     return(AnnuityAD(Schedule.AsOf, Schedule.StartDate, Schedule.EndDate, Schedule.Frequency, Schedule.DayCount, Schedule.DayRule, Method));
 }
예제 #21
0
 public LinearRateModel(Curve discCurve, FwdCurveContainer fwdCurveCollection, InterpMethod interpolation)
 {
     Interpolation      = interpolation;
     DiscCurve          = discCurve;
     FwdCurveCollection = fwdCurveCollection;
 }
예제 #22
0
 public static double InterpolateCurve(Curve curve, DateTime inputDate, InterpMethod method)
 {
     return(InterpolateCurve(curve.Dates, inputDate, curve.Values, method));
 }
예제 #23
0
        public static double InterpolateCurve(List <DateTime> dates, DateTime inputDate, List <double> values, InterpMethod method)
        {
            double[] xArr = dates.Select(i => i.ToOADate()).ToArray();
            double   x    = inputDate.ToOADate();

            return(Interpolate(xArr, x, values.ToArray(), method));
        }
예제 #24
0
        public static double Interpolate(List <double> xArr, double x, List <double> yArr, InterpMethod Method)
        {
            // Main interpolation method. All other methods calls this

            if (xArr.Count() != yArr.Count())
            {
                throw new ArgumentException("Number of dates has to correspond to number of values");
            }

            int n = xArr.Count();

            // Extrapolation (flat)
            if (xArr[0] > x)
            {
                return(yArr[0]);
            }
            else if (xArr[n - 1] <= x)
            {
                return(yArr[n - 1]);
            }
            else
            {
                // No extrapolation - find relevant index in arrays
                int i = 0;
                int j = 0;

                for (i = 0; i < n; i++)
                {
                    if (xArr[i] <= x)
                    {
                        j = j + 1;
                    }
                    else
                    {
                        break;
                    }
                }

                j = j - 1;

                switch (Method)
                {
                case InterpMethod.Constant:

                    return(yArr[j]);

                case InterpMethod.Linear:

                    double tempLinear = (x - xArr[j]) * yArr[j + 1] + (xArr[j + 1] - x) * yArr[j];
                    return(tempLinear / (xArr[j + 1] - xArr[j]));

                case InterpMethod.LogLinear:

                    // Log-Linear interpolation is only valid for positive stuff
                    if (yArr[j + 1] < 0 || yArr[j] < 0)
                    {
                        return(yArr[j]);
                    }
                    else
                    {
                        double tempLogLinear1 = (x - xArr[j]) / (xArr[j + 1] - xArr[j]);
                        double tempLogLinear2 = (xArr[j + 1] - x) / (xArr[j + 1] - xArr[j]);
                        return(Math.Pow(yArr[j + 1], tempLogLinear1) * Math.Pow(yArr[j], tempLogLinear2));
                    }

                case InterpMethod.Hermite:

                    double bi, bk, hi, mi, ci, di;
                    int    k = j + 1;
                    if (j == 0)
                    {
                        bi  = (xArr[2] + xArr[1] - 2 * xArr[0]) * (yArr[1] - yArr[0]) / (xArr[1] - xArr[0]);
                        bi += -1 * (xArr[1] - xArr[0]) * (yArr[2] - yArr[1]) / (xArr[2] - xArr[1]);
                        bi *= Math.Pow(xArr[2] - xArr[0], -1.0);

                        bk  = (xArr[k + 1] - xArr[k]) * (yArr[k] - yArr[k - 1]) / (xArr[k] - xArr[k - 1]);
                        bk += (xArr[k] - xArr[k - 1]) * (yArr[k + 1] - yArr[k]) / (xArr[k + 1] - xArr[k]);
                        bk *= Math.Pow(xArr[k + 1] - xArr[k - 1], -1);
                    }
                    else if (j == n - 2)
                    {
                        bi  = (xArr[j + 1] - xArr[j]) * (yArr[j] - yArr[k - 1]) / (xArr[j] - xArr[j - 1]);
                        bi += (xArr[j] - xArr[j - 1]) * (yArr[j + 1] - yArr[j]) / (xArr[j + 1] - xArr[j]);
                        bi *= Math.Pow(xArr[j + 1] - xArr[j - 1], -1.0);

                        bk  = -1 * (xArr[n - 1] - xArr[n - 2]) * (yArr[n - 2] - yArr[n - 3]) / (xArr[n - 2] - xArr[n - 3]);
                        bk += -1 * (xArr[n - 1] - xArr[n - 2] - (xArr[n - 1] - xArr[n - 3])) * (yArr[n - 1] - yArr[n - 2]) / (xArr[n - 1] - xArr[n - 2]);
                        bk *= Math.Pow(xArr[n - 1] - xArr[n - 3], -1.0);
                    }
                    else
                    {
                        bi  = (xArr[j + 1] - xArr[j]) * (yArr[j] - yArr[j - 1]) / (xArr[j] - xArr[j - 1]);
                        bi += (xArr[j] - xArr[j - 1]) * (yArr[j + 1] - yArr[j]) / (xArr[j + 1] - xArr[j]);
                        bi *= Math.Pow(xArr[j + 1] - xArr[j - 1], -1.0);

                        bk  = (xArr[k + 1] - xArr[k]) * (yArr[k] - yArr[k - 1]) / (xArr[k] - xArr[k - 1]);
                        bk += (xArr[k] - xArr[k - 1]) * (yArr[k + 1] - yArr[k]) / (xArr[k + 1] - xArr[k]);
                        bk *= Math.Pow(xArr[k + 1] - xArr[k - 1], -1.0);
                    }

                    hi = xArr[j + 1] - xArr[j];
                    mi = (yArr[j + 1] - yArr[j]) / hi;
                    ci = (3.0 * mi - bk - 2.0 * bi) / hi;
                    di = (bk + bi - 2.0 * mi) * Math.Pow(hi, -2.0);

                    return(yArr[j] + bi * (x - xArr[j]) + ci * Math.Pow(x - xArr[j], 2.0) + di * Math.Pow(x - xArr[j], 3.0));

                default:
                    throw new InvalidOperationException("Interpolation method is not valid");
                }
            }
        }
예제 #25
0
        public ADouble AnnuityAD(DateTime asOf, DateTime startDate, DateTime endDate, CurveTenor tenor, DayCount dayCount, DayRule dayRule, InterpMethod interpolation)
        {
            SwapSchedule annuitySchedule = new SwapSchedule(asOf, startDate, endDate, dayCount, dayRule, tenor);
            ADouble      result          = 0.0;

            for (int i = 0; i < annuitySchedule.AdjEndDates.Count; i++)
            {
                result = result + annuitySchedule.Coverages[i] * ADDiscCurve.DiscFactor(asOf, annuitySchedule.AdjEndDates[i], dayCount, interpolation);
            }
            return(result);
        }
예제 #26
0
 public ADouble Interp(DateTime date, InterpMethod interpolation)
 {
     return(MyMath.InterpolateCurve(Dates, date, Values, interpolation));
 }
예제 #27
0
 public LinearRateModel(Curve_AD discCurve, ADFwdCurveContainer fwdCurveCollection, InterpMethod interpolation = InterpMethod.Linear)
 {
     ADDiscCurve          = discCurve;
     ADFwdCurveCollection = fwdCurveCollection;
     Interpolation        = interpolation;
 }
예제 #28
0
 public ADouble ZeroRate(DateTime date, InterpMethod interpolation)
 {
     return(this.Interp(date, interpolation));
 }
예제 #29
0
 /// <summary>
 /// Set interpolation method.
 /// </summary>
 private void SetInterpolationMethod(InterpMethod method)
 {
     Runtime.ValidateRefCounted(this);
     ValueAnimation_SetInterpolationMethod(handle, method);
 }
예제 #30
0
 public ADouble DiscFactor(DateTime asOf, DateTime date, DayCount dayCount, InterpMethod interpolation)
 {
     return(ADouble.Exp(-1.0 * ZeroRate(date, interpolation) * DateHandling.Cvg(asOf, date, dayCount)));
 }