예제 #1
0
        /// <summary>
        /// Classify our data using Logistic Regression classifer and save the model.
        /// </summary>
        /// <param name="train_data">Frame objects that we will use to train classifers.</param>
        /// <param name="test_data">Frame objects that we will use to test classifers.</param>
        /// <param name="train_label">Labels of the train data.</param>
        /// <param name="test_label">Labels of the test data.</param>
        /// <param name="Classifier_Path">Path where we want to save the classifer on the disk.</param>
        /// <param name="Classifier_Name">Name of the classifer we wnat to save.</param>
        /// <returns></returns>
        public void LogisticRegression(double[][] train_data, double[][] test_data, int[] train_label, int[] test_label, String Classifier_Path, String Classifier_Name)
        {
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-4,
                MaxIterations  = 100,
                Regularization = 0
            };

            LogisticRegression regression = learner.Learn(train_data, train_label);

            double ageOdds   = regression.GetOddsRatio(0);
            double smokeOdds = regression.GetOddsRatio(1);

            double[] scores = regression.Probability(test_data);

            //bool[] pre = regression.Decide(test_data);

            var cm = GeneralConfusionMatrix.Estimate(regression, test_data, test_label);

            double error = cm.Error;

            Console.WriteLine(error);

            regression.Save(Path.Combine(Classifier_Path, Classifier_Name));
        }
예제 #2
0
        static public int [] IterativeLeastSquares(double[][] input1, int[] output1, string fName)
        {
            double[] labels  = System.Array.ConvertAll <int, double>(output1, x => x);
            var      learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                // Gets or sets the tolerance value used to determine whether the algorithm has converged.
                Tolerance     = 1e-4, // Let's set some convergence parameters
                MaxIterations = 10,
                //MaxIterations = 100,  // maximum number of iterations to perform
                Regularization = 0
            };

            // Now, we can use the learner to finally estimate our model:
            LogisticRegression regression = learner.Learn(input1, output1);

            double [] coefficients = learner.Solution;

            double[] scores = regression.Probability(input1);

            regression.Save(fName.Replace(".csv", ".IRLS.save"), compression: SerializerCompression.None);

            // Finally, if we would like to arrive at a conclusion regarding
            // each sample, we can use the Decide method, which will transform
            // the probabilities (from 0 to 1) into actual true/false values:

            return(Funcs.Utility.BoolToInt(regression.Decide(input1)));

            // mean(double(p == y)) * 100);
        }
예제 #3
0
        public double regressionProbability()
        {
            double probablity = regression.Probability(new double[] { lam.Age, lam.Gender, lam.Total_Bilirubin, lam.Direct_Bilirubin
                                                                      , lam.Alkaline_Phosphotase, lam.Alamine_Aminotransferase
                                                                      , lam.Aspartate_Aminotransferase, lam.Total_Protiens
                                                                      , lam.Albumin, lam.Albumin_and_Globulin_Ratio });

            return(probablity);
        }
예제 #4
0
        /// <summary>
        /// Evaluates a model by calculating it's loss from a set.
        /// </summary>
        /// <returns>An array of measurements. (SME, False Positive Rate, Accuracy)</returns>
        /// <param name="crunchingScores">The scores for crunching studios.</param>
        /// <param name="nonCrunchingScores">The scores for non-crunching studios.</param>
        /// <param name="modelName">The file name of the model.</param>
        public static float[] EvaluateModel(List <KeyValuePair <string, float[]> > crunchingScores, List <KeyValuePair <string, float[]> > nonCrunchingScores, string modelName)
        {
            //Get the model file and deserialize the weights.
            if (!File.Exists("Logistic Regression Model/models/" + modelName + ".txt"))
            {
                throw new Exception("Model doesn't exist. Check your spelling and try again.");
            }

            string line = File.ReadAllLines("Logistic Regression Model/models/" + modelName + ".txt")[0];

            double[] weights = Extensions.Extensions.LoadWeights(line);

            //Create a regression model.
            LogisticRegression regression = new LogisticRegression();

            regression.Weights   = weights.Take(weights.Length - 1).ToArray();
            regression.Intercept = weights[weights.Length - 1];

            //Get the actual and expected values from the scores.
            double[] actual   = new double[crunchingScores.Count + nonCrunchingScores.Count];
            double[] expected = new double[crunchingScores.Count + nonCrunchingScores.Count];

            int[] actualClass   = new int[crunchingScores.Count + nonCrunchingScores.Count];
            int[] expectedClass = new int[crunchingScores.Count + nonCrunchingScores.Count];

            //Get expected and actual values for crunching studios.
            for (int a = 0; a < crunchingScores.Count; a++)
            {
                actual[a]   = regression.Probability(crunchingScores[a].Value.Select((arg) => (double)arg).ToArray());
                expected[a] = 1;

                actualClass[a]   = (actual[a] > 0.5f) ? 1 : 0;
                expectedClass[a] = 1;
            }

            //Get expected and actual values for non-crunching studios.
            for (int b = 0; b < nonCrunchingScores.Count; b++)
            {
                actual[b + crunchingScores.Count]   = regression.Probability(nonCrunchingScores[b].Value.Select((arg) => (double)arg).ToArray());
                expected[b + crunchingScores.Count] = 0;

                actualClass[b + crunchingScores.Count]   = (actual[b + crunchingScores.Count] > 0.5f) ? 1 : 0;
                expectedClass[b + crunchingScores.Count] = 0;
            }

            //Calculate and return the loss.
            float loss = (float)new SquareLoss(expected)
            {
                Mean = true,
                Root = true
            }.Loss(actual);

            //Get R-squared loss
            float r2 = (float)new RSquaredLoss(crunchingScores.First().Value.Length, expected).Loss(actual);

            //Calculate confusion matrix
            ConfusionMatrix gcm = new ConfusionMatrix(actualClass.ToBoolArray(), expectedClass.ToBoolArray());

            //Calculate false negative and positive rates
            float falsePR = (float)gcm.FalsePositiveRate;

            //Calculate overall accuracy
            float accuracy = (float)gcm.Accuracy;

            //Calculate the high confidence rate
            //(how many are correct and have a probability within 0.2 of either extreme)
            int correctHighConfidence = 0, highConfidence = 0;

            for (int a = 0; a < actual.Length; a++)
            {
                //If it's in the range of 0 - 0.2 or 0.8 - 1, it's considered 'high confidence'
                if (Math.Abs(actual[a] - 0.5f) > 0.3f)
                {
                    //Is it correct?
                    if (actualClass[a] == expectedClass[a])
                    {
                        correctHighConfidence++;
                    }

                    highConfidence++;
                }
            }

            float correctHC = ((float)correctHighConfidence) / highConfidence;

            return(new float[] { loss, r2, falsePR, accuracy, correctHC });
        }
예제 #5
0
        /// <summary>
        /// Uses data from <paramref name="fileName">fileName</paramref> to train a logistic regression model./>
        /// </summary>
        /// <param name="fileName">The name of the data file.</param>
        /// <returns>A string to print giving information about the weights and odds ratios.</returns>
        public static string Learn(string fileName)
        {
            //Read all inputs and outputs from training file.
            string[]   lines   = File.ReadAllLines("Logistic Regression Model/data/" + fileName + ".txt");
            double[][] inputs  = new double[lines.Length][];
            int[]      outputs = new int[lines.Length];

            for (int a = 0; a < lines.Length; a++)
            {
                string[] split = lines[a].Split(':');

                //Dynamically get variables from file.
                string[] scores = split[1].Split('&');
                inputs[a] = new double[scores.Length];
                for (int b = 0; b < scores.Length; b++)
                {
                    inputs[a][b] = double.Parse(scores[b]);
                }

                outputs[a] = int.Parse(split[2]);
            }

            //Set up Accord.NET learner.
            IterativeReweightedLeastSquares <LogisticRegression> learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-4,
                MaxIterations  = 100,
                Regularization = 1e-10
            };

            //Shuffle the input and output pairs to eliminate some inherent bias from
            //training data.
            Dictionary <double[], int> map = inputs.Zip(outputs, (arg1, arg2) => new { arg1, arg2 }).ToDictionary(x => x.arg1, x => x.arg2);

            map.Shuffle();
            inputs  = map.Keys.ToArray();
            outputs = map.Values.ToArray();


            //Train Regression
            LogisticRegression regression = learner.Learn(inputs, outputs.ToBoolArray());



            //Save to a Model file.
            int counter = 0;

            while (File.Exists("Logistic Regression Model/models/Model-" + counter + ".txt"))
            {
                counter++;
            }

            //Create a file writer
            FileStream   fs     = File.Create("Logistic Regression Model/models/Model-" + counter + ".txt");
            StreamWriter writer = new StreamWriter(fs);

            //Print the weights
            string result = "Weights: " + regression.Weights.GetString() + "\n";

            //Write lines.
            writer.WriteLine(regression.Weights.Append(regression.Intercept).ToArray().GetString());
            for (int c = 0; c < regression.Weights.Length; c++)
            {
                writer.WriteLine(regression.GetOddsRatio(c));
                result += "Odds Ratio " + c + ": " + regression.GetOddsRatio(c) + "\n";
            }

            //Get Loss values.
            double[] actual   = new double[inputs.Length];
            double[] expected = new double[outputs.Length];
            for (int a = 0; a < actual.Length; a++)
            {
                actual[a]   = regression.Probability(inputs[a]);
                expected[a] = outputs[a];
            }

            //Calculate and print square loss.
            string loss = "Loss: " + new SquareLoss(expected)
            {
                Mean = true,
                Root = true
            }.Loss(actual);

            result += loss + "\n";
            writer.WriteLine(loss);

            Console.WriteLine("\n\n" + loss);


            //Calculate and print R-squared Loss
            string r2 = "R2: " + new RSquaredLoss(inputs[0].Length, expected).Loss(actual);

            result += r2;
            writer.WriteLine(r2);

            //Cleanup
            writer.Close();
            writer.Dispose();

            fs.Close();
            fs.Dispose();

            Console.WriteLine("Model trained successfully!");
            Console.WriteLine("\nEvaluating...\n");

            //Get the VIFs
            float[] VIFs = CalculateVIFs(inputs);

            //Log it
            for (int a = 0; a < VIFs.Length; a++)
            {
                Logger.Log("Variance Inflation Factor #" + a + ": " + VIFs[a]);
            }

            return(result);
        }
예제 #6
0
        private void GetResultButton_Click(object sender, EventArgs e)
        {
            Settings.Default["AlgorName"] = label3.Text;
            ResultBox.Items.Clear();
            if (label3.Text == "Statics")
            {
                if (!(PF is Istatics))
                {
                    MessageBox.Show("Production Facade doesn't have this interface yet");
                    return;
                }
                Settings.Default["Parameter"] = textBox1.Text;
                int    numOfParmas = PF.GetData()[0].GetNumOfParams();
                double mean, sd;
                for (int i = 0; i < numOfParmas; i++)
                {
                    mean = PF.GetMean(i);
                    ResultBox.Items.Add("Mean of param " + i + ": " + mean);
                    sd = PF.GetStandardDeviation(i);
                    ResultBox.Items.Add("StandardDeviation of param " + i + ": " + sd);
                    if (textBox1.Text != "")
                    {
                        double number    = Double.Parse(textBox1.Text);
                        double Threshold = PF.GetThreshold(number);
                        ResultBox.Items.Add("Threshold of param " + i + ": " + Threshold);
                        ResultBox.Items.Add("");
                    }
                }
            }
            else if (label3.Text == "Peak")
            {
                if (!(PF is Ipeek))
                {
                    MessageBox.Show("Production Facade doesn't have this interface yet");
                    return;
                }
                if (XaxiscomboBox.Text == "" && YaxiscomboBox.Text == "")
                {
                    return;
                }
                sortData();
                Settings.Default["NumOfPoints"] = numBox.Text;
                Settings.Default["Range"]       = rangeBox.Text;
                if (YaxiscomboBox.Text.Contains("#"))
                {
                    string text        = YaxiscomboBox.Text.Substring(10).Trim();
                    int    num         = Int32.Parse(text);
                    int    numOfpoints = 3;
                    double percentage  = 0.2;
                    if (numBox.Text != "")
                    {
                        numOfpoints = Int32.Parse(numBox.Text);
                    }
                    if (rangeBox.Text != "")
                    {
                        percentage = double.Parse(rangeBox.Text);
                    }
                    dataDict = PF.CalculateTimesEachX(num);
                    List <PeekValleyData> peaks = PF.GetPeaksWithNumOfSamePoints(num, numOfpoints, percentage);
                    peaksData = peaks;

                    /*
                     * for (int i = 0; i < PF.GetData().Length; i++)
                     * {
                     *  ResultBox.Items.Add(PF.GetData()[i].WriteToLine());
                     * }*/
                    if (peaksData.Count() == 0)
                    {
                        ResultBox.Items.Add("No Peak found yet.");
                    }
                    foreach (var item in peaks)
                    {
                        ResultBox.Items.Add("Peak Value: " + item.value + "   Times: " + item.times);
                    }
                }
                else if (YaxiscomboBox.Text != "")
                {
                    string text = YaxiscomboBox.Text.Substring(5).Trim();

                    int Yindex = Int32.Parse(text);
                    // MessageBox.Show(Yindex.ToString());
                    int    numOfpoints = 3;
                    double percentage  = 0.2;
                    if (numBox.Text != "")
                    {
                        numOfpoints = Int32.Parse(numBox.Text);
                    }
                    if (rangeBox.Text != "")
                    {
                        percentage = double.Parse(rangeBox.Text);
                    }
                    text = XaxiscomboBox.Text.Substring(5).Trim();
                    int Xindex;
                    if (XaxiscomboBox.Text.Contains("TimeSpan"))
                    {
                        Xindex = -1;
                    }
                    else
                    {
                        Xindex = Int32.Parse(text);
                    }
                    MyData[] MD = PF.GetData();
                    dataPoints = new points[MD.Count()];
                    for (int i = 0; i < MD.Count(); i++)
                    {
                        dataPoints[i].x = MD[i].GetParameters()[Xindex];
                        dataPoints[i].y = MD[i].GetParameters()[Yindex];
                    }
                    List <PeekValleyData> peaks = PF.GetPeaksWithXY(Yindex, Xindex, numOfpoints, percentage);
                    peaksData = peaks;

                    /*
                     * for (int i = 0; i < PF.GetData().Length; i++)
                     * {
                     *  ResultBox.Items.Add(PF.GetData()[i].WriteToLine());
                     * }
                     * */
                    foreach (var item in peaks)
                    {
                        ResultBox.Items.Add("X: " + item.x + "   Y: " + item.y);
                    }
                }
            }
            else if (label3.Text == "Polynomial Fit")
            {
                if (!(PF is Ipolyfit))
                {
                    MessageBox.Show("Production Facade doesn't have this interface yet");
                    return;
                }
                if (XaxiscomboBox.Text == "" && YaxiscomboBox.Text == "")
                {
                    return;
                }
                sortData();
                int power = 1;
                if (powerBox.Text != "")
                {
                    power = Int32.Parse(powerBox.Text);
                }
                if (YaxiscomboBox.Text.Contains("#"))
                {
                    string text = YaxiscomboBox.Text.Substring(10).Trim();
                    int    num  = Int32.Parse(text);
                    Dictionary <double, int> dict = PF.CalculateTimesEachX(num);
                    dataDict    = dict;
                    double[,] x = new double[dict.Keys.Count(), power];
                    double[] keys = dict.Keys.ToArray();
                    for (int i = 0; i < dict.Keys.Count(); i++)
                    {
                        for (int j = 0; j < power; j++)
                        {
                            x[i, j] = Math.Pow(keys[i], j + 1);
                        }
                    }
                    double[] y = new double[keys.Length];
                    for (int i = 0; i < keys.Length; i++)
                    {
                        y[i] = dict[keys[i]];
                    }
                    double[,] result = PF.GetPolyFit(x, y, power);
                    paramsResult     = result;
                    string r = "y=";
                    r = r + result[0, 0].ToString("#.000");
                    for (int i = 1; i <= power; i++)
                    {
                        r = r + "+ " + result[i, 0].ToString("#.000") + "x^" + i.ToString();
                    }
                    ResultBox.Items.Add(r);
                }
                else if (YaxiscomboBox.Text != "")
                {
                    string text   = YaxiscomboBox.Text.Substring(5).Trim();
                    int    Yindex = Int32.Parse(text);
                    int    Xindex;
                    if (XaxiscomboBox.Text.Contains("TimeSpan"))
                    {
                        MessageBox.Show("We have not support Timespan in this algorithm yet.");
                        return;
                    }
                    else
                    {
                        text   = XaxiscomboBox.Text.Substring(5).Trim();
                        Xindex = Int32.Parse(text);
                    }
                    MyData[] MD = PF.GetData();
                    dataPoints  = new points[MD.Count()];
                    double[,] x = new double[MD.Count(), power];
                    for (int i = 0; i < MD.Count(); i++)
                    {
                        dataPoints[i].x = MD[i].GetParameters()[Xindex];
                        for (int j = 0; j < power; j++)
                        {
                            x[i, j] = Math.Pow(MD[i].GetParameters()[Xindex], j + 1);
                        }
                    }
                    double[] y = new double[MD.Count()];
                    for (int i = 0; i < y.Length; i++)
                    {
                        y[i]            = MD[i].GetParameters()[Yindex];
                        dataPoints[i].y = y[i];
                    }
                    double[,] result = PF.GetPolyFit(x, y, power);
                    paramsResult     = result;
                    string r = "y=";
                    r = r + result[0, 0].ToString("#.000");
                    for (int i = 1; i <= power; i++)
                    {
                        r = r + "+ " + result[i, 0].ToString("#.000") + "x^" + i.ToString();
                    }
                    ResultBox.Items.Add(r);
                }
                if (xvalueBox.Text != "")
                {
                    double x = Convert.ToDouble(xvalueBox.Text);
                    double y = 0;
                    for (int j = 0; j < paramsResult.GetLength(0); j++)
                    {
                        y = y + Math.Pow(x, j) * paramsResult[j, 0];
                    }
                    ResultBox.Items.Add("X-value: " + x.ToString("#.000") + " Y-value: " + y.ToString("#.000"));
                    Settings.Default["Xvalue"] = xvalueBox.Text;
                }
                Settings.Default["Power"] = powerBox.Text;
            }
            else if (label3.Text == "Normal Distribution Fit")
            {
                if (!(PF is INormalDistributionFit))
                {
                    MessageBox.Show("Production Facade doesn't have this interface yet");
                    return;
                }
                if (XaxiscomboBox.Text == "" && YaxiscomboBox.Text == "")
                {
                    return;
                }
                sortData();

                /*
                 * for (int i = 0; i < PF.GetData().Length; i++)
                 * {
                 *  ResultBox.Items.Add(PF.GetData()[i].WriteToLine());
                 * }*/
                if (YaxiscomboBox.Text.Contains("#"))
                {
                    string text = YaxiscomboBox.Text.Substring(10).Trim();
                    int    num  = Int32.Parse(text);
                    dataDict = PF.CalculateTimesEachX(num);
                    NormalDistributionInfo ndi = PF.GetNormalDistributionFitWithNumOfSamePoints(num);
                    NDinfo = ndi;
                    ResultBox.Items.Add("Mean: " + ndi.mean.ToString() + " SD: " + ndi.SD.ToString());
                }
                else
                {
                    if (XaxiscomboBox.Text == "" || YaxiscomboBox.Text == "")
                    {
                        return;
                    }
                    string text   = YaxiscomboBox.Text.Substring(5).Trim();
                    int    Yindex = Int32.Parse(text);
                    int    Xindex;
                    if (XaxiscomboBox.Text.Contains("TimeSpan"))
                    {
                        MessageBox.Show("We have not support Timespan in this algorithm yet.");
                        return;
                    }
                    else
                    {
                        text   = XaxiscomboBox.Text.Substring(5).Trim();
                        Xindex = Int32.Parse(text);
                    }
                    MyData[] MD = PF.GetData();
                    dataPoints = new points[MD.Count()];
                    for (int i = 0; i < MD.Count(); i++)
                    {
                        dataPoints[i].x = MD[i].GetParameters()[Xindex];
                        dataPoints[i].y = MD[i].GetParameters()[Yindex];
                    }
                    int num = Int32.Parse(text);
                    dataDict = PF.CalculateTimesEachX(num);
                    NormalDistributionInfo ndi = PF.GetNormalDistributionFitWithNumOfSamePoints(num);
                    NDinfo = ndi;
                    ResultBox.Items.Add("Mean: " + ndi.mean.ToString() + " SD: " + ndi.SD.ToString());
                }
                if (xvalueBox.Text != "")
                {
                    double x = Convert.ToDouble(xvalueBox.Text);
                    double y = 0;
                    y = (1 / (NDinfo.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((x - NDinfo.mean), 2) / (2 * Math.Pow(NDinfo.SD, 2)));
                    int numberOfdata = 0;
                    foreach (var item in dataDict)
                    {
                        numberOfdata = numberOfdata + item.Value;
                    }
                    y = y * numberOfdata;
                    ResultBox.Items.Add("X-value: " + x.ToString("#.000") + " Y-value: " + y.ToString("#.000"));
                    Settings.Default["Xvalue"] = xvalueBox.Text;
                }
            }
            else if (label3.Text == "Logistic Regression")
            {
                if (!(PF is ILogisticRegression))
                {
                    MessageBox.Show("Production Facade doesn't have this interface yet");
                    return;
                }
                if (comboBox2.Text != "")
                {
                    string text  = comboBox2.Text.Substring(5).Trim();
                    int    index = Int32.Parse(text);
                    dataDict = PF.CalculateTimesEachX(index);
                    double percentage = 0.1;
                    PF.SortData(index);
                    if (rangeBox.Text != "")
                    {
                        percentage = double.Parse(rangeBox.Text);
                    }
                    LogisticRegression LR = PF.GetLogisticRegressionParams(index, percentage);
                    LogisticInfo       LI = new LogisticInfo();
                    LI.LogisticParams    = new double[2];
                    LI.LogisticParams[0] = LR.Intercept;
                    LI.LogisticParams[1] = LR.GetOddsRatio(1) - 1;
                    ResultBox.Items.Add("Param0: " + LI.LogisticParams[0] + " Parma1: " + LI.LogisticParams[1]);
                    if (xvalueBox.Text != "")
                    {
                        double   x        = Convert.ToDouble(xvalueBox.Text);
                        double[] valueArr = new double[] { x, 0 };
                        ResultBox.Items.Add("Value: " + x + " Probability: " + LR.Probability(valueArr) + " Conclusion:" + LR.Decide(valueArr));
                    }
                    MyData[] MD        = PF.GetData();
                    double   threshold = -1;

                    /*
                     * for (int i = 0; i < MD.Length; i++)
                     * {
                     *  double value = MD[i].GetParameters()[index];
                     *  double[] valueArr = new double[] { value, 0 };
                     *  ResultBox.Items.Add("Value: " + value + " Probability: " + LR.Probability(valueArr) + " Conclusion:" + LR.Decide(valueArr));
                     * }*/
                    Dictionary <double, int> lowerDict  = new Dictionary <double, int>();
                    Dictionary <double, int> higherDict = new Dictionary <double, int>();
                    for (int i = 0; i < MD.Length; i++)
                    {
                        double   value    = MD[i].GetParameters()[index];
                        double[] valueArr = new double[] { value, 0 };
                        if (threshold == -1 && LR.Decide(valueArr) == true)
                        {
                            threshold = value;
                        }
                        if (threshold == -1)
                        {
                            if (lowerDict.ContainsKey(value))
                            {
                                lowerDict[value]++;
                            }
                            else
                            {
                                lowerDict.Add(value, 1);
                            }
                        }
                        else
                        {
                            if (higherDict.ContainsKey(value))
                            {
                                higherDict[value]++;
                            }
                            else
                            {
                                higherDict.Add(value, 1);
                            }
                        }
                    }
                    ResultBox.Items.Add("Threshold: " + threshold);
                }
            }
            else if (label3.Text == "Two Peaks")
            {
                if (!(PF is Ipeek))
                {
                    MessageBox.Show("Production Facade doesn't have this interface yet");
                    return;
                }
                if (peak1Box.Text != "" && peak2Box.Text != "")
                {
                    string text        = YaxiscomboBox.Text.Substring(10).Trim();
                    int    index       = Int32.Parse(text);
                    double peak1       = Convert.ToDouble(peak1Box.Text);
                    double peak2       = Convert.ToDouble(peak2Box.Text);
                    int    numOfpoints = 5;
                    if (numBox.Text != "")
                    {
                        numOfpoints = Int32.Parse(numBox.Text);
                    }
                    NormalDistributionInfo peak1Info = PF.GetPeakNormalDistribution(peak1, index, numOfpoints);
                    ResultBox.Items.Add("Mean: " + peak1Info.mean.ToString("#.000") + " SD: " + peak1Info.SD.ToString("#.000"));

                    NormalDistributionInfo peak2Info = PF.GetPeakNormalDistribution(peak2, index, numOfpoints);
                    ResultBox.Items.Add("Mean: " + peak2Info.mean.ToString("#.000") + " SD: " + peak2Info.SD.ToString("#.000"));

                    if (xvalueBox.Text != "")
                    {
                        double x   = Convert.ToDouble(xvalueBox.Text);
                        double end = 0;
                        double y   = (1 / (peak1Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((x - peak1Info.mean), 2) / (2 * Math.Pow(peak1Info.SD, 2)));
                        if (y < 0.01)
                        {
                            double x1 = x - 1;
                            double y1 = (1 / (peak1Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((x1 - peak1Info.mean), 2) / (2 * Math.Pow(peak1Info.SD, 2)));
                            if (y1 < y)
                            {
                                end = x1;
                            }
                            else
                            {
                                end = x + 1;
                            }
                        }
                        else
                        {
                            double x1 = x - 1;
                            double y1 = (1 / (peak1Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((x1 - peak1Info.mean), 2) / (2 * Math.Pow(peak1Info.SD, 2)));
                            if (y1 < y)
                            {
                                end = x1;
                                while (y1 > 0.01)
                                {
                                    end--;
                                    y1 = (1 / (peak1Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((end - peak1Info.mean), 2) / (2 * Math.Pow(peak1Info.SD, 2)));
                                }
                            }
                            else
                            {
                                end = x + 1;
                                y1  = (1 / (peak1Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((end - peak1Info.mean), 2) / (2 * Math.Pow(peak1Info.SD, 2)));
                                while (y1 > 0.01)
                                {
                                    end++;
                                    y1 = (1 / (peak1Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((end - peak1Info.mean), 2) / (2 * Math.Pow(peak1Info.SD, 2)));
                                }
                            }
                        }
                        Func <double, double> f1 = (a) => (1 / (peak1Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((a - peak1Info.mean), 2) / (2 * Math.Pow(peak1Info.SD, 2)));
                        double result1           = MathNet.Numerics.Integration.NewtonCotesTrapeziumRule.IntegrateTwoPoint(f1, x, end);
                        if (x <= peak1)
                        {
                            result1 = 1 - Math.Abs(result1);
                            if (result1 > 1)
                            {
                                result1 = 1;
                            }
                        }
                        ResultBox.Items.Add("Probablity of keeping bad chips: " + Math.Abs(result1).ToString("#.000"));
                        double end1 = 0;
                        y = (1 / (peak2Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((x - peak2Info.mean), 2) / (2 * Math.Pow(peak2Info.SD, 2)));
                        if (y < 0.01)
                        {
                            double x1 = x - 1;
                            double y1 = (1 / (peak2Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((x1 - peak2Info.mean), 2) / (2 * Math.Pow(peak2Info.SD, 2)));
                            if (y1 < y)
                            {
                                end1 = x1;
                            }
                            else
                            {
                                end1 = x + 1;
                            }
                        }
                        else
                        {
                            double x1 = x - 1;
                            double y1 = (1 / (peak2Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((x1 - peak2Info.mean), 2) / (2 * Math.Pow(peak2Info.SD, 2)));
                            if (y1 < y)
                            {
                                end1 = x1;
                                while (y1 > 0.01)
                                {
                                    end1--;
                                    y1 = (1 / (peak2Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((end1 - peak2Info.mean), 2) / (2 * Math.Pow(peak2Info.SD, 2)));
                                }
                            }
                            else
                            {
                                end1 = x + 1;
                                y1   = (1 / (peak2Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((end1 - peak2Info.mean), 2) / (2 * Math.Pow(peak2Info.SD, 2)));
                                while (y1 > 0.01)
                                {
                                    end1++;
                                    y1 = (1 / (peak2Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((end1 - peak2Info.mean), 2) / (2 * Math.Pow(peak2Info.SD, 2)));
                                }
                            }
                        }
                        Func <double, double> f2 = (a) => (1 / (peak2Info.SD * Math.Sqrt(2 * Math.PI))) * Math.Exp(-Math.Pow((a - peak2Info.mean), 2) / (2 * Math.Pow(peak2Info.SD, 2)));
                        double result2           = MathNet.Numerics.Integration.NewtonCotesTrapeziumRule.IntegrateTwoPoint(f2, x, end1);
                        if (x >= peak2)
                        {
                            result2 = 1 - Math.Abs(result2);
                            if (result2 > 1)
                            {
                                result2 = 1;
                            }
                        }
                        ResultBox.Items.Add("probability of losing good chips: " + Math.Abs(result2).ToString("#.000"));
                    }
                    dataDict = PF.CalculateTimesEachX(index);



                    TDinfo            = new TwoDictInfo();
                    TDinfo.peak1Info  = peak1Info;
                    TDinfo.peak2Info  = peak2Info;
                    TDinfo.peak1value = peak1;
                    TDinfo.peak2value = peak2;
                    TDinfo.peak1times = dataDict[peak1];
                    TDinfo.peak2times = dataDict[peak2];
                    TDinfo.last       = dataDict.Keys.ToList().Last();
                    //peak1Box.Text = "";
                    // peak2Box.Text = "";
                }
                else
                {
                    if (XaxiscomboBox.Text == "" && YaxiscomboBox.Text == "")
                    {
                        return;
                    }
                    sortData();
                    Settings.Default["NumOfPoints"] = numBox.Text;
                    Settings.Default["Range"]       = rangeBox.Text;
                    if (YaxiscomboBox.Text.Contains("#"))
                    {
                        string text        = YaxiscomboBox.Text.Substring(10).Trim();
                        int    num         = Int32.Parse(text);
                        int    numOfpoints = 3;
                        double percentage  = 0.2;
                        if (numBox.Text != "")
                        {
                            numOfpoints = Int32.Parse(numBox.Text);
                        }
                        if (rangeBox.Text != "")
                        {
                            percentage = double.Parse(rangeBox.Text);
                        }
                        dataDict = PF.CalculateTimesEachX(num);
                        List <PeekValleyData> peaks = PF.GetPeaksWithNumOfSamePoints(num, numOfpoints, percentage);
                        peaksData = peaks;

                        /*
                         * for (int i = 0; i < PF.GetData().Length; i++)
                         * {
                         *  ResultBox.Items.Add(PF.GetData()[i].WriteToLine());
                         * }*/
                        if (peaksData.Count() == 0)
                        {
                            ResultBox.Items.Add("No Peak found yet.");
                        }
                        foreach (var item in peaks)
                        {
                            ResultBox.Items.Add("Peak Value: " + item.value + "   Times: " + item.times);
                        }
                    }
                    else if (YaxiscomboBox.Text != "")
                    {
                        string text = YaxiscomboBox.Text.Substring(5).Trim();

                        int Yindex = Int32.Parse(text);
                        // MessageBox.Show(Yindex.ToString());
                        int    numOfpoints = 3;
                        double percentage  = 0.2;
                        if (numBox.Text != "")
                        {
                            numOfpoints = Int32.Parse(numBox.Text);
                        }
                        if (rangeBox.Text != "")
                        {
                            percentage = double.Parse(rangeBox.Text);
                        }
                        text = XaxiscomboBox.Text.Substring(5).Trim();
                        int Xindex;
                        if (XaxiscomboBox.Text.Contains("TimeSpan"))
                        {
                            Xindex = -1;
                        }
                        else
                        {
                            Xindex = Int32.Parse(text);
                        }
                        MyData[] MD = PF.GetData();
                        dataPoints = new points[MD.Count()];
                        for (int i = 0; i < MD.Count(); i++)
                        {
                            dataPoints[i].x = MD[i].GetParameters()[Xindex];
                            dataPoints[i].y = MD[i].GetParameters()[Yindex];
                        }
                        List <PeekValleyData> peaks = PF.GetPeaksWithXY(Yindex, Xindex, numOfpoints, percentage);
                        peaksData = peaks;

                        /*
                         * for (int i = 0; i < PF.GetData().Length; i++)
                         * {
                         *  ResultBox.Items.Add(PF.GetData()[i].WriteToLine());
                         * }
                         * */
                        foreach (var item in peaks)
                        {
                            ResultBox.Items.Add("X: " + item.x + "   Y: " + item.y);
                        }
                    }
                    if (peaksData.Count() < 2)
                    {
                        ResultBox.Items.Add("We need at least two peaks in this algorithm");
                    }
                    else
                    {
                        peak1Box.Show();
                        peak2Box.Show();
                        label13.Show();
                        label12.Show();
                        xvalueBox.Show();
                        label10.Show();
                        peak1Box.Items.Clear();
                        peak2Box.Items.Clear();
                        points[] pts = GetPeakPoints();
                        foreach (var point in pts)
                        {
                            if (!peak1Box.Items.Contains(point.x))
                            {
                                peak1Box.Items.Add(point.x);
                            }
                            if (!peak2Box.Items.Contains(point.x))
                            {
                                peak2Box.Items.Add(point.x);
                            }
                        }
                    }
                }
            }
            Settings.Default.Save();
        }