private HeatingCoolingDegreeDays HeatingCoolingDegreeDaysValueOf(BPPairAccord bpPair, List <WeatherData> weatherDataList) { HeatingCoolingDegreeDays hcdd = new HeatingCoolingDegreeDays { CDD = 0.0, HDD = 0.0, }; foreach (WeatherData weatherData in weatherDataList) { if (!weatherData.AvgTmp.HasValue) { throw new Exception("WeatherData.AvgTmp is null for " + weatherData.ZipCode + " on " + weatherData.RDate); } else if (bpPair.CoolingBalancePoint > 0 && weatherData.AvgTmp >= bpPair.CoolingBalancePoint) { hcdd.CDD += (weatherData.AvgTmp.Value - bpPair.CoolingBalancePoint); } else if (bpPair.HeatingBalancePoint > 0 && weatherData.AvgTmp < bpPair.HeatingBalancePoint) { hcdd.HDD += (bpPair.HeatingBalancePoint - weatherData.AvgTmp.Value); } } return(hcdd); }
private AccordResultNew CalculateLinearRegression(List <BPPairAccord> allBalancePointPairs, NormalParamsAccord nPKey) { var allBalancePointGroups = allBalancePointPairs.GroupBy(s => new { s.CoolingBalancePoint, s.HeatingBalancePoint }); List <AccordResultNew> accordResults = new List <AccordResultNew>(); //List<AccordResult> rejectedAccords = new List<AccordResult>(); foreach (var group in allBalancePointGroups) { try { List <BPPairAccord> IdenticalBalancePointPairsFromAllReadings = group.ToList(); BPPairAccord _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 (BPPairAccord 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 (fullYData.Sum() == 0) //{ // AccordResultNew empty = new AccordResultNew(); // accordResults.Add(empty); //} 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!"); //} AccordResultNew accordResult = new AccordResultNew() { IsSimpleSingleRegression = true, HeatingBP = _pointPair.HeatingBalancePoint, CoolingBP = _pointPair.CoolingBalancePoint, Intercept = modelParams[0], R2Accord = r2, }; accordResults.Add(accordResult); } 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); bool FTestFailed = !mlra.FTest.Significant; //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); AccordResultNew accordResult = new AccordResultNew() { IsMultipleLinearRegression = true, //MultipleRegression = regressionAccord, 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); } //else //{ // rejectedAccords.Add(accordResult); //} } catch (Exception e) { Console.WriteLine(nPKey.AccID + " " + nPKey.UtilID + " " + nPKey.UnitID + " " + 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 r2Accord = 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 = nPKey.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 ); AccordResultNew accordResult = new AccordResultNew() { IsSimpleSingleRegression = true, HeatingBP = _pointPair.HeatingBalancePoint, Intercept = regressionAccord.Intercept, B2 = regressionAccord.Slope, R2Accord = r2Accord }; 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 = nPKey.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 ); AccordResultNew accordResult = new AccordResultNew() { IsSimpleSingleRegression = true, CoolingBP = _pointPair.CoolingBalancePoint, Intercept = regressionAccord.Intercept, B4 = regressionAccord.Slope, R2Accord = rAccord }; if (tTest.Significant) { accordResults.Add(accordResult); } //else //{ // rejectedAccords.Add(accordResult); //} } ; } catch (Exception e) { Console.WriteLine(nPKey.AccID + " " + nPKey.UtilID + " " + nPKey.UnitID + " " + e.Message + e.StackTrace); } } //rejectedAccords = rejectedAccords.OrderByDescending(s => s.R2Accord).ToList(); AccordResultNew accordWinner = accordResults .Where(s => s.Intercept >= 0) .OrderByDescending(s => s.R2Accord).ToList().FirstOrDefault(); return(accordWinner); }
private List <BPPairAccord> CalculateOneYearOfDegreeDaysForAllBalancePoints(List <WNRdngData> wNRdngData) { List <BPPairAccord> allBalancePointPairs = new List <BPPairAccord>(); DateTime _yearOfReadsDateStart = wNRdngData.First().DateStart; DateTime _yearOfReadsDateEnd = wNRdngData.Last().DateEnd; int _readingsCount = wNRdngData.First().MoID; int daysInYear = 0; foreach (WNRdngData reading in wNRdngData) { var t = reading.DateEnd.Subtract(reading.DateStart).Days; daysInYear += t; } foreach (WNRdngData reading in wNRdngData) { int daysInReading = reading.DateEnd.Subtract(reading.DateStart).Days; HeatingCoolingDegreeDays hcdd = new HeatingCoolingDegreeDays { CDD = 0.0, HDD = 0.0 }; List <WeatherData> weatherDataList = _weatherRepository.GetWeatherDataByZipStartAndEndDate(reading.Zip, reading.DateStart, reading.DateEnd); if (weatherDataList.Count != daysInReading) { Log.Error($"WeatherData.Count != daysInReading: " + weatherDataList.Count + " " + daysInReading + " AccID: " + reading.AccID + " UtilID: " + reading.UtilID + " UnitID: " + reading.UnitID + " Zip: " + reading.Zip + " MoID: " + reading.MoID); } int rangeMin = 45; int rangeMax = 75; int range = rangeMax - rangeMin + 1; List <int[]> comboList = new List <int[]>(); for (int i = 0; i < range; i++) { int[] hdsOnly = new int[2] { rangeMin + i, 0 }; int[] cdsOnly = new int[2] { 0, rangeMin + i }; comboList.Add(hdsOnly); comboList.Add(cdsOnly); int k = range - 1 - i; while (k >= 0) { int[] both = new int[2] { rangeMin + i, rangeMin + i + k }; k--; comboList.Add(both); } } comboList.Add(new int[] { 0, 0 }); //int expectedComboListCount = 0; ///* cases where (heating bp) != (cooling bp) */ //expectedComboListCount = (range + 1) * ((range) / 2); //if (range % 2 != 0) //{ // expectedComboListCount += ((range) / 2) + 1; //} ///* cases where (heating bp) != 0 && (cooling bp) == 0 or // * (heating bp) == 0 && (cooling bp) != 0; and // * (heating bp) == 0 && (cooling bp) == 0. // */ //expectedComboListCount += (range * 2) + 1; foreach (int[] combo in comboList) { //BalancePointPair bpPair = new BalancePointPair //{ // CoolingBalancePoint = combo[1], // HeatingBalancePoint = combo[0] //}; //hcdd = HeatingCoolingDegreeDaysValueOf(bpPair, weatherDataList); //bpPair.CoolingDegreeDays = hcdd.CDD; //bpPair.HeatingDegreeDays = hcdd.HDD; //bpPair.ActualUsage = reading.Units; //bpPair.YearOfReadsDateStart = _yearOfReadsDateStart; //bpPair.YearOfReadsDateEnd = _yearOfReadsDateEnd; //bpPair.ReadingsInNormalYear = _readingsCount; ////DaysInNormalYear = days; //bpPair.WthZipCode = reading.Zip; //bpPair.DaysInReading = daysInReading; BPPairAccord bpPair = new BPPairAccord { CoolingBalancePoint = combo[1], HeatingBalancePoint = combo[0] }; hcdd = HeatingCoolingDegreeDaysValueOf(bpPair, weatherDataList); bpPair.CoolingDegreeDays = hcdd.CDD; bpPair.HeatingDegreeDays = hcdd.HDD; bpPair.ActualUsage = reading.Units; bpPair.ZipCode = reading.Zip; bpPair.DaysInReading = daysInReading; bpPair.DaysInYear = daysInYear; allBalancePointPairs.Add(bpPair); } } return(allBalancePointPairs); }