私は完璧に動作する次のコードを持っています:
from __future__ import division
from math import log, sqrt
from scipy.stats import norm
from datetime import datetime
#Default values used for testing
s1 = 10; s2 = 20
sigma1 = 1.25; sigma2 = 1.45
t = 0.5; rho = 0.89
rate=0.05; y=0; k0 = 0.000001
sigma = lambda sig1=sigma1, sig2=sigma2, corr=rho: sqrt(sig1**2+sig2**2 -2*corr*sig1*sig2)
m_d1 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: (log(stock1/stock2)+1/2*sigma()**2*time)/(sigma()*sqrt(time))
m_d2 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: m_d1() -sigma()*sqrt(time)
m_delta1 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: norm.cdf(m_d1())
m_delta2 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: -norm.cdf(m_d2())
m_gamma11 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: norm.pdf(m_d1())/(stock1*sigma()*sqrt(time))
m_gamma22 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: norm.pdf(m_d2())/(stock2*sigma()*sqrt(time))
m_gamma12 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho:-norm.pdf(m_d1())/(stock2*sigma()*sqrt(time))
m_theta = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: -stock1*sigma()*norm.pdf(m_d1())/(2*sqrt(time))
m_vega1 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: stock1*sqrt(t)*norm.pdf(m_d1())*((sig1-(corr*sig2))/sigma())
m_vega2 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: stock1*sqrt(t)*norm.pdf(m_d1())*((sig2-(corr*sig1))/sigma())
m_correlation = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: -stock1*sqrt(t)*norm.pdf(m_d1())*((sig1*sig2)/sigma())
m_margrabe = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: stock1*norm.cdf(m_d1())-stock2*norm.cdf(m_d2())
print "Margrabe = "+str(m_margrabe()) + "\n"
print "THE GREEKS \n"
print "Delta Asset 1 = "+str(m_delta1())
print "Delta Stock 2 = "+str(m_delta2()) +"\n"
print "Gamma Asset 11 = "+str(m_gamma11())
print "Gamma Stock 12 = "+str(m_gamma12())
print "Gamma Stock 22 = "+str(m_gamma22()) + "\n"
print "Theta = "+str(m_theta()) +"\n"
print "Vega sigma 1 = "+str(m_vega1())
print "Vega sigma 2 = "+str(m_vega2()) + "\n"
print "Correlation = "+str(m_correlation()) + "\n"
完璧に動作しますが、私のコンピューターがこのプログラムを実行するのにかかる時間を知りたいです。問題をグーグルで調べた後、2つの選択肢が見つかりました:
1) timeit import Timer から。結果を印刷する部分に、次のように追加しました。
print "Margrabe = "+str(m_margrabe()) +"\n"
t1 = Timer(lambda: m_margrabe()).timeit(number=1)
print "THE GREEKS \n"
print "Delta Asset 1 = "+str(m_delta1())
t2 = Timer(lambda: m_delta1()).timeit(number=1)
print "Delta Stock 2 = "+str(m_delta2()) +"\n"
t3 = Timer(lambda: m_delta2()).timeit(number=1)
print "Gamma Asset 11 = "+str(m_gamma11())
t4 = Timer(lambda: m_gamma11()).timeit(number=1)
print "Gamma Stock 12 = "+str(m_gamma12())
t5 = Timer(lambda: m_gamma12()).timeit(number=1)
print "Gamma Stock 22 = "+str(m_gamma22()) +"\n"
t6 = Timer(lambda: m_gamma22()).timeit(number=1)
print "Theta = "+str(m_theta()) + "\n"
t7 = Timer(lambda: m_theta()).timeit(number=1)
print "Vega sigma 1 = "+str(m_vega1())
t8 = Timer(lambda: m_vega1()).timeit(number=1)
print "Vega sigma 2 = "+str(m_vega2()) +"\n"
t9 = Timer(lambda: m_vega2()).timeit(number=1)
print "Correlation = "+str(m_correlation()) + "\n"
t10 = Timer(lambda: m_correlation()).timeit(number=1)
print "Running Time = " + str(t1+t2+t3+t4+t5+t6+t7+t8+t9+t10)
これは正常に動作します: Running Time = 0.00682769470029を取得します
2) from datetime import datetime および:
startTime = datetime.now()
# Same code as before"
print(datetime.now()-startTime)
これも正常に動作し、次のようになります: 0:00:00.010000
私の質問は、なぜ両方の方法が異なるのですか? 異なる結果が得られるのはなぜですか? この「実行時間の計算」を1行で行う方法はありますか?
ありがとうございました
アップデート:
def printit():
print "Margrabe = "+str(m_margrabe()) + "\n"
print "THE GREEKS \n"
print "Delta Asset 1 = "+str(m_delta1())
print "Delta Stock 2 = "+str(m_delta2()) +"\n"
print "Gamma Asset 11 = "+str(m_gamma11())
print "Gamma Stock 12 = "+str(m_gamma12())
print "Gamma Stock 22 = "+str(m_gamma22()) + "\n"
print "Theta = "+str(m_theta()) +"\n"
print "Vega sigma 1 = "+str(m_vega1())
print "Vega sigma 2 = "+str(m_vega2()) + "\n"
print "Correlation = "+str(m_correlation()) + "\n"
if __name__=='__main__':
printit()
だから私は入力することができます(私のファイルはtime1.pyとして保存されます)
python -m timeit -s "import time1; time1.main()"
しかし、エラーが表示されます:「属性エラー:「モジュール」オブジェクトには属性「メイン」がありません