def durationBond(rate, couponRate, maturity): """Objective : estimte the durtion for a given bond rate : discount rate couponRate: coupon rate maturity : number of years Example 1: >>>discountRate=0.1 >>>couponRate=0.04 >>> n=4 >>>durationBond(rate,couponRate,n) 3.5616941835365492 Example #2>>>durationBond(0.1,0.04,4) 3.7465335177625576 """ d = 0 n = maturity for i in sp.arange(n): d += (i + 1) * sp.pv(rate, i + 1, 0, -couponRate) d += n * sp.pv(rate, n, 0, -1) return d / sp.pv(rate, n, -couponRate, -1)
def myPV(r, n, fv): """ Objective: estimate present value of one future cash flow r : period rate n : number of periods fv: fture value formula used : pv=fv/(1+r)**n Example 1: >>>myPV(0.1,1,100) 90.9090909090909 Example #2 >>>myPV(r=0.1,fv=100,n=1) 90.9090909090909 """ import scipy as sp return (-sp.pv(r, n, 0, fv))
def test_port(start_year, h, r=discount_r, ws_bench=0.6, c=1.8, v_th=100, eta=1.5, lookback=50, full_ret=full_ret): v_init = sp.pv(r, h, c, -v_th) v = v_init v_bench = v_init time_remain = h i = 0 while time_remain >= 1: train_ret = full_ret.loc[(start_year + i - lookback):(start_year + i - 1)] policy = pd.Series() for s in [t * 0.05 for t in range(0, 21)]: policy[s] = calc_state_utility(time_remain, train_ret, s, v_init=v, c=c, v_th=v_th, eta=eta) ws = policy.argmax() port_ret = ws * full_ret.loc[start_year + i, 'stock'] + ( 1 - ws) * full_ret.loc[start_year + i, 'bond'] bench_ret = ws_bench * full_ret.loc[start_year + i, 'stock'] + ( 1 - ws_bench) * full_ret.loc[start_year + i, 'bond'] # update portfolio and benchmark value v = v * (1 + port_ret) + c v_bench = v_bench * (1 + bench_ret) + c i = i + 1 time_remain = time_remain - 1 return (v, v_bench)
def myPV(r, n, fv): import scipy as sp return (-sp.pv(r, n, 0, fv))
# -*- coding: utf-8 -*- """ Created on Tue Jul 28 22:22:27 2020 @author: yomer """ import scipy as sp # 미래가치 구하기 : 연금리 10%, 100달러 예금 2년 후 print(sp.fv(0.1, 2, 0, 100)) # 현재가치 구하기 : 금리1.45%, 횟수5, 미래가치 234 print(sp.pv(0.0145, 5, 0, 234))
import scipy as sp ''' 每期利息率为:0.05 期数:3 每月等额收支:0 终值:100 求:现值 ''' pv1 = sp.pv(0.05, 3, 0, 100, when='begin') print(pv1) ''' 每期利息率为:0.05/12 期数:10*12 每月等额收支:+100 终值:0 求:现值 ''' pv2 = sp.pv(0.05 / 12, 10 * 12, 100, 0, when='begin') print(pv2) ''' 每期利息率为:0.05/12 期数:30*12 每月等额收支:+3000 终值:0 求:现值 60岁开始,每月领取3000元,共领取30年领完,60随时需要存入多少钱 ''' pv3 = sp.pv(0.05 / 12, 30 * 12, 3000, 0, when='begin') print(pv3)
x.sum() x.sum(axis=0) ##sum by row ### simulation ret = np.array(range(1, 100), float)/100 ##create a grid of return pct rand_50 = np.random.rand(50) ##50 rand num btw 0 and 1 rnorm_100 = np.random.normal(size=100) ## 2. Scipy ------------ ### Compute the NPV of a Cashflow cf = [-100, 50, 40, 20, 10, 50] sp.npv(rate=0.1, values=cf) ### Monthly payment: APR = 0.045, compounded monthly, mortgage = 250k, T=30yrs sp.pmt(rate=0.045/12, nper=12*30, pv=250000) ### PV of one future cf sp.pv(rate=0.1,nper=5,pmt=0,fv=100) ### PV of Annuity (series of payments) sp.pv(rate=0.1, nper=5, pmt=100) ### Geometric mean returns def geoMeanreturn(ret): return pow(sp.prod(ret+1), 1./len(ret)) - 1 ret = np.array([0.1, 0.05, -0.02]) geoMeanreturn(ret) ### other fundamental stuff sp.unique([1,1,2,3,4,1,1,1]) sp.median([1,1,2,3,4,1,1,1]) ## 3. matplotlib ------------ ### Series of Normal Distribution plt.plot(rnorm_100)
""" Name : 4375OS_06_07_pv_opposite_sign.py Book : Python for Finance Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 12/26/2013 email : [email protected] [email protected] """ import scipy as sp print round(sp.pv(0.1, 5, 0, 100), 2) print round(sp.pv(0.1, 5, 0, -100), 2)
def pv(*args): rate, nper, pmt = args[0], args[1], args[2] return scipy.pv(rate, nper, pmt)
PV_simple_1 = FV / (1 + r * n) PV_simple_1 #%% m-기간 기준단리: FV=PV*(1+r/m*n*m) ==> PV=FV/(1+r/m*n*m) # 년기준복리: FV=PV*(1+r)**n ==> PV=FV/(1+r)**n # m-기간 기준복리: FV=PV*(1+r/m)*(n*m) ==> PV=FV/(1+r/m)*(n*m) # 기존 모듈 이용 import scipy as sp import numpy as np help(sp.pv) help(np.pv) fv = 1276.2815625000003 r = 0.05 n = 5 m = 1 PV_sp = sp.pv(r, n, 0, fv) # pv(rate, nper, pmt, fv=0, when='end') print(PV_sp) PV_np = np.pv(r, n, 0, fv) print(PV_np) #%%[과제 43-2] 이자가 6개월에 한번 복리로 계산되고 년이자율이 5%인 계좌의 5년 후1000원의 현재가치는 얼마인가? #i) 사용자 정의 함수를 사용하여 계산하여 보세요. import numpy as np FV = 1000 r = 0.05 n = 5 m = 2 def get_pv(Fv, r, n, m): return FV / (1 + r / m * n * m)
""" Name : 4375OS_06_07_pv_opposite_sign.py Book : Python for Finance Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 12/26/2013 email : [email protected] [email protected] """ import scipy as sp print round(sp.pv(0.1,5,0,100),2) print round(sp.pv(0.1,5,0,-100),2)
import scipy as sp ''' 每期利息率为:0.05 期数:3 每月等额收支:0 终值:100 求:现值 ''' pv1 = sp.pv(0.05,3,0,100,when='begin') print(pv1) ''' 每期利息率为:0.05/12 期数:10*12 每月等额收支:+100 终值:0 求:现值 ''' pv2 = sp.pv(0.05/12, 10*12, 100, 0,when = 'begin') print(pv2) ''' 每期利息率为:0.05/12 期数:30*12 每月等额收支:+3000 终值:0 求:现值 60岁开始,每月领取3000元,共领取30年领完,60随时需要存入多少钱 ''' pv3 = sp.pv(0.05/12,30*12,3000,0,when = 'begin') print(pv3)
import scipy as sp n = float(raw_input('N = ')) iy = float(raw_input('I/Y = ')) pv = float(raw_input('PV = ')) pmt = float(raw_input('PMT = ')) fv = float(raw_input('FV = ')) sol = raw_input('Solve for (n,iy,pv,pmt,fv): ') if sol == "n": print("N = %f" % sp.nper(iy, pmt, pv, fv)) if sol == "iy": print("I/Y = %f" % sp.rate(n, pmt, pv, fv)) if sol == "pv": print("PV = %f" % sp.pv(iy, n, pmt, fv)) if sol == "pmt": print("PMT = %f" % sp.pmt(iy, n, pv, fv)) if sol == "fv": print("FV = %f" % sp.fv(iy, n, pmt, pv)) ''' print 'N = %f, ' % n print 'I/Y = %f, ' % iy print 'PV = %f, ' % pv print 'PMT = %f, ' % pmt print 'FV = %f, ' % fv '''
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> import scipy as sp >>> sp.fv(.0025,25,0,5000) -5322.057184363162 >>> import scipy as sp >>> sp.pv(.0541/2,20,55,0,1) -863.7816463604273 >>> import scipy as sp >>> sp.pmt(.0312,5,2400) -525.8478442817194 >>>
#!/usr/bin/env python # # Classes and functions given as examples from Chapter 5 # in the book. # # Stock valuation using the dividend discount model # import scipy as sp if __name__ == "__main__": """ Go go super awesome stock valutation DDM """ """ Present Value demo: Given R=12%; D=1; and FV=50, where R is appropriate cost of equity D is the dividend and FV is the future value """ sp.pv(0.12, 1, 1 + 50) """ Grammatical growth demo: """ dividends = [1.80, 2.07, 2.48193, 2.680, 2.7877] R = 0.182 g = 0.03 print(float(sp.npv(R, dividends[:-1]) * (1 + R)))