示例#1
0
def main (*args):
    OPT_N = 4000000
    iterations = 10
    if len(args) >= 2:
        iterations = int(args[0])

    callResultNumpy = np.zeros(OPT_N)
    putResultNumpy = -np.ones(OPT_N)
    stockPrice = randfloat(np.random.random(OPT_N), 5.0, 30.0)
    optionStrike = randfloat(np.random.random(OPT_N), 1.0, 100.0)
    optionYears = randfloat(np.random.random(OPT_N), 0.25, 10.0)
    callResultNumba = np.zeros(OPT_N)
    putResultNumba = -np.ones(OPT_N)

    time0 = time.time()
    for i in range(iterations):
        black_scholes(callResultNumpy, putResultNumpy, stockPrice,
                      optionStrike, optionYears, RISKFREE, VOLATILITY)
    time1 = time.time()
    print("Numpy Time: %f msec" %
          ((1000 * (time1 - time0)) / iterations))

    time0 = time.time()
    for i in range(iterations):
        black_scholes_numba(callResultNumba, putResultNumba, stockPrice,
                            optionStrike, optionYears, RISKFREE, VOLATILITY)
    time1 = time.time()
    print("Numba Time: %f msec" %
          ((1000 * (time1 - time0)) / iterations))

    delta = np.abs(callResultNumpy - callResultNumba)
    L1norm = delta.sum() / np.abs(callResultNumpy).sum()
    print("L1 norm: %E" % L1norm)
    print("Max absolute error: %E" % delta.max())
示例#2
0
 def leverage(self, strike):
     return 1.0 / black_scholes ((-1 if self.style == "put" else 1), 
                 self.price,
                 strike, 
                 self.t/365.0, 
                 self.vol,
                 self.r, 
                 self.d)
示例#3
0
def main(*args):
    OPT_N = 40000    
    callResult = np.zeros(OPT_N)
    putResult = -np.ones(OPT_N)
    stockPrice = randfloat(np.random.random(OPT_N), 5.0, 30.0)
    optionStrike = randfloat(np.random.random(OPT_N), 1.0, 100.0)
    optionYears = randfloat(np.random.random(OPT_N), 0.25, 10.0)

    c = black_scholes(stockPrice, optionStrike, optionYears, RISKFREE, VOLATILITY)
    p = None
    return c, p
示例#4
0
 def portfolio_items(self,  prices = None, mtm=False, payoff_asset=None, date=None, dt = 0.0):
     retval = []
     if prices == None:
         prices = self.prices
     for asset in self.portfolio:
         if asset[1] == "cash":
             retval.append(asset[0])
         elif asset[1] == "spot":
             quantity = asset[0]
             underlying = asset[2]
             purchase = asset[3]
             if  purchase < 0.0:
                 raise ValueError
             retval.append(quantity * prices[underlying])
         elif asset[1] == "put" or asset[1] == "call":
             quantity = asset[0]
             style = asset[1]
             expiry = asset[2]
             strike = asset[3]
             underlying = asset[4]
             purchase = asset[5]
             price = prices[underlying]
             value = 0.0
             if strike < 0.0 or purchase < 0.0:
                 raise ValueError
             if not mtm or underlying == payoff_asset:
                 if asset[1] == "put" and price < strike:
                     value = strike - price
                 if asset[1] == "call" and price > strike:
                     value = price - strike
             else:
                 t = (date_fraction(date, expiry) -dt/365.0) 
                 if (t < 0.0):
                     t = 0.0
                 vol = self.vols[underlying]
                 if (price < 0.0):
                     price = 0.0
                 value = black_scholes ((-1 if style == "put" else 1), price,                                              strike, t, vol, self.r, 0.0)
             retval.append(quantity * value )             
         elif asset[1] == "comment":
             pass
         else:
             raise Exception ("unknown asset")
     return retval
示例#5
0
 def gearing(self, strike):
     """Calculate return dependence for given strike"""
     options = self.leverage(strike)
     return (lambda price_new, dt : options * black_scholes ((-1 if self.style == "put" else 1), price_new,                                                      strike, ((self.t-dt)/365.0), self.vol,  self.r, self.d))