public static LinearRegression ( double xVals, double yVals, int inclusiveStart, int exclusiveEnd, double &rsquared, double &yintercept, double &slope ) : void | ||
xVals | double | The x-axis values. |
yVals | double | The y-axis values. |
inclusiveStart | int | The inclusive inclusiveStart index. |
exclusiveEnd | int | The exclusive exclusiveEnd index. |
rsquared | double | The r^2 value of the line. |
yintercept | double | The y-intercept value of the line (i.e. y = ax + b, yintercept is b). |
slope | double | The slop of the line (i.e. y = ax + b, slope is a). |
return | void |
public void addSampleToRegression(int sample) { bufferSamples.Add(sample); DateTime now = DateTime.UtcNow; int nowInMillis = (((now.Hour * 60) + now.Minute) * 60 + now.Second) * 1000 + now.Millisecond; times.Add(nowInMillis); if (bufferSamples.Count > max_num_of_samples) { bufferSamples.RemoveAt(0); times.RemoveAt(0); } Regression.LinearRegression(times.ToArray(), bufferSamples.ToArray().Select(i => (double)i).ToArray(), 0, times.Count, out rsquared, out yintercept, out slope); }
private void PerformLinear_Click(object sender, RoutedEventArgs e) { double[,] points = getPointSeries(); if (points == null) { MessageBox.Show("No points :("); return; } Coefficients aAndb = Regression.LinearRegression(points); PlotModel m = new PlotModel { }; m.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom }); m.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Maximum = 10, Minimum = -10 }); m.Series.Add(new FunctionSeries(x => aAndb.a * x + aAndb.b, -10f, 10f, 0.1f, null)); LineSeries series1 = new LineSeries { LineStyle = LineStyle.None, MarkerType = MarkerType.Circle, MarkerSize = 4, MarkerStroke = OxyColors.White }; for (int i = points.GetLength(0) - 1; i >= 0; i--) { series1.Points.Add(new DataPoint(points[i, 0], points[i, 1])); } m.Series.Add(series1); Graph.Model = m; }
private void PlotBreakEvenPoint() { plot4.Clear(); Finances finances = GameController.instance.player.finances; List <int> priceData = new List <int>(); priceData.AddRange(finances.revenue); priceData.Sort(); List <int> quantityData = finances.GetQuantityData(); Regression regression = new Regression(); float a = 0; float b = 0; float rSq = 0; //regression.LinearRegression(quantityData.ToArray(), finances.revenue.ToArray(),out a, out b, out rSq); List <float> regressionData = new List <float>(); float xSize = (quantityData.Max() - quantityData.Min()) / quantityData.Count; List <float> quantityNormalizedData = new List <float>() { 0 }; for (int i = 0; i < quantityData.Count; i++) { quantityNormalizedData.Add(quantityNormalizedData.LastOrDefault() + xSize); } float ySize = (priceData.Max() - priceData.Min()) / priceData.Count; List <float> regressionDataNormalized = new List <float>() { 0 }; for (int i = 0; i < priceData.Count; i++) { regressionDataNormalized.Add(regressionDataNormalized.LastOrDefault() + ySize); } regressionDataNormalized.Remove(regressionDataNormalized.LastOrDefault()); // regressionData.AddRange(regression.Function(quantityNormalizedData, a, b)); List <int> x = new List <int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; LineGraphVisual originalVisual = new LineGraphVisual(containers[3], dotSprite, 12, false, new Color(1, 1, 1, 0.0f), Color.white); LineGraphVisual revenueVisual = new LineGraphVisual(containers[3], dotSprite, 12, false, Color.cyan, Color.white); LineGraphVisual regressionVisual = new LineGraphVisual(containers[3], dotSprite, 12, false, Color.yellow, Color.white); LineGraphVisual expencesRegressionVisual = new LineGraphVisual(containers[3], dotSprite, 12, false, Color.red, Color.red); List <float> extrems = new List <float>(); List <float> expencesRegression = new List <float>(); if (finances.revenue.Max() >= finances.staticExpences.Max()) { extrems = plot4.ShowPlot(finances.revenue, originalVisual, 14, containers[3], 0, 0, null, displayGrid: false); } else { extrems = plot4.ShowPlot(finances.staticExpences, originalVisual, -1, containers[3], 0, 0, displayGrid: false); } plot4.ShowPlot(regressionDataNormalized, revenueVisual, 14, containers[3], extrems[0], extrems[1], (int i) => Mathf.RoundToInt(quantityNormalizedData[i]).ToString(), (float f) => String.Format("{0:n0} $", Mathf.RoundToInt(f)), displayGrid: true); List <int> yearExpences = finances.GetOverYearExpences(); if (yearExpences.Count < x.Count) { for (int i = yearExpences.Count; i < x.Count; i++) { yearExpences.Add(yearExpences.LastOrDefault()); } } regression.LinearRegression(x.ToArray(), yearExpences.ToArray(), out a, out b, out rSq); // take last 12 elements and delete the last one in order to make regression. Adjust x accordingly expencesRegression = regression.Function(x.ConvertAll(p => (float)p), a, b); Debug.Log("-----Linear Regression-----"); Debug.Log("A: " + a + " B: " + b + "Rsquare: " + rSq); plot4.ShowPlot(expencesRegression, expencesRegressionVisual, -1, containers[3], extrems[0], extrems[1], displayGrid: false); }
private bool FitModel(Steema.TeeChart.Styles.Series source, ref double[] yhat, out double[] coeffs, int modelindex) { double[] y = new double[source.Count]; double[] x = new double[source.Count]; double[] w = new double[source.Count]; bool validmodel = true; switch (modelindex) { case 0: // y(x)=a*Exp(b*x) // linear model: ln(y) = b*x + ln(a) for (int i = 0; i < source.Count; i++) { x[i] = source.notMandatory[i]; y[i] = Math.Log(source.mandatory[i]); w[i] = source.mandatory[i] * source.mandatory[i]; } Regression.LinearRegression(source.Count, x, y, w, out coeffs); coeffs[0] = Math.Exp(coeffs[0]); for (int i = 0; i < source.Count; i++) { yhat[i] = coeffs[0] * Math.Exp(coeffs[1] * x[i]); } break; case 1: // Power model y=a*x^b // linear model : ln(y) = b*ln(x) + ln(a) for (int i = 0; i < source.Count; i++) { x[i] = Math.Log(source.notMandatory[i]); y[i] = Math.Log(source.mandatory[i]); w[i] = source.mandatory[i] * source.mandatory[i]; } Regression.LinearRegression(source.Count, x, y, w, out coeffs); coeffs[0] = Math.Exp(coeffs[0]); for (int i = 0; i < source.Count; i++) { yhat[i] = coeffs[0] * Math.Pow(source.notMandatory[i], coeffs[1]); } break; case 2: // Logarithmic model y = b*ln(x)+a for (int i = 0; i < source.Count; i++) { x[i] = Math.Log(source.notMandatory[i]); y[i] = source.mandatory[i]; w[i] = source.mandatory[i] * source.mandatory[i]; } Regression.LinearRegression(source.Count, x, y, w, out coeffs); for (int i = 0; i < source.Count; i++) { yhat[i] = coeffs[1] * Math.Log(source.notMandatory[i]) + coeffs[0]; } break; default: coeffs = new double[0]; break; } return(validmodel); }