""" AUTHOR JONATHAN LAU""" import math """ def f(x): return 0.05+ 0.005* math.sqrt(1+x) return math.exp((-x**2.0)/2.0) ==> e^(-x^2/2), which is the integral in the cumulative distributive function """ def f(x): return math.exp((-x**2.0)/2.0) def mid_rule (a, b, n): h = (b-a)/float(n) total = 0.0 for i in range (1, n+1): z = (a + (i -0.5)*h ) total = total + f((a + (i- 0.5)*h)) return total * h def simp_rule(a, b, n): h = (b-a)/float(n) total = f(a) / 6.0 + f(b) / 6.0 for i in range (1, n): total = total + f(a + i*h) / 3.0 for i in range (1, n+1): total = total + 2 * f(a + (i-0.5) * h) / 3.0 return total * h def cum_distrib(x, n): total = 0.5 return total + 1/(math.sqrt(2.0 * math.pi)) * simp_rule(0.0, x, n) """ x refers to N(x); for example, N(0) should be equal to 1/2""" def cum_distrib_tol(tol, x): tolerance = float(tol) n = 4 old_int = cum_distrib(x,n) n = 2 * n new_int = cum_distrib(x,n) while abs(new_int - old_int) > tolerance: old_int = new_int n = 2* n new_int = cum_distrib(x,n) return (n, new_int, abs(new_int - old_int) ) """The previous definitions are tied to one another, it is to solve N(x), which is the cumulative distribution of the standard normal. I have repeated f(x) and simp approx below for other problems""" def f_all(x): return 0.05 / (1 + 2 * math.exp(-(1+x)**2.0) ) def simp_rule_all(a, b, n): h = (b-a)/float(n) total = f_all(a) / 6.0 + f_all(b) / 6.0 for i in range (1, n): total = total + f_all(a + i*h) / 3.0 for i in range (1, n+1): total = total + 2 * f_all(a + (i-0.5) * h) / 3.0 return total * h def disc_simp_rule_all (a, b, n): return math.exp(- simp_rule_all(a, b, n) ) def disc_simp_rule_all_tol (a, b, tol): tolerance = float(tol) n = 4 old_int = disc_simp_rule_all(a, b, n) n = 2 * n new_int = disc_simp_rule_all(a, b, n) while abs(new_int - old_int) > tolerance: old_int = new_int n = 2* n new_int = disc_simp_rule_all(a, b, n) return (n, new_int, abs(new_int - old_int) ) """Bond Calculator, bond price function given defined rate function""" def zero_rate (period): return 0.02 + period / (200.0 - period) def disc_factor(period): return math.exp( - period * zero_rate(period)) """ n = number of cash flows t = vector of cash flow dates (of size n) - think of this as your time part of the discount factor v = cash flows (of size n) """ def BondPrice_ZeroRate (n, t_cashflow, v_cashflow): B = 0.0 disc = range(0,n) for i in range(0, n): disc[i] = math.exp(- t_cashflow[i] * zero_rate(t_cashflow[i]) ) print disc[i] B = B + v_cashflow[i] * disc[i] return B """ number 8 on HW 2""" def Bond_BDC (n, t_cashflow, v_cashflow, y): B = 0.0 D = 0.0 C = 0.0 disc = range(0,n) for i in range (0,n): disc[i] = math.exp(- t_cashflow[i] * y) print disc[i] B = B + v_cashflow[i] * disc[i] D = D + t_cashflow[i] * v_cashflow[i] * disc[i] C = C + (t_cashflow[i] ** 2.0) * v_cashflow[i] * disc[i] return [B, D/B, C/B] def BondPrice_no9 (n, t_cashflow, v_cashflow): B = 0.0 disc = range(0,n) for i in range(0, n): disc[i] = disc_simp_rule_all(0, t_cashflow[i], 64) print disc[i] B = B + v_cashflow[i] * disc[i] return B def PVcouponCF(couponrate, numbcomp, period): return 100.0 * couponrate / numbcomp * discountfactor(period) """ print f(1.0) print discountfactor(1.0) print PVcouponCF(0.07, 2, 1.0) print mid_rule(0, 2, 8) print simp_rule(0, 2, 4) """ """ print cum_distrib(1.0,128) print cum_distrib_tol(10**-12, 1.0) print disc_factor(3/12.0) print BondPrice_ZeroRate(5, [3/12.0, 7/12.0, 11/12.0, 15/12.0, 19/12.0], [1, 1, 1, 1, 101] ) print Bond_BDC(4, [1/12.0, 7/12.0, 13/12.0, 19/12.0], [2, 2, 2, 102], 0.025 )""" print disc_simp_rule_all(0, 1.5, 32) print simp_rule_all(0, 1.5, 1000) print disc_simp_rule_all_tol (0, 2.0, 10**-8) print BondPrice_no9 (4, [0.5, 1.0, 1.5, 2.0], [2.5, 2.5, 2.5, 102.5])
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question