/// <summary> /// Drawing plot of a given regression /// </summary> /// <param name="container"></param> private void DrawPlot(RegressionContainer container) { AbstractRegression regression = container.Regression; Brush brush = colorValuePairs[container.BrushKeyValue]; int predictDays = container.PredictDays; double min = regression.yVals.Min(); double max = regression.yVals.Max(); TopYLabel.Content = $"{max:f2}"; BottomYLabel.Content = $"{min:f2}"; double height = max - min; MiddleYLabel.Content = $"{(min + max) / 2.0:f2}"; double predictCoefficient = 1 + (double)predictDays / regression.xVals.Length; double width = regression.xVals.Length; double heightCoefficient = PlotCanvas.ActualHeight / (height * predictCoefficient); double widthCoefficient = PlotCanvas.ActualWidth / (width * predictCoefficient); Line line = new Line { Y2 = Math.Min(PlotCanvas.ActualHeight, PlotCanvas.ActualHeight - heightCoefficient * (regression.PredictY(0.1) - min)), X2 = 0.1 * widthCoefficient, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, StrokeThickness = 1.25, Stroke = brush }; for (double i = 0.1; i < regression.xVals.Length * predictCoefficient; i += 0.5) { double lastY = line.Y2; double lastX = line.X2; line = new Line { Y1 = lastY, X1 = lastX, Y2 = Math.Min(PlotCanvas.ActualHeight, PlotCanvas.ActualHeight - heightCoefficient * (regression.PredictY(i) - min)), X2 = i * widthCoefficient, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, StrokeThickness = 1.25, Stroke = brush }; PlotCanvas.Children.Add(line); } }
/// <summary> /// Drawing source values /// </summary> /// <param name="container"></param> private void DrawPoints(RegressionContainer container) { AbstractRegression regression = container.Regression; int predictDays = container.PredictDays; double min = regression.yVals.Min(); double max = regression.yVals.Max(); TopYLabel.Content = $"{max:f2}"; BottomYLabel.Content = $"{min:f2}"; double height = max - min; MiddleYLabel.Content = $"{(min + max) / 2.0:f2}"; double predictCoefficient = 1 + (double)predictDays / regression.xVals.Length; double width = regression.xVals.Length; double heightCoefficient = PlotCanvas.ActualHeight / (height * predictCoefficient); double widthCoefficient = PlotCanvas.ActualWidth / (width * predictCoefficient); for (int i = 0; i < regression.xVals.Length; i++) { Rectangle point = new Rectangle { HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, Stroke = Brushes.Red, Width = 2, Height = 2, Margin = new Thickness(regression.xVals[i] * widthCoefficient, PlotCanvas.ActualHeight - heightCoefficient * (regression.yVals[i] - min), regression.xVals[i] * widthCoefficient, PlotCanvas.ActualHeight - heightCoefficient * (regression.yVals[i] - min)) }; PlotCanvas.Children.Add(point); } companyChanged = false; }
/// <summary> /// Downloading data, making regression list, drawing /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DrawButton_Click(object sender, RoutedEventArgs e) { double[] xVals; double[] yVals; CsvFile file; string name; try { name = CompanyListBox.SelectedItem.ToString(); GetVals(out xVals, out yVals, out file, name); } catch (Exception) { return; } int baseDays = 0, predictDays = 0; if (!TryParseTextBoxes(ref baseDays, ref predictDays)) { MessageBox.Show("Неверный ввод данных для предсказания"); return; } baseDays = Math.Min(baseDays, file.Fields[0].Count); Array.Resize(ref xVals, baseDays); double[] yValsConv = new double[baseDays]; for (int i = Math.Max(yVals.Length - baseDays, 0); i < yVals.Length; i++) { yValsConv[i - yVals.Length + baseDays] = yVals[i]; } try { GetRegressionsList(xVals, yValsConv, file, predictDays, name); } catch (ArgumentException) { MessageBox.Show("Не удалось предсказать котировки по скачанным данным, попробуйте другую компанию"); ClearButton_Click(this, new RoutedEventArgs()); return; } if (OutputCheck.IsChecked == true) { SaveFileDialog fileDialog = new SaveFileDialog { Filter = "Comma separated file (*.csv)|*.csv" }; try { if (fileDialog.ShowDialog() == true) { file.Save(fileDialog.FileName, regressions); } } catch (IOException) { MessageBox.Show("Не удалось получить доступ к файлу по указанному пути"); } } if (regressions.Count != 0) { TopXLabel.Content = file.Fields[0][0]; BottomXLabel.Content = file.Fields[0][baseDays - 1]; MiddleXLabel.Content = file.Fields[0][baseDays / 2]; DrawPoints(regressions[regressions.Count - 1]); double bestCorreliation = regressions.Max(rc => rc.Regression.correlationCoefficient); AbstractRegression bestRegression = regressions.Where(rc => Math.Abs(rc.Regression.correlationCoefficient - bestCorreliation) < double.Epsilon).ToList()[0].Regression; MessageBox.Show($"Основываясь на лучшей модели предсказания: {bestRegression.ToString()} с коэффициентом корреляции {bestCorreliation:f5}, " + $"вы можете заработать ${-bestRegression.yVals[bestRegression.yVals.Length - 1] + bestRegression.PredictY(xVals.Length + 2):f5} за каждую акцию, если вложитесь сейчас и продадите акции через" + $" {predictDays} дней"); } }