问:“有什么建议可以解决这个问题吗?”
- 使过程迭代和
s0-依赖,原来不是,
- 使过程计算效率更高,您重新计算常量值
num_reps 次
- 根据
B_shift 修改过程是一个常数值,而备注表明一些演变
- 按照
st修改流程,如果不在np.min( st )中,这可能反映了原始代码设计意图的矢量化形式,这不是她添加的。
import numpy as np
P = 30 # This is what you pay (S -/- F)
S = 360 # spot price of the stock
K = 340 # Exercise price is equal to the stop-loss barrier (as far as I understand it)
B = 340 # the stop-loss barrier at which the option is cancelled
F = 330 # The financing level
T = 1 # Time to maturity. But in priciple, the option runs indefintely as long as S > B
i = 0.02 # Annualised interest rate on the financing F
r = 0.00135 # Risk-free rate of return
impliedVolatility = 0.3
num_reps = 100
def barrier_option( option_type, # the option-type { 'c': CALL | 'p': PUT }
s0, # the spot price of the option underlying asset
strike, # the strike price
B, # the stop-loss barrier at which the option is cancelled
F, # the financing level
maturity, # the option time to maturity
i, # the annualised interest rate on financing F
r, # the risk-free rate of returns
sigma, # the sigma - implied volatility
num_reps # the number of repetitions
):
""" __doc__ [DOC-ME] [TEST-ME] [PERF-ME]
SYNTAX: barrier_option( option_type, # the option-type { 'c': CALL | 'p': PUT }
s0, # the spot price of the option underlying asset
strike, # the strike price
B, # the stop-loss barrier at which the option is cancelled
F, # the financing level
maturity, # the option time to maturity
i, # the annualised interest rate on financing F
r, # the risk-free rate of returns
sigma, # the sigma - implied volatility
num_reps # the number of repetitions
)
PARAMETERS: a string option_type, # the option-type { 'c': CALL | 'p': PUT }
a float-alike s0, # the spot price of the option underlying asset
a float-alike strike, # the strike price
a float-alike B, # the stop-loss barrier at which the option is cancelled
a float-alike F, # the financing level
a float-alike maturity, # the option time to maturity
a float-alike i, # the annualised interest rate on financing F
a float-alike r, # the risk-free rate of returns
a float-alike sigma, # the sigma - implied volatility
an int-alike num_reps # the number of repetitions
RETURNS: a float
THROWS: ValueError on inappropriate option_type specifier
EXAMPLE:
"""
if option_type not in ( 'c', 'p' ): #...............................# FUSE: protect your own code
raise ValueError( "EXC: a call to barrier_option() contained an illegal option-type specifier {0:}".format( repr( option_type ) ) )
payoff_sum = 0 # initial settings
st = S # initial settings ..................................# shan't be here the sO-parameter from the call-signature, instead of a reference to a global S ?
const_sqrtMATURITY = np.sqrt( maturity )
const_1stPartOfEXP = ( r - ( sigma**2 )*0.5 )*maturity
const_2ndPartOfEXP = sigma *const_sqrtMATURITY
const_B_shift = B + F*(1 + i) *const_sqrtMATURITY
for j in range( num_reps ): #.......................................# PERF:
#t = S # removed from loop, it will restore the referenced global S into a local st for each loop again, which is re-shortcutting the process
#t = st*e**( ( r - 0.5 * sigma**2 ) * maturity + sigma * np.sqrt( maturity ) * np.random.normal( 0, 1 ) )
#...............................................................# improved PERF: re-use const-s, that are loop-invariant
st*= np.exp( const_1stPartOfEXP # 100 x const re-used
+ np.random.normal(0, 1) * const_2ndPartOfEXP # 100 x const re-used
)
#_shift = B + F * ( 1 + i ) * np.sqrt( maturity ) # Here the interest I on F gets adjusted by increasing B
#...............................................................# REVISE: THE increasing B NOT VISIBLE ANYWHERE IN THE ORIGNAL CODE - IS IT CORRECT ?
#...............................................................# improved PERF: avoid re-calculations of a const, that is loop-invariant
#non_touch = ( np.min(st) > B_shift ) * 1 # REVISE: the code here may first meet a vector-ised kind of st: np.min(st)
payoff_sum += ( max( 0, st - strike ) if option_type == 'c' else max( 0, strike - st ) ) if ( np.min(st) > const_B_shift ) else 0. # REVISE: the code here may first meet a vector-ised kind of st: np.min(st)
#remium = (payoff_sum/float(num_reps))*e**(-r*maturity)
#eturn premium
#...................................................................# improved PERF: avoid creation of named variables just to RET a value ( variables & namespace management operations are non-zero expenses )
return ( payoff_sum * np.exp( -r * maturity ) ) / float( num_reps ) # improved (im)precission
Bar = barrier_option( 'c',
S, K, B, F,
( T * 252 ) / 365., #..... be self-consistent, if 've used float( num_reps ) inside, fuse-protect with decimal point also elsewhere, in spite of Py3 "tolerating" in how to handle the int-divisors, which Py2 is not
i, r, impliedVolatility,
num_reps
)