private void openRegressionModelsDatabaseToolStripMenuItem_Click(object sender, EventArgs e) { var allModels = _databaseHelper.GetAllRegressionModels(); var loginResult = new AuthorizationRegressionAnalysis().ShowDialog(); RegressionModelsDatabase databaseForm; switch (loginResult) { case DialogResult.Yes: databaseForm = new RegressionModelsDatabase(_databaseHelper, allModels, true); break; case DialogResult.No: databaseForm = new RegressionModelsDatabase(_databaseHelper, allModels, false); break; default: return; } var result = databaseForm.ShowDialog(); if (result == DialogResult.OK) { var selectedModel = databaseForm.SelectedEquation; if (selectedModel.IsLinearRegression) { _linearEquation = selectedModel; ShowLinearRegressionModel(); } else { _multipleRegressionEquation = selectedModel; ShowMultipleRegressionModel(); } } }
private void SaveRegressionModel(RegressionModelEquation model) { if (model == null) { MessageBox.Show("There is no model to save!"); return; } ModelNameDateSelection.GetRegressionModelSaveInfo(out string name, out DateTime creationDate); _databaseHelper.AddRegressionModel(model, name, creationDate); MessageBox.Show($"Regression model {name} successfully saved!"); }
void DrawMainChart(List <PointD> points, bool writeModel) { double[] linearRegression = Regression.LinearRegression(points, checkBoxNormalizeValuesPage1.Checked); var rmse = Regression.RMSE(points, linearRegression[1], linearRegression[0]); var series = chartRegressionModel.Series["SourceData"]; series.Points.Clear(); foreach (var point in points) { series.Points.AddXY(point.X, point.Y, point.Y, linearRegression[0] + linearRegression[1] * point.X); } double minX = (int)points.Min(item => item.X); double maxX = (int)points.Max(item => item.X); double minY = (int)points.Min(item => item.Y); double maxY = (int)points.Max(item => item.Y); if (writeModel) { _linearEquation = new RegressionModelEquation("DefaultName", linearRegression, rmse, minX, maxX, minY, maxY, comboBoxInputParams.SelectedItem.ToString(), comboBoxQualityParams.SelectedItem.ToString(), checkBoxNormalizeValuesPage1.Checked); } _linearEquation.GetLinearRegressionModelInfo(out string modelText, out string xAxisTitle, out string yAxisTitle, out double[] coefficients, out minX, out maxX, out string rmseString, out bool normalizeValues); labelEquasion.Text = modelText; labelRMSE.Text = rmseString; checkBoxNormalizeValuesPage1.Checked = normalizeValues; chartRegressionModel.ChartAreas["ChartAreaMain"].AxisX.Title = xAxisTitle; chartRegressionModel.ChartAreas["ChartAreaMain"].AxisY.Title = yAxisTitle; chartRegressionModel.ChartAreas["ChartAreaMain"].AxisX.Minimum = minX - 1; chartRegressionModel.ChartAreas["ChartAreaMain"].AxisX.Maximum = maxX + 1; chartRegressionModel.ChartAreas["ChartAreaMain"].AxisY.Minimum = minY - 1; chartRegressionModel.ChartAreas["ChartAreaMain"].AxisY.Maximum = maxY + 1; chartRegressionModel.ChartAreas["ChartAreaMain"].AxisX.Interval = (maxX - minX + 2) / LineChartXAxisIntervalsCount; chartRegressionModel.ChartAreas["ChartAreaMain"].AxisY.Interval = (maxY - minY + 2) / LineChartYAxisIntervalsCount; DrawMainChartLine(linearRegression, minX - 1, maxX + 1, minY - 1, maxY + 1); }
private void CreateInputParametersTable(RegressionModelEquation model) { DataTable table = new DataTable(); table.Columns.Add("ParamId", typeof(string)); table.Columns.Add("ParamName", typeof(string)); table.Columns.Add("ParamCoefficient", typeof(double)); table.Columns.Add("ParamUpperBound", typeof(string)); table.Columns.Add("ParamLowerBound", typeof(string)); foreach (var parameter in model.InputParameters) { table.Rows.Add(parameter.Id.HasValue ? parameter.Id.Value.ToString() : string.Empty, parameter.Id.HasValue ? XMLWork.FindNameWithID(parameter.Id.Value, Properties.Settings.Default.Languages) : "free term", Math.Round(parameter.Coefficient, EquationSignificantDigitsCount), parameter.UpperBound.HasValue ? Math.Round(parameter.UpperBound.Value, EquationSignificantDigitsCount).ToString() : string.Empty, parameter.LowerBound.HasValue ? Math.Round(parameter.LowerBound.Value, EquationSignificantDigitsCount).ToString() : string.Empty); } dataGridViewInputParametersInfo.DataSource = table; }
bool GetMultipleRegressionModel(string qualityParam) { double[,] processParametersData; double[] qualityParameterData; var inputParametersToCheck = checkedListBoxInputParametersToCheckPage3.CheckedItems.Cast <string>().ToList(); int qualityParameterID = XMLWork.FindIDWithName(qualityParam, Properties.Settings.Default.Languages); processParametersData = new double[TrainData.Train.Count, inputParametersToCheck.Count + 1]; qualityParameterData = new double[TrainData.Train.Count]; //Setting first column of input values of regression matrix X with "1" values for (int i = 0; i < processParametersData.GetLength(0); i++) { processParametersData[i, 0] = 1; } List <double> parametersLowerBounds = new List <double>(), parametersUpperBounds = new List <double>(); //Setting all input data for (int paramNumber = 0; paramNumber < inputParametersToCheck.Count; paramNumber++) { double minValue = double.MaxValue, maxValue = double.MinValue; int inputParameterID = XMLWork.FindIDWithName(inputParametersToCheck[paramNumber], Properties.Settings.Default.Languages); for (int rowNumber = 0; rowNumber < TrainData.Train.Count; rowNumber++) { processParametersData[rowNumber, paramNumber + 1] = (double)TrainData.Train[rowNumber].Input[inputParameterID]; if (processParametersData[rowNumber, paramNumber + 1] < minValue) { minValue = processParametersData[rowNumber, paramNumber + 1]; } if (processParametersData[rowNumber, paramNumber + 1] > maxValue) { maxValue = processParametersData[rowNumber, paramNumber + 1]; } } parametersLowerBounds.Add(minValue); parametersUpperBounds.Add(maxValue); } //Setting output data for (int i = 0; i < TrainData.Train.Count; i++) { qualityParameterData[i] = (double)TrainData.Train[i].Output[qualityParameterID]; } //Call for multiple regression calculation method try { for (int j = 1; j < processParametersData.GetLength(1); j++) { bool throwData = true; for (int i = 0; i < processParametersData.GetLength(0); i++) { if (Math.Abs(processParametersData[i, j]) > DataSensitivityThreshhold) { throwData = false; break; } } if (throwData) { throw new Exception("Incorrect input data"); } } if (!qualityParameterData.Any(item => Math.Abs(item) > DataSensitivityThreshhold)) { throw new Exception("Incorrect input data"); } var coefficients = Regression.MultipleRegression(processParametersData, qualityParameterData, checkBoxNormalizeValuesPage3.Checked); double rmse = Regression.MultipleRMSE(processParametersData, qualityParameterData, coefficients); labelRMSEPage3.Text = $"RMSE = {Math.Round(rmse, RMSESignificantDigitsCount)}"; _multipleRegressionEquation = new RegressionModelEquation("DefaultName", qualityParam, inputParametersToCheck, rmse, coefficients, inputParametersToCheck.ToArray(), parametersLowerBounds.ToArray(), parametersUpperBounds.ToArray(), checkBoxNormalizeValuesPage3.Checked); ShowRegressionEquasion(qualityParam, coefficients); ShowRegressionEquasionHelp(inputParametersToCheck.ToArray()); return(true); } catch (Exception ex) { MessageBox.Show($"Exception thrown in multiple regression calculation: \"{ex.Message}\"!"); return(false); } }