internal List <double> GetOutputDistribution(ISampleableDistribution <double> inputDistribution, SimpleLinearRegression regression) { var returnList = new List <double>(); for (int i = 0; i < 1000; i++) { returnList.Add(regression.Transform(inputDistribution.Generate())); } return(returnList); }
public IEnumerable <XtoY> Predict(IEnumerable <double> xvalues) { EnsureAlreadyTrained(); double [] xvaluesArray = xvalues.ToArray(); double [] yvaluesArray = _simpleLinearRegression.Transform(xvaluesArray); for (int i = 0; i < xvaluesArray.Length; ++i) { yield return(new XtoY() { X = xvaluesArray[i], Y = yvaluesArray[i] }); } }
public IEnumerable <XtoY> Predict(IEnumerable <double> xvalues) { EnsureAlreadyTrained(); double[] date = new double[mylist.Count]; for (int i = 0; i < mylist.Count; i++) { date[i] = i + 1; } double[] xvaluesArray = date; double[] yvaluesArray = _simpleLinearRegression.Transform(xvaluesArray); for (int i = 0; i < xvaluesArray.Length; ++i) { yield return(new XtoY() { X = xvaluesArray[i], Y = yvaluesArray[i] }); } }
private static void linearRegression() { // Declare some sample test data. double[] inputs = { 80, 60, 10, 20, 30 }; double[] outputs = { 20, 40, 30, 50, 60 }; // Use Ordinary Least Squares to learn the regression OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); // Use OLS to learn the simple linear regression SimpleLinearRegression regression = ols.Learn(inputs, outputs); // Compute the output for a given input: double y = regression.Transform(85); // The answer will be 28.088 // We can also extract the slope and the intercept term // for the line. Those will be -0.26 and 50.5, respectively. double s = regression.Slope; // -0.264706 double c = regression.Intercept; // 50.588235 }
public static void test3() { var poly2 = CreateFunc(1, 1); Random rnd = new Random(); var pos = Enumerable.Range(0, 20).Select(x => new double [] { x, poly2(x) + rnd.NextDouble() }).ToArray(); double[] inputs = pos.Select(x => x[0]).ToArray(); double[] outputs = pos.Select(x => x[1]).ToArray(); var ls = new PolynomialLeastSquares() { Degree = 2 }; PolynomialRegression poly = ls.Learn(inputs, outputs); double a = poly.Weights[0]; // a = 0 double b = poly.Weights[1]; // b = 0 double c = poly.Intercept; // c = 1 double[] predicted = poly.Transform(inputs); double error = new SquareLoss(outputs).Loss(predicted); var ols = new OrdinaryLeastSquares(); SimpleLinearRegression mul = ols.Learn(inputs, outputs); double a1 = mul.Slope; // a = 0 double b1 = mul.Intercept; // b = 0 double[] simplepredict = mul.Transform(inputs); double erroe2 = new SquaredHingeLoss(outputs).Loss(simplepredict); Console.WriteLine("Done"); }
public void learn_test() { #region doc_learn // Let's say we have some univariate, continuous sets of input data, // and a corresponding univariate, continuous set of output data, such // as a set of points in R². A simple linear regression is able to fit // a line relating the input variables to the output variables in which // the minimum-squared-error of the line and the actual output points // is minimum. // Declare some sample test data. double[] inputs = { 80, 60, 10, 20, 30 }; double[] outputs = { 20, 40, 30, 50, 60 }; // Use Ordinary Least Squares to learn the regression OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); // Use OLS to learn the simple linear regression SimpleLinearRegression regression = ols.Learn(inputs, outputs); // Compute the output for a given input: double y = regression.Transform(85); // The answer will be 28.088 // We can also extract the slope and the intercept term // for the line. Those will be -0.26 and 50.5, respectively. double s = regression.Slope; // -0.264706 double c = regression.Intercept; // 50.588235 #endregion // Expected slope and intercept double eSlope = -0.264706; double eIntercept = 50.588235; Assert.AreEqual(28.088235294117649, y, 1e-10); Assert.AreEqual(eSlope, s, 1e-5); Assert.AreEqual(eIntercept, c, 1e-5); Assert.IsFalse(double.IsNaN(y)); }
public void logarithm_learn() { #region doc_learn // This is the same data from the example available at // http://mathbits.com/MathBits/TISection/Statistics2/logarithmic.htm // Declare your inputs and output data double[] inputs = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; double[] outputs = { 6, 9.5, 13, 15, 16.5, 17.5, 18.5, 19, 19.5, 19.7, 19.8 }; // Transform inputs to logarithms double[] logx = Matrix.Log(inputs); // Use Ordinary Least Squares to learn the regression OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); // Use OLS to learn the simple linear regression SimpleLinearRegression lr = ols.Learn(logx, outputs); // Compute predicted values for inputs double[] predicted = lr.Transform(logx); // Get an expression representing the learned regression model // We just have to remember that 'x' will actually mean 'log(x)' string result = lr.ToString("N4", CultureInfo.InvariantCulture); // Result will be "y(x) = 6.1082x + 6.0993" // The mean squared error between the expected and the predicted is double error = new SquareLoss(outputs).Loss(predicted); // 0.261454 #endregion Assert.AreEqual(0.26145460024250794, error, 1e-8); Assert.AreEqual(6.1081800414945704, lr.Slope, 1e-8); Assert.AreEqual(6.0993411396126653, lr.Intercept, 1e-8); Assert.AreEqual("y(x) = 6.1082x + 6.0993", result); }
public void new_api_test() { #region doc_learn // Fix the random number generator Accord.Math.Random.Generator.Seed = 0; double[,] data = // This is the same data used in the RANSAC sample app { { 1.0, 0.79 }, { 3, 2.18 }, { 5, 5.99 }, { 7.0, 7.65 }, { 9.0, 9.55 }, { 11, 11.89 }, { 13, 13.73 }, { 15.0, 14.77 }, { 17.0, 18.00 }, { 1.2, 1.45 }, { 1.5, 1.18 }, { 1.8, 1.92 }, { 2.1, 1.47 }, { 2.4, 2.41 }, { 2.7, 2.35 }, { 3.0, 3.41 }, { 3.3, 3.78 }, { 3.6, 3.21 }, { 3.9, 4.76 }, { 4.2, 5.03 }, { 4.5, 4.19 }, { 4.8, 3.81 }, { 5.1, 6.07 }, { 5.4, 5.74 }, { 5.7, 6.39 }, { 6, 6.11 }, { 6.3, 6.86 }, { 6.6, 6.35 }, { 6.9, 7.9 }, { 7.2, 8.04 }, { 7.5, 8.48 }, { 7.8, 8.07 }, { 8.1, 8.22 }, { 8.4, 8.41 }, { 8.7, 9.4 }, { 9, 8.8 }, { 9.3, 8.44 }, { 9.6, 9.32 }, { 9.9, 9.18 }, { 10.2, 9.86 }, { 10.5, 10.16 }, { 10.8, 10.28 }, { 11.1, 11.07 }, { 11.4, 11.66 }, { 11.7, 11.13 }, { 12, 11.55 }, { 12.3, 12.62 }, { 12.6, 12.27 }, { 12.9, 12.33 }, { 13.2, 12.37 }, { 13.5, 12.75 }, { 13.8, 14.44 }, { 14.1, 14.71 }, { 14.4, 13.72 }, { 14.7, 14.54 }, { 15, 14.67 }, { 15.3, 16.04 }, { 15.6, 15.21 }, { 1, 3.9 }, { 2, 11.5 }, { 3.0, 13.0 }, { 4, 0.9 }, { 5, 5.5 }, { 6, 16.2 }, { 7.0, 0.8 }, { 8, 9.4 }, { 9, 9.5 }, { 10, 17.5 }, { 11.0, 6.3 }, { 12, 12.6 }, { 13, 1.5 }, { 14, 1.5 }, { 2.0, 10 }, { 3, 9 }, { 15, 2 }, { 15.5, 1.2 }, }; // First, fit simple linear regression directly for comparison reasons. double[] x = data.GetColumn(0); // Extract the independent variable double[] y = data.GetColumn(1); // Extract the dependent variable // Use Ordinary Least Squares to learn the regression OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); // Estimate a line passing through the (x, y) points SimpleLinearRegression regression = ols.Learn(x, y); // Now, compute the values predicted by the // regression for the original input points double[] commonOutput = regression.Transform(x); // Now, fit simple linear regression using RANSAC int maxTrials = 1000; int minSamples = 20; double probability = 0.950; double errorThreshold = 1000; // Create a RANSAC algorithm to fit a simple linear regression var ransac = new RANSAC <SimpleLinearRegression>(minSamples) { Probability = probability, Threshold = errorThreshold, MaxEvaluations = maxTrials, // Define a fitting function Fitting = (int[] sample) => { // Build a Simple Linear Regression model return(new OrdinaryLeastSquares() .Learn(x.Get(sample), y.Get(sample))); }, // Define a inlier detector function Distances = (SimpleLinearRegression r, double threshold) => { var inliers = new List <int>(); for (int i = 0; i < x.Length; i++) { // Compute error for each point double error = r.Transform(x[i]) - y[i]; // If the square error is low enough, if (error * error < threshold) { inliers.Add(i); // the point is considered an inlier. } } return(inliers.ToArray()); } }; // Now that the RANSAC hyperparameters have been specified, we can // compute another regression model using the RANSAC algorithm: int[] inlierIndices; SimpleLinearRegression robustRegression = ransac.Compute(data.Rows(), out inlierIndices); // Compute the output of the model fitted by RANSAC double[] ransacOutput = robustRegression.Transform(x); #endregion Assert.AreEqual(ransac.TrialsNeeded, 0); Assert.AreEqual(ransac.TrialsPerformed, 1); string a = inlierIndices.ToCSharp(); string b = ransacOutput.ToCSharp(); int[] expectedInliers = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75 }; double[] expectedOutput = new double[] { 1.96331236445045, 3.42042856976283, 4.87754477507521, 6.33466098038758, 7.79177718569996, 9.24889339101234, 10.7060095963247, 12.1631258016371, 13.6202420069495, 2.10902398498169, 2.32759141577855, 2.5461588465754, 2.76472627737226, 2.98329370816912, 3.20186113896597, 3.42042856976283, 3.63899600055969, 3.85756343135654, 4.0761308621534, 4.29469829295026, 4.51326572374711, 4.73183315454397, 4.95040058534082, 5.16896801613768, 5.38753544693454, 5.6061028777314, 5.82467030852825, 6.04323773932511, 6.26180517012196, 6.48037260091882, 6.69894003171568, 6.91750746251253, 7.13607489330939, 7.35464232410625, 7.5732097549031, 7.79177718569996, 8.01034461649682, 8.22891204729367, 8.44747947809053, 8.66604690888738, 8.88461433968424, 9.1031817704811, 9.32174920127795, 9.54031663207481, 9.75888406287167, 9.97745149366852, 10.1960189244654, 10.4145863552622, 10.6331537860591, 10.8517212168559, 11.0702886476528, 11.2888560784497, 11.5074235092465, 11.7259909400434, 11.9445583708402, 12.1631258016371, 12.3816932324339, 12.6002606632308, 1.96331236445045, 2.69187046710664, 3.42042856976283, 4.14898667241902, 4.87754477507521, 5.6061028777314, 6.33466098038758, 7.06321908304377, 7.79177718569996, 8.52033528835615, 9.24889339101234, 9.97745149366852, 10.7060095963247, 11.4345676989809, 2.69187046710664, 3.42042856976283, 12.1631258016371, 12.5274048529652 }; Assert.IsTrue(inlierIndices.IsEqual(expectedInliers)); Assert.IsTrue(ransacOutput.IsEqual(expectedOutput, 1e-10)); }
public void prediction_test() { // example data from http://www.real-statistics.com/regression/confidence-and-prediction-intervals/ double[][] input = { new double[] { 5, 80 }, new double[] { 23, 78 }, new double[] { 25, 60 }, new double[] { 48, 53 }, new double[] { 17, 85 }, new double[] { 8, 84 }, new double[] { 4, 73 }, new double[] { 26, 79 }, new double[] { 11, 81 }, new double[] { 19, 75 }, new double[] { 14, 68 }, new double[] { 35, 72 }, new double[] { 29, 58 }, new double[] { 4, 92 }, new double[] { 23, 65 }, }; double[] cig = input.GetColumn(0); double[] exp = input.GetColumn(1); // Use Ordinary Least Squares to learn the regression OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); // Use OLS to learn the simple linear regression SimpleLinearRegression regression = ols.Learn(cig, exp); Assert.AreEqual(1, regression.NumberOfInputs); Assert.AreEqual(1, regression.NumberOfOutputs); double x0 = 20; double y0 = regression.Transform(x0); Assert.AreEqual(y0, 73.1564, 1e-4); double syx = regression.GetStandardError(cig, exp); Assert.AreEqual(7.974682, syx, 1e-5); double ssx = cig.Subtract(cig.Mean()).Pow(2).Sum(); Assert.AreEqual(2171.6, ssx, 1e-5); double n = exp.Length; double x0c = x0 - cig.Mean(); double var = 1 / n + (x0c * x0c) / ssx; Assert.AreEqual(0.066832443052741455, var, 1e-10); double expected = syx * Math.Sqrt(var); double actual = regression.GetStandardError(x0, cig, exp); Assert.AreEqual(2.061612, expected, 1e-5); Assert.AreEqual(expected, actual, 1e-10); DoubleRange ci = regression.GetConfidenceInterval(x0, cig, exp); Assert.AreEqual(ci.Min, 68.702569616457751, 1e-5); Assert.AreEqual(ci.Max, 77.610256563931543, 1e-5); actual = regression.GetPredictionStandardError(x0, cig, exp); Assert.AreEqual(8.2368569010499666, actual, 1e-10); DoubleRange pi = regression.GetPredictionInterval(x0, cig, exp); Assert.AreEqual(pi.Min, 55.361765613397054, 1e-5); Assert.AreEqual(pi.Max, 90.95106056699224, 1e-5); }
public RecognitionState Recognition(double[] inputs, double[] outputs) { RecognitionState ret = new RecognitionState(); if (inputs.Length > 5) { /*一次线性回归*/ SimpleLinearRegression regression = this.ols.Learn(inputs, outputs, null); double k = regression.Slope; const double TAN45 = 1.0; const double TAN15 = 0.2679492; double k1 = Math.Abs(k); if (k1 > TAN45) { ret.Slope = SlopeState.Steep; } else if (k1 > TAN15) { ret.Slope = SlopeState.moderate; } else { ret.Slope = SlopeState.gentle; } /*二次线性回归*/ PolynomialRegression poly = this.pls.Learn(inputs, outputs, null); double a = poly.Weights[0]; double b = poly.Weights[1]; if (k > 0 && a > 0) { ret.Shape = ShapeState.Rise; } else if (k > 0 && a < 0) { ret.Shape = ShapeState.FallAfterRise; } else if (k < 0 && a < 0) { ret.Shape = ShapeState.Fall; } else if (k < 0 && a > 0) { ret.Shape = ShapeState.RiseAfterFall; } double last = inputs[inputs.Length - 1]; double s = 2 * a * last + b; double s1 = Math.Abs(s); if (s1 > TAN45) { ret.Speed = SpeedState.Rapid; } else if (s1 > TAN15) { ret.Speed = SpeedState.Steady; } else { ret.Speed = SpeedState.Slow; } /*显示图形*/ if (this.showForm != null) { double[] outputs2 = regression.Transform(inputs); double[] outputs3 = poly.Transform(inputs); this.showForm.ShowGraph(inputs, outputs, inputs, outputs2, inputs, outputs3); } Console.WriteLine("k={0},a={1},b={2}", k, a, b); } return(ret); }
private void btnCompute_Click(object sender, EventArgs e) { DataTable dataTable = dgvAnalysisSource.DataSource as DataTable; if (dataTable == null) { return; } // Gather the available data double[][] data = dataTable.ToArray(); // First, fit simple linear regression directly for comparison reasons. double[] x = data.GetColumn(0); // Extract the independent variable double[] y = data.GetColumn(1); // Extract the dependent variable // Use Ordinary Least Squares to learn the regression OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); // Estimate a line passing through the (x, y) points SimpleLinearRegression regression = ols.Learn(x, y); // Now, compute the values predicted by the // regression for the original input points double[] commonOutput = regression.Transform(x); // Now, fit simple linear regression using RANSAC int maxTrials = (int)numMaxTrials.Value; int minSamples = (int)numSamples.Value; double probability = (double)numProbability.Value; double errorThreshold = (double)numThreshold.Value; // Create a RANSAC algorithm to fit a simple linear regression var ransac = new RANSAC <SimpleLinearRegression>(minSamples) { Probability = probability, Threshold = errorThreshold, MaxEvaluations = maxTrials, // Define a fitting function Fitting = delegate(int[] sample) { // Retrieve the training data double[] inputs = x.Get(sample); double[] outputs = y.Get(sample); // Build a Simple Linear Regression model return(new OrdinaryLeastSquares().Learn(inputs, outputs)); }, // Define a check for degenerate samples Degenerate = delegate(int[] sample) { // In this case, we will not be performing such checks. return(false); }, // Define a inlier detector function Distances = delegate(SimpleLinearRegression r, double threshold) { List <int> inliers = new List <int>(); for (int i = 0; i < x.Length; i++) { // Compute error for each point double error = r.Transform(x[i]) - y[i]; // If the squared error is below the given threshold, // the point is considered to be an inlier. if (error * error < threshold) { inliers.Add(i); } } return(inliers.ToArray()); } }; // Now that the RANSAC hyperparameters have been specified, we can // compute another regression model using the RANSAC algorithm: int[] inlierIndices; SimpleLinearRegression robustRegression = ransac.Compute(data.Length, out inlierIndices); if (robustRegression == null) { lbStatus.Text = "RANSAC failed. Please try again after adjusting its parameters."; return; // the RANSAC algorithm did not find any inliers and no model was created } // Compute the output of the model fitted by RANSAC double[] ransacOutput = robustRegression.Transform(x); // Create scatter plot comparing the outputs from the standard // linear regression and the RANSAC-fitted linear regression. CreateScatterplot(graphInput, x, y, commonOutput, ransacOutput, x.Get(inlierIndices), y.Get(inlierIndices)); lbStatus.Text = "Regression created! Please compare the RANSAC " + "regression (blue) with the simple regression (in red)."; }
public double GetTransform(double xPred) { return(regression.Transform(xPred)); }
public HttpResponseMessage ScrapeSheet(SheetDocumentModel model) { try { var watch = System.Diagnostics.Stopwatch.StartNew(); var filePath = AppDomain.CurrentDomain.BaseDirectory + "//RawData//" + model.FileName; File.WriteAllBytes(filePath, Convert.FromBase64String(model.FileData)); var connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={filePath};Extended Properties=Excel 12.0;"; var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString); var ds = new DataSet(); adapter.Fill(ds, "DataTable"); var data = ds.Tables["DataTable"].AsEnumerable(); var scrapedResult = data.Where(w => w.Field <DateTime?>("DateTime") != null).Select(x => new SheetResult() { DateTime = x.Field <DateTime?>("DateTime"), Value = x.Field <double?>("Value"), Unit = x.Field <string>("Unit"), }).ToList(); var regressionInput = scrapedResult.Where(w => w.Value != null).Select(s => Convert.ToDouble(s.DateTime?.Hour)).ToArray(); var regressionOutput = scrapedResult.Where(w => w.Value != null).Select(s => Convert.ToDouble(s.Value.Value)).ToArray(); // Use Ordinary Least Squares to learn the regression OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); // Use OLS to learn the simple linear regression SimpleLinearRegression regression = ols.Learn(regressionInput, regressionOutput); // Compute the output for a given input: foreach (var v in scrapedResult) { if (v.Value != null) { continue; } v.Value = regression.Transform(Convert.ToDouble(v.DateTime?.Hour)); v.PredictedValue = true; regressionInput = scrapedResult.Where(w => w.Value != null).Select(s => Convert.ToDouble(s.DateTime?.Hour)).ToArray(); regressionOutput = scrapedResult.Where(w => w.Value != null).Select(s => Convert.ToDouble(s.Value.Value)).ToArray(); regression = ols.Learn(regressionInput, regressionOutput); } var list = new JavaScriptSerializer().Serialize(scrapedResult); var dataFormatted = JToken.Parse(list).ToString(Formatting.Indented); _db.Data.Add(new DataEntity() { CreatedOn = DateTime.Now, IdCollectionType = (int)CollectionTypeEnum.Sheet, JsonObject = dataFormatted }); _db.SaveChanges(); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; System.Diagnostics.Debug.WriteLine("Timp sheet scraper: " + elapsedMs); return(Request.CreateResponse(HttpStatusCode.OK, scrapedResult)); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } }
private List <AccordResult> CalculateLinearRegression(List <BalancePointPair> allBalancePointPairs, WthNormalParams normalParamsKey) { var allBalancePointGroups = allBalancePointPairs.GroupBy(s => new { s.CoolingBalancePoint, s.HeatingBalancePoint }); List <AccordResult> accordResults = new List <AccordResult>(); List <AccordResult> rejectedAccords = new List <AccordResult>(); foreach (var group in allBalancePointGroups) { try { List <BalancePointPair> IdenticalBalancePointPairsForAllReadings = group.ToList(); BalancePointPair _pointPair = IdenticalBalancePointPairsForAllReadings.First(); int readingsCount = IdenticalBalancePointPairsForAllReadings.Count; double[] fullYData = new double[readingsCount]; double[] fullYDataDailyAvg = new double[readingsCount]; double[][] hcddMatrix = new double[readingsCount][]; double[][] hcddMatrixNonDaily = new double[readingsCount][]; foreach (BalancePointPair balancePointPair in IdenticalBalancePointPairsForAllReadings) { fullYData[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] = (balancePointPair.ActualUsage); fullYDataDailyAvg[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] = (balancePointPair.ActualUsage / balancePointPair.DaysInReading); hcddMatrix[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] = new double[] { (balancePointPair.HeatingDegreeDays / balancePointPair.DaysInReading), (balancePointPair.CoolingDegreeDays / balancePointPair.DaysInReading) }; } double[] avgHddsForEachReadingInYear = new double[readingsCount]; double[] avgCddsForEachReadingInYear = new double[readingsCount]; for (int i = 0; i < readingsCount; i++) { avgHddsForEachReadingInYear[i] = hcddMatrix[i][0]; avgCddsForEachReadingInYear[i] = hcddMatrix[i][1]; } double[] modelParams = new double[3]; modelParams[0] = 0; modelParams[1] = 0; modelParams[2] = 0; if (fullYData.Sum() == 0) { AccordResult empty = new AccordResult { bpPair = _pointPair }; accordResults.Add(empty); } else if (_pointPair.HeatingBalancePoint == 0 && _pointPair.CoolingBalancePoint == 0) { double[] onesVector = new double[readingsCount]; for (int i = 0; i < readingsCount; i++) { onesVector[i] = 1; } modelParams[0] = Fit.LineThroughOrigin(onesVector, fullYDataDailyAvg); OrdinaryLeastSquares ols = new OrdinaryLeastSquares() { UseIntercept = false }; SimpleLinearRegression regressionAccord = ols.Learn(onesVector, fullYDataDailyAvg); //double[] predictedAccord = regressionAccord.Transform(onesVector); double r2 = MathNet.Numerics.GoodnessOfFit.CoefficientOfDetermination(onesVector.Select(x => x * modelParams[0]), fullYDataDailyAvg); //double mean = fullYDataDailyAvg.Mean(); //if (mean != modelParams[0] || mean != regressionAccord.Slope) //{ // Console.WriteLine("Hey!"); //} //double r2Accord = regressionAccord.CoefficientOfDetermination(onesVector, fullYDataDailyAvg); //double sxx = onesVector.Subtract(onesVector.Mean()).Pow(2).Sum(); //double hypothesizedValue = 0; //try //{ // TTest test = new TTest( // estimatedValue: regressionAccord.Slope, standardError: sxx, degreesOfFreedom: _pointPair.ReadingsInNormalYear - 2, // hypothesizedValue: hypothesizedValue, alternate: OneSampleHypothesis.ValueIsDifferentFromHypothesis // ); // if (test.Significant) // { AccordResult accordResult = new AccordResult() { SimpleLinearRegression = regressionAccord, R2Accord = r2, IsSimpleSingleRegression = true, HeatingBP = _pointPair.HeatingBalancePoint, CoolingBP = _pointPair.CoolingBalancePoint, Intercept = regressionAccord.Slope, bpPair = _pointPair }; accordResults.Add(accordResult); // } //} //catch (Exception e) //{ // Console.WriteLine(e.Message + e.StackTrace); //} } else if (_pointPair.CoolingBalancePoint != 0 && _pointPair.HeatingBalancePoint != 0) { //modelParams = MultipleRegression.QR(hcddMatrix, fullYDataDailyAvg, intercept: true); //Accord //var ols = new OrdinaryLeastSquares() //{ // UseIntercept = true //}; try { MultipleLinearRegressionAnalysis mlra = new MultipleLinearRegressionAnalysis(intercept: true); mlra.Learn(hcddMatrix, fullYDataDailyAvg); // //MultipleLinearRegression regressionAccord = ols.Learn(hcddMatrix, fullYDataDailyAvg); var regressionAccord = mlra.Regression; double[] predicted = regressionAccord.Transform(hcddMatrix); double r2Accord = new RSquaredLoss(numberOfInputs: 2, expected: fullYDataDailyAvg) { Adjust = false }.Loss(predicted); double r2Coeff = regressionAccord.CoefficientOfDetermination(hcddMatrix, fullYDataDailyAvg, adjust: false); //double r2Math = MathNet.Numerics.GoodnessOfFit.CoefficientOfDetermination(hcddMatrix.Select( // x => (x[0] * regressionAccord.Weights[0]) + (x[1] * regressionAccord.Weights[1]) + regressionAccord.Intercept //), fullYDataDailyAvg); //double r2MathPred = MathNet.Numerics.GoodnessOfFit.CoefficientOfDetermination(predicted, fullYDataDailyAvg); AccordResult accordResult = new AccordResult() { //MultipleRegression = regressionAccord, R2Accord = r2Accord, R2Coeff = r2Coeff, HeatingBP = _pointPair.HeatingBalancePoint, CoolingBP = _pointPair.CoolingBalancePoint, IsSimpleSingleRegression = false, MLRA = mlra, Intercept = regressionAccord.Intercept, bpPair = _pointPair, IsMultipleLinearRegression = true }; if (mlra.Coefficients.All(x => x.TTest.Significant)) { accordResults.Add(accordResult); } else { rejectedAccords.Add(accordResult); } } catch (Exception e) { Console.WriteLine(e.Message + " " + e.StackTrace); } } else if (_pointPair.HeatingBalancePoint > 0) { // Tuple<double, double> heatingTuple = Fit.Line(avgHddsForEachReadingInYear, fullYDataDailyAvg); // modelParams[0] = heatingTuple.Item1; // modelParams[1] = heatingTuple.Item2; // double r = MathNet.Numerics.GoodnessOfFit.CoefficientOfDetermination( // avgHddsForEachReadingInYear.Select(x => heatingTuple.Item1 + heatingTuple.Item2 * x), fullYDataDailyAvg); OrdinaryLeastSquares ols = new OrdinaryLeastSquares() { UseIntercept = true }; SimpleLinearRegression regressionAccord = ols.Learn(avgHddsForEachReadingInYear, fullYDataDailyAvg); double[] predictedAccord = regressionAccord.Transform(avgHddsForEachReadingInYear); double rAccord = new RSquaredLoss(1, fullYDataDailyAvg).Loss(predictedAccord); //double rAccord2 = regressionAccord.CoefficientOfDetermination(avgHddsForEachReadingInYear, fullYDataDailyAvg, adjust: false); //double r2Math = MathNet.Numerics.GoodnessOfFit.CoefficientOfDetermination(avgHddsForEachReadingInYear.Select( // x => (x * regressionAccord.Slope) + regressionAccord.Intercept // ), fullYDataDailyAvg); //double r2 = MathNet.Numerics.GoodnessOfFit.CoefficientOfDetermination(predictedAccord, fullYDataDailyAvg); int degreesOfFreedom = _pointPair.ReadingsInNormalYear - 2; double ssx = Math.Sqrt((avgHddsForEachReadingInYear.Subtract(avgHddsForEachReadingInYear.Mean())).Pow(2).Sum()); double s = Math.Sqrt(((fullYDataDailyAvg.Subtract(predictedAccord).Pow(2)).Sum()) / degreesOfFreedom); double error = regressionAccord.GetStandardError(avgHddsForEachReadingInYear, fullYDataDailyAvg); double seSubB = s / ssx; double hypothesizedValue = 0; TTest tTest = new TTest( estimatedValue: regressionAccord.Slope, standardError: seSubB, degreesOfFreedom: degreesOfFreedom, hypothesizedValue: hypothesizedValue, alternate: OneSampleHypothesis.ValueIsDifferentFromHypothesis ); AccordResult accordResult = new AccordResult() { SimpleLinearRegression = regressionAccord, R2Accord = rAccord, IsSimpleSingleRegression = true, HeatingBP = _pointPair.HeatingBalancePoint, CoolingBP = _pointPair.CoolingBalancePoint, TTest = tTest, Intercept = regressionAccord.Intercept, bpPair = _pointPair }; if (tTest.Significant) { accordResults.Add(accordResult); } else { rejectedAccords.Add(accordResult); } } else if (_pointPair.CoolingBalancePoint > 0) { //Tuple<double, double> coolingTuple = Fit.Line(avgCddsForEachReadingInYear, fullYDataDailyAvg); //modelParams[0] = coolingTuple.Item1; //modelParams[2] = coolingTuple.Item2; OrdinaryLeastSquares ols = new OrdinaryLeastSquares() { UseIntercept = true }; SimpleLinearRegression regressionAccord = ols.Learn(avgCddsForEachReadingInYear, fullYDataDailyAvg); double[] predictedAccord = regressionAccord.Transform(avgCddsForEachReadingInYear); double rAccord = new RSquaredLoss(1, fullYDataDailyAvg).Loss(predictedAccord); //double r2Math = MathNet.Numerics.GoodnessOfFit.CoefficientOfDetermination(avgCddsForEachReadingInYear.Select( // x => (x * regressionAccord.Slope) + regressionAccord.Intercept // ), fullYDataDailyAvg); //double r2 = MathNet.Numerics.GoodnessOfFit.CoefficientOfDetermination(predictedAccord, fullYDataDailyAvg); int degreesOfFreedom = _pointPair.ReadingsInNormalYear - 2; double ssx = Math.Sqrt(avgCddsForEachReadingInYear.Subtract(avgCddsForEachReadingInYear.Mean()).Pow(2).Sum()); double s = Math.Sqrt(((fullYDataDailyAvg.Subtract(predictedAccord).Pow(2)).Sum()) / degreesOfFreedom); double seSubB = s / ssx; double hypothesizedValue = 0; double myT = seSubB / regressionAccord.Slope; TTest tTest = new TTest( estimatedValue: regressionAccord.Slope, standardError: seSubB, degreesOfFreedom: degreesOfFreedom, hypothesizedValue: hypothesizedValue, alternate: OneSampleHypothesis.ValueIsDifferentFromHypothesis ); AccordResult accordResult = new AccordResult() { SimpleLinearRegression = regressionAccord, R2Accord = rAccord, IsSimpleSingleRegression = true, HeatingBP = _pointPair.HeatingBalancePoint, CoolingBP = _pointPair.CoolingBalancePoint, TTest = tTest, Intercept = regressionAccord.Intercept, bpPair = _pointPair }; if (tTest.Significant) { accordResults.Add(accordResult); } else { rejectedAccords.Add(accordResult); } } ; } catch (Exception e) { Console.WriteLine(normalParamsKey.AccID + " " + normalParamsKey.UtilID + " " + normalParamsKey.UnitID + " " + e.Message + e.StackTrace); } } //rejectedAccords = rejectedAccords.OrderByDescending(s => s.R2Accord).ToList(); //accordResults = accordResults.OrderByDescending(s => s.R2Accord).ToList(); return(accordResults); }
private AccordResult CalculateLinearRegression(List <BalancePointPair> allBalancePointPairs, WthNormalParams normalParamsKey) { var allBalancePointGroups = allBalancePointPairs.GroupBy(s => new { s.CoolingBalancePoint, s.HeatingBalancePoint }); List <AccordResult> accordResults = new List <AccordResult>(); foreach (var group in allBalancePointGroups) { try { List <BalancePointPair> IdenticalBalancePointPairsFromAllReadings = group.ToList(); BalancePointPair _pointPair = IdenticalBalancePointPairsFromAllReadings.First(); int readingsCount = IdenticalBalancePointPairsFromAllReadings.Count; double[] fullYData = new double[readingsCount]; double[] fullYDataDailyAvg = new double[readingsCount]; double[][] hcddMatrix = new double[readingsCount][]; double[][] hcddMatrixNonDaily = new double[readingsCount][]; foreach (BalancePointPair balancePointPair in IdenticalBalancePointPairsFromAllReadings) { fullYData[IdenticalBalancePointPairsFromAllReadings.IndexOf(balancePointPair)] = (balancePointPair.ActualUsage); fullYDataDailyAvg[IdenticalBalancePointPairsFromAllReadings.IndexOf(balancePointPair)] = (balancePointPair.ActualUsage / balancePointPair.DaysInReading); hcddMatrix[IdenticalBalancePointPairsFromAllReadings.IndexOf(balancePointPair)] = new double[] { (balancePointPair.HeatingDegreeDays / balancePointPair.DaysInReading), (balancePointPair.CoolingDegreeDays / balancePointPair.DaysInReading) }; } double[] avgHddsForEachReadingInYear = new double[readingsCount]; double[] avgCddsForEachReadingInYear = new double[readingsCount]; for (int i = 0; i < readingsCount; i++) { avgHddsForEachReadingInYear[i] = hcddMatrix[i][0]; avgCddsForEachReadingInYear[i] = hcddMatrix[i][1]; } double[] modelParams = new double[3]; modelParams[0] = 0; modelParams[1] = 0; modelParams[2] = 0; if (_pointPair.HeatingBalancePoint == 0 && _pointPair.CoolingBalancePoint == 0) { double[] onesVector = new double[readingsCount]; for (int i = 0; i < readingsCount; i++) { onesVector[i] = 1; } modelParams[0] = Fit.LineThroughOrigin(onesVector, fullYDataDailyAvg); OrdinaryLeastSquares ols = new OrdinaryLeastSquares() { UseIntercept = false }; double r2 = MathNet.Numerics.GoodnessOfFit.CoefficientOfDetermination( onesVector.Select(x => x * modelParams[0]), fullYDataDailyAvg); AccordResult accordResult = new AccordResult() { IsSimpleSingleRegression = true, HeatingBP = _pointPair.HeatingBalancePoint, CoolingBP = _pointPair.CoolingBalancePoint, Intercept = modelParams[0], R2Accord = r2, }; accordResults.Add(accordResult); } else if (_pointPair.CoolingBalancePoint != 0 && _pointPair.HeatingBalancePoint != 0) { try { MultipleLinearRegressionAnalysis mlra = new MultipleLinearRegressionAnalysis(intercept: true); mlra.Learn(hcddMatrix, fullYDataDailyAvg); var regressionAccord = mlra.Regression; double[] predicted = regressionAccord.Transform(hcddMatrix); double r2Accord = new RSquaredLoss(numberOfInputs: 2, expected: fullYDataDailyAvg) { Adjust = false }.Loss(predicted); double r2Coeff = regressionAccord.CoefficientOfDetermination(hcddMatrix, fullYDataDailyAvg, adjust: false); bool FTestFailed = !mlra.FTest.Significant; AccordResult accordResult = new AccordResult() { IsMultipleLinearRegression = true, HeatingBP = _pointPair.HeatingBalancePoint, CoolingBP = _pointPair.CoolingBalancePoint, Intercept = regressionAccord.Intercept, B2 = regressionAccord.Weights[0], B4 = regressionAccord.Weights[1], R2Accord = r2Accord, FTestFailed = FTestFailed }; if (mlra.Coefficients.All(x => x.TTest.Significant)) { accordResults.Add(accordResult); } } catch (Exception e) { Log.Debug(normalParamsKey.AccID + " " + normalParamsKey.UtilID + " " + normalParamsKey.UnitID + " " + e.Message + " " + e.StackTrace); } } else if (_pointPair.HeatingBalancePoint > 0) { OrdinaryLeastSquares ols = new OrdinaryLeastSquares() { UseIntercept = true }; SimpleLinearRegression regressionAccord = ols.Learn(avgHddsForEachReadingInYear, fullYDataDailyAvg); double[] predictedAccord = regressionAccord.Transform(avgHddsForEachReadingInYear); double r2Accord = new RSquaredLoss(1, fullYDataDailyAvg).Loss(predictedAccord); int degreesOfFreedom = normalParamsKey.MoCt - 2; double ssx = Math.Sqrt((avgHddsForEachReadingInYear.Subtract(avgHddsForEachReadingInYear.Mean())).Pow(2).Sum()); double s = Math.Sqrt(((fullYDataDailyAvg.Subtract(predictedAccord).Pow(2)).Sum()) / degreesOfFreedom); double error = regressionAccord.GetStandardError(avgHddsForEachReadingInYear, fullYDataDailyAvg); double seSubB = s / ssx; double hypothesizedValue = 0; TTest tTest = new TTest( estimatedValue: regressionAccord.Slope, standardError: seSubB, degreesOfFreedom: degreesOfFreedom, hypothesizedValue: hypothesizedValue, alternate: OneSampleHypothesis.ValueIsDifferentFromHypothesis ); AccordResult accordResult = new AccordResult() { IsSimpleSingleRegression = true, HeatingBP = _pointPair.HeatingBalancePoint, Intercept = regressionAccord.Intercept, B2 = regressionAccord.Slope, R2Accord = r2Accord }; if (tTest.Significant) { accordResults.Add(accordResult); } } else if (_pointPair.CoolingBalancePoint > 0) { OrdinaryLeastSquares ols = new OrdinaryLeastSquares() { UseIntercept = true }; SimpleLinearRegression regressionAccord = ols.Learn(avgCddsForEachReadingInYear, fullYDataDailyAvg); double[] predictedAccord = regressionAccord.Transform(avgCddsForEachReadingInYear); double rAccord = new RSquaredLoss(1, fullYDataDailyAvg).Loss(predictedAccord); int degreesOfFreedom = normalParamsKey.MoCt - 2; double ssx = Math.Sqrt(avgCddsForEachReadingInYear.Subtract(avgCddsForEachReadingInYear.Mean()).Pow(2).Sum()); double s = Math.Sqrt(((fullYDataDailyAvg.Subtract(predictedAccord).Pow(2)).Sum()) / degreesOfFreedom); double seSubB = s / ssx; double hypothesizedValue = 0; double myT = seSubB / regressionAccord.Slope; TTest tTest = new TTest( estimatedValue: regressionAccord.Slope, standardError: seSubB, degreesOfFreedom: degreesOfFreedom, hypothesizedValue: hypothesizedValue, alternate: OneSampleHypothesis.ValueIsDifferentFromHypothesis ); AccordResult accordResult = new AccordResult() { IsSimpleSingleRegression = true, CoolingBP = _pointPair.CoolingBalancePoint, Intercept = regressionAccord.Intercept, B4 = regressionAccord.Slope, R2Accord = rAccord }; if (tTest.Significant) { accordResults.Add(accordResult); } } ; } catch (Exception e) { Log.Debug(normalParamsKey.AccID + " " + normalParamsKey.UtilID + " " + normalParamsKey.UnitID + " " + e.Message + e.StackTrace); } } AccordResult accordWinner = accordResults .Where(s => s.Intercept >= 0) .OrderByDescending(s => s.R2Accord).ToList().FirstOrDefault(); return(accordWinner); }
private AccordResult CalculateLinearRegression(List <BalancePointPair> allBalancePointPairs, WthNormalParams normalParamsKey) { var allBalancePointGroups = allBalancePointPairs.GroupBy(s => new { s.CoolingBalancePoint, s.HeatingBalancePoint }); List <AccordResult> accordResults = new List <AccordResult>(); foreach (var group in allBalancePointGroups) { try { List <BalancePointPair> IdenticalBalancePointPairsFromAllReadings = group.ToList(); BalancePointPair _pointPair = IdenticalBalancePointPairsFromAllReadings.First(); int readingsCount = IdenticalBalancePointPairsFromAllReadings.Count; double[] fullYData = new double[readingsCount]; double[] fullYDataDailyAvg = new double[readingsCount]; double[][] hcddMatrix = new double[readingsCount][]; double[][] hcddMatrixNonDaily = new double[readingsCount][]; foreach (BalancePointPair balancePointPair in IdenticalBalancePointPairsFromAllReadings) { fullYData[IdenticalBalancePointPairsFromAllReadings.IndexOf(balancePointPair)] = (balancePointPair.ActualUsage); fullYDataDailyAvg[IdenticalBalancePointPairsFromAllReadings.IndexOf(balancePointPair)] = (balancePointPair.ActualUsage / balancePointPair.DaysInReading); hcddMatrix[IdenticalBalancePointPairsFromAllReadings.IndexOf(balancePointPair)] = new double[] { (balancePointPair.HeatingDegreeDays / balancePointPair.DaysInReading), (balancePointPair.CoolingDegreeDays / balancePointPair.DaysInReading) }; } if (!(fullYData.Sum() > 0)) { return(new AccordResult()); } double[] avgHddsForEachReadingInYear = new double[readingsCount]; double[] avgCddsForEachReadingInYear = new double[readingsCount]; for (int i = 0; i < readingsCount; i++) { avgHddsForEachReadingInYear[i] = hcddMatrix[i][0]; avgCddsForEachReadingInYear[i] = hcddMatrix[i][1]; } double[] modelParams = new double[3]; modelParams[0] = 0; modelParams[1] = 0; modelParams[2] = 0; if (_pointPair.HeatingBalancePoint == 0 && _pointPair.CoolingBalancePoint == 0) { double[] onesVector = new double[readingsCount]; for (int i = 0; i < readingsCount; i++) { onesVector[i] = 1; } modelParams[0] = Fit.LineThroughOrigin(onesVector, fullYDataDailyAvg); OrdinaryLeastSquares ols = new OrdinaryLeastSquares() { UseIntercept = false }; double r2 = MathNet.Numerics.GoodnessOfFit.CoefficientOfDetermination( onesVector.Select(x => x * modelParams[0]), fullYDataDailyAvg); AccordResult accordResult = new AccordResult() { IsSimpleSingleRegression = true, HeatingBP = _pointPair.HeatingBalancePoint, CoolingBP = _pointPair.CoolingBalancePoint, Intercept = modelParams[0], R2Accord = r2, //R2Accord = 0 }; accordResults.Add(accordResult); } else if (_pointPair.CoolingBalancePoint != 0 && _pointPair.HeatingBalancePoint != 0) { try { MultipleLinearRegressionAnalysis mlra = new MultipleLinearRegressionAnalysis(intercept: true); mlra.Learn(hcddMatrix, fullYDataDailyAvg); var regressionAccord = mlra.Regression; double[] predictedAccord = regressionAccord.Transform(hcddMatrix); double r2Accord = new RSquaredLoss(numberOfInputs: 2, expected: fullYDataDailyAvg) { Adjust = false }.Loss(predictedAccord); double r2Coeff = regressionAccord.CoefficientOfDetermination(hcddMatrix, fullYDataDailyAvg, adjust: false); bool FTestFailed = !mlra.FTest.Significant; AccordResult accordResult = new AccordResult() { IsMultipleLinearRegression = true, HeatingBP = _pointPair.HeatingBalancePoint, CoolingBP = _pointPair.CoolingBalancePoint, Intercept = regressionAccord.Intercept, B2 = regressionAccord.Weights[0], B4 = regressionAccord.Weights[1], R2Accord = r2Accord, FTestFailed = FTestFailed }; //int degreesOfFreedom = normalParamsKey.MoCt - 3; double degreesOfFreedomAsDouble = mlra.Regression.GetDegreesOfFreedom(readingsCount); int degreesOfFreedom = Convert.ToInt32(degreesOfFreedomAsDouble); //if (degreesOfFreedom != 9) //{ // Log.Warning($"Multivariable regression. DOF expected to be 9. is: {degreesOfFreedom}"); //} //if (degreesOfFreedom != dof) //{ // Console.WriteLine($"dof different. mlra.dof = {dof} expected = {degreesOfFreedom}"); //} double s = Math.Sqrt(fullYDataDailyAvg.Subtract(predictedAccord).Pow(2).Sum() / degreesOfFreedom); double ssxHdd = Math.Sqrt((avgHddsForEachReadingInYear.Subtract(avgHddsForEachReadingInYear.Mean())).Pow(2).Sum()); double ssxCdd = Math.Sqrt((avgCddsForEachReadingInYear.Subtract(avgCddsForEachReadingInYear.Mean())).Pow(2).Sum()); double seSubHdd = s / ssxHdd; double seSubCdd = s / ssxCdd; double tStatisticHdd = regressionAccord.Weights[0] / seSubHdd; double tStatisticCdd = regressionAccord.Weights[1] / seSubCdd; double tCriticalFivePercent = 2.262156; double tCriticalTenPercent = 1.833113; bool myTestHdd = Math.Abs(tStatisticHdd) >= tCriticalTenPercent; bool myTestCdd = Math.Abs(tStatisticCdd) >= tCriticalTenPercent; //if (myTestHdd != mlra.Coefficients[0].TTest.Significant && degreesOfFreedom != 9) //{ // Console.WriteLine($"nope. mystat - {tStatisticHdd} accordstat - {mlra.Coefficients[0].TTest.Statistic} " + // $"accordCritical - {mlra.Coefficients[0].TTest.CriticalValue}"); //} //if (myTestCdd != mlra.Coefficients[1].TTest.Significant && degreesOfFreedom != 9) //{ // Console.WriteLine($"nope. mystat - {tStatisticCdd} accordstat - {mlra.Coefficients[1].TTest.Statistic} " + // $"accordCritical - {mlra.Coefficients[1].TTest.CriticalValue}"); //} //if (mlra.Coefficients.All(x => x.TTest.Significant) && // mlra.Coefficients.All(x => x.Value > 0) && // mlra.Regression.Intercept > 0 && // r2Accord >= 0.7500) //{ // accordResults.Add(accordResult); //} if ( myTestHdd && myTestCdd && mlra.Coefficients.All(x => x.Value > 0) && mlra.Regression.Intercept > 0 //&& accordResult.R2Accord >= 0.75 ) { accordResults.Add(accordResult); } } catch (Exception e) { Log.Debug($"AccID/UtilID/UnitID: {normalParamsKey.AccID}/{normalParamsKey.UtilID}/{normalParamsKey.UnitID} >> " + $"MultipleLinearRegressionAnalysis Exception: {e.Message}"); } } else if (_pointPair.HeatingBalancePoint > 0) { OrdinaryLeastSquares ols = new OrdinaryLeastSquares() { UseIntercept = true }; SimpleLinearRegression regressionAccord = ols.Learn(avgHddsForEachReadingInYear, fullYDataDailyAvg); double[] predictedAccord = regressionAccord.Transform(avgHddsForEachReadingInYear); double r2Accord = new RSquaredLoss(1, fullYDataDailyAvg).Loss(predictedAccord); //int degreesOfFreedom = normalParamsKey.MoCt - 2; double degreesOfFreedomAsDouble = regressionAccord.GetDegreesOfFreedom(readingsCount); int degreesOfFreedom = Convert.ToInt32(degreesOfFreedomAsDouble); //if (degreesOfFreedom != 10) //{ // Log.Warning($"Single variable regression. DOF expected to be 10. is: {degreesOfFreedom}"); //} double ssx = Math.Sqrt((avgHddsForEachReadingInYear.Subtract(avgHddsForEachReadingInYear.Mean())).Pow(2).Sum()); double s = Math.Sqrt(fullYDataDailyAvg.Subtract(predictedAccord).Pow(2).Sum() / degreesOfFreedom); double error = regressionAccord.GetStandardError(avgHddsForEachReadingInYear, fullYDataDailyAvg); double seSubB = s / ssx; double hypothesizedValue = 0; double tStatistic = regressionAccord.Slope / seSubB; double tCriticalFivePercent = 2.228138; double tCriticalTenPercent = 1.812461; bool myTest = Math.Abs(tStatistic) >= tCriticalTenPercent; //TTest tTest = new TTest( // estimatedValue: regressionAccord.Slope, standardError: seSubB, degreesOfFreedom: degreesOfFreedom, // hypothesizedValue: hypothesizedValue, alternate: OneSampleHypothesis.ValueIsDifferentFromHypothesis // ); //if (myTest != tTest.Significant) //{ // Console.WriteLine($"nope. mystat - {tStatistic} accordstat - {tTest.Statistic} accordCritical - {tTest.CriticalValue}"); //} AccordResult accordResult = new AccordResult() { IsSimpleSingleRegression = true, HeatingBP = _pointPair.HeatingBalancePoint, Intercept = regressionAccord.Intercept, B2 = regressionAccord.Slope, R2Accord = r2Accord }; //if (tTest.Significant && accordResult.B2 > 0 && r2Accord >= 0.7500) //{ // accordResults.Add(accordResult); //} if (myTest && accordResult.B2 > 0 && accordResult.Intercept > 0 //&& r2Accord >= 0.7500 ) { accordResults.Add(accordResult); } } else if (_pointPair.CoolingBalancePoint > 0) { OrdinaryLeastSquares ols = new OrdinaryLeastSquares() { UseIntercept = true }; SimpleLinearRegression regressionAccord = ols.Learn(avgCddsForEachReadingInYear, fullYDataDailyAvg); double[] predictedAccord = regressionAccord.Transform(avgCddsForEachReadingInYear); double r2Accord = new RSquaredLoss(1, fullYDataDailyAvg).Loss(predictedAccord); //int degreesOfFreedom = normalParamsKey.MoCt - 2; double degreesOfFreedomAsDouble = regressionAccord.GetDegreesOfFreedom(readingsCount); int degreesOfFreedom = Convert.ToInt32(degreesOfFreedomAsDouble); //if (degreesOfFreedom != 10) //{ // Log.Warning($"Single variable regression. DOF expected to be 10. is: {degreesOfFreedom}"); //} double ssx = Math.Sqrt(avgCddsForEachReadingInYear.Subtract(avgCddsForEachReadingInYear.Mean()).Pow(2).Sum()); double s = Math.Sqrt(fullYDataDailyAvg.Subtract(predictedAccord).Pow(2).Sum() / degreesOfFreedom); double seSubB = s / ssx; double hypothesizedValue = 0; double tStatistic = regressionAccord.Slope / seSubB; double tCriticalFivePercent = 2.22813885198627; double tCriticalTenPercent = 1.812461; bool myTest = Math.Abs(tStatistic) >= tCriticalTenPercent; //TTest tTest = new TTest( // estimatedValue: regressionAccord.Slope, standardError: seSubB, degreesOfFreedom: degreesOfFreedom, // hypothesizedValue: hypothesizedValue, alternate: OneSampleHypothesis.ValueIsDifferentFromHypothesis // ); //if (myTest != tTest.Significant) //{ // Console.WriteLine($"nope. mystat - {tStatistic} accordstat - {tTest.Statistic} accordCritical - {tTest.CriticalValue}"); //} AccordResult accordResult = new AccordResult() { IsSimpleSingleRegression = true, CoolingBP = _pointPair.CoolingBalancePoint, Intercept = regressionAccord.Intercept, B4 = regressionAccord.Slope, R2Accord = r2Accord }; //if (tTest.Significant && accordResult.B4 > 0 && r2Accord >= 0.7500) //{ // accordResults.Add(accordResult); //} if ( myTest && accordResult.B4 > 0 //&& r2Accord >= 0.7500 ) { accordResults.Add(accordResult); } } } catch (Exception e) { Log.Debug($"AccID/UtilID/UnitID: {normalParamsKey.AccID}/{normalParamsKey.UtilID}/{normalParamsKey.UnitID} >> {e.Message} {e.StackTrace}"); } } AccordResult accordWinner = accordResults .Where(s => s.Intercept >= 0) .OrderByDescending(s => s.R2Accord).ToList().FirstOrDefault(); return(accordWinner); }