Exemple #1
0
        public PartyPollingResultsSet(
            string party,
            bool isConstituency,
            List <OpinionPoll> polls
            )
        {
            PartyAbbreviation = party;
            IsConstituency    = isConstituency;
            PollingResults    = new List <PollingResult>();
            _startDate        = null;
            _splineParameters = null;

            // loop through the polls getting the polling values
            SetupPollingResults(party, isConstituency, polls);

            // get the x and y values
            double[] xValues = PollingResults.Select(x => (double)x.DaysSinceStart).ToArray();
            double[] yValues = PollingResults.Select(x => (double)x.Percentage).ToArray();

            // fit the curves
            if (xValues.Length > 0 && yValues.Length > 0)
            {
                alglib.spline1dfitpenalized(
                    xValues,
                    yValues,
                    yValues.Length,
                    1d,
                    out _,
                    out alglib.spline1dinterpolant splineParameters,
                    out alglib.spline1dfitreport _);

                _splineParameters = splineParameters;
            }
        }
        private void InitInterpolantModel()
        {
            var xData = ProbabilityFunctionValues.Select(p => p.X).ToArray();
            var yData = ProbabilityFunctionValues.Select(p => p.Y).ToArray();

            alglib.spline1dinterpolant spline1Dinterpolant;
            alglib.spline1dbuildcubic(xData, yData, out spline1Dinterpolant);
            InterpolantModel = spline1Dinterpolant;
        }
        /// <summary>
        /// Spline Interpolation using a math/stats assembly from http://www.alglib.net/download.php
        /// Interpolation method mimics UofI disaggregation method of taking a 31 day window and interpolating
        /// the values in index numbers 11 - 21
        /// Lots of magic numbers...
        /// </summary>
        private static Series SplineCalc(Series sOrig)
        {
            // Magic numbers to assign index values to the data points
            double[] X = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
            // Magic numbers for values to be overwritten with interpolated values
            double[] evalpts = new double[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 };

            // Generates values from the input series corresponding with 'X'
            List <double> tempVals = new List <double>();

            for (int i = 0; i < 10; i++)
            {
                tempVals.Add(sOrig[i].Value);
            }
            for (int i = 21; i < 31; i++)
            {
                tempVals.Add(sOrig[i].Value);
            }
            double[] Y = tempVals.ToArray();

            //This Cubic Spline replicates the Excel output used by UofI's A.Acharya and Dr.Ryu.
            alglib.spline1dinterpolant C = new alglib.spline1dinterpolant();
            var results = new List <double>();

            for (int i = 0; i < evalpts.Length; i++)
            {
                double evalpt = evalpts[i];
                alglib.spline1dbuildcubic(X, Y, out C);
                double s = alglib.spline1dcalc(C, evalpt);
                results.Add(s);
            }

            // Loop to populate interpolated series.
            Series sNewInterp = new Series();

            for (int j = 0; j < sOrig.Count; j++)
            {
                if (j >= 10 && j <= 20)
                {   // Assigns interpolated value to the new series so long as the interpolated value is > 0
                    if (results[j - 10] > 0)
                    {
                        sNewInterp.Add(sOrig[j].DateTime, results[j - 10], sOrig[j].Flag);
                    }
                    else
                    {
                        sNewInterp.Add(sOrig[j]);
                    }
                }
                else
                {
                    sNewInterp.Add(sOrig[j]);
                }
            }
            return(sNewInterp);
        }
Exemple #4
0
        private static List <PointD> GetMiddle(List <PointD> points)
        {
            var size   = points.Count;
            var middle = new List <PointD>(size);

            var maxPoints = GetLocalMaximums(points);
            var minPoints = GetLocalMinimums(points);

            if (maxPoints.Count > 1 && minPoints.Count > 1)
            {
                alglib.spline1dinterpolant splineMax = new alglib.spline1dinterpolant();
                alglib.spline1dinterpolant splineMin = new alglib.spline1dinterpolant();

                alglib.spline1dbuildcubic(
                    maxPoints.Select(p => p.X).ToArray(),
                    maxPoints.Select(p => p.Y).ToArray(),
                    out splineMax);

                alglib.spline1dbuildcubic(
                    minPoints.Select(p => p.X).ToArray(),
                    minPoints.Select(p => p.Y).ToArray(),
                    out splineMin);

                for (int i = 0; i < size; i++)
                {
                    var xValue    = points[i].X;
                    var yMaxValue = alglib.spline1dcalc(splineMax, xValue);
                    var yMinValue = alglib.spline1dcalc(splineMin, xValue);

                    middle.Add(new PointD
                    {
                        X = xValue,
                        Y = (yMaxValue + yMinValue) / 2.0
                    });
                }
            }

            return(middle);
        }
        /// <summary>
        /// Spline Interpolation using a math/stats assembly from http://www.alglib.net/download.php
        /// Interpolation method mimics UofI disaggregation method of taking a 31 day window and interpolating
        /// the values in index numbers 11 - 21
        /// Lots of magic numbers...
        /// </summary>
        private static Series SplineCalc(Series sOrig)
        {
            // Magic numbers to assign index values to the data points
            double[] X = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
            // Magic numbers for values to be overwritten with interpolated values
            double[] evalpts = new double[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 };

            // Generates values from the input series corresponding with 'X'
            List<double> tempVals = new List<double>();
            for (int i = 0; i < 10; i++) { tempVals.Add(sOrig[i].Value); }
            for (int i = 21; i < 31; i++) { tempVals.Add(sOrig[i].Value); }
            double[] Y = tempVals.ToArray();

            //This Cubic Spline replicates the Excel output used by UofI's A.Acharya and Dr.Ryu.
            alglib.spline1dinterpolant C = new alglib.spline1dinterpolant();
            var results = new List<double>();
            for (int i = 0; i < evalpts.Length; i++)
            {
                double evalpt = evalpts[i];
                alglib.spline1dbuildcubic(X, Y, out C);
                double s = alglib.spline1dcalc(C, evalpt);
                results.Add(s);
            }

            // Loop to populate interpolated series.
            Series sNewInterp = new Series();
            for (int j = 0; j < sOrig.Count; j++)
            {
                if (j >= 10 && j <= 20)
                {   // Assigns interpolated value to the new series so long as the interpolated value is > 0
                    if (results[j - 10] > 0)
                    { sNewInterp.Add(sOrig[j].DateTime, results[j - 10],sOrig[j].Flag); }
                    else { sNewInterp.Add(sOrig[j]); }
                }
                else { sNewInterp.Add(sOrig[j]); }
            }
            return sNewInterp;
        }