Пример #1
0
        public async Task <bool> SetObservedMathResult(string mathType, string distType,
                                                       string mathSubType, double mostLikely, double[] obsTs)
        {
            bool bHasSet = false;
            //this is observed data rather than randomly sampled data
            StringBuilder sb = new StringBuilder();

            if (obsTs.Count() > 0)
            {
                //xy array of 10 points on cdf
                //var array = qTs.ToArray();
                Array.Sort(obsTs);
                int i00  = 0;
                int i10  = (int)(obsTs.Length * .10);
                int i20  = (int)(obsTs.Length * .20);
                int i30  = (int)(obsTs.Length * .30);
                int i40  = (int)(obsTs.Length * .40);
                int i50  = (int)(obsTs.Length * .50);
                int i60  = (int)(obsTs.Length * .60);
                int i70  = (int)(obsTs.Length * .70);
                int i80  = (int)(obsTs.Length * .80);
                int i90  = (int)(obsTs.Length * .90);
                int i100 = (int)(obsTs.Length - 1);
                sb.AppendLine(Errors.GetMessage("STATS_DESC5"));
                sb.AppendLine(string.Concat(
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(obsTs, obsTs[i00]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(obsTs, obsTs[i10]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(obsTs, obsTs[i20]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(obsTs, obsTs[i30]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(obsTs, obsTs[i40]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(obsTs, obsTs[i50]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(obsTs, obsTs[i60]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(obsTs, obsTs[i70]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(obsTs, obsTs[i80]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(obsTs, obsTs[i90]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(obsTs, obsTs[i100]).ToString("N2", CultureInfo.InvariantCulture)));
                sb.AppendLine(string.Concat(
                                  obsTs[i00].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  obsTs[i10].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  obsTs[i20].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  obsTs[i30].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  obsTs[i40].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  obsTs[i50].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  obsTs[i60].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  obsTs[i70].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  obsTs[i80].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  obsTs[i90].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  obsTs[i100].ToString("F4", CultureInfo.InvariantCulture)));
                sb.AppendLine(Errors.GetMessage("STATS_DESC3"));
                sb.AppendLine(Errors.GetMessage("STATS_DESC2"));
                //qT is the result of math and stored in sixth col
                var stats = new MathNet.Numerics.Statistics.DescriptiveStatistics(obsTs);
                sb.AppendLine(string.Concat(stats.Count.ToString(), Constants.CSV_DELIMITER, obsTs.Sum().ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                            stats.Mean.ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                            MathNet.Numerics.Statistics.Statistics.Median(obsTs).ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                            stats.StandardDeviation.ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER, stats.Variance.ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                            stats.Minimum.ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER, stats.Maximum.ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER));
                string sLowerCI = string.Concat(Errors.GetMessage("LOWER"), this.CILevel.ToString(), Errors.GetMessage("CI_PCT"));
                string sUpperCI = string.Concat(Errors.GetMessage("UPPER"), this.CILevel.ToString(), Errors.GetMessage("CI_PCT"));
                this.IndicatorQT.QTM     = stats.Mean;
                this.IndicatorQT.QTL     = stats.Mean - CalculatorHelpers.GetConfidenceInterval(this.CILevel, stats.Count, stats.StandardDeviation);
                this.IndicatorQT.QTU     = stats.Mean + CalculatorHelpers.GetConfidenceInterval(this.CILevel, stats.Count, stats.StandardDeviation);
                this.IndicatorQT.QTLUnit = sLowerCI;
                this.IndicatorQT.QTUUnit = sUpperCI;
            }
            else
            {
                sb.AppendLine(Errors.MakeStandardErrorMsg("DATAURL_BADDATA"));
            }
            if (this.IndicatorQT.MathResult.ToLower().StartsWith("http"))
            {
                bool bHasSaved = await CalculatorHelpers.SaveTextInURI(
                    Params.ExtensionDocToCalcURI, sb.ToString(), this.IndicatorQT.MathResult);

                if (!string.IsNullOrEmpty(Params.ExtensionDocToCalcURI.ErrorMessage))
                {
                    this.IndicatorQT.MathResult += Params.ExtensionDocToCalcURI.ErrorMessage;
                    //done with errormsg
                    Params.ExtensionDocToCalcURI.ErrorMessage = string.Empty;
                }
            }
            else
            {
                this.IndicatorQT.MathResult = sb.ToString();
            }
            bHasSet = true;
            return(bHasSet);
        }
Пример #2
0
        public async Task <bool> SetRange(double mostLikelyEstimate, double lowEstimate, double highEstimate,
                                          double[] data, List <double> cdf = null)
        {
            bool          bHasSet = false;
            StringBuilder sb      = new StringBuilder();

            if (data == null)
            {
                sb.AppendLine("This indicator does not have the properties needed for descriptive statistics.");
            }
            if (data.Count() == 0)
            {
                sb.AppendLine("This indicator does not have the properties needed for descriptive statistics.");
            }
            //default has no range of values
            double lowRange       = lowEstimate;
            double highRange      = highEstimate;
            double mostLikely     = mostLikelyEstimate;
            string mostLikelyUnit = string.Empty;
            string lowUnit        = string.Empty;
            string highUnit       = string.Empty;
            bool   bNeedsCDF      = false;
            var    stats          = new MathNet.Numerics.Statistics.DescriptiveStatistics(data);
            string sLowerCI       = string.Concat(Errors.GetMessage("LOWER"), this.CILevel.ToString(), Errors.GetMessage("CI_PCT"));
            string sUpperCI       = string.Concat(Errors.GetMessage("UPPER"), this.CILevel.ToString(), Errors.GetMessage("CI_PCT"));

            //some data has all zeros and does not use sampled data
            if (stats.Mean != 0)
            {
                mostLikely     = stats.Mean;
                mostLikelyUnit = "mean";
                lowRange       = mostLikely - CalculatorHelpers.GetConfidenceInterval(this.CILevel, stats.Count, stats.StandardDeviation);
                lowUnit        = sLowerCI;
                highRange      = mostLikely + CalculatorHelpers.GetConfidenceInterval(this.CILevel, stats.Count, stats.StandardDeviation);
                highUnit       = sUpperCI;
                bNeedsCDF      = true;
            }

            if (bNeedsCDF)
            {
                //csv strings use f4 not n4
                sb.AppendLine(Errors.GetMessage("STATS_DESC1"));
                sb.AppendLine(Errors.GetMessage("STATS_DESC2"));
                sb.AppendLine(string.Concat(stats.Count.ToString(), ", ", data.Sum().ToString("F4", CultureInfo.InvariantCulture), ", ",
                                            stats.Mean.ToString("F4", CultureInfo.InvariantCulture), ", ",
                                            MathNet.Numerics.Statistics.Statistics.Median(data).ToString("F4", CultureInfo.InvariantCulture), ", ",
                                            stats.StandardDeviation.ToString("F4", CultureInfo.InvariantCulture), ", ", stats.Variance.ToString("F4", CultureInfo.InvariantCulture), ", ",
                                            stats.Minimum.ToString("F4", CultureInfo.InvariantCulture), ", ", stats.Maximum.ToString("F4", CultureInfo.InvariantCulture), ", "));
                //xy array of 10 points on cdf
                var array = data.ToArray();
                Array.Sort(array);
                int i00  = 0;
                int i10  = (int)(array.Length * .10);
                int i20  = (int)(array.Length * .20);
                int i30  = (int)(array.Length * .30);
                int i40  = (int)(array.Length * .40);
                int i50  = (int)(array.Length * .50);
                int i60  = (int)(array.Length * .60);
                int i70  = (int)(array.Length * .70);
                int i80  = (int)(array.Length * .80);
                int i90  = (int)(array.Length * .90);
                int i100 = (int)(array.Length - 1);
                sb.AppendLine(Errors.GetMessage("STATS_DESC4"));
                sb.AppendLine(string.Concat(
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(data, array[i00]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(data, array[i10]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(data, array[i20]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(data, array[i30]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(data, array[i40]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(data, array[i50]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(data, array[i60]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(data, array[i70]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(data, array[i80]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(data, array[i90]).ToString("N2", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  MathNet.Numerics.Statistics.Statistics.EmpiricalCDF(data, array[i100]).ToString("N2", CultureInfo.InvariantCulture)));
                sb.AppendLine(string.Concat(
                                  array[i00].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  array[i10].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  array[i20].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  array[i30].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  array[i40].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  array[i50].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  array[i60].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  array[i70].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  array[i80].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  array[i90].ToString("F4", CultureInfo.InvariantCulture), Constants.CSV_DELIMITER,
                                  array[i100].ToString("F4", CultureInfo.InvariantCulture)));
                if (cdf == null)
                {
                    cdf = new List <double>();
                }
                cdf.Add(array[i00]);
                cdf.Add(array[i10]);
                cdf.Add(array[i20]);
                cdf.Add(array[i30]);
                cdf.Add(array[i40]);
                cdf.Add(array[i50]);
                cdf.Add(array[i60]);
                cdf.Add(array[i70]);
                cdf.Add(array[i80]);
                cdf.Add(array[i90]);
                cdf.Add(array[i100]);
            }
            else
            {
                sb.AppendLine(Errors.GetMessage("STATS_DESC6"));
            }
            //doubles need restricted digits
            mostLikely = Math.Round(mostLikely, 4);
            lowRange   = Math.Round(lowRange, 4);
            highRange  = Math.Round(highRange, 4);
            string sD1Unit = "low";
            string sD2Unit = "high";

            this.IndicatorQT.QTD1 = lowEstimate;
            if (this.IndicatorQT.QTD1Unit == string.Empty || this.IndicatorQT.QTD1Unit == Constants.NONE)
            {
                this.IndicatorQT.QTD1Unit = sD1Unit;
            }
            this.IndicatorQT.QTD2 = highEstimate;
            if (this.IndicatorQT.QTD2Unit == string.Empty || this.IndicatorQT.QTD2Unit == Constants.NONE)
            {
                this.IndicatorQT.QTD2Unit = sD2Unit;
            }
            //computed results
            this.IndicatorQT.QTM = mostLikely;
            if (this.IndicatorQT.QTMUnit == string.Empty || this.IndicatorQT.QTMUnit == Constants.NONE)
            {
                this.IndicatorQT.QTMUnit = mostLikelyUnit;
            }
            this.IndicatorQT.QTL = lowRange;
            if (this.IndicatorQT.QTLUnit == string.Empty || this.IndicatorQT.QTLUnit == Constants.NONE)
            {
                this.IndicatorQT.QTLUnit = lowUnit;
            }
            this.IndicatorQT.QTU = highRange;
            if (this.IndicatorQT.QTUUnit == string.Empty || this.IndicatorQT.QTUUnit == Constants.NONE)
            {
                this.IndicatorQT.QTUUnit = highUnit;
            }
            if (this.IndicatorQT.MathResult.ToLower().StartsWith("http"))
            {
                bool bHasSaved = await CalculatorHelpers.SaveTextInURI(
                    Params.ExtensionDocToCalcURI, sb.ToString(), this.IndicatorQT.MathResult);

                if (!string.IsNullOrEmpty(Params.ExtensionDocToCalcURI.ErrorMessage))
                {
                    this.IndicatorQT.MathResult += Params.ExtensionDocToCalcURI.ErrorMessage;
                    //done with errormsg
                    Params.ExtensionDocToCalcURI.ErrorMessage = string.Empty;
                }
            }
            else
            {
                this.IndicatorQT.MathResult = sb.ToString();
            }
            bHasSet = true;
            return(bHasSet);
        }