Beispiel #1
0
 public void Standardize(double mean, double standardDeviation)
 {
     Descriptive.Standardize(this.Elements, mean, standardDeviation);
     ClearAllMeasures();
     InvalidateAll();
     //this.Size = 0;
 }
        public virtual double Moment(int k, double c)
        {
            if (k < 0)
            {
                throw new ArgumentException(Cern.LocalizedResources.Instance().Exception_KMustBePositive);
            }
            //checkOrder(k);
            if (!HasSumOfPowers(k))
            {
                return(Double.NaN);
            }

            int             maxOrder  = System.Math.Min(k, GetMaxOrderForSumOfPowers());
            DoubleArrayList sumOfPows = new DoubleArrayList(maxOrder + 1);

            sumOfPows.Add(Size);
            sumOfPows.Add(Sum);
            sumOfPows.Add(SumOfSquares);
            for (int i = 3; i <= maxOrder; i++)
            {
                sumOfPows.Add(GetSumOfPowers(i));
            }

            return(Descriptive.Moment(k, c, Size, sumOfPows.ToArray()));
        }
Beispiel #3
0
        public override double GetSumOfPowers(int k)
        {
            // no chaching for this measure
            if (k >= -1 && k <= 2)
            {
                return(base.GetSumOfPowers(k));
            }

            return(Descriptive.SumOfPowers(this.Elements, k));
        }
Beispiel #4
0
        public void PopulateDescriptiveStatistics(PlottableProperty plottableProperty)
        {
            var plottableValues = Array.ConvertAll(SampleInstances[plottableProperty.PropertyName].ToArray(), input => Convert.ToDouble(input));

            if (plottableValues.Count() != 0)
            {
                PopulateBins(plottableValues, _numberBins, plottableProperty.PropertyName);
                Descriptive descriptiveStats = new Descriptive(plottableValues);
                descriptiveStats.Analyze();
                Statistics     = descriptiveStats.Result;
                StatisticsList = new ObservableCollection <KeyValuePair <string, string> >(PlottingUtilities.GetFieldAndPropertyValueList(descriptiveStats.Result));
            }
        }
        private double CalculateWindowThreshold(double[] list, double parameterOmega)
        {
            //lstDHG.DHGs.Select(x => x.HeartbeatScore).ToArray<double>()
            if (list.Length == 1)
            {
                return(list[0]);
            }

            Descriptive desp = new Descriptive(list);

            desp.Analyze();

            return(desp.Result.Mean + parameterOmega * desp.Result.StdDev);
        }
        public void PopulateDescriptiveStatistics(PlottableProperty plottableProperty)
        {
            var plottableValues = SampleInstances.Select(i => new PlottableValue <T> {
                X = SampleInstances.IndexOf(i), Value = i
            }).ToList();
            var plottablePoints = PlottingUtilities.ToPlottable <T>(plottableValues, plottableProperty).Select(p => p.Y).ToArray();

            if (plottablePoints.Count() != 0)
            {
                PopulateBins(plottablePoints, _numberBins, plottableProperty.PropertyName);
                Descriptive descriptiveStats = new Descriptive(plottablePoints);
                descriptiveStats.Analyze();
                Statistics     = descriptiveStats.Result;
                StatisticsList = new ObservableCollection <KeyValuePair <string, string> >(PlottingUtilities.GetFieldAndPropertyValueList(descriptiveStats.Result));
            }
        }
        public override void AddAllOfFromTo(DoubleArrayList list, int from, int to)
        {
            base.AddAllOfFromTo(list, from, to);

            if (_sumOfPowers != null)
            {
                //int max_k = this.min_k + this.SumOfPowers.Length-1;
                Descriptive.IncrementalUpdateSumsOfPowers(list, from, to, 3, GetMaxOrderForSumOfPowers(), ref _sumOfPowers);
            }

            if (this.HasSumOfInversions)
            {
                this.SumOfInversions += Descriptive.SumOfInversions(list, from, to);
            }

            if (this.HasSumOfLogarithms)
            {
                this.SumOfLogarithms += Descriptive.SumOfLogarithms(list, from, to);
            }
        }
Beispiel #8
0
        /// <summary>
        /// assertion: isBasicParametersValid == false
        ///
        /// </summary>
        protected void UpdateIncrementalStats()
        {
            // prepare arguments
            double[] arguments = new double[4];
            arguments[0] = base.Min;
            arguments[1] = base.Max;
            arguments[2] = base.Sum;
            arguments[3] = base.SumOfSquares;

            Descriptive.IncrementalUpdate(this.Elements, this.Size, this.Elements.Size - 1, ref arguments);

            // store the new parameters back
            base.Min          = arguments[0];
            base.Max          = arguments[1];
            base.Sum          = arguments[2];
            base.SumOfSquares = arguments[3];

            this.isIncrementalStatValid = true;
            //this.Size = this.Elements.Count; // next time we don't need to redo the stuff we have just done..
        }
Beispiel #9
0
        public override void AddAllOfFromTo(DoubleArrayList list, int from, int to)
        {
            //if (this.arguments == null) setUpCache();
            lock (arguments) {
                // prepare arguments
                arguments[0] = this.min;
                arguments[1] = this.max;
                arguments[2] = this.sum;
                arguments[3] = this.sum_xx;

                Descriptive.IncrementalUpdate(list, from, to, ref arguments);

                // store the new parameters back
                this.min    = arguments[0];
                this.max    = arguments[1];
                this.sum    = arguments[2];
                this.sum_xx = arguments[3];

                this.size += to - from + 1;
            }
        }
Beispiel #10
0
 public double TrimmedMean(int s, int l)
 {
     // no caching for this parameter.
     return(Descriptive.TrimmedMean(sortedElements_unsafe(), Mean(), s, l));
 }
        protected void FillEstadisticas(int[] l,double[] li  )
        {
            try
            {
                statistics s = new statistics();
                s.list = l;
                TxtMediana.Text = s.mediana().ToString();
                TxtMediaAcot.Text = s.mean().ToString();
                TxtPromedio.Text = s.mean().ToString();
                Descriptive d = new Descriptive(li);
                d.Analyze();
                DescriptiveResult res = d.Result;
                TxtDesviacionEs.Text = res.StdDev.ToString("0,0.00", CultureInfo.InvariantCulture);

                if (res.StdDev <= 30)
                {
                    TxtVarianza.Text = "7";
                }
                else if (res.StdDev <= 60)
                {
                    TxtVarianza.Text = "6";
                }
                else if (res.StdDev <= 120)
                {
                    TxtVarianza.Text = "5";
                }
                else if (res.StdDev <= 500)
                {
                    TxtVarianza.Text = "4";
                }
                else if (res.StdDev <= 1000)
                {
                    TxtVarianza.Text = "3";
                }
                else if (res.StdDev <= 2000)
                {
                    TxtVarianza.Text = "2";
                }
                else
                {
                    TxtVarianza.Text = "1";
                }
                //SI(E5<=30;7;SI(E5<=60;6;SI(E5<=120;5;SI(E5<=500;4;SI(E5<=1000;3;SI(E5<=2000;2;1))))))
            }
            catch (Exception ex)
            {
                Log.EscribirError(ex);
                Log.EscribirTraza("Error al calcular estadisticas del acta" + Session["acta"].ToString());
            }
        }
        /// <summary>
        /// Test building blocks of interest rate swap exposure calculation.
        /// </summary>
        public void TestBasics(LinearGaussianModel model, IEnumerable <NArray> allZeroRatesT0,
                               TimePoint[] timePoints, SimulationGraph graph)
        {
            bool testModel = true;

            if (testModel)
            {
                var check1 = model[1, timePoints[1].DateTime.AddDays(182)].First();
                Assert.IsTrue(TestHelpers.AgreesAbsolute(check1, 1.0015314301020275));

                var check2 = model[1, timePoints[1].DateTime.AddDays(91)].First();
                Assert.IsTrue(TestHelpers.AgreesAbsolute(check2, 1.0007451895710209));

                var check3 = model.ForwardRate(1,
                                               timePoints[1].DateTime.AddDays(91), timePoints[1].DateTime.AddDays(182)).First();
                Assert.IsTrue(TestHelpers.AgreesAbsolute(check3, -0.0031509366920208916));
            }

            bool testProfile = true;

            if (testProfile)
            {
                var testVariates0 = graph.RegisterFactor <NormalVariates>("IR_DiscountFactor_EUR_Factor0");
                var testVariates1 = graph.RegisterFactor <NormalVariates>("IR_DiscountFactor_EUR_Factor1");
                var testVariates2 = graph.RegisterFactor <NormalVariates>("IR_DiscountFactor_EUR_Factor2");

                var check = Descriptive.Correlation(testVariates1.Value, testVariates2.Value);

                // check 3M rolling tenor
                var percentiles = new double[] { 1, 10, 50, 90, 99 };
                var measures    = new List <IList <double> >();
                for (int i = 0; i < timePoints.Length; ++i)
                {
                    var tenorMonths = 3;
                    var tenorYears  = tenorMonths / 12.0;
                    var df          = model[i, timePoints[i].DateTime.AddMonths(3)];
                    var zeroRate    = -NMath.Log(df) / tenorYears;
                    var values      = NMath.Percentiles(zeroRate, percentiles).ToList();
                    measures.Add(values);
                }

                var times     = timePoints.Select(p => p.YearsFromBaseDate).ToArray();
                var profile10 = measures.Select(p => p[1]).ToArray();
                var profile90 = measures.Select(p => p[3]).ToArray();
            }

            bool testForwardRate = true;

            if (testForwardRate)
            {
                // check 3M rolling tenor
                var percentiles = new double[] { 1, 10, 50, 90, 99 };
                var measures    = new List <IList <double> >();
                for (int i = 0; i < timePoints.Length; ++i)
                {
                    var tenorMonths = 3;
                    var tenorYears  = tenorMonths / 12.0;
                    var forwardRate = model.ForwardRate(i, timePoints[i].DateTime.AddMonths(3), timePoints[i].DateTime.AddMonths(6));
                    var values      = NMath.Percentiles(forwardRate, percentiles).ToList();
                    measures.Add(values);
                }

                var times     = timePoints.Select(p => p.YearsFromBaseDate).ToArray();
                var profile10 = measures.Select(p => p[1]).ToArray();
                var profile90 = measures.Select(p => p[3]).ToArray();
            }

            bool testAAD = true;

            if (testAAD)
            {
                // just get the result
                var result0 = NArray.Evaluate(() =>
                {
                    var df = model[10, timePoints[10].DateTime.AddMonths(3)];
                    return(df);
                });

                // get the result and single derivative
                var result1 = NArray.Evaluate(() =>
                {
                    var df = model[10, timePoints[10].DateTime.AddMonths(3)];
                    return(df);
                }, model.ZeroRatesT0[6].Value);

                var log = new StringBuilder();

                // get the result and all derivatives (and log output)
                var result2 = NArray.Evaluate(() =>
                {
                    var df = model[10, timePoints[10].DateTime.AddMonths(3)];
                    return(df);
                }, log, allZeroRatesT0.ToArray());

                // now forward rate and all derivatives
                var result3 = NArray.Evaluate(() =>
                {
                    var df = model.ForwardRate(10, timePoints[10].DateTime.AddMonths(3), timePoints[10].DateTime.AddMonths(6));
                    return(df);
                }, allZeroRatesT0.ToArray());

                var unbumped0 = model[10, timePoints[10].DateTime.AddMonths(3)];

                var expected0 = unbumped0.DebugDataView.ToArray();
                var unbumped3 = model.ForwardRate(10, timePoints[10].DateTime.AddMonths(3), timePoints[10].DateTime.AddMonths(6));
                var expected3 = unbumped3.DebugDataView.ToArray();

                var obtained0 = result1[0].DebugDataView.ToArray();
                var obtained1 = result1[0].DebugDataView.ToArray();
                var obtained2 = result2[0].DebugDataView.ToArray();
                var obtained3 = result3[0].DebugDataView.ToArray();

                Assert.IsTrue(TestHelpers.AgreesAbsolute(expected0, obtained0));
                Assert.IsTrue(TestHelpers.AgreesAbsolute(expected0, obtained1));
                Assert.IsTrue(TestHelpers.AgreesAbsolute(expected0, obtained2));
                Assert.IsTrue(TestHelpers.AgreesAbsolute(expected3, obtained3));

                var logCheck = log.ToString();

                var obtained_deriv1 = result1[1].DebugDataView.ToArray();
                var obtained_deriv2 = result2[7].DebugDataView.ToArray();
                var obtained_deriv3 = result3[7].DebugDataView.ToArray();

                model.ZeroRatesT0.Data[6] = new DataPoint(model.ZeroRatesT0.Data[6].Time, model.ZeroRatesT0.Data[6].Value + 1e-6);

                var bumped0 = model[10, timePoints[10].DateTime.AddMonths(3)];
                var bumped3 = model.ForwardRate(10, timePoints[10].DateTime.AddMonths(3), timePoints[10].DateTime.AddMonths(6));

                var expected_deriv1 = ((bumped0 - unbumped0) / 1e-6)
                                      .DebugDataView.ToArray();

                var expected_deriv3 = ((bumped3 - unbumped3) / 1e-6)
                                      .DebugDataView.ToArray();

                Assert.IsTrue(TestHelpers.AgreesAbsolute(expected_deriv1, obtained_deriv1, 1e-5));
                Assert.IsTrue(TestHelpers.AgreesAbsolute(expected_deriv1, obtained_deriv2, 1e-5));
                Assert.IsTrue(TestHelpers.AgreesAbsolute(expected_deriv3, obtained_deriv3, 1e-5));
            }

            bool checkTimings = true;

            if (checkTimings)
            {
                // check timings
                Console.WriteLine("Immediate execution");
                VectorAccelerator.Tests.TestHelpers.Timeit(() =>
                {
                    var df = model[10, timePoints[10].DateTime.AddMonths(3)];
                }, 10, 10);

                Console.WriteLine(); Console.WriteLine("Deferred execution no derivatives");
                VectorAccelerator.Tests.TestHelpers.Timeit(() =>
                {
                    NArray.Evaluate(() =>
                    {
                        var df = model[10, timePoints[10].DateTime.AddMonths(3)];
                        return(df);
                    });
                }, 10, 10);

                Console.WriteLine(); Console.WriteLine("Deferred execution all derivatives");
                VectorAccelerator.Tests.TestHelpers.Timeit(() =>
                {
                    NArray.Evaluate(() =>
                    {
                        var df = model[10, timePoints[10].DateTime.AddMonths(3)];
                        return(df);
                    }, allZeroRatesT0.ToArray());
                }, 10, 10);
            }
        }
Beispiel #13
0
 public override double Moment(int k, double c)
 {
     // currently no caching for this parameter
     return(Descriptive.Moment(this.Elements, k, c));
 }
Beispiel #14
0
 /// <summary>
 ///
 /// </summary>
 protected void UpdateSumOfLogarithms()
 {
     base.SumOfLogarithms        = Descriptive.SumOfLogarithms(this.Elements, 0, Size - 1);
     this.isSumOfLogarithmsValid = true;
 }
 public virtual double Kurtosis()
 {
     return(Descriptive.Kurtosis(Moment(4, Mean()), StandardDeviation()));
 }
 public virtual double HarmonicMean()
 {
     return(Descriptive.HarmonicMean(Size, SumOfInversions));
 }
 public virtual double GeometricMean()
 {
     return(Descriptive.GeometricMean(Size, SumOfLogarithms));
 }
Beispiel #18
0
        public decimal Mean(dynamic DataPoints)
        {
            var Result = Descriptive.Mean(DataPoints);

            return(Result);
        }
Beispiel #19
0
 public override double QuantileInverse(double element)
 {
     return(Descriptive.QuantileInverse(sortedElements_unsafe(), element));
 }
Beispiel #20
0
 /// <summary>
 /// assertion: isBasicParametersValid == false
 ///
 /// </summary>
 protected void UpdateSumOfInversions()
 {
     base.SumOfInversions        = Descriptive.SumOfInversions(this.Elements, 0, Size - 1);
     this.isSumOfInversionsValid = true;
 }
 public double StandardError()
 {
     return(Descriptive.StandardError(Size, Variance()));
 }
Beispiel #22
0
 public void Frequencies(DoubleArrayList distinctElements, IntArrayList frequencies)
 {
     Descriptive.Frequencies(sortedElements_unsafe(), distinctElements, frequencies);
 }
 public double Rms()
 {
     return(Descriptive.Rms(Size, SumOfSquares));
 }
Beispiel #24
0
 public override double Quantile(double phi)
 {
     return(Descriptive.Quantile(sortedElements_unsafe(), phi));
 }
 public double Variance()
 {
     return(Descriptive.SampleVariance(Size, Sum, SumOfSquares));
 }
Beispiel #26
0
 /// <summary>
 /// Returns the exact Quantiles of the specified percentages.
 /// </summary>
 /// <param name="percentages">the percentages for which Quantiles are to be computed.</param>
 /// Each percentage must be in the interval <i>(0.0,1.0]</i>d <i>percentages</i> must be sorted ascending.
 /// <returns>the exact Quantiles.</returns>
 public override DoubleArrayList Quantiles(DoubleArrayList percentages)
 {
     return(Descriptive.Quantiles(sortedElements_unsafe(), percentages));
 }
 /// <summary>
 /// Returns the product, which is <tt>Prod( x[i] )</tt>.
 /// In other words: <tt>x[0]*x[1]*...*x[size()-1]</tt>.
 /// </summary>
 /// <returns>the product; <tt>Double.NaN</tt> if <tt>!hasSumOfLogarithms()</tt>.</returns>
 public virtual double Product()
 {
     return(Descriptive.Product(Size, SumOfLogarithms));
 }
 public virtual double Skew()
 {
     return(Descriptive.Skew(Moment(3, Mean()), StandardDeviation()));
 }
Beispiel #29
0
 /// <summary>
 /// Gets the Percentile value.
 /// </summary>
 /// <param name="percent">Pecentile, between 0 to 100</param>
 /// <returns>Percentile value</returns>
 public double Percentile(double percent)
 {
     return(Descriptive.Percentile(this.sortedData, percent));
 }