public double GetHistoricalVolatility(string underlyingName, DateTime dateTime, int days)
        {
            var historicalVolatilitySet = this.GetHistoricalData(underlyingName, dateTime, days);

            var statistics = new MathNet.Numerics.Statistics.DescriptiveStatistics(historicalVolatilitySet.Skip(1).Select(x => x.Volatility));

            // 252: open market days in a year
            return statistics.StandardDeviation * Math.Sqrt(252);
        }
Example #2
0
        public static double GetNormalizedValue(string subIndNormType, double startValue,
                                                MathNet.Numerics.Statistics.DescriptiveStatistics stats)
        {
            double dbNValue = startValue;

            if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.none.ToString() ||
                string.IsNullOrEmpty(subIndNormType))
            {
                //data has already been normalized
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.zscore.ToString())
            {
                //z-score: (x – mean(x)) / stddev(x)
                dbNValue = (startValue - stats.Mean) / stats.StandardDeviation;
                dbNValue = CalculatorHelpers.CheckForNaNandRound4(dbNValue);
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.minmax.ToString())
            {
                //min-max: (x – min(x)) / (max(x) – min(x))
                dbNValue = (startValue - stats.Minimum) / (stats.Maximum - stats.Minimum);
                dbNValue = CalculatorHelpers.CheckForNaNandRound4(dbNValue);
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.logistic.ToString())
            {
                //logistic: 1 / (1 + exp(-x))
                dbNValue = MathNet.Numerics.SpecialFunctions.Logistic(startValue);
                dbNValue = CalculatorHelpers.CheckForNaNandRound4(dbNValue);
                //or
                //siIndex[x] = 1 / (1 + Math.Exp(-siIndex[x]));
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.logit.ToString())
            {
                //logit: inverese of logistic for y between 0 and 1
                //this assumes x is actually y
                dbNValue = MathNet.Numerics.SpecialFunctions.Logit(startValue);
                dbNValue = CalculatorHelpers.CheckForNaNandRound4(dbNValue);
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.tanh.ToString())
            {
                //hyperbolic tangent
                dbNValue = MathNet.Numerics.Trig.Tanh(startValue);
                dbNValue = CalculatorHelpers.CheckForNaNandRound4(dbNValue);
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.pnorm.ToString())
            {
                //pnorm is for complete vectors in next function
            }
            else
            {
                //indicator 2 in drr1 (p and q, not norm and index)
            }
            return(dbNValue);
        }
        void _dataList2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.data2          = (ISpatialData)this._dataList2.SelectedValue;
            this._data2Max.Text = this.data2.Max.ToString();
            this._data2Min.Text = this.data2.Min.ToString();
            var result = new MathNet.Numerics.Statistics.DescriptiveStatistics(this.data2.Data.Values);

            this._data2Mean.Text     = result.Mean.ToString();
            this._data2Variance.Text = result.Variance.ToString();
            this._boxPlot2.DrawHorizontally(this.data2, 3);
            this.loadStats();
        }
Example #4
0
        void _canvas_Loaded(object sender, RoutedEventArgs e)
        {
            this._data1Max.Text = this._dataField.Max.ToString(format);
            this._data1Min.Text = this._dataField.Min.ToString(format);
            var result = new MathNet.Numerics.Statistics.DescriptiveStatistics(this._dataField.Data.Values);

            this._data1Mean.Text     = result.Mean.ToString(format);
            this._data1Variance.Text = result.Variance.ToString(format);
            this._dataSize.Text      = result.Count.ToString();
            double sum = result.Mean * this._dataField.Data.Count;

            this._sum.Text = sum.ToString(format);
            double integral = sum * this._host.cellularFloor.CellSize * this._host.cellularFloor.CellSize;

            this._integration.Text           = integral.ToString(format);
            this._dataStandardDeviation.Text = result.StandardDeviation.ToString(format);
            this._boxPlot.DrawVertically(this._dataField, 3);
        }
Example #5
0
        /// <summary>
        /// Se estiman las propiedades estáticas en función de la profundidad del core 
        /// </summary>
        public void Estimar()
        {
            // se toma la informacion de segmentacion vs areas de interes y se estiman las propiedades petrofisicas estaticas

            // se preparan los generadores de CT aleatorios para cada phantom
            var phantom1High = new MathNet.Numerics.Distributions.Normal(padre.actual.phantom1.mediaHigh, padre.actual.phantom1.desvHigh);
            var phantom1Low = new MathNet.Numerics.Distributions.Normal(padre.actual.phantom1.mediaLow, padre.actual.phantom1.desvLow);

            var phantom2High = new MathNet.Numerics.Distributions.Normal(padre.actual.phantom2.mediaHigh, padre.actual.phantom2.desvHigh);
            var phantom2Low = new MathNet.Numerics.Distributions.Normal(padre.actual.phantom2.mediaLow, padre.actual.phantom2.desvLow);

            var phantom3High = new MathNet.Numerics.Distributions.Normal(padre.actual.phantom3.mediaHigh, padre.actual.phantom3.desvHigh);
            var phantom3Low = new MathNet.Numerics.Distributions.Normal(padre.actual.phantom3.mediaLow, padre.actual.phantom3.desvLow);

            // se prepara un vector de valores CT high y low para cada phantom
            // este vector representa un slide y solo se guarda un valor promedio del slide
            double[] temp = new double[padre.actual.datacuboHigh.dataCube[0].segCore.Count];
            MathNet.Numerics.Statistics.DescriptiveStatistics stats;

            // se preparan los vectores para densidad y zeff
            this.Dfm = new double[padre.actual.datacuboHigh.dataCube.Count];
            this.Zfme = new double[padre.actual.datacuboHigh.dataCube.Count];
            this.Pefm = new double[padre.actual.datacuboHigh.dataCube.Count];

            double ctP1High, ctP2High, ctP3High, ctP1Low, ctP2Low, ctP3Low;
            double A, B, C, D, E, F;

            List<double> Df, Zf, Zeff, Pef;

            int iarea;

            // se empieza a recorrer cada slide que se encuentre dentro de las areas de interes
            // para cada slide se genera una colección de datos aleatorios simulando cada phantom, y se estima la media para cada phantom en cada slide
            bool slide = false;
            for (int i = 0; i < padre.actual.datacuboHigh.dataCube.Count; i++)
            {
                slide = false;
                iarea = -1;

                for (int j = 0; j < padre.actual.areasCore.Count; j++)
                {
                    // se busca si este slide esta dentro de al menos un area de interes
                    if ((i >= padre.actual.areasCore[j].ini) & (i <= padre.actual.areasCore[j].fin))
                    {
                        slide = true;
                        iarea = j;
                    }
                }

                if (slide)
                {
                    // el slide pertenece al menos a un area de interes, por tanto se procede a calcular la densidad y zeff para este slide

                    // se generan los valores CT para cada phantom y se toma su media
                    phantom1High.Samples(temp);
                    stats = new MathNet.Numerics.Statistics.DescriptiveStatistics(temp);
                    ctP1High = stats.Mean;

                    phantom2High.Samples(temp);
                    stats = new MathNet.Numerics.Statistics.DescriptiveStatistics(temp);
                    ctP2High = stats.Mean;

                    phantom3High.Samples(temp);
                    stats = new MathNet.Numerics.Statistics.DescriptiveStatistics(temp);
                    ctP3High = stats.Mean;

                    phantom1Low.Samples(temp);
                    stats = new MathNet.Numerics.Statistics.DescriptiveStatistics(temp);
                    ctP1Low = stats.Mean;

                    phantom2Low.Samples(temp);
                    stats = new MathNet.Numerics.Statistics.DescriptiveStatistics(temp);
                    ctP2Low = stats.Mean;

                    phantom3Low.Samples(temp);
                    stats = new MathNet.Numerics.Statistics.DescriptiveStatistics(temp);
                    ctP3Low = stats.Mean;

                    // se resuelve el sistema lineal para obtener las constantes A,B,C,D,E,F
                    var matriz = MathNet.Numerics.LinearAlgebra.Matrix<double>.Build.DenseOfArray(new double[,] { { ctP1Low, ctP1High, 1 }, { ctP2Low, ctP2High, 1 }, { ctP3Low, ctP3High, 1 } });
                    var sol = MathNet.Numerics.LinearAlgebra.Vector<double>.Build.Dense(new double[] { padre.actual.phantom1.densidad, padre.actual.phantom2.densidad, padre.actual.phantom3.densidad });
                    var x = matriz.Solve(sol);

                    A = x[0];
                    B = x[1];
                    C = x[2];

                    sol = MathNet.Numerics.LinearAlgebra.Vector<double>.Build.Dense(new double[] { padre.actual.phantom1.zeff, padre.actual.phantom2.zeff, padre.actual.phantom3.zeff });
                    x = matriz.Solve(sol);

                    D = x[0];
                    E = x[1];
                    F = x[2];

                    // se empieza a recorrer cada voxel, en la segmentacion del actual i-slide, se revisa que este dentro del area de interes

                    Df = new List<double>();
                    Zf = new List<double>();
                    Zeff = new List<double>();
                    Pef = new List<double>();

                    int jkindex = 0;
                    double dx;
                    double dy;

                    double tDf, tZf, tZeff, tPef;

                    // dado que se recorre fila a fila, entonces el indice j corresponde al eje Y y el indice k al eje X
                    for (int j = 0; j < padre.actual.datacuboHigh.widthSeg; j++)
                    {
                        for (int k = 0; k < padre.actual.datacuboHigh.widthSeg; k++)
                        {
                            // se calcula la distancia de la posicion (j,k) al centro del area de interes
                            // si la distancia es menor que el radio entonces se agrega al calculo, sino no

                            dx = k - padre.actual.areasCore[iarea].x;
                            dx = dx * dx;
                            dy = j - padre.actual.areasCore[iarea].y;
                            dy = dy * dy;
                            if (Math.Sqrt(dx + dy) <= padre.actual.datacuboHigh.widthSeg)
                            {
                                // la coordenada (j,k) esta dentro del area de interes
                                // se calculan las propiedades estaticas

                                tDf = A * padre.actual.datacuboLow.dataCube[i].segCore[jkindex] + B * padre.actual.datacuboHigh.dataCube[i].segCore[jkindex] + C;
                                Df.Add(tDf);

                                tZf = D * padre.actual.datacuboLow.dataCube[i].segCore[jkindex] + E * padre.actual.datacuboHigh.dataCube[i].segCore[jkindex] + F;
                                Zf.Add(tZf);

                                //tZeff = Math.Pow(Math.Pow((tZf / (0.9342 * tDf + 0.1759)), 10), 1 / 36);
                                tZeff = Math.Pow((tZf / (0.9342 * tDf + 0.1759)), 1/3.6);
                                Zeff.Add(tZeff);

                                tPef = Math.Pow(Math.Pow((tZeff / 10), 36), 0.1);
                                Pef.Add(tPef);
                            }

                            jkindex++;
                        }
                    }

                    stats = new MathNet.Numerics.Statistics.DescriptiveStatistics(Df);
                    Dfm[i] = stats.Mean;

                    stats = new MathNet.Numerics.Statistics.DescriptiveStatistics(Zeff);
                    Zfme[i] = stats.Mean;

                    stats = new MathNet.Numerics.Statistics.DescriptiveStatistics(Pef);
                    Pefm[i] = stats.Mean;
                }
                else
                {
                    // se llenan los vectores de densidad y zeff con valores -1... si el valor es -1 entonces no se grafica
                    this.Dfm[i] = -1;
                    this.Zfme[i] = -1;
                    this.Pefm[i] = -1;
                }
            }

            DateTime fin = DateTime.Now;
        }
Example #6
0
        public static Vector <double> GetNormalizedVector(
            string subIndNormType, double startValue,
            bool scaleUp4Digits, double[] subIndicatorData)
        {
            //normalize them
            var             stats   = new MathNet.Numerics.Statistics.DescriptiveStatistics(subIndicatorData);
            Vector <double> siIndex = Vector <double> .Build.Dense(subIndicatorData);

            if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.none.ToString() ||
                string.IsNullOrEmpty(subIndNormType))
            {
                //data has already been normalized
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.zscore.ToString())
            {
                //z-score: (x – mean(x)) / stddev(x)
                for (int x = 0; x < siIndex.Count; x++)
                {
                    siIndex[x] = (siIndex[x] - stats.Mean) / stats.StandardDeviation;
                    if (scaleUp4Digits)
                    {
                        //scale the 4 digits by multiplying by 10,000
                        siIndex[x] = siIndex[x] * 10000.00;
                        siIndex[x] = Math.Round(siIndex[x], 2);
                    }
                    else
                    {
                        siIndex[x] = CalculatorHelpers.CheckForNaNandRound4(siIndex[x]);
                    }
                }
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.minmax.ToString())
            {
                //min-max: (x – min(x)) / (max(x) – min(x))
                for (int x = 0; x < siIndex.Count; x++)
                {
                    siIndex[x] = (siIndex[x] - stats.Minimum) / (stats.Maximum - stats.Minimum);
                    if (scaleUp4Digits)
                    {
                        //scale the 4 digits by multiplying by 10,000
                        siIndex[x] = siIndex[x] * 10000.00;
                        siIndex[x] = Math.Round(siIndex[x], 2);
                    }
                    else
                    {
                        siIndex[x] = CalculatorHelpers.CheckForNaNandRound4(siIndex[x]);
                    }
                }
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.logistic.ToString())
            {
                for (int x = 0; x < siIndex.Count; x++)
                {
                    //logistic: 1 / (1 + exp(-x))
                    siIndex[x] = MathNet.Numerics.SpecialFunctions.Logistic(siIndex[x]);
                    //or
                    //siIndex[x] = 1 / (1 + Math.Exp(-siIndex[x]));
                    if (scaleUp4Digits)
                    {
                        //scale the 4 digits by multiplying by 10,000
                        siIndex[x] = siIndex[x] * 10000.00;
                        siIndex[x] = Math.Round(siIndex[x], 2);
                    }
                    else
                    {
                        siIndex[x] = CalculatorHelpers.CheckForNaNandRound4(siIndex[x]);
                    }
                }
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.logit.ToString())
            {
                for (int x = 0; x < siIndex.Count; x++)
                {
                    //logit: inverese of logistic for y between 0 and 1
                    //this assumes x is actually y
                    siIndex[x] = MathNet.Numerics.SpecialFunctions.Logit(siIndex[x]);
                    if (scaleUp4Digits)
                    {
                        //scale the 4 digits by multiplying by 10,000
                        siIndex[x] = siIndex[x] * 10000.00;
                        siIndex[x] = Math.Round(siIndex[x], 2);
                    }
                    else
                    {
                        siIndex[x] = CalculatorHelpers.CheckForNaNandRound4(siIndex[x]);
                    }
                }
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.tanh.ToString())
            {
                for (int x = 0; x < siIndex.Count; x++)
                {
                    //hyperbolic tangent
                    siIndex[x] = MathNet.Numerics.Trig.Tanh(siIndex[x]);
                    if (scaleUp4Digits)
                    {
                        //scale the 4 digits by multiplying by 10,000
                        siIndex[x] = siIndex[x] * 10000.00;
                        siIndex[x] = Math.Round(siIndex[x], 2);
                    }
                    else
                    {
                        siIndex[x] = CalculatorHelpers.CheckForNaNandRound4(siIndex[x]);
                    }
                }
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.pnorm.ToString())
            {
                //p value for ttest with n-1
                double pValue = Shared.GetPValueForTDist(siIndex.Count() - 1,
                                                         startValue, stats.Mean, stats.Variance);
                pValue = CalculatorHelpers.CheckForNaNandRound4(pValue);
                //p norm
                siIndex = siIndex.Normalize(pValue);
                if (scaleUp4Digits)
                {
                    //scale the 4 digits by multiplying by 10,000
                    for (int x = 0; x < siIndex.Count; x++)
                    {
                        siIndex[x] = siIndex[x] * 10000.00;
                        siIndex[x] = Math.Round(siIndex[x], 2);
                    }
                }
            }
            else if (subIndNormType == CalculatorHelpers.NORMALIZATION_TYPES.weights.ToString())
            {
                for (int x = 0; x < siIndex.Count; x++)
                {
                    //rand 2016 technique
                    siIndex[x] = siIndex[x] / startValue;
                    if (scaleUp4Digits)
                    {
                        //scale the 4 digits by multiplying by 10,000
                        siIndex[x] = siIndex[x] * 10000.00;
                        siIndex[x] = Math.Round(siIndex[x], 2);
                    }
                    else
                    {
                        siIndex[x] = CalculatorHelpers.CheckForNaNandRound4(siIndex[x]);
                    }
                }
            }
            else
            {
                //indicator 2 in drr1 (p and q, not norm and index)
            }
            //add them to parent cat index
            return(siIndex);
        }
Example #7
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);
        }
Example #8
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);
        }
Example #9
0
        public DataTable_DescStats(DataTable dt)
        {
            this.Columns.Add("Varname", typeof(string));
            this.Columns.Add("N", typeof(int));
            this.Columns.Add("Nunq", typeof(int));
            this.Columns.Add("M", typeof(double));
            this.Columns.Add("SD", typeof(double));
            this.Columns.Add("Min", typeof(double));
            this.Columns.Add("Max", typeof(double));
            this.Columns.Add("Median", typeof(double));
            this.Columns.Add("Mode", typeof(double));
            this.Columns.Add("Pctile25", typeof(double));
            this.Columns.Add("Pctile75", typeof(double));
            this.Columns.Add("IQR", typeof(double));
            this.Columns.Add("N_GT1pt5IQR", typeof(int));
            this.Columns.Add("N_LT1pt5IQR", typeof(int));
            this.Columns.Add("MinZ", typeof(double));
            this.Columns.Add("MaxZ", typeof(double));
            this.Columns.Add("Skewness", typeof(double));
            this.Columns.Add("Kurtosis", typeof(double));
            this.Columns.Add("Sum", typeof(double));


            //foreach(DataColumn c in this.Columns)
            //{
            //    c.AllowDBNull = true;
            //}

            foreach (DataColumn col in dt.Columns)
            {
                string Varname     = "";
                int?   N           = 0;
                int?   Nunq        = 0;
                double?M           = null;
                double?SD          = null;
                double?Min         = null;
                double?Max         = null;
                double?Median      = null;
                double?Mode        = null;
                double?Pctile25    = null;
                double?Pctile75    = null;
                double?IQR         = null;
                int?   N_GT1pt5IQR = null;
                int?   N_LT1pt5IQR = null;
                double?MinZ        = null;
                double?MaxZ        = null;
                //double? Skewness = null;
                //double? Kurtosis = null;
                double?Sum = null;

                List <double> data = new List <double>();

                List <string> vars_to_skip = new List <string> {
                    "indexnum", "studymeasID", "isREL", "timepointnum", "days_from_txstart"
                };
                if (col.IsNumeric() & !vars_to_skip.Contains(col.ColumnName))
                {
                    Varname = col.ColumnName;

                    //Convert to double and get rid of DBNull values
                    var origdata = dt.AsEnumerable().Select(x => (x[col.ColumnName] == DBNull.Value) ? -998877 : x[col.ColumnName]).ToList();
                    data = origdata.ConvertAll(x => Convert.ToDouble(x)).Where(x => x != -998877).ToList();

                    //N = data.Count();
                    Nunq = data.Distinct().Count();
                    Sum  = data.Sum();

                    MathNet.Numerics.Statistics.DescriptiveStatistics myDS = new MathNet.Numerics.Statistics.DescriptiveStatistics(data);

                    Median   = MathNet.Numerics.Statistics.Statistics.Median(data);
                    Mode     = data.Mode(); //using LinqStatistics for mode
                    IQR      = MathNet.Numerics.Statistics.Statistics.InterquartileRange(data);
                    Pctile25 = MathNet.Numerics.Statistics.Statistics.LowerQuartile(data);
                    Pctile75 = MathNet.Numerics.Statistics.Statistics.UpperQuartile(data);
                    if (Median > 0 & IQR > 0)
                    {
                        N_LT1pt5IQR = data.Where(x => x < Pctile25 - IQR).Count();
                        N_GT1pt5IQR = data.Where(x => x > Pctile75 + IQR).Count();
                    }

                    MinZ = (myDS.Minimum - myDS.Mean) / myDS.StandardDeviation;
                    MaxZ = (myDS.Maximum - myDS.Mean) / myDS.StandardDeviation;

                    if (myDS.Count == 0)
                    {
                        this.Rows.Add(Varname
                                      , myDS.Count, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
                    }
                    else if (Nunq == 1)
                    {
                        this.Rows.Add(Varname
                                      , myDS.Count, Nunq, myDS.Mean, null, null, null, null, null, null, null, null, null, null, null, null);
                    }
                    else
                    {
                        this.Rows.Add(Varname
                                      , myDS.Count
                                      , Nunq
                                      , myDS.Mean
                                      , myDS.StandardDeviation
                                      , myDS.Minimum
                                      , myDS.Maximum
                                      , Median
                                      , Mode
                                      , Pctile25
                                      , Pctile75
                                      , IQR
                                      , N_LT1pt5IQR
                                      , N_GT1pt5IQR
                                      , MinZ
                                      , MaxZ
                                      , myDS.Skewness
                                      , myDS.Kurtosis
                                      , Sum);
                    }



                    Debug.Print(String.Format("N={0} Nunq={1} M={2} SD={3} Min={4} Max={5}   {6}", N, Nunq, M, SD, Min, Max, col.ColumnName));
                }
            }
        }