From e73f193667766857cedc7b3f237d2accb008b409 Mon Sep 17 00:00:00 2001 From: Arthur Date: Sat, 15 Oct 2016 15:20:18 -0400 Subject: [PATCH 1/2] test pull request --- test.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..45a72c4 --- /dev/null +++ b/test.py @@ -0,0 +1 @@ +print("testing pull") From 2f225476f3cf4f8dfc22a8f925d42354cbf2c256 Mon Sep 17 00:00:00 2001 From: Arthur Date: Sat, 15 Oct 2016 15:54:47 -0400 Subject: [PATCH 2/2] real submission --- hw-lightning.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ test.py | 1 - 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 hw-lightning.py delete mode 100644 test.py diff --git a/hw-lightning.py b/hw-lightning.py new file mode 100644 index 0000000..ff8b6ca --- /dev/null +++ b/hw-lightning.py @@ -0,0 +1,52 @@ +def getMwr(allSnaps): + ''' + Uses Newtons method for finding IRR/mwr #UW MATH represent + It converges quadratically aka it's pretty fast :) + + I was gonna use brent's method but that's not as clear. + I decided to use Newton's since it's much clearer code wise + ''' + numIterations=0 + maxIterations=400 + allowedError=0.0001 #10x the precision + + prevGuess=[1, 0] #prevGuess[0] = 2nd last guess, prevGuess[1] = last guess + finalMarketValue=allSnaps[-1][2] + numPeriods = len(allSnaps) + + while(abs(prevGuess[0] - prevGuess[1]) > allowedError): + prevGuess[0] = prevGuess[1] + derivRate = derivIrr(allSnaps, prevGuess[0], numPeriods) + eir = (evalIrr(allSnaps, prevGuess[0], numPeriods)-finalMarketValue) + #Will cause div zero error, and having a zero derivRate means you + #contribute nothing but gained market value (money from thin air?) + if (derivRate == 0): + return "undefined" + prevGuess[1] = prevGuess[0] - eir / derivRate + numIterations+=1 + #Too many iterations probably means non-convergence + if (numIterations > maxIterations): + return "undefined" + return prevGuess[1] + +def evalIrr(allSnaps, testIrr, numYears): + retVal = 0 + for year, cf, mv in allSnaps: + retVal += cf*(1+testIrr)**(numYears-year-1) + return retVal + +def derivIrr(allSnaps, testIrr, numYears): + retVal = 0 + for year, cf, mv in allSnaps: + retVal += (numYears-year-1)*cf*(1+testIrr)**(numYears-year-2) + return retVal + +f = open('test.csv', 'r') + +#Parsing +#convert input into nested list +allSnaps = [] +for snapshot in f.read().split()[1:]: #ignore line 0 b/c it's not numbers + allSnaps.append([float(number) for number in snapshot.split(",")]) + +print(getMwr(allSnaps)) diff --git a/test.py b/test.py deleted file mode 100644 index 45a72c4..0000000 --- a/test.py +++ /dev/null @@ -1 +0,0 @@ -print("testing pull")