diff --git a/lib/calc_prelim_model.py b/financemodels/lib/calc_prelim_model.py similarity index 94% rename from lib/calc_prelim_model.py rename to financemodels/lib/calc_prelim_model.py index fc0bd60581ea7d6926965022868e7d84ac27db13..7665eb1d11687a2585e426dee1f16381b1511be5 100644 --- a/lib/calc_prelim_model.py +++ b/financemodels/lib/calc_prelim_model.py @@ -1,17 +1,8 @@ -import statsmodels.api as sm -from dateutil.parser import * -import calendar -import numpy as np -import pandas as pd -from datetime import * -import datetime, time # import datetime as dt -import xlrd -from scipy.optimize import linprog import matplotlib matplotlib.use('TkAgg') -from lib.prelim_model import * +from financemodels.lib.prelim_model import * # from buildingdata.models import IncomeStatement diff --git a/lib/prelim_model.py b/financemodels/lib/prelim_model.py similarity index 100% rename from lib/prelim_model.py rename to financemodels/lib/prelim_model.py diff --git a/financemodels/utilbills/billprojections.py b/financemodels/utilbills/billprojections.py new file mode 100644 index 0000000000000000000000000000000000000000..02119f7db8a4898be45158da2824f89a8300d6b9 --- /dev/null +++ b/financemodels/utilbills/billprojections.py @@ -0,0 +1,323 @@ +import statsmodels.api as sm +import datetime +import calendar + +year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + + +def add_list(obj_list, number): + return [x + number for x in obj_list] + + +def cal_last_day(obj_year, obj_month): + _, last_day_num = calendar.monthrange(obj_year, obj_month) + return last_day_num + + +def form_bill_calendar(date_start, year_term): + bdstart = [] + bdend = [] + day = date_start + for term in range(12 * year_term): + first = datetime.date(day=1, month=day.month, year=day.year) + bdstart.append(first) + last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) + bdend.append(last) + day = last + datetime.timedelta(days=1) + return [bdstart, bdend] + + +def form_date_calendar(date_start, date_end): + bdstart = [] + bdend = [] + day = date_start + i = 1 + while day <= date_end: + if i > 9999: + return False + first = datetime.date(day=1, month=day.month, year=day.year) + bdstart.append(first) + last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) + bdend.append(last) + day = last + datetime.timedelta(days=1) + i += 1 + return [bdstart, bdend] + + +def form_year_month(target_terms): + obj_list = [] + for term in target_terms: + obj_list.append((term.year, term.month)) + return obj_list + + +def cal_inflation_product(inflation_rate_dict, present_date): + date_start = sorted(inflation_rate_dict)[0] + product = 1 + for term in form_date_calendar(date_start, present_date)[1]: + product = product * inflation_rate_dict[term] + return product + + +def cal_inflated_item(base_list, target_terms, inflation_coeff_dict, + present_date): # base_list is the base value near the present month + new_dict = dict(zip([], [])) + for term in target_terms: + new_dict[term] = base_list[term.month - 1] * inflation_coeff_dict[term] + return [new_dict[term] for term in sorted(new_dict)] + + +def form_inflation_ratedict(inflationyears, inflationhist): + inflationterms = form_bill_calendar(datetime.date(int(inflationyears[0]), 1, 31), len(inflationyears))[1] + inflation_rate = [] + for year, rate in zip(inflationyears, inflationhist): + for i in range(12): + inflation_rate.append(rate / 12) + inflation_rate = add_list(inflation_rate, 1) + dict1 = dict(zip(inflationterms, inflation_rate)) # return (2016,2,29 : 100.67%) + return dict1 + + +def form_inflation_coeffdict(inflationyears, inflationhist, present_date): + inflation_rate_dict = form_inflation_ratedict(inflationyears, inflationhist) + inflationterms = sorted(inflation_rate_dict) + new_dict = dict(zip([], [])) + for term in inflationterms: + new_dict[term] = cal_inflation_product(inflation_rate_dict, term) / cal_inflation_product(inflation_rate_dict, + present_date) + return new_dict + + +def daily_average_use_or_charge(from_date, to_date, total_utility_use_or_charge): + fromdate = [] + todate = [] + for x in from_date: + fromdate.append(x) + for y in to_date: + todate.append(y) + interstore = [list(x) for x in zip(fromdate, todate)] + finalstore = [] + for x in interstore: + f = (x[1] - x[0]) + finalstore.append(f.days) + + bill_daily_average = [x / y for x, y in zip(total_utility_use_or_charge, finalstore)] + perbill_useorcharge = [] + for x in bill_daily_average: + perbill_useorcharge.append(x) + return perbill_useorcharge + + +# returns 2d list +# [0, 4, 20...] means first bill with 4 days in feb and 20 days in march +def firstmatrix(from_date, to_date): + rangeID = 0 + for x in from_date: + rangeID += 1 + monthmatrix = [[0 for i in range(12)] for j in range(rangeID)] + for (fromdate, enddate, rangeID) in zip(from_date, to_date, range(rangeID)): + date = fromdate + datetime.timedelta(1) + while date <= enddate: + for month in range(1, 13): # range(1,13)=[1:12] + if date.month == month: + monthmatrix[rangeID][month - 1] += 1 + date = date + datetime.timedelta(1) + return monthmatrix + + +# returns 2d list +# [0, 4, 20...] means for jan, bill 1 has 0 days, bill 2 has 4 days +def finalmatrix(matrixchoice): + newmatrix = [] + for i in range(len(matrixchoice[0])): + bill = [] + for item in matrixchoice: + bill.append(item[i]) + newmatrix.append(bill) + return newmatrix + + +def normalize_average(matrix_choice): + newlist = [] + for i in matrix_choice: + newlist.append((sum(list(i)))) + divider = [x / y for x, y in zip(newlist, year)] + return divider + + +# calculate charge and usage as one array with 12 values representing the month +def monthly_info(billing_info, matrix, divider): + monthly_average = [] + for month in range(1, 13): + matrixcolumn = [] + for rangeID in range(len(billing_info)): + matrixcolumn.append(matrix[rangeID][month - 1]) + z = [daily_avg * (days) for (daily_avg, days) in zip(billing_info, matrixcolumn)] + total = 0 + for i in range(len(z)): + total += z[i] + monthly_average.append(total) + monthly_values = [x / y for x, y in zip(monthly_average, divider)] + return monthly_values + + +# 12 prices which are the same +def hist_oil_price(oilcharge, oiluse): + a = sum(oilcharge) + b = sum(oiluse) + oprice = a / b + newlist = [] + for i in range(1, 13): + newlist.append(oprice) + return newlist + + +def monthly_price(charge, use): + finalmonthprice = [x / y for x, y in zip(charge, use)] + return finalmonthprice + + +def occupancy_final_list(occupancy_rate_list): + hours_in_day = 24 + occupancy_list = [] + for x in occupancy_rate_list: + occupancy_list.append(x * 24) + return occupancy_list + + +def regression_base_sum(bill_start_utility, bill_end_utility, x_value_calendar, x_values): + day = [] + month = [] + year = [] + for i in bill_start_utility: + day.append(i.day) + month.append(i.month) + year.append(i.year) + + day2 = [] + month2 = [] + year2 = [] + for i in x_value_calendar: + day2.append(i.day) + month2.append(i.month) + year2.append(i.year) + + datezipper = [(x, y, z) for x, y, z in zip(year, month, day)] + datezipper2 = [(x, y, z) for x, y, z in zip(year2, month2, day2)] + + list_subtractor = [x - y for x, y in zip(bill_end_utility, bill_start_utility)] + day_counter = [] + for number in list_subtractor: + if number is not None: + day_counter.append(abs(number.days)) + else: + day_counter.append(0) + + index_finder = [] + for i in datezipper: + for x in datezipper2: + if x == i: + index_finder.append(datezipper2.index(x)) + + list_creator = [] + for i in range(len(index_finder)): + list_creator.append(x_values[index_finder[i]:index_finder[i] + day_counter[i]]) + + x_totals = [] + for i in list_creator: + x_totals.append(sum(i)) + + return x_totals + + +def regression_coefficients(x_list, y_list, is_intercept): + X = x_list + Y = y_list + if is_intercept == True: + X = sm.add_constant(X) + model = sm.OLS(Y, X) + results = model.fit() + coefficients = results.params + return coefficients + + +def regression_predicting_y(reg_history, periodstart, periodend, x_value_calendar, x_values): + x_list = regression_base_sum(periodstart, periodend, x_value_calendar, x_values) + future_y = [] + for i in x_list: + future_y.append(reg_history[0] + reg_history[1] * i) + return future_y + + +def rate_plan_cons(date, con, limit, leap, supply_charge, basic_charge, tax_rate, adjustments): + temp_list = [] + y = date.month - 1 + for i in range(len(limit)): + if (i + 1) < (len(limit)) and con > limit[i + 1][y]: + first = ((limit[i + 1][y] - limit[i][y]) * leap[i][y]) + temp_list.append(first) + if (i + 1) < (len(limit)) and con < limit[i + 1][y] and con > limit[i][y]: + second = ((con - limit[i][y]) * leap[i][y]) + temp_list.append(second) + if (i + 1) == len(limit): + third = max(((con - limit[len(leap) - 1][y]) * leap[i][y]), 0) + temp_list.append(third) + else: + temp_list.append(0) + result = sum(temp_list) + con * (supply_charge[y]) + basic_charge[y] + final_result = result * (1 + tax_rate[y]) * (1 + adjustments[y]) + + return final_result + + +# This function needs to be worked on +def calc_charge_list(conlist, datelist, limit, leap, supply_charge, basic_charge, tax_rate, adjustments): + charge = [] + for con, date in zip(conlist, datelist): + temp_charge = rate_plan_cons(date, con, limit, leap, supply_charge, basic_charge, tax_rate, adjustments) + charge.append(temp_charge) + return charge + + +def price_x_consumption(price, con_list): + return [x * price for x in con_list] + + +def utility_charge_sum(list1, list2, list3): + return [x + y + z for x, y, z in zip(list1, list2, list3)] + + +def charge_dictionary(billperiod, charge_list): + bdday = [] + bdmonth = [] + bdyear = [] + + for i in billperiod: + bdmonth.append(i.month) + for i in billperiod: + bdyear.append(i.year) + + bdzipper = [(x, y) for x, y in zip(bdyear, bdmonth)] + + dicthelper = {key: value for key, value in zip(billperiod, charge_list)} + + return dicthelper + + +def charge_with_inflation(billperiod, bill_dictionary, inf_dictionary): + bdmonth = [] + bdyear = [] + for i in billperiod: + bdmonth.append(i.month) + for i in billperiod: + bdyear.append(i.year) + bdzipper = [(x, y) for x, y in zip(bdyear, bdmonth)] + list_helper = [] + for a, b in bill_dictionary.items(): + for c, d in inf_dictionary.items(): + if a == c: + list_helper.append(b * d) + dict_final = {key: value for key, value in zip(bdzipper, list_helper)} + + # return dict_final + return [(x, dict_final[x]) for x in sorted(dict_final)] diff --git a/financemodels/utilbills/calcbill.py b/financemodels/utilbills/calcbill.py new file mode 100644 index 0000000000000000000000000000000000000000..81538e565bedba564bd048cb3e21cc1d6cd9969f --- /dev/null +++ b/financemodels/utilbills/calcbill.py @@ -0,0 +1,323 @@ +from datetime import timedelta, datetime +# from buildingdata.models import Building, UtilityBill, RatePlan, RatePlanMonth +# from reference_data.models import Inflation, HddWeather, CddWeather +from .billprojections import * + + +def calculate_utility_bill(building_id): + inflation = Inflation.objects.all().order_by('year') + + inflation_year_info = [i.year for i in inflation] # [2014, 2015...] + inflation_rate_info = [i.inflation_rate for i in inflation] # [2.0, 2.3, ...] + current_date = datetime.date(2016, 9, 21) + + inflation_dictionary = form_inflation_coeffdict(inflation_year_info, inflation_rate_info, current_date) + + util_bill_el = UtilityBill.objects.filter(building_id=building_id, utility_type_id=1).order_by('from_date') + util_bill_gas = UtilityBill.objects.filter(building_id=building_id, utility_type_id=2).order_by('from_date') + + bd_start_el = [i.from_date for i in util_bill_el] # [1/1/2014, 4/30/2014, ...] + bd_start_gas = [i.from_date for i in util_bill_gas] + + bd_end_el = [i.to_date for i in util_bill_el] # [4/30/2014, 6/4/2014, ...] + bd_end_gas = [i.to_date for i in util_bill_gas] + + el_charge = [i.total_charge for i in util_bill_el] # [432, 123, ...] + gas_charge = [i.total_charge for i in util_bill_gas] + + daily_el_charge = daily_average_use_or_charge(bd_start_el, bd_end_el, el_charge) + daily_gas_charge = daily_average_use_or_charge(bd_start_gas, bd_end_gas, gas_charge) + + el_usage = [i.total_usage for i in util_bill_el] # [434, 4234, ...] + gas_usage = [i.total_usage for i in util_bill_gas] + + daily_el_use = daily_average_use_or_charge(bd_start_el, bd_end_el, el_usage) + daily_gas_use = daily_average_use_or_charge(bd_start_gas, bd_end_gas, gas_usage) + + month_matrix_el = firstmatrix(bd_start_el, bd_end_el) + month_matrix_gas = firstmatrix(bd_start_gas, bd_end_gas) + + new_matrix_el = finalmatrix(month_matrix_el) + new_matrix_gas = finalmatrix(month_matrix_gas) + + divider_el = normalize_average(new_matrix_el) + divider_gas = normalize_average(new_matrix_gas) + + monthly_use_el = monthly_info(daily_el_use, month_matrix_el, divider_el) + monthly_charge_el = monthly_info(daily_el_charge, month_matrix_el, divider_el) + + monthly_use_gas = monthly_info(daily_gas_use, month_matrix_gas, divider_gas) + monthly_charge_gas = monthly_info(daily_gas_charge, month_matrix_gas, divider_gas) + + util_bill_oil = UtilityBill.objects.filter(building_id=building_id, utility_type_id=3).order_by('from_date') + + oil_charge = [i.total_charge for i in util_bill_oil] # [434, 2352, ...] + oil_purchase = [i.total_usage for i in util_bill_oil] # [434, 234, 32, 4, ...] + + hist_oil_pricing = hist_oil_price(oil_charge, oil_purchase) + + hist_monthly_el_price = monthly_price(monthly_charge_el, monthly_use_el) + hist_monthly_gas_price = monthly_price(monthly_charge_gas, monthly_use_gas) + + starting_d0 = datetime.date(2012, 12, 1) + starting_d1 = datetime.date(2031, 1, 31) + + ending_d0 = datetime.date(2012, 12, 31) + ending_d1 = datetime.date(2031, 2, 1) + + occupancy_date = [] + starting_months = [] + ending_months = [] + + delta = starting_d1 - starting_d0 + for i in range(delta.days + 1): + my_date = starting_d0 + timedelta(days=i) + occupancy_date.append(my_date) + + if my_date.day is 1: + starting_months.append(my_date) + + delta = ending_d1 - ending_d0 + for i in range(delta.days + 1): + my_date = ending_d0 + timedelta(days=i) + + if my_date.day is 1: + ending_months.append(my_date - timedelta(days=1)) + + occupancy_rate = [0.95] * (delta.days + 3) + # I added 3 to match the Jupyter Notebook length + # This might be an issue of leap year day count + + occupancy_list = occupancy_final_list(occupancy_rate) + + hdd_data = HddWeather.objects.all().order_by('date') + cdd_data = CddWeather.objects.all().order_by('date') + + hdd_date = [] + hdd_projected = [] + for i in hdd_data: + hdd_date.append(i.date.date()) + hdd_projected.append(i.hdd) + + cdd_date = [] + cdd_projected = [] + for i in cdd_data: + cdd_date.append(i.date.date()) + cdd_projected.append(i.cdd) + + base_el_heat_hdd = regression_base_sum(bd_start_el, bd_end_el, hdd_date, hdd_projected) + base_el_cool_cdd = regression_base_sum(bd_start_el, bd_end_el, cdd_date, cdd_projected) + base_el_light_occ = regression_base_sum(bd_start_el, bd_end_el, occupancy_date, occupancy_list) + base_el_other_occ = regression_base_sum(bd_start_el, bd_end_el, occupancy_date, occupancy_list) + + base_gas_heat_hdd = regression_base_sum(bd_start_gas, bd_end_gas, hdd_date, hdd_projected) + base_gas_cool_cdd = regression_base_sum(bd_start_gas, bd_end_gas, cdd_date, cdd_projected) + base_gas_dhw_occ = regression_base_sum(bd_start_gas, bd_end_gas, occupancy_date, occupancy_list) + base_gas_other_occ = regression_base_sum(bd_start_gas, bd_end_gas, occupancy_date, occupancy_list) + + split_start_date_oil = [i.from_date for i in util_bill_oil] + split_end_date_oil = [i.to_date for i in util_bill_oil] + + base_oil_heat_hdd = regression_base_sum(split_start_date_oil, split_end_date_oil, hdd_date, hdd_projected) + base_oil_dhw_occ = regression_base_sum(split_start_date_oil, split_end_date_oil, occupancy_date, occupancy_list) + base_oil_other_occ = regression_base_sum(split_start_date_oil, split_end_date_oil, occupancy_date, occupancy_list) + + heat_el_use = [] + cool_el_use = [] + light_el_use = [] + other_el_use = [] + for i in util_bill_el: + heat_el_use.append(i.heat_usage) + cool_el_use.append(i.cool_usage) + light_el_use.append(i.light_usage) + other_el_use.append(i.other_usage) + + heat_gas_use = [] + cool_gas_use = [] + dhw_gas_use = [] + other_gas_use = [] + for i in util_bill_gas: + heat_gas_use.append(i.heat_usage) + cool_gas_use.append(i.cool_usage) + dhw_gas_use.append(i.dhw_usage) + other_gas_use.append(i.other_usage) + + heat_oil_use = [] + dhw_oil_use = [] + other_oil_use = [] + for i in util_bill_oil: + heat_oil_use.append(i.heat_usage) + dhw_oil_use.append(i.dhw_usage) + other_oil_use.append(i.other_usage) + + reg_el_heat_hdd = regression_coefficients(base_el_heat_hdd, heat_el_use, True) + reg_el_cool_cdd = regression_coefficients(base_el_cool_cdd, cool_el_use, True) + reg_el_light_occ = regression_coefficients(base_el_light_occ, light_el_use, True) + reg_el_other_occ = regression_coefficients(base_el_other_occ, other_el_use, True) + + reg_gas_heat_hdd = regression_coefficients(base_gas_heat_hdd, heat_gas_use, True) + reg_gas_cool_cdd = regression_coefficients(base_gas_cool_cdd, cool_gas_use, True) + reg_gas_dhw_occ = regression_coefficients(base_gas_dhw_occ, dhw_gas_use, True) + reg_gas_other_occ = regression_coefficients(base_gas_other_occ, other_gas_use, True) + + reg_oil_heat_hdd = regression_coefficients(base_oil_heat_hdd, heat_oil_use, True) + reg_oil_dhw_occ = regression_coefficients(base_oil_dhw_occ, dhw_oil_use, True) + reg_oil_other_occ = regression_coefficients(base_oil_other_occ, other_oil_use, True) + + reg_output_el_heat = regression_predicting_y(reg_el_heat_hdd, starting_months, + ending_months, hdd_date, hdd_projected) + reg_output_el_cool = regression_predicting_y(reg_el_cool_cdd, starting_months, + ending_months, cdd_date, cdd_projected) + reg_output_el_light = regression_predicting_y(reg_el_light_occ, starting_months, + ending_months, occupancy_date, occupancy_list) + reg_output_el_other = regression_predicting_y(reg_el_other_occ, starting_months, + ending_months, occupancy_date, occupancy_list) + + reg_output_gas_heat = regression_predicting_y(reg_gas_heat_hdd, + starting_months, + ending_months, + hdd_date, + hdd_projected) + + reg_output_gas_cool = regression_predicting_y(reg_gas_cool_cdd, + starting_months, + ending_months, + cdd_date, + cdd_projected) + + reg_output_gas_dhw = regression_predicting_y(reg_gas_dhw_occ, + starting_months, + ending_months, + occupancy_date, + occupancy_list) + + reg_output_gas_other = regression_predicting_y(reg_gas_other_occ, + starting_months, + ending_months, + occupancy_date, + occupancy_list) + + reg_output_oil_heat = regression_predicting_y(reg_oil_heat_hdd, starting_months, ending_months, + hdd_date, hdd_projected) + reg_output_oil_dhw = regression_predicting_y(reg_oil_dhw_occ, starting_months, ending_months, + occupancy_date, occupancy_list) + reg_output_oil_other = regression_predicting_y(reg_oil_other_occ, starting_months, ending_months, + occupancy_date, occupancy_list) + + building = Building.objects.get(id=building_id) + rate_plan_el = RatePlan.objects.get(id=building.electric_rate_plan_id, utility_type_id=1) + rate_plan_el_months = RatePlanMonth.objects.filter(rate_plan_id=rate_plan_el.id).order_by('month') + rate_plan_gas = RatePlan.objects.get(id=building.gas_rate_plan_id, utility_type_id=2) + rate_plan_gas_months = RatePlanMonth.objects.filter(rate_plan_id=rate_plan_gas.id).order_by('month') + + supply_charge_el = [] + basic_charge_el = [] + tax_rate_el = [] + adjustments_el = [] + + upper_limit_el_1 = [] + upper_limit_el_2 = [] + upper_limit_el_3 = [] + upper_limit_el_4 = [] + + upper_leap_el_1 = [] + upper_leap_el_2 = [] + upper_leap_el_3 = [] + upper_leap_el_4 = [] + + for i in rate_plan_el_months: + upper_limit_el_1.append(i.upper_limit_1) + upper_limit_el_2.append(i.upper_limit_2) + upper_limit_el_3.append(i.upper_limit_3) + upper_limit_el_4.append(i.upper_limit_4) + + upper_leap_el_1.append(i.leap_1) + upper_leap_el_2.append(i.leap_2) + upper_leap_el_3.append(i.leap_3) + upper_leap_el_4.append(i.leap_4) + + supply_charge_el.append(i.supply_charge) + basic_charge_el.append(i.delivery_charge) + tax_rate_el.append(i.tax_rate) + adjustments_el.append(i.adjustments) + + limit_el = [upper_limit_el_1, upper_limit_el_2, upper_limit_el_3, upper_limit_el_4] + leap_el = [upper_leap_el_1, upper_leap_el_2, upper_leap_el_3, upper_leap_el_4] + + supply_charge_gas = [] + basic_charge_gas = [] + tax_rate_gas = [] + adjustments_gas = [] + + upper_limit_gas_1 = [] + upper_limit_gas_2 = [] + upper_limit_gas_3 = [] + upper_limit_gas_4 = [] + + upper_leap_gas_1 = [] + upper_leap_gas_2 = [] + upper_leap_gas_3 = [] + upper_leap_gas_4 = [] + + for i in rate_plan_gas_months: + upper_limit_gas_1.append(i.upper_limit_1) + upper_limit_gas_2.append(i.upper_limit_2) + upper_limit_gas_3.append(i.upper_limit_3) + upper_limit_gas_4.append(i.upper_limit_4) + + upper_leap_gas_1.append(i.leap_1) + upper_leap_gas_2.append(i.leap_2) + upper_leap_gas_3.append(i.leap_3) + upper_leap_gas_4.append(i.leap_4) + + supply_charge_gas.append(i.supply_charge) + basic_charge_gas.append(i.delivery_charge) + tax_rate_gas.append(i.tax_rate) + adjustments_gas.append(i.adjustments) + + # In limit_gas and leap_gas, I only appended three limits and leaps. + # The correct number of limits and leaps must be noted!! + + limit_gas = [upper_limit_gas_1, upper_limit_gas_2, upper_limit_gas_3] + leap_gas = [upper_leap_gas_1, upper_leap_gas_2, upper_leap_gas_3] + + calc_charge_el_heat = calc_charge_list(reg_output_el_heat, ending_months, limit_el, leap_el, + supply_charge_el, basic_charge_el, tax_rate_el, adjustments_el) + calc_charge_el_cool = calc_charge_list(reg_output_el_cool, ending_months, limit_el, leap_el, + supply_charge_el, basic_charge_el, tax_rate_el, adjustments_el) + calc_charge_el_light = calc_charge_list(reg_output_el_light, ending_months, limit_el, leap_el, + supply_charge_el, basic_charge_el, tax_rate_el, adjustments_el) + calc_charge_el_other = calc_charge_list(reg_output_el_other, ending_months, limit_el, leap_el, + supply_charge_el, basic_charge_el, tax_rate_el, adjustments_el) + + calc_charge_gas_heat = calc_charge_list(reg_output_gas_heat, ending_months, limit_gas, leap_gas, + supply_charge_gas, basic_charge_gas, tax_rate_gas, adjustments_gas) + calc_charge_gas_cool = calc_charge_list(reg_output_gas_cool, ending_months, limit_gas, leap_gas, + supply_charge_gas, basic_charge_gas, tax_rate_gas, adjustments_gas) + calc_charge_gas_dhw = calc_charge_list(reg_output_gas_dhw, ending_months, limit_gas, leap_gas, + supply_charge_gas, basic_charge_gas, tax_rate_gas, adjustments_gas) + # gas_other is 0 right now + + calc_charge_gas_other = calc_charge_list(reg_output_gas_other, ending_months, limit_gas, leap_gas, + supply_charge_gas, basic_charge_gas, tax_rate_gas, adjustments_gas) + + calc_charge_oil_heat = price_x_consumption(hist_oil_pricing[1], reg_output_oil_heat) + calc_charge_oil_dhw = price_x_consumption(hist_oil_pricing[1], reg_output_oil_dhw) + calc_charge_oil_other = price_x_consumption(hist_oil_pricing[1], reg_output_oil_other) + + el_charge_no_savings = [w + x + y + z for w, x, y, z in + zip(calc_charge_el_heat, calc_charge_el_cool, calc_charge_el_light, + calc_charge_el_other)] + + # gas_other is not included + gas_charge_no_savings = [w + x + y for w, x, y in + zip(calc_charge_gas_heat, calc_charge_gas_cool, calc_charge_gas_dhw)] + + oil_charge_no_savings = [x + y + z for x, y, z in + zip(calc_charge_oil_heat, calc_charge_oil_dhw, calc_charge_oil_other)] + + bill_no_savings = utility_charge_sum(el_charge_no_savings, gas_charge_no_savings, oil_charge_no_savings) + bill_dict_no_savings = charge_dictionary(ending_months, bill_no_savings) + bill_dict_no_savings_inf = charge_with_inflation(ending_months, bill_dict_no_savings, inflation_dictionary) + + return bill_dict_no_savings_inf diff --git a/test/main.py b/test/testpreliminarymodel/test_preliminary_model.py similarity index 99% rename from test/main.py rename to test/testpreliminarymodel/test_preliminary_model.py index 5616d06849c801cbdbb12ce7ecca85a8b79de26a..ff67404a625895c83acf5f1b16ba53fa69184cd6 100644 --- a/test/main.py +++ b/test/testpreliminarymodel/test_preliminary_model.py @@ -1,5 +1,7 @@ -from lib.calc_prelim_model import pre_calc -import datetime, time +import datetime + +from financemodels.lib.calc_prelim_model import pre_calc + # import datetime as dt # hard number inputs diff --git a/test/testutilbills/Energy_Bill_Model_Python_WorkSheet_v1.2.xlsx b/test/testutilbills/Energy_Bill_Model_Python_WorkSheet_v1.2.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..6858ff02edb8d2ea68320292f06b6dfe6ccf00bf Binary files /dev/null and b/test/testutilbills/Energy_Bill_Model_Python_WorkSheet_v1.2.xlsx differ diff --git a/test/testutilbills/test_util_bills.py b/test/testutilbills/test_util_bills.py new file mode 100644 index 0000000000000000000000000000000000000000..83cd772c4f6c967bcf17750a0effb8aa648c40d8 --- /dev/null +++ b/test/testutilbills/test_util_bills.py @@ -0,0 +1,179 @@ +import pandas as pd +import datetime +from financemodels.utilbills.calcbill import calculate_utility_bill + +excel_master = pd.ExcelFile('./Energy_Bill_Model_Python_WorkSheet_v1.2.xlsx') + +customer_financials = excel_master.parse("Customer_Financials") +cash_flow = excel_master.parse("Project_Cash_Flow") +loan_information = excel_master.parse("Loan_Information") +utility_bill_w_savings = excel_master.parse("Utility_Bills_w_Savings") +utility_bill_no_savings = excel_master.parse("Utility_Bill_No_Saving") +project_economics = excel_master.parse("Project_Economics") +ecm_savings = excel_master.parse("ECM_Savings") +hist_el = excel_master.parse("Historical_Electricity_Bill") +split_el = excel_master.parse("Split_Electricity_Bill") +hist_gas = excel_master.parse("Historical_Gas_Bill") +split_gas = excel_master.parse("Split_Gas_Bill") +hist_oil = excel_master.parse("Historical_Oil_Bill") +split_oil = excel_master.parse("Split_Oil_Bill") +gas_rate_plan = excel_master.parse("Gas_Rate_Plans") +el_rate_plan = excel_master.parse("Electricity_Rate_Plans") +ecm_eng_info = excel_master.parse("Sheet1") +cdd_data = excel_master.parse("CDD") +hdd_data = excel_master.parse("HDD") +inflation_sheet = excel_master.parse("Inflation") + +# Engineering Input: Utility Use w/ Savings +bill_period_start_w_savings = utility_bill_w_savings["From Date"] +bill_period_end_w_savings = utility_bill_w_savings["End Date"] + +# Engineering Input: Utility Use w/o Savings +bd_start_wo_savings = utility_bill_no_savings["From Date"] +bd_end_wo_savings = utility_bill_no_savings["End Date"] + +# Project Economics + +# ECMSavings +ecm_electricity = ecm_savings["Savings on Electricity, kwh"] +ecm_gas = ecm_savings["Savings on Gas, mmBTU"] +ecm_oil = ecm_savings["Savings on Oil, mmBTU"] +ecm_id = ecm_savings["ECM ID"] + +# Historical Electricity Bill +bd_start_el = hist_el['From Date'] +bd_end_el = hist_el['To Date'] +el_usage = hist_el['Electric Usage (kWh)'] +el_charge = hist_el['Total Electric Charge'] + +# Split Electricity Bill +split_start_date_el = split_el["From Date"] +split_end_date_el = split_el["To Date"] +heat_el_use = split_el["Heat Electricity Use"] +cool_el_use = split_el["Cooling Electricity Use"] +light_el_use = split_el["Lighting Electricity Use"] +other_el_use = split_el["Other Use"] + +# Historical Gas Bill +bd_start_gas = hist_gas['From Date'] +bd_end_gas = hist_gas['To Date'] +gas_usage = hist_gas['Gas Usage (mmBTU)'] +gas_charge = hist_gas['Total Gas Charge'] + +# Split Gas Bill +split_start_date_gas = split_gas["From Date"] +split_end_date_gas = split_gas["To Date"] +heat_gas_use = split_gas["Heat Gas Use"] +cool_gas_use = split_gas["Cooling Gas Use"] +dhw_gas_use = split_gas["DHW Gas Use"] +other_gas_use = split_gas["Other Use"] + +# Historical Oil Bill +bd_start_oil = hist_oil["From Date"] +bd_end_oil = hist_oil["To Date"] +oil_charge = hist_oil["Total oil charge"] +oil_purchase = hist_oil["Oil Purchase (mmBTU)"] + +# Split Oil Bill +split_start_date_oil = split_oil["From Date"] +split_end_date_oil = split_oil["To Date"] +heat_oil_use = split_oil["Heat Oil Usage"] +dhw_oil_use = split_oil["DHW Oil Use"] +other_oil_use = split_oil["Other Oil Use"] + +# ECM Engineering Data +el_eng_from_date = ecm_eng_info["El From date"] +el_eng_to_date = ecm_eng_info["El To date"] +el_lighting_ecm_a_kwh = ecm_eng_info["Lighting Electricity ECM A kWh Savings"] +el_lighting_ecm_a_dollar = ecm_eng_info["Lighting Electricity ECM A Dollar Savings"] +ecm_el_total_kwh = ecm_eng_info["Total kWh Savings Electricity"] +ecm_el_total_dollar = ecm_eng_info["Total Dollar Savings Electricity"] +gas_eng_from_date = ecm_eng_info["Gas From date"] +gas_eng_to_date = ecm_eng_info["Gas To date"] +gas_dhw_ecm_a_mmbtu = ecm_eng_info["DHW Gas ECM A mmBTU Savings"] +gas_dhw_ecm_a_dollar = ecm_eng_info["DHW Gas ECM A Dollar Savings"] +gas_heating_ecm_b_mmbtu = ecm_eng_info["Heating Gas ECM B mmBTU Savings"] +gas_heating_ecm_b_dollar = ecm_eng_info["Heating Gas ECM B Dollar Savings"] +gas_heating_ecm_c_mmbtu = ecm_eng_info["Heating Gas ECM C mmBTU Savings"] +gas_heating_ecm_c_dollar = ecm_eng_info["Heating Gas ECM C Dollar Savings"] +gas_heating_ecm_d_mmbtu = ecm_eng_info["Heating Gas ECM D mmBTU Savings"] +gas_heating_ecm_d_dollar = ecm_eng_info["Heating Gas ECM D Dollar Savings"] +ecm_gas_total_mmbtu = ecm_eng_info["Total mmBTU Savings Gas"] +ecm_gas_total_dollar = ecm_eng_info["Total Dollar Savings Gas"] +oil_eng_from_date = ecm_eng_info["Oil From date"] +oil_eng_to_date = ecm_eng_info["Oil To date"] +oil_heating_ecm_a_mmbtu = ecm_eng_info["Heating Oil ECM A mmBTU Savings"] +oil_heating_ecm_a_dollar = ecm_eng_info["Heating Oil ECM A Dollar Savings"] +oil_dhw_ecm_b_mmbtu = ecm_eng_info["DHW Oil ECM B mmBTU Savings"] +oil_dhw_ecm_b_dollar = ecm_eng_info["DHW Oil ECM B Dollar Savings"] +ecm_oil_total_mmbtu = ecm_eng_info["Total mmBTU Savings Oil"] +ecm_oil_total_dollar = ecm_eng_info["Total Dollar Savings Oil"] + +# Months +year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + +# RatePlans +service_classification_el = el_rate_plan["Service Classification"] +service_classification_gas = gas_rate_plan["Service Classification"] + +basic_charge_el = el_rate_plan["Delivery basic charge"] +uplimit_1_el = el_rate_plan["Upper Limit 1"] +leap_1_el = el_rate_plan["Leap 1"] +uplimit_2_el = el_rate_plan["Upper Limit 2"] +leap_2_el = el_rate_plan["Leap 2"] +uplimit_3_el = el_rate_plan["Upper Limit 3"] +leap_3_el = el_rate_plan["Leap 3"] +uplimit_4_el = el_rate_plan["Upper Limit 4"] +leap_4_el = el_rate_plan["Leap 4"] +supply_charge_el = el_rate_plan["Supply Charge"] +tax_rate_el = el_rate_plan["Tax rate"] +adjustments_el = el_rate_plan["Other/Adjustment"] + +limit_el = [uplimit_1_el, uplimit_2_el, uplimit_3_el, uplimit_4_el] +leap_el = [leap_1_el, leap_2_el, leap_3_el, leap_4_el] +SC_1_el = [limit_el, leap_el] + +basic_charge_gas = gas_rate_plan["Delivery basic charge"] +uplimit_1_gas = gas_rate_plan["Upper Limit 1"] +leap_1_gas = gas_rate_plan["Leap 1"] +uplimit_2_gas = gas_rate_plan["Upper Limit 2"] +leap_2_gas = gas_rate_plan["Leap 2"] +uplimit_3_gas = gas_rate_plan["Upper Limit 3"] +leap_3_gas = gas_rate_plan["Leap 3"] +supply_charge_gas = gas_rate_plan["Supply charge"] +tax_rate_gas = gas_rate_plan["Tax rate"] +adjustments_gas = gas_rate_plan["Other/Adjustment"] + +limit_gas = [uplimit_1_gas, uplimit_2_gas, uplimit_3_gas] +leap_gas = [leap_1_gas, leap_2_gas, leap_3_gas] +SC_1_gas = [limit_gas, leap_gas] + +# HDD and CDD +hdd_date = hdd_data["Date"] +hdd_projected = hdd_data["Projected HDD"] +cdd_date = cdd_data["Date"] +cdd_projected = cdd_data["Projected CDD"] + +# Inflation +inflation_rate_info = inflation_sheet["inflation"] +inflation_year_info = inflation_sheet["inflationyear"] +current_date = datetime.date(2015, 1, 31) + +if __name__ == "__main__": + solve = calculate_utility_bill(inflation_rate_info, inflation_year_info, current_date, + bd_start_el, bd_end_el, el_charge, + bd_start_gas, bd_end_gas, gas_charge, + el_usage, gas_usage, + oil_charge, oil_purchase, + hdd_date, hdd_projected, + cdd_date, cdd_projected, + split_start_date_oil, split_end_date_oil, + basic_charge_el, limit_el, leap_el, + tax_rate_el, adjustments_el, supply_charge_el, + basic_charge_gas, limit_gas, leap_gas, + tax_rate_gas, adjustments_gas, supply_charge_gas, + heat_el_use, cool_el_use, light_el_use, other_el_use, + heat_gas_use, cool_gas_use, dhw_gas_use, other_gas_use, + heat_oil_use, dhw_oil_use, other_oil_use + ) + print(solve)