private void PopulateMyWthExpUsage() { //List<ReadingsQueryResult> allReadings = _weatherRepository.GetReadingsFromExpUsageOriginal(); List <ReadingsQueryResult> allReadings = _weatherRepository.GetReadingsFromExpUsageOriginalCorrected(); var readingsByAccount = allReadings.GroupBy(r => new { r.AccID, r.UtilID, r.RUnitID }); foreach (var group in readingsByAccount) { try { List <ReadingsQueryResult> readings = group.ToList(); WthNormalParams _params = _weatherRepository.GetParamsForReading(readings.First().AccID, readings.First().UtilID, readings.First().RUnitID); foreach (ReadingsQueryResult reading in readings) { WthExpUsage wthExpUsage = new WthExpUsage(); wthExpUsage.RdngID = reading.RdngID; wthExpUsage.AccID = reading.AccID; wthExpUsage.UtilID = reading.UtilID; wthExpUsage.UnitID = reading.RUnitID; decimal expUsage_Old = decimal.Round(Convert.ToDecimal(reading.ExpUsage), 4, MidpointRounding.AwayFromZero); wthExpUsage.ExpUsage_Old = expUsage_Old; wthExpUsage.Units = reading.Units; List <WeatherData> weatherDataList = _weatherRepository.GetWeatherDataByZipStartAndEndDate(_params.WthZipCode, reading.DateStart, reading.DateEnd); reading.B3 = _params.B3_New; reading.B5 = _params.B5_New; HeatingCoolingDegreeDays hcdd = HeatingCoolingDegreeDaysValueOf(reading, weatherDataList); decimal expUsage_New = _params.B1_New * reading.Days + (_params.B2_New * decimal.Round(Convert.ToDecimal(hcdd.HDD), 4, MidpointRounding.AwayFromZero)) + (_params.B4_New * decimal.Round(Convert.ToDecimal(hcdd.CDD), 4, MidpointRounding.AwayFromZero)); wthExpUsage.ExpUsage_New = expUsage_New; if (reading.Units != 0) { wthExpUsage.PercentDelta_Old = Math.Abs((expUsage_Old - reading.Units) / reading.Units); wthExpUsage.PercentDelta_New = Math.Abs((expUsage_New - reading.Units) / reading.Units); } wthExpUsage.DateStart = reading.DateStart; wthExpUsage.DateEnd = reading.DateEnd; _weatherRepository.InsertMyWthExpUsage(wthExpUsage); } } catch (Exception e) { Console.WriteLine(e.Message + " " + group.ToList().First().RdngID + " " + group.ToList().First().AccID + " " + group.ToList().First().RUnitID + "\n" + e.StackTrace); } } }
public bool GetWthNormalParamsExists(WthNormalParams normalParams) { bool exists; string sql = "select count(1) from WthNormalParams where AccID = @AccID and UtilID = @UtilID and UnitID = @UnitID"; using (IDbConnection db = new SqlConnection(_jitWebData3ConnectionString)) { exists = db.ExecuteScalar <bool>(sql, new { normalParams.AccID, normalParams.UtilID, normalParams.UnitID }); } return(exists); }
private string GetEndDateAfterFullYear(string YearDateStart, WthNormalParams normalParamsKey) { var dateTimeStart = new DateTime(); var dateTimeEnd = new DateTime(); string sqlForFirstReading = @"SELECT TOP(1) DateStart from Readings WHERE DateStart >= @DateStart and AccID = @AccID and UtilID = @UtilID and UnitID = @UnitID order by DateStart asc"; using (IDbConnection db = new SqlConnection(_jitWebData3ConnectionString)) { dateTimeStart = db.Query <DateTime>(sqlForFirstReading, new { DateStart = YearDateStart, normalParamsKey.AccID, normalParamsKey.UtilID, normalParamsKey.UnitID }).First(); } if (dateTimeStart > new DateTime(2017, 2, 1)) { throw new Exception("Readings did not start after 2016-12-1 within two months"); } string sqlForLastReading = @"Select top(1) DateEnd from Readings WHERE DateEnd >= @DateEnd and AccID = @AccID and UtilID = @UtilID and UnitID = @UnitID order by DateEnd asc"; using (IDbConnection dbTwo = new SqlConnection(_jitWebData3ConnectionString)) { dateTimeEnd = dbTwo.Query <DateTime>(sqlForLastReading, new { DateEnd = dateTimeStart.AddYears(1).AddDays(-1), normalParamsKey.AccID, normalParamsKey.UtilID, normalParamsKey.UnitID }).First(); } //string sqlForLastReading = @"Select top(1) DateEnd from Readings // where AccID = @AccID and UtilID = @UtilID and UnitID = @UnitID order by DateEnd desc"; //using (IDbConnection dbTwo = new SqlConnection(_jitWebData3ConnectionString)) //{ // dateTimeEnd = dbTwo.Query<DateTime>(sqlForLastReading, new // { // normalParamsKey.AccID, // normalParamsKey.UtilID, // normalParamsKey.UnitID // }).First(); //} return(dateTimeEnd.ToString()); }
public List <ReadingsQueryResult> GetReadingsForExpUsageUpdate(int MoID, WthNormalParams normalParams) { string DateEnd = GetMostRecentWeatherDataDate().AddDays(1).ToShortDateString(); var data = new List <ReadingsQueryResult>(); //string Sql = @"select r.RdngID, b.Zip, r.DateStart, r.DateEnd, r.Days, r.Units, // wnp.AccID, wnp.UtilID, wnp.UnitID, wnp.B1, wnp.B2, wnp.B3, wnp.B4, wnp.B5 // from Readings r // join WthNormalParams wnp on wnp.AccID = r.AccID // and wnp.UtilID = r.UtilID // and wnp.UnitID = r.UnitID // join Accounts a on a.AccID = r.AccID // join Buildings b on b.BldID = a.BldID // where // wnp.AccID = @AccID and // wnp.UtilID = @UtilID and // wnp.UnitID = @UnitID and // r.MoID >= @MoID and // r.DateEnd <= @DateEnd // and wnp.R2 is not null // order by DateStart asc"; string Sql = @"select r.RdngID, wnp.ZipW as Zip, r.DateStart, r.DateEnd, r.Days, r.Units, wnp.AccID, wnp.UtilID, wnp.UnitID, wnp.B1, wnp.B2, wnp.B3, wnp.B4, wnp.B5 from Readings r join WthNormalParams wnp on wnp.AccID = r.AccID and wnp.UtilID = r.UtilID and wnp.UnitID = r.UnitID where wnp.AccID = @AccID and wnp.UtilID = @UtilID and wnp.UnitID = @UnitID and r.MoID >= @MoID and r.DateEnd <= @DateEnd and wnp.R2 is not null order by DateStart asc"; using (IDbConnection db = new SqlConnection(_jitWebData3ConnectionString)) { return(db.Query <ReadingsQueryResult>(Sql, new { normalParams.AccID, normalParams.UtilID, normalParams.UnitID, MoID, DateEnd }).AsList()); } }
private bool UpdateOrInsertWthNormalParams(WthNormalParams normalParams) { bool success; bool normalParamsExists = _weatherRepository.GetWthNormalParamsExists(normalParams); if (normalParamsExists) { success = _weatherRepository.UpdateWthNormalParams(normalParams); if (success) { Log.Information($"Updated WthNormalParams >> AccID: {normalParams.AccID}. UtilID: {normalParams.UtilID} UnitID: {normalParams.UnitID} " + $"B1: {normalParams.B1} B2: {normalParams.B2} B3: {normalParams.B3} B4: {normalParams.B4} B5: {normalParams.B5} R2: {normalParams.R2}."); } else { Log.Error($"Failed to Update WthNormalParams >> AccID: {normalParams.AccID}. UtilID: {normalParams.UtilID} UnitID: {normalParams.UnitID} " + $"B1: {normalParams.B1} B2: {normalParams.B2} B3: {normalParams.B3} B4: {normalParams.B4} B5: {normalParams.B5} R2: {normalParams.R2}."); } } else { success = _weatherRepository.InsertWthNormalParams(normalParams); if (success) { Log.Information($"Inserted WthNormalParams >> AccID: {normalParams.AccID}. UtilID: {normalParams.UtilID} UnitID: {normalParams.UnitID} " + $"B1: {normalParams.B1} B2: {normalParams.B2} B3: {normalParams.B3} B4: {normalParams.B4} B5: {normalParams.B5} R2: {normalParams.R2}."); } else { Log.Error($"Failed to Insert into WthNormalParams >> AccID: {normalParams.AccID}. UtilID: {normalParams.UtilID} UnitID: {normalParams.UnitID} " + $"B1: {normalParams.B1} B2: {normalParams.B2} B3: {normalParams.B3} B4: {normalParams.B4} B5: {normalParams.B5} R2: {normalParams.R2}."); } } return(success); }
public bool InsertWthNormalParams(WthNormalParams normalParams) { int rowsAffected = 0; string sql = @" INSERT INTO [WthNormalParams] ( [AccID], [UtilID], [UnitID], [WstID], [ZipW], [B1], [B2], [B3], [B4], [B5], [R2], [EndDate], [EMoID], [MoCt] ) VALUES ( @AccID, @UtilID, @UnitID, @WstID, @ZipW, @B1, @B2, @B3, @B4, @B5, @R2, @EndDate, @EMoID, @MoCt )"; using (IDbConnection db = new SqlConnection(_jitWebData3ConnectionString)) { rowsAffected = db.Execute(sql, new { normalParams.AccID, normalParams.UtilID, normalParams.UnitID, normalParams.WstID, normalParams.ZipW, normalParams.B1, normalParams.B2, normalParams.B3, normalParams.B4, normalParams.B5, normalParams.R2, normalParams.EndDate, normalParams.EMoID, normalParams.MoCt }); } return(rowsAffected == 1); }
public bool UpdateWthNormalParams(WthNormalParams normalParams) { int rowsAffected = 0; string sql = @"update WthNormalParams set B1 = @B1, B2 = @B2, B3 = @B3, B4 = @B4, B5 = @B5, R2 = @R2, EndDate = @EndDate, EMoID = @EMoID where AccID = @AccID and UtilID = @UtilID and UnitID = @UnitID"; using (IDbConnection db = new SqlConnection(_jitWebData3ConnectionString)) { rowsAffected = db.Execute(sql, new { normalParams.B1, normalParams.B2, normalParams.B3, normalParams.B4, normalParams.B5, normalParams.R2, normalParams.EndDate, normalParams.EMoID, normalParams.AccID, normalParams.UtilID, normalParams.UnitID }); } return(rowsAffected == 1); }
private List <BalancePointPair> CalculateOneYearOfDegreeDaysForAllBalancePoints(string DesiredStartDate, WthNormalParams accountUtilAndUnit) { List <BalancePointPair> allBalancePointPairs = new List <BalancePointPair>(); List <ReadingsQueryResult> readings = _weatherRepository.GetReadingsForRegressionYear(DesiredStartDate, accountUtilAndUnit); if (readings.Count == 0) { return(allBalancePointPairs); } DateTime _ReadDateStart = readings.First().DateStart; DateTime _ReadDateEnd = readings.Last().DateEnd; int days = 0; foreach (ReadingsQueryResult reading in readings) { days += reading.Days; } foreach (ReadingsQueryResult reading in readings) { HeatingCoolingDegreeDays hcdd = new HeatingCoolingDegreeDays { CDD = 0.0, HDD = 0.0 }; List <WeatherData> weatherDataList = _weatherRepository.GetWeatherDataByZipStartAndEndDate(reading.Zip, reading.DateStart, reading.DateEnd); 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); } //for (int j = 0; j < range; j++) //{ // int[] allBoth = new int[2] { rangeMin + i, rangeMin + j }; // comboList.Add(allBoth); //} } 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) { reading.B3 = combo[0]; reading.B5 = combo[1]; hcdd = HeatingCoolingDegreeDaysValueOf(reading, weatherDataList); BalancePointPair balancePoint = new BalancePointPair { RdngID = reading.RdngID, DaysInReading = reading.Days, CoolingBalancePoint = reading.B5, HeatingBalancePoint = reading.B3, CoolingDegreeDays = hcdd.CDD, HeatingDegreeDays = hcdd.HDD, ActualUsage = reading.Units, YearOfReadsDateStart = _ReadDateStart, YearOfReadsDateEnd = _ReadDateEnd, ReadingsInNormalYear = readings.Count, DaysInNormalYear = days, WthZipCode = reading.Zip }; allBalancePointPairs.Add(balancePoint); } } return(allBalancePointPairs); }
public bool InsertWthNormalParams(WthNormalParams normalParams, bool Accord) { string sql = ""; if (!Accord) { sql = @" INSERT INTO [WthNormalParams] ([AccID], [UtilID], [UnitID], [WthZipCode], [B1_New], [B1_Original], [B2_New], [B2_Original], [B3_New], [B3_Original], [B4_New], [B4_Original], [B5_New], [B5_Original], [R2_New], [R2_Original], [YearOfReadsDateStart], [YearOfReadsDateEnd], [EndDate_Original], [Readings], [Days]) VALUES (@AccID, @UtilID, @UnitID, @WthZipCode, @B1_New, @B1_Original, @B2_New, @B2_Original, @B3_New, @B3_Original, @B4_New, @B4_Original, @B5_New, @B5_Original, @R2_New, @R2_Original, @YearOfReadsDateStart, @YearOfReadsDateEnd, @EndDate_Original, @Readings, @Days)"; } else if (Accord) { sql = @" INSERT INTO [WthNormalParamsAccord] ([AccID], [UtilID], [UnitID], [WthZipCode], [B1_New], [B1_Original], [B2_New], [B2_Original], [B3_New], [B3_Original], [B4_New], [B4_Original], [B5_New], [B5_Original], [R2_New], [R2_Original], [FTestFailed], [YearOfReadsDateStart], [YearOfReadsDateEnd], [EndDate_Original], [Readings], [Days]) VALUES (@AccID, @UtilID, @UnitID, @WthZipCode, @B1_New, @B1_Original, @B2_New, @B2_Original, @B3_New, @B3_Original, @B4_New, @B4_Original, @B5_New, @B5_Original, @R2_New, @R2_Original, @FTestFailed, @YearOfReadsDateStart, @YearOfReadsDateEnd, @EndDate_Original, @Readings, @Days)"; } //string sql = @" //INSERT INTO [WthNormalParams] ([AccID], [UtilID], [UnitID], [WthZipCode], //[B1] //[B2], [B3], [B4], [B5], [R2], [R2Original], [ReadDateStart], [ReadDateEnd], //[EndDateOriginal], [Readings], [Days]) //VALUES (@AccID, @UtilID, @UnitID, @WthZipCode, @B1_Original, @B1_New, @B2, @B3, @B4, @B5, @R2, @R2Original, @ReadDateStart, @ReadDateEnd, @EndDateOriginal, @Readings, @Days)"; using (IDbConnection db = new SqlConnection(_myConnectionString)) { int rowsAffected = db.Execute(sql, new { normalParams.AccID, normalParams.UtilID, normalParams.UnitID, normalParams.WthZipCode, normalParams.B1_New, normalParams.B1_Original, normalParams.B2_New, normalParams.B2_Original, normalParams.B3_New, normalParams.B3_Original, normalParams.B4_New, normalParams.B4_Original, normalParams.B5_New, normalParams.B5_Original, normalParams.R2_New, normalParams.R2_Original, normalParams.FTestFailed, normalParams.YearOfReadsDateStart, normalParams.YearOfReadsDateEnd, normalParams.EndDate_Original, normalParams.Readings, normalParams.Days }); return(rowsAffected == 1); } }
public List <ReadingsQueryResult> GetReadingsForExpUsageUpdate(string DateStart, WthNormalParams normalParams) { string ReadingDateEnd = GetMostRecentWeatherDataDate().AddDays(1).ToShortDateString(); var data = new List <ReadingsQueryResult>(); string Sql = @"select r.RdngID, b.Zip, r.DateStart, r.DateEnd, r.Days, r.UnitID as rUnitID, wnp.UnitID as wnpUnitID, wnp.B1, wnp.B2, wnp.B3, wnp.B4, wnp.B5 from Readings r join WthNormalParams wnp on wnp.AccID = r.AccID and wnp.UtilID = r.UtilID and wnp.UnitID = r.UnitID join Accounts a on a.AccID = r.AccID join Buildings b on b.BldID = a.BldID where wnp.AccID = @AccID and wnp.UtilID = @UtilID and wnp.UnitID = @UnitID and r.DateStart >= @DateStart and r.DateEnd <= @DateEnd order by DateStart asc"; using (IDbConnection db = new SqlConnection(_jitWebData3ConnectionString)) { return(db.Query <ReadingsQueryResult>(Sql, new { DateStart, ReadingDateEnd }).AsList()); } }
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); }
public List <ReadingsQueryResult> GetReadingsForRegressionYear(string DesiredStartDate, WthNormalParams normalParamsKey) { //string DateEnd = GetEndDateAfterFullYear(DesiredStartDate, normalParamsKey); //string Sql = @"select r.RdngID, b.Zip, r.DateStart, r.DateEnd, r.Days, r.Units, r.AccID, r.UnitID as rUnitID, // wnp.UnitID as wnpUnitID, wnp.B1, wnp.B2, wnp.B3, wnp.B4, wnp.B5, wnp.EndDate as EndDateOriginal // from Readings r // join WthNormalParams wnp on wnp.AccID = r.AccID // and wnp.UtilID = r.UtilID // and wnp.UnitID = r.UnitID // join Accounts a on a.AccID = r.AccID // and a.AccID = @AccID // and r.UtilID = @UtilID // and r.UnitID = @UnitID // join Buildings b on b.BldID = a.BldID // where r.DateStart >= @DateStart // and r.DateEnd <= @DateEnd // order by DateStart asc"; //using (IDbConnection db = new SqlConnection(_jitWebData3ConnectionString)) //{ // return db.Query<ReadingsQueryResult>(Sql, // new { normalParamsKey.AccID, normalParamsKey.UtilID, normalParamsKey.UnitID, DateStart = DesiredStartDate, DateEnd }).AsList(); //} string Sql = @"select r.RdngID, b.Zip, r.DateStart, r.DateEnd, r.Days, r.Units, r.AccID, r.UtilID, r.UnitID as rUnitID, wnp.UnitID as wnpUnitID, wnp.B1, wnp.B2, wnp.B3, wnp.B4, wnp.B5, wnp.EndDate as EndDateOriginal from Readings r join WthNormalParams wnp on wnp.AccID = r.AccID and wnp.UtilID = r.UtilID and wnp.UnitID = r.UnitID join Accounts a on a.AccID = r.AccID and a.AccID = @AccID and r.UtilID = @UtilID and r.UnitID = @UnitID join Buildings b on b.BldID = a.BldID where r.Yr = @Year order by DateStart asc"; using (IDbConnection db = new SqlConnection(_jitWebData3ConnectionString)) { return(db.Query <ReadingsQueryResult>(Sql, new { normalParamsKey.AccID, normalParamsKey.UtilID, normalParamsKey.UnitID, Year = DesiredStartDate }).AsList()); } }
private void PopulateMyWthExpUsage(AccordResult accord) { //List<ReadingsQueryResult> allReadings = _weatherRepository.GetReadingsFromExpUsageOriginal(); List <ReadingsQueryResult> allReadings = _weatherRepository.GetReadingsFromExpUsageOriginalCorrected(accord); var readingsByAccount = allReadings.GroupBy(r => new { r.AccID, r.UtilID, r.RUnitID }); foreach (var group in readingsByAccount) { try { List <ReadingsQueryResult> readings = group.ToList(); WthNormalParams _params = _weatherRepository.GetParamsForReading(readings.First().AccID, readings.First().UtilID, readings.First().RUnitID); foreach (ReadingsQueryResult reading in readings) { WthExpUsage wthExpUsage = new WthExpUsage(); wthExpUsage.RdngID = reading.RdngID; wthExpUsage.AccID = reading.AccID; wthExpUsage.UtilID = reading.UtilID; wthExpUsage.UnitID = reading.RUnitID; decimal expUsage_Old = decimal.Round(Convert.ToDecimal(reading.ExpUsage), 4, MidpointRounding.AwayFromZero); wthExpUsage.ExpUsage_Old = expUsage_Old; wthExpUsage.Units = reading.Units; List <WeatherData> weatherDataList = _weatherRepository.GetWeatherDataByZipStartAndEndDate(_params.WthZipCode, reading.DateStart, reading.DateEnd); decimal B1 = 0; decimal B2 = 0; decimal B4 = 0; reading.B3 = accord.HeatingBP; reading.B5 = accord.CoolingBP; if (accord.IsSimpleSingleRegression) { if (reading.B3 > 0) { B1 = decimal.Round(Convert.ToDecimal(accord.SimpleLinearRegression.Intercept), 12, MidpointRounding.AwayFromZero); B2 = decimal.Round(Convert.ToDecimal(accord.SimpleLinearRegression.Slope), 12, MidpointRounding.AwayFromZero); B4 = 0; } else if (reading.B5 > 0) { B1 = decimal.Round(Convert.ToDecimal(accord.SimpleLinearRegression.Intercept), 12, MidpointRounding.AwayFromZero); B2 = 0; B4 = decimal.Round(Convert.ToDecimal(accord.SimpleLinearRegression.Slope), 12, MidpointRounding.AwayFromZero); } } else { if (reading.B3 > 0 && reading.B5 > 0) { B1 = decimal.Round(Convert.ToDecimal(accord.MultipleRegression.Weights[0]), 12, MidpointRounding.AwayFromZero); B2 = decimal.Round(Convert.ToDecimal(accord.MultipleRegression.Weights[1]), 12, MidpointRounding.AwayFromZero); B4 = decimal.Round(Convert.ToDecimal(accord.MultipleRegression.Weights[2]), 12, MidpointRounding.AwayFromZero); } else if (reading.B3 > 0) { B1 = decimal.Round(Convert.ToDecimal(accord.MultipleRegression.Weights[0]), 12, MidpointRounding.AwayFromZero); B2 = decimal.Round(Convert.ToDecimal(accord.MultipleRegression.Weights[1]), 12, MidpointRounding.AwayFromZero); } else if (reading.B5 > 0) { B1 = decimal.Round(Convert.ToDecimal(accord.MultipleRegression.Weights[0]), 12, MidpointRounding.AwayFromZero); B4 = decimal.Round(Convert.ToDecimal(accord.MultipleRegression.Weights[1]), 12, MidpointRounding.AwayFromZero); } } HeatingCoolingDegreeDays hcdd = HeatingCoolingDegreeDaysValueOf(reading, weatherDataList); decimal expUsage_New = (B1 * reading.Days) + (B2 * decimal.Round(Convert.ToDecimal(hcdd.HDD), 12, MidpointRounding.AwayFromZero)) + (B4 * decimal.Round(Convert.ToDecimal(hcdd.CDD), 12, MidpointRounding.AwayFromZero)); wthExpUsage.ExpUsage_New = expUsage_New; if (reading.Units != 0) { wthExpUsage.PercentDelta_Old = Math.Abs((expUsage_Old - reading.Units) / reading.Units); wthExpUsage.PercentDelta_New = Math.Abs((expUsage_New - reading.Units) / reading.Units); } wthExpUsage.DateStart = reading.DateStart; wthExpUsage.DateEnd = reading.DateEnd; _weatherRepository.InsertMyWthExpUsage(wthExpUsage, Accord: true); } } catch (Exception e) { Console.WriteLine(e.Message + " " + group.ToList().First().RdngID + " " + group.ToList().First().AccID + " " + group.ToList().First().RUnitID + "\n" + e.StackTrace); } } }
private void PopulateWthNormalParams() { List <WthNormalParams> newNormalParamsList = new List <WthNormalParams>(); List <WthNormalParams> modelsWithNotTwelveReadings = new List <WthNormalParams>(); List <WthNormalParams> modelsWithReadingOrWeatherIssues = new List <WthNormalParams>(); List <WthNormalParams> modelsWithOtherIssues = new List <WthNormalParams>(); Log.Information("Starting PopulateWthNormalParams(). Searching WNRdngData01 stored procedure... "); List <WNRdngData> allWNRdngData = _weatherRepository.GetAllReadingsFromStoredProcedure(); var wNRdngDataGroups = allWNRdngData.GroupBy(s => new { s.AccID, s.UtilID, s.UnitID }); //List<WNRdngData> heyy = wNRdngDataGroups.Contains(s => new { }) int updateCount = 0; int insertCount = 0; int failCount = 0; foreach (var wNRdngGroup in wNRdngDataGroups) { List <WNRdngData> wNRdngList = wNRdngGroup.OrderBy(s => s.MoID).ToList(); WNRdngData lastRead = wNRdngList.LastOrDefault(); WthNormalParams normalParams = new WthNormalParams() { AccID = lastRead.AccID, UtilID = lastRead.UtilID, UnitID = lastRead.UnitID, WstID = lastRead.WstID, ZipW = lastRead.Zip, EndDate = lastRead.DateEnd, EMoID = lastRead.EMoID, MoCt = lastRead.MoCt }; if (wNRdngList.Count < 6) { Log.Error("There are only {0} readings for AccID/UtilID/UnitID: {1}/{2}/{3}. No model found.", wNRdngList.Count, normalParams.AccID, normalParams.UtilID, normalParams.UnitID); continue; } if (wNRdngList.Count % 12 != 0) { modelsWithNotTwelveReadings.Add(normalParams); Log.Warning($"wNRdngList is not a multiple of 12 .. {wNRdngList.Count} Readings.\"MoCt\": {normalParams.MoCt}, still proceeding..."); } bool normalParamsExists = _weatherRepository.GetWthNormalParamsExists(normalParams); try { List <BalancePointPair> allBalancePointStatsFromYear = CalculateOneYearOfDegreeDaysForAllBalancePoints(wNRdngList); //int daysInYear = allBalancePointStatsFromYear.FirstOrDefault().DaysInYear; //normalParams.DaysInYear = daysInYear; bool success = false; if (allBalancePointStatsFromYear.Count == 0) { Log.Error($"AccID/UtilID/UnitID: {normalParams.AccID}/{normalParams.UtilID}/{normalParams.UnitID} >> " + $"Why are there no BalancePointStatsFromYear?"); if (normalParamsExists) { success = UpdateOrInsertWthNormalParams(normalParams); if (success) { newNormalParamsList.Add(normalParams); updateCount++; } else { failCount++; } } else { success = UpdateOrInsertWthNormalParams(normalParams); if (success) { newNormalParamsList.Add(normalParams); insertCount++; } else { failCount++; } } continue; } AccordResult accord = CalculateLinearRegression(allBalancePointStatsFromYear, normalParams); //if (accord.R2Accord < 0.7500) //{ // success = UpdateOrInsertWthNormalParams(normalParams); // if (success && normalParamsExists) // { // newNormalParamsList.Add(normalParams); // updateCount++; // } // else if (success && !normalParamsExists) // { // newNormalParamsList.Add(normalParams); // insertCount++; // } // else // { // failCount++; // } // continue; //} normalParams.B1 = decimal.Round(Convert.ToDecimal(accord.Intercept), 9, MidpointRounding.AwayFromZero); if (accord.IsSimpleSingleRegression == true && accord.HeatingBP > 0) { normalParams.B2 = decimal.Round(Convert.ToDecimal(accord.B2), 9, MidpointRounding.AwayFromZero); normalParams.B3 = accord.HeatingBP; } else if (accord.IsSimpleSingleRegression == true && accord.CoolingBP > 0) { normalParams.B4 = decimal.Round(Convert.ToDecimal(accord.B4), 9, MidpointRounding.AwayFromZero); normalParams.B5 = accord.CoolingBP; } else if (accord.IsMultipleLinearRegression == true) { normalParams.B2 = decimal.Round(Convert.ToDecimal(accord.B2), 9, MidpointRounding.AwayFromZero); normalParams.B3 = accord.HeatingBP; normalParams.B4 = decimal.Round(Convert.ToDecimal(accord.B4), 9, MidpointRounding.AwayFromZero); normalParams.B5 = accord.CoolingBP; } if (!double.IsNaN(accord.R2Accord) && !Double.IsInfinity(accord.R2Accord)) { normalParams.R2 = decimal.Round(Convert.ToDecimal(accord.R2Accord), 9, MidpointRounding.AwayFromZero); } if (accord.FTestFailed) { Log.Warning($"AccID/UtilID/UnitID: {normalParams.AccID}/{normalParams.UtilID}/{normalParams.UnitID}: " + $"Best Regression Model's F-Test failed and model was not rejected. R2 = {normalParams.R2}"); } success = UpdateOrInsertWthNormalParams(normalParams); if (success && normalParamsExists) { newNormalParamsList.Add(normalParams); updateCount++; } else if (success && !normalParamsExists) { newNormalParamsList.Add(normalParams); insertCount++; } else if (!success) { modelsWithOtherIssues.Add(normalParams); failCount++; } } catch (BadWNRdngDataException bdEx) { failCount++; modelsWithReadingOrWeatherIssues.Add(normalParams); Log.Error(bdEx.Message + " " + bdEx.StackTrace); } catch (Exception e) { failCount++; modelsWithOtherIssues.Add(normalParams); Log.Error($"AccID/UtilID/UnitID: {normalParams.AccID}/{normalParams.UtilID}/{normalParams.UnitID} >> {e.Message} {e.StackTrace}"); } } foreach (WthNormalParams normalParams in modelsWithNotTwelveReadings) { Log.Warning($"AccID/UtilID/UnitID: {normalParams.AccID}/{normalParams.UtilID}/{normalParams.UnitID} >> " + $"Model was inserted but did not have (a multiple of) 12 readings from SP. "); } //foreach(WthNormalParams normalParams in modelsWithReadingOrWeatherIssues) //{ // Log.Warning($"AccID/UtilID/UnitID: {normalParams.AccID}/{normalParams.UtilID}/{normalParams.UnitID} >> No model found. " + // $"Readings had Bad/Null data from SP."); //} foreach (WthNormalParams normalParams in modelsWithOtherIssues) { Log.Error($"AccID/UtilID/UnitID: {normalParams.AccID}/{normalParams.UtilID}/{normalParams.UnitID} >> No model found. See daily log for more detail."); } if (failCount == 0) { Log.Information($"Finished PopulateWthNormalParams(). Rows Updated: {updateCount}. Rows Inserted: {insertCount}. Failures: {failCount}"); } else { Log.Warning($"Finished PopulateWthNormalParams() with failures. Rows Updated: {updateCount}. Rows Inserted: {insertCount}. Failures: {failCount}"); } //UpdateWthExpUsage(newNormalParamsList); }
private List <BalancePointPair> CalculateLinearRegression(List <BalancePointPair> allBalancePointPairs, WthNormalParams normalParamsKey) { var updatedBalancePointPairs = new List <BalancePointPair>(); var allBalancePointGroups = allBalancePointPairs.GroupBy(s => new { s.CoolingBalancePoint, s.HeatingBalancePoint }); foreach (var group in allBalancePointGroups) { List <BalancePointPair> IdenticalBalancePointPairsForAllReadings = group.ToList(); int readingsCount = IdenticalBalancePointPairsForAllReadings.Count; BalancePointPair _pointPair = IdenticalBalancePointPairsForAllReadings.First(); bool NonWeatherDependant = (normalParamsKey.B2_Original == 0 && normalParamsKey.B4_Original == 0); //List<double> expUsageDaily = new List<double>(); //List<double> hddsDaily = new List<double>(); //List<double> cddsDaily = new List<double>(); //List<double> actualUsageDaily = new List<double>(); double[] fullXData = new double[readingsCount]; //double[] fullXData_Original = new double[12 * balancePointPairGroup.Count]; double[] xDays = new double[readingsCount]; double[] fullYData = new double[readingsCount]; double[] fullYDataAvg = new double[readingsCount]; //double?[] hddsDaily = new double?[readingsCount * _pointPair.DaysInNormalYear]; //double?[] cddsDaily = new double?[readingsCount * _pointPair.DaysInNormalYear]; //double[][] hcddMatrix = new double[2][]; //hcddMatrix[0] = new double[readingsCount]; //hcddMatrix[1] = new double[readingsCount]; double[][] hcddMatrix = new double[readingsCount][]; foreach (BalancePointPair balancePointPair in IdenticalBalancePointPairsForAllReadings) { //double[] hddsDailyAvgInReading = new double[readingsCount]; //double[] cddsDailyAvgInReading = new double[readingsCount]; //expUsageDaily.Add(Convert.ToDouble(balancePointPair.ExpUsage) / balancePointPair.Days); //hddsDaily.Add(balancePointPair.HeatingDegreeDays / balancePointPair.Days); //cddsDaily.Add(balancePointPair.CoolingDegreeDays / balancePointPair.Days); //actualUsageDaily.Add(balancePointPair.ActualUsage / balancePointPair.Days); fullXData[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] = Convert.ToDouble(balancePointPair.ExpUsage_New); xDays[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] = balancePointPair.DaysInReading; fullYData[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] = (balancePointPair.ActualUsage); fullYDataAvg[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] = (balancePointPair.ActualUsage / balancePointPair.DaysInReading); //hddsDaily[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair) * ] // = balancePointPair.HddList[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)]; //cddsDaily[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] // = balancePointPair.CddList[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)]; //hddsDailyAvgInReading[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] // = (balancePointPair.HeatingDegreeDays / balancePointPair.DaysInReading); //cddsDailyAvgInReading[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] // = (balancePointPair.CoolingDegreeDays / balancePointPair.DaysInReading); hcddMatrix[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] = new double[] { (balancePointPair.HeatingDegreeDays / balancePointPair.DaysInReading), (balancePointPair.CoolingDegreeDays / balancePointPair.DaysInReading) }; //hcddMatrix[0][IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] = (balancePointPair.HeatingDegreeDays / balancePointPair.DaysInReading); //hcddMatrix[1][IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] = (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[] expUsageByDay = new double[daysInYear]; //double[] fullYDataDaiy = new double[daysInYear]; //List<double> expUsageByDay = new List<double>(); //List<double> fullYDataByDay = new List<double>(); //foreach (BalancePointPair balancePointPair in IdenticalBalancePointPairsForAllReadings) //{ // double dailyExpusage = 0; // dailyExpusage += normalParamsKey.B3 * balancePointPair.HddList[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)].Value; // dailyExpusage += normalParamsKey.B5 * balancePointPair.CddList[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)].Value; // expUsageByDay.Add(dailyExpusage); // fullYDataByDay.Add(balancePointPair.ActualUsage / balancePointPair.DaysInReading); //} //double[] hddsDailyArr = hddsDaily.ToArray(); //double[] cddsDailyArr = cddsDaily.ToArray(); //double[][] xy = new double[cddsDailyArr.Length][]; //for (int i = 0; i < cddsDailyArr.Length; i++) //{ // double[] row = new double[2]; // xy[i] = row; //} //for (int i = 0; i < hddsDailyArr.Length; i++) //{ // xy[i][0] = hddsDailyArr[i]; // xy[i][1] = cddsDailyArr[i]; //} //double[] p = Fit.LinearMultiDim(hcddMatrix, fullYDataAvg, // d => 1.0, // d => d[0], // d => d[1]); //Matrix<double>.Build.DenseOfColumnArrays(hcddMatrix); //Tuple<double, double> pSingular; double[] p = new double[3]; p[0] = 0; p[1] = 0; p[2] = 0; if (_pointPair.HeatingBalancePoint == 0 && _pointPair.CoolingBalancePoint == 0) { double[] onesVector = new double[readingsCount]; for (int i = 0; i < readingsCount; i++) { onesVector[i] = 1; } p[0] = Fit.LineThroughOrigin(onesVector, fullYDataAvg); } else if (_pointPair.CoolingBalancePoint != 0 && _pointPair.HeatingBalancePoint != 0) { p = MultipleRegression.QR(hcddMatrix, fullYDataAvg, intercept: true); } else if (_pointPair.CoolingBalancePoint == 0) { Tuple <double, double> heatingTuple = Fit.Line(avgHddsForEachReadingInYear, fullYDataAvg); p[0] = heatingTuple.Item1; p[1] = heatingTuple.Item2; } else if (_pointPair.HeatingBalancePoint == 0) { Tuple <double, double> coolingTuple = Fit.Line(avgCddsForEachReadingInYear, fullYDataAvg); p[0] = coolingTuple.Item1; p[2] = coolingTuple.Item2; } //double[] p = Fit.MultiDim(hcddMatrix, fullYDataAvg, intercept: true); //double[] fullXData_NewFit = new double[fullYDataAvg.Length]; double[] fullXData_NewFit = new double[readingsCount]; foreach (BalancePointPair balancePointPair in IdenticalBalancePointPairsForAllReadings) { double t1 = 0; if (IsDoubleNotNaNOrInfinity(p[0])) { t1 = p[0] * balancePointPair.DaysInReading; } double t2 = 0; if (IsDoubleNotNaNOrInfinity(p[1])) { t2 = p[1] * balancePointPair.HeatingDegreeDays; } double t3 = 0; if (IsDoubleNotNaNOrInfinity(p[2])) { t3 = p[2] * balancePointPair.CoolingDegreeDays; } fullXData_NewFit[IdenticalBalancePointPairsForAllReadings.IndexOf(balancePointPair)] = t1 + t2 + t3; } //double rBest = GoodnessOfFit.CoefficientOfDetermination( // hcddMatrix.Select(x => p[0] + (p[1] * x[0]) + (p[2] * x[1])), // fullYDataAvg); double rBest = GoodnessOfFit.CoefficientOfDetermination(fullXData_NewFit, fullYData); //double[,] comma = new double[hddsDailyArr.Length, hddsDailyArr.Length]; //for(int i = 0; i < hddsDailyArr.Length; i++) //{ // double left = xy[i / 2][0]; // double right = xy[i / 2][1]; // comma[i, i] = [left, right]; //} //double RSquared = GoodnessOfFit.CoefficientOfDetermination(xy.Select((x, y) => p[0], +(p[1] * xy[0]) + (p[2] * y)), ydata); //double RSquared = GoodnessOfFit.CoefficientOfDetermination(comma => p[0], +(p[1] * xy[0]) + (p[2] * y))), ydata); BalancePointPair groupLeader = _pointPair; double rSquared = GoodnessOfFit.CoefficientOfDetermination(fullXData, fullYData); //double rSquaredDaily = GoodnessOfFit.CoefficientOfDetermination(expUsageByDay, fullYDataByDay); double standardError_New = MathNet.Numerics.GoodnessOfFit.StandardError(fullXData, fullYData, groupLeader.ReadingsInNormalYear - 2); groupLeader.StandardError = standardError_New; if (!Double.IsNaN(rBest) && !Double.IsInfinity(rBest)) { groupLeader.RSquared_New = rBest; } else { groupLeader.RSquared_New = null; } groupLeader.B1_New = decimal.Round(Convert.ToDecimal(p[0]), 9, MidpointRounding.AwayFromZero); groupLeader.B2_New = decimal.Round(Convert.ToDecimal(p[1]), 9, MidpointRounding.AwayFromZero); groupLeader.B4_New = decimal.Round(Convert.ToDecimal(p[2]), 9, MidpointRounding.AwayFromZero); //if (NonWeatherDependant) //{ // groupLeader.RSquared = 0; // groupLeader.NewRSquaredNonWeather = 0; //double b = Fit.LineThroughOrigin(xDays, fullYData); //double[] newXData = new double[xDays.Length]; //for (int i = 0; i < newXData.Length; i++) //{ // newXData[i] = xDays[i] * b; //} //double NewRSquaredNonWeather = GoodnessOfFit.CoefficientOfDetermination(newXData, fullYData); //if (!Double.IsNaN(NewRSquaredNonWeather) && !Double.IsInfinity(NewRSquaredNonWeather)) //{ // groupLeader.NewRSquaredNonWeather = GoodnessOfFit.CoefficientOfDetermination(newXData, fullYData); // groupLeader.B1_New = decimal.Round(Convert.ToDecimal(b), 9, MidpointRounding.AwayFromZero); //} //else //{ // groupLeader.NewRSquaredNonWeather = null; // groupLeader.B1_New = null; //} //} updatedBalancePointPairs.Add(groupLeader); } return(updatedBalancePointPairs); }
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 void PopulateWthNormalParams() { List <WNRdngData> allWNRdngData = _weatherRepository.GetAllReadingsFromStoredProcedure(); var wNRdngDataGroups = allWNRdngData.GroupBy(s => new { s.AccID, s.UtilID, s.UnitID }); List <WthNormalParams> newNormalParamsList = new List <WthNormalParams>(); int updateCount = 0; int insertCount = 0; foreach (var wNRdngGroup in wNRdngDataGroups) { List <WNRdngData> wNRdngList = wNRdngGroup.OrderBy(s => s.MoID).ToList(); WNRdngData lastRead = wNRdngList.LastOrDefault(); WthNormalParams normalParams = new WthNormalParams() { AccID = lastRead.AccID, UtilID = lastRead.UtilID, UnitID = lastRead.UnitID, WstID = lastRead.WstID, ZipW = lastRead.Zip, EndDate = lastRead.DateEnd, EMoID = lastRead.EMoID, MoCt = lastRead.MoCt }; bool normalParamsExists = _weatherRepository.GetWthNormalParamsExists(normalParams); try { List <BalancePointPair> allBalancePointStatsFromYear = CalculateOneYearOfDegreeDaysForAllBalancePoints(wNRdngList); int daysInYear = allBalancePointStatsFromYear.FirstOrDefault().DaysInYear; normalParams.DaysInYear = daysInYear; if (allBalancePointStatsFromYear.Count == 0) { if (normalParamsExists) { _weatherRepository.UpdateWthNormalParams(normalParams); newNormalParamsList.Add(normalParams); updateCount++; Log.Debug($"Updated WthNormalParams >> AccID: {normalParams.AccID}. UtilID: {normalParams.UtilID} UnitID: {normalParams.UnitID} " + $"B1: {normalParams.B1} B2: {normalParams.B2} B3: {normalParams.B3} B4: {normalParams.B4} B5: {normalParams.B5} R2: {normalParams.R2}."); } else { _weatherRepository.InsertWthNormalParams(normalParams); newNormalParamsList.Add(normalParams); insertCount++; Log.Debug($"Inserted WthNormalParams >> AccID: {normalParams.AccID}. UtilID: {normalParams.UtilID} UnitID: {normalParams.UnitID} " + $"B1: {normalParams.B1} B2: {normalParams.B2} B3: {normalParams.B3} B4: {normalParams.B4} B5: {normalParams.B5} R2: {normalParams.R2}."); } continue; } AccordResult accord = CalculateLinearRegression(allBalancePointStatsFromYear, normalParams); if (accord.FTestFailed) { Log.Information("F Test failed... " + normalParams.AccID + " " + normalParams.UtilID + " " + normalParams.UnitID); } normalParams.B1 = decimal.Round(Convert.ToDecimal(accord.Intercept), 9, MidpointRounding.AwayFromZero); if (accord.IsSimpleSingleRegression == true && accord.HeatingBP > 0) { normalParams.B2 = decimal.Round(Convert.ToDecimal(accord.B2), 9, MidpointRounding.AwayFromZero); normalParams.B3 = accord.HeatingBP; } else if (accord.IsSimpleSingleRegression == true && accord.CoolingBP > 0) { normalParams.B4 = decimal.Round(Convert.ToDecimal(accord.B4), 9, MidpointRounding.AwayFromZero); normalParams.B5 = accord.CoolingBP; } else if (accord.IsMultipleLinearRegression == true) { normalParams.B2 = decimal.Round(Convert.ToDecimal(accord.B2), 9, MidpointRounding.AwayFromZero); normalParams.B3 = accord.HeatingBP; normalParams.B4 = decimal.Round(Convert.ToDecimal(accord.B4), 9, MidpointRounding.AwayFromZero); normalParams.B5 = accord.CoolingBP; } if (!Double.IsNaN(accord.R2Accord) && !Double.IsInfinity(accord.R2Accord)) { normalParams.R2 = decimal.Round(Convert.ToDecimal(accord.R2Accord), 9, MidpointRounding.AwayFromZero); } if (normalParamsExists) { _weatherRepository.UpdateWthNormalParams(normalParams); newNormalParamsList.Add(normalParams); updateCount++; Log.Debug($"Updated WthNormalParams >> AccID: {normalParams.AccID}. UtilID: {normalParams.UtilID} UnitID: {normalParams.UnitID} " + $"B1: {normalParams.B1} B2: {normalParams.B2} B3: {normalParams.B3} B4: {normalParams.B4} B5: {normalParams.B5} R2: {normalParams.R2}."); } else { _weatherRepository.InsertWthNormalParams(normalParams); newNormalParamsList.Add(normalParams); insertCount++; Log.Debug($"Inserted WthNormalParams >> AccID: {normalParams.AccID}. UtilID: {normalParams.UtilID} UnitID: {normalParams.UnitID} " + $"B1: {normalParams.B1} B2: {normalParams.B2} B3: {normalParams.B3} B4: {normalParams.B4} B5: {normalParams.B5} R2: {normalParams.R2}."); } } catch (Exception e) { Log.Error(normalParams.AccID + " " + normalParams.UtilID + " " + normalParams.UnitID + " " + e.Message + " " + e.StackTrace); } } Log.Information($"PopulateWthNormalParams Finished. Rows Updated: {updateCount}. Rows Inserted: {insertCount}."); UpdateWthExpUsage(newNormalParamsList); }
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 List <BalancePointPair> CalculateOneYearExpUsageForAllBalancePoints(string DesiredStartDate, WthNormalParams accountUtilAndUnit) { List <BalancePointPair> allBalancePointPairs = new List <BalancePointPair>(); List <ReadingsQueryResult> readings = _weatherRepository.GetReadingsForRegressionYear(DesiredStartDate, accountUtilAndUnit); DateTime _ReadDateStart = readings.First().DateStart; DateTime _ReadDateEnd = readings.Last().DateEnd; int days = 0; decimal expUsage_Original = 0; foreach (ReadingsQueryResult reading in readings) { days += reading.Days; //expUsage_Original += _weatherRepository.GetExpUsageOriginal(reading.RdngID); } foreach (ReadingsQueryResult reading in readings) { HeatingCoolingDegreeDays hcdd = new HeatingCoolingDegreeDays { CDD = 0.0, HDD = 0.0 }; List <WeatherData> weatherDataList = _weatherRepository.GetWeatherDataByZipStartAndEndDate(reading.Zip, reading.DateStart, reading.DateEnd); int coolingBalancePoint = reading.B5; int heatingBalancePoint = reading.B3; double coolingCoefficiant = reading.B4; double heatingCoefficiant = reading.B2; if (heatingCoefficiant == 0 && coolingCoefficiant == 0) { decimal expUsage = CalculateExpUsageForNomalizing(reading, hcdd); BalancePointPair balancePointPair = new BalancePointPair { RdngID = reading.RdngID, DaysInReading = reading.Days, CoolingBalancePoint = reading.B5, HeatingBalancePoint = reading.B3, CoolingDegreeDays = (double)hcdd.CDD, HeatingDegreeDays = (double)hcdd.HDD, ExpUsage_New = expUsage, ActualUsage = reading.Units, ExpUsage_Original = expUsage_Original, YearOfReadsDateStart = _ReadDateStart, YearOfReadsDateEnd = _ReadDateEnd, ReadingsInNormalYear = readings.Count, DaysInNormalYear = days, WthZipCode = reading.Zip }; allBalancePointPairs.Add(balancePointPair); } else { int[] allCoolingBalancePoints = new int[31]; for (int k = 45; k <= 75; k++) { allCoolingBalancePoints[k - 45] = k; } int[] allHeatingBalancePoints = new int[31]; for (int k = 45; k <= 75; k++) { allHeatingBalancePoints[k - 45] = k; } if (coolingCoefficiant > 0) { foreach (int coolingPointInstance in allCoolingBalancePoints) { reading.B5 = coolingPointInstance; if (heatingCoefficiant > 0) { foreach (int heatingPointInstance in allHeatingBalancePoints) { reading.B3 = heatingPointInstance; hcdd = HeatingCoolingDegreeDaysValueOf(reading, weatherDataList); decimal expUsage = CalculateExpUsageForNomalizing(reading, hcdd); BalancePointPair balancePoint = new BalancePointPair { RdngID = reading.RdngID, DaysInReading = reading.Days, CoolingBalancePoint = reading.B5, HeatingBalancePoint = reading.B3, CoolingDegreeDays = (double)hcdd.CDD, HeatingDegreeDays = (double)hcdd.HDD, ExpUsage_New = expUsage, ActualUsage = reading.Units, //ExpUsageOriginal = WthExpUsageOriginal, YearOfReadsDateStart = _ReadDateStart, YearOfReadsDateEnd = _ReadDateEnd, ReadingsInNormalYear = readings.Count, DaysInNormalYear = days, WthZipCode = reading.Zip }; allBalancePointPairs.Add(balancePoint); } } else { hcdd = HeatingCoolingDegreeDaysValueOf(reading, weatherDataList); decimal expUsage = CalculateExpUsageForNomalizing(reading, hcdd); BalancePointPair balancePoint = new BalancePointPair { RdngID = reading.RdngID, DaysInReading = reading.Days, CoolingBalancePoint = reading.B5, HeatingBalancePoint = reading.B3, CoolingDegreeDays = (double)hcdd.CDD, HeatingDegreeDays = (double)hcdd.HDD, ExpUsage_New = expUsage, ActualUsage = reading.Units, //ExpUsageOriginal = WthExpUsageOriginal, YearOfReadsDateStart = _ReadDateStart, YearOfReadsDateEnd = _ReadDateEnd, ReadingsInNormalYear = readings.Count, DaysInNormalYear = days, WthZipCode = reading.Zip }; allBalancePointPairs.Add(balancePoint); } } } else if (heatingCoefficiant > 0) { foreach (int heatingIncrement in allHeatingBalancePoints) { reading.B3 = heatingIncrement; hcdd = HeatingCoolingDegreeDaysValueOf(reading, weatherDataList); decimal expUsage = CalculateExpUsageForNomalizing(reading, hcdd); BalancePointPair balancePoint = new BalancePointPair { RdngID = reading.RdngID, DaysInReading = reading.Days, CoolingBalancePoint = reading.B5, HeatingBalancePoint = reading.B3, CoolingDegreeDays = (double)hcdd.CDD, HeatingDegreeDays = (double)hcdd.HDD, ExpUsage_New = expUsage, ActualUsage = reading.Units, //ExpUsageOriginal = WthExpUsageOriginal, YearOfReadsDateStart = _ReadDateStart, YearOfReadsDateEnd = _ReadDateEnd, ReadingsInNormalYear = readings.Count, DaysInNormalYear = days, WthZipCode = reading.Zip }; allBalancePointPairs.Add(balancePoint); } } } } return(allBalancePointPairs); }