#!/usr/bin/python3 import csv, os, sys, math, json cpi_data = {} def get_cpi_data(cpi_data, year, month=12): return cpi_data[str(year)][month-1] def calculate_return(start, end, startYear, endYear, cpi_data, adjust_cpi): if adjust_cpi: # inflation adjusted ratio = (end/get_cpi_data(cpi_data, endYear))/(start/get_cpi_data(cpi_data, startYear)) else: # non inflation-adjusted ratio = end/start return 100*(math.pow(ratio, float(1)/(endYear - startYear)) - 1) dollar_shares_per_year = [] dollar_shares_cpi_per_year = [] def populate_dollar_shares_per_year(full_data, startYear, endYear): for year in range(startYear, endYear): number_of_shares = 100.0 / full_data[str(year) + '-12'] number_of_shares_cpi = 100.0 / (full_data[str(year) + '-12'] / get_cpi_data(cpi_data, year, 12)) for month in range(1, 12): number_of_shares += 100.0 / full_data[str(year+1) + '-%02d' % month] number_of_shares_cpi += 100.0 / (full_data[str(year+1) + '-%02d' % month] / get_cpi_data(cpi_data, year+1, month)) dollar_shares_per_year.append(number_of_shares) dollar_shares_cpi_per_year.append(number_of_shares_cpi) def calculate_dollar_return(full_data, trueStartYear, startYear, endYear, cpi_data, adjust_cpi): number_of_shares = 0 # Always start in December for year in range(startYear, endYear): if adjust_cpi: number_of_shares += dollar_shares_cpi_per_year[year-trueStartYear] else: number_of_shares += dollar_shares_per_year[year-trueStartYear] if adjust_cpi: number_of_shares /= get_cpi_data(cpi_data, endYear, 12) end_ratio = (number_of_shares*full_data[str(endYear) + '-12'])/(1200.0 * (endYear - startYear)) # spent 100.0/month # TODO - reaaaallly not sure about this return 100*(math.pow(end_ratio, float(1)/(endYear - startYear)) - 1) def print_returns(in_filename): data = [] full_data = {} reader = csv.reader(open(in_filename, 'r'), delimiter=',') for row in reader: data.append([row[0], float(row[1])]) full_data[row[0]] = float(row[1]) data = data[::-1] #print data realData = [] for d in data: if d[0][-2:] == '12': realData.append(d) data = realData reader = csv.reader(open('cpidata.csv', 'r'), delimiter=',') for row in reader: cpi_data[row[0]] = [float(x) for x in row[1:]] cpiReturnsData = [] noncpiReturnsData = [] cpiDollarReturnsData = [] noncpiDollarReturnsData = [] returnsData = {} returnsData['startYear'] = int(data[0][0][:4]) returnsData['endYear'] = int(data[-1][0][:4]) returnsData['cpiData'] = cpi_data populate_dollar_shares_per_year(full_data, returnsData['startYear'], returnsData['endYear']) for i in range(len(data)): cpiTempData = [] noncpiTempData = [] cpiDollarTempData = [] noncpiDollarTempData = [] yearI = int(data[i][0][:4]) for j in range(i+1, len(data)): yearJ = int(data[j][0][:4]) cpiTempData.append(calculate_return(data[i][1], data[j][1], yearI, yearJ, cpi_data, True)) noncpiTempData.append(calculate_return(data[i][1], data[j][1], yearI, yearJ, cpi_data, False)) cpiDollarTempData.append(calculate_dollar_return(full_data, returnsData['startYear'], yearI, yearJ, cpi_data, True)) noncpiDollarTempData.append(calculate_dollar_return(full_data, returnsData['startYear'], yearI, yearJ, cpi_data, False)) cpiReturnsData.append(cpiTempData) noncpiReturnsData.append(noncpiTempData) cpiDollarReturnsData.append(cpiDollarTempData) noncpiDollarReturnsData.append(noncpiDollarTempData) returnsData['cpiReturns'] = cpiReturnsData returnsData['noncpiReturns'] = noncpiReturnsData returnsData['cpiDollarReturns'] = cpiDollarReturnsData returnsData['noncpiDollarReturns'] = noncpiDollarReturnsData print(json.dumps(returnsData)) def main(): print_returns('sandpdata.csv') if (__name__ == '__main__'): main()