public LogisticRegression GetLogisticRegressionParams(int index, double percentage) { LogisticInfo LI = new LogisticInfo(); LI.LogisticParams = new double[2]; int length = MD.Length; int samplesize = (int)(length * percentage); double [][] inputArr = new double[samplesize * 2][]; for (int i = 0; i < samplesize * 2; i++) { inputArr[i] = new double[2]; } double[] outputArr = new double[samplesize * 2]; for (int i = 0; i < samplesize; i++) { inputArr[i][0] = MD[i].GetParameters()[index]; inputArr[i][1] = 0; outputArr[i] = 0; } int j = length - samplesize; for (int i = samplesize; i < samplesize * 2; i++) { inputArr[i][0] = MD[j].GetParameters()[index]; inputArr[i][1] = 0; outputArr[i] = 1; j++; } LogisticRegression LR = new LogisticRegression(); LR.NumberOfInputs = 1; var learner = new IterativeReweightedLeastSquares <LogisticRegression>() { Tolerance = 1e-4, // Let's set some convergence parameters Iterations = 100, // maximum number of iterations to perform Regularization = 0 }; LogisticRegression regression = learner.Learn(inputArr, outputArr); LI.LogisticParams[0] = -(regression.Intercept + 1); LI.LogisticParams[1] = regression.GetOddsRatio(1) - 1; return(regression); }
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(); }