From e6957eb7f488417a9b1c650eaaf3d4a9c5753339 Mon Sep 17 00:00:00 2001 From: Andi Gu Date: Sat, 15 Oct 2016 15:51:05 -0400 Subject: [PATCH 1/3] Initial commit --- solution.py | 33 +++++++++++++++++++++++++++++++++ test2.csv | 7 +++++++ 2 files changed, 40 insertions(+) create mode 100644 solution.py create mode 100644 test2.csv diff --git a/solution.py b/solution.py new file mode 100644 index 0000000..ab88800 --- /dev/null +++ b/solution.py @@ -0,0 +1,33 @@ +def derivative(x): # Derivative function of polynomial + ans = 0 + for i in range(n): + degree = n - i - 1 + ans += degree * cash_flow[i] * x ** (degree - 1) + return ans + + +def f(x): # Polynomial + ans = 0 + for i in range(n): + ans += cash_flow[i] * x ** (n - i - 1) + ans -= market_value[-1] + return ans + + +def solution(): + current = 1.5 + x = 20 # Adjust to higher value for more precision + for i in range(x): + current = current - (f(current) / derivative(current)) + return current - 1 + + +data = open("test.csv").readlines() +cash_flow = [] +market_value = [] +for line in data[1:]: + a, b, c = [float(i) for i in line.split(",")] + cash_flow.append(b) + market_value.append(c) +n = len(cash_flow) +print(solution()) diff --git a/test2.csv b/test2.csv new file mode 100644 index 0000000..8e23876 --- /dev/null +++ b/test2.csv @@ -0,0 +1,7 @@ + +0,1000,1000 +1,1500,2600 +2,0,2700 +3,0,3000 +4,0,3400 +5,0,3806 \ No newline at end of file From 7ab9adbbd37ad0813dd1a1b8d3d785589c8bac82 Mon Sep 17 00:00:00 2001 From: Andi Gu Date: Sat, 15 Oct 2016 15:52:42 -0400 Subject: [PATCH 2/3] Added a few comments --- solution.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/solution.py b/solution.py index ab88800..5d53497 100644 --- a/solution.py +++ b/solution.py @@ -9,20 +9,21 @@ def derivative(x): # Derivative function of polynomial def f(x): # Polynomial ans = 0 for i in range(n): - ans += cash_flow[i] * x ** (n - i - 1) - ans -= market_value[-1] + degree = n - i - 1 + ans += cash_flow[i] * x ** degree + ans -= market_value[-1] # Last market value return ans def solution(): current = 1.5 - x = 20 # Adjust to higher value for more precision + x = 500 # Adjust to higher value for more precision for i in range(x): current = current - (f(current) / derivative(current)) return current - 1 -data = open("test.csv").readlines() +data = open("test2.csv").readlines() cash_flow = [] market_value = [] for line in data[1:]: From dd71f9b7ae209d11f402025e6afa95581785603a Mon Sep 17 00:00:00 2001 From: Andi Gu Date: Sat, 15 Oct 2016 15:53:34 -0400 Subject: [PATCH 3/3] A few more comments! --- solution.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution.py b/solution.py index 5d53497..962aabe 100644 --- a/solution.py +++ b/solution.py @@ -16,7 +16,7 @@ def f(x): # Polynomial def solution(): - current = 1.5 + current = 1.5 # Guess for initial interest (current = 1 + x, therefore initial guess is 0.5%) x = 500 # Adjust to higher value for more precision for i in range(x): current = current - (f(current) / derivative(current))