diff --git a/bpfin/lib/other.py b/bpfin/lib/other.py index 793055b640e79de2586dd41d593ee3686c2ec9a3..075ef93037be395b27518db7ca4d57e01c0ac55a 100644 --- a/bpfin/lib/other.py +++ b/bpfin/lib/other.py @@ -4,6 +4,24 @@ import numpy as np from scipy.optimize import linprog +def add_year_dictionary(dict_1, dict_2): + summed_dictionary = {} + for year_1, item_1 in dict_1.items(): + for year_2, item_2 in dict_2.items(): + if year_1 == year_2: + summed_dictionary[year_1] = item_1 + item_2 + return summed_dictionary + + +def subtract_year_dictionary(dict_1, dict_2): + summed_dictionary = {} + for year_1, item_1 in dict_1.items(): + for year_2, item_2 in dict_2.items(): + if year_1 == year_2: + summed_dictionary[year_1] = item_1 - item_2 + return summed_dictionary + + def add_list(obj_list, number): """Add a number to each value in a list. @@ -45,7 +63,8 @@ def form_bill_calendar(date_start, year_term): 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)) + 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] @@ -69,7 +88,8 @@ def form_date_calendar(date_start, date_end): 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)) + 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 @@ -133,13 +153,15 @@ def form_inflation_ratedict(inflationyears, inflationhist): Returns: dictionary: {datetime, adjusted inflation} """ - inflationterms = form_bill_calendar(datetime.date(int(inflationyears[0]), 1, 31), len(inflationyears))[1] + inflationterms = form_bill_calendar( + datetime.date(int(inflationyears[0]), 1, 31), len(inflationyears))[1] inflation_rate = [] for year, rate in zip(inflationyears, inflationhist): for number 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%) + dict1 = dict(zip(inflationterms, + inflation_rate)) # return (2016,2,29 : 100.67%) return dict1 @@ -153,12 +175,14 @@ def form_inflation_coeffdict(inflationyears, inflationhist, present_date): Returns: dictionary: date:inflation relative to present date """ - inflation_rate_dict = form_inflation_ratedict(inflationyears, inflationhist) + 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) + new_dict[term] = cal_inflation_product( + inflation_rate_dict, term) / cal_inflation_product( + inflation_rate_dict, present_date) return new_dict @@ -178,6 +202,7 @@ def occupancy_final_list(occupancy_rate_list): ########################################### need to check duplication ################### + ##math calculations # divide list x by list y, return list z def division(x, y): @@ -255,7 +280,6 @@ def date_diff(startdate, enddate): # _, last_day_num = calendar.monthrange(obj_year, obj_month) # return last_day_num - # def form_bill_calendar(date_start, year_term): # # year_term=1 # bdstart = [] @@ -269,7 +293,6 @@ def date_diff(startdate, enddate): # day = last + datetime.timedelta(days=1) # return [bdstart, bdend] - # def form_date_calendar(date_start, date_end): # bdstart = [] # bdend = [] @@ -295,6 +318,7 @@ def form_year_terms(year): # print(form_year_terms(2013)) + def convert_timestamp_date(list1): return list(map(lambda x: datetime.date(x.year, x.month, x.day), list1)) @@ -324,7 +348,7 @@ def cal_cagr(start_value, end_value, n): float: anual growth rate """ if start_value != 0: - cagr = (end_value / start_value) ** (1 / n) - 1 + cagr = (end_value / start_value)**(1 / n) - 1 else: cagr = 0 return cagr @@ -334,7 +358,8 @@ def cal_cagr(start_value, end_value, n): def form_month_matrix(bdstart, bdend): monthmatrix = [[0 for i in range(12)] for j in range(len(bdstart))] # rangeID=0 - for (fromdate, enddate, rangeID) in zip(bdstart, bdend, range(len(bdstart))): + for (fromdate, enddate, rangeID) in zip(bdstart, bdend, + range(len(bdstart))): date = fromdate + datetime.timedelta(1) while date <= enddate: for month in range(1, 13): # range(1,13)=[1:12] @@ -352,8 +377,10 @@ def sumproduct(list1, list2): def monthly_average(daily, matrix): month_average = [] for month in range(1, 13): - matrixcolumn = list(map(lambda rangeID: matrix[rangeID][month - 1], range(len(daily)))) - total = sumproduct(daily, matrixcolumn) * days_in_month(month) / sum(matrixcolumn) + matrixcolumn = list( + map(lambda rangeID: matrix[rangeID][month - 1], range(len(daily)))) + total = sumproduct( + daily, matrixcolumn) * days_in_month(month) / sum(matrixcolumn) month_average.append(total) return month_average @@ -412,7 +439,8 @@ def align2term(terms1, list1, target_terms): # calculate loan's (loan amount/debt service), just like a payback def cal_loan_payback(interest, duration): - return ((1 + interest) ** duration - 1) / (interest * (1 + interest) ** duration) + return ( + (1 + interest)**duration - 1) / (interest * (1 + interest)**duration) # cauclate loan amount should be borrowed from each bank, given assumed total cost and loan info @@ -423,7 +451,8 @@ def loan_allocate(total_cost, loan_list): for loan in loan_list: sum_loan_max += loan.max_amount c_base.append( - 1 / loan.payback) # loan amount / ratio = DS, targeting minimum total DS, equivalent to longest payback + 1 / loan.payback + ) # loan amount / ratio = DS, targeting minimum total DS, equivalent to longest payback bound_list.append((0, loan.max_amount)) c = c_base # c_base is target function A = [-1] * len(loan_list) @@ -442,8 +471,11 @@ def loan_allocate(total_cost, loan_list): # def transpose_matrix(matrix1): # return [[r[col] for r in matrix1] for col in range(len(matrix1[0]))] + def month_shift(month): - month_shift_dict = dict(zip([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1])) + month_shift_dict = dict( + zip([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], + [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1])) return month_shift_dict[month] @@ -491,4 +523,3 @@ def month_shift(month): # 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)] - diff --git a/bpfin/tests/test_lib/__init__.py b/bpfin/tests/test_lib/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/bpfin/tests/test_lib/test_other.py b/bpfin/tests/test_lib/test_other.py new file mode 100644 index 0000000000000000000000000000000000000000..78a6f3da844409bf8587c1cfca23e3c01a54cfc1 --- /dev/null +++ b/bpfin/tests/test_lib/test_other.py @@ -0,0 +1,19 @@ +from bpfin.lib.other import add_year_dictionary, subtract_year_dictionary +from bpfin.tests.testdata import sample_data as db + + +def test_add_year_dictionary(): + year_1_input = db.dict_year_1 + year_2_input = db.dict_year_2 + output = db.dict_add + result = add_year_dictionary(year_1_input, year_2_input) + assert result == output + + +def test_subtract_year_dictionary(): + + year_1_input = db.dict_year_1 + year_2_input = db.dict_year_2 + output = db.dict_sub + result = subtract_year_dictionary(year_1_input, year_2_input) + assert result == output diff --git a/bpfin/tests/testdata/sample_data.py b/bpfin/tests/testdata/sample_data.py index 25b1038d454925047cd0b615ca95156dd2a85474..a187a3cbe54ea9ff5e2517b4122c85642a57feac 100644 --- a/bpfin/tests/testdata/sample_data.py +++ b/bpfin/tests/testdata/sample_data.py @@ -285,6 +285,14 @@ raw_income_input = { } } +dict_year_1 = {2012: 500, 2013: 1000, 2014: 2000} + +dict_year_2 = {2012: 200, 2013: 3000, 2014: 1000} + +dict_add = {2012: 700, 2013: 4000, 2014: 3000} + +dict_sub = {2012: 300, 2013: -2000, 2014: 1000} + income_statement_full = { 2014: { 'year': 2014, 'revenue': 90000, 'utility_expense': 55000, 'energy_opex': 35789.76719691614,