Beispiel #1
0
        /// <summary>
        /// Fits a Nelson Siegel curve to data
        /// </summary>
        /// <param name="t"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static NelsonSiegel Fit(Date anchorDate, Date[] dates, double[] rates)
        {
            var times = new double[dates.Length];

            for (var i = 0; i < dates.Length; i++)
            {
                times[i] = dates[i] - anchorDate;
            }

            Func <double[], double> f = x => ErrorFunction(x, times, rates);

            var nm       = new NelderMead(4, f);
            var success  = nm.Minimize(new[] { rates[0], rates[0], rates[0], times.Last() / 5.0 });
            var minValue = nm.Value;
            var solution = nm.Solution;
            var curve    = new NelsonSiegel(anchorDate, solution[0], solution[1], solution[2], solution[3]);

            return(curve);
        }
        /// <summary>
        /// Fits a Nelson Siegel curve to data
        /// </summary>
        /// <param name="t"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static NelsonSiegel Fit(Date anchorDate, Date[] dates, double[] rates)
        {
            double[] times = new double[dates.Length];
            for (int i = 0; i < dates.Length; i++)
            {
                times[i] = dates[i] - anchorDate;
            }

            Func <double[], double> f = (x) => ErrorFunction(x, times, rates);

            var    nm       = new NelderMead(numberOfVariables: 4, function: f);
            bool   success  = nm.Minimize(new double[] { rates[0], rates[0], rates[0], times.Last() / 5.0 });
            double minValue = nm.Value;

            double[]     solution = nm.Solution;
            NelsonSiegel curve    = new NelsonSiegel(anchorDate, solution[0], solution[1], solution[2], solution[3]);

            return(curve);
        }