Deep Calibration: Heston model calibration by machine learning the pricing functional

The following code is part of Matteo Gambara's PhD thesis project.

The purpose of the code is to train a neural network to approximate the map from the parameters of the model to implied volatility, to have a fast and efficient calibration tool. Notice that this first attempt to do calibration by machine learning techniques is theoretically well founded: the map $$ \text{ parameter } \mapsto \text{ prices } $$ is usually very regular (often smooth) and can be learned on randomly sampled subsets (usually the parameter space is a linear space). Then the trained network is used for the formulation of the inverse problem.

The code heavily relies on QuantLib, which is an open-source library for quantitative finance.

The set of parameters we try to calibrate is $\Theta = \{\theta, \kappa, \sigma, \rho\}$. All parameters are splitted into two sets: the "observables" are the spot price $S_0$, the interest rate and dividend yields $r$, $q$ respectively, and $v_0$, the initial variance of the Heston model; the "model parameters" are those contained in $\Theta$.

The set of maturities and strikes is fixed a priori. For this reason, these parameters are not given as input to the NN.

In [2]:
# Import packages
from __future__ import division
import pandas as pd
import numpy as np
import datetime as dt

import QuantLib as ql
# This controls whether functionEvaluation is available in QuantLib library
has_function_evals = True
float_type = np.float32

from itertools import product
import matplotlib.pyplot as plt
import pickle
import os
data_dir = os.getcwd()

from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils import check_array
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.pipeline import Pipeline

from scipy.stats import norm
In [3]:
# Calendar conventions
day_count = ql.Actual365Fixed()
calendar = ql.UnitedStates()
# Option conventions
moneyness = [.8, .85, .9, .95, .975, .99, 1.0, 1.01, 1.025, 1.05, 1.1, 1.15, 1.2] # S_0/K 
delta_times = [15, 30, 45, 60, 90, 120, 180, 240, 300, 360, 420]

'''
Dictionary defining model
It requires 3 parameters:
    -name
    -process
    -model: a function that creates a model. It takes as parameter a 
            yield curve handle
    -engine: a function that creates an engine. It takes as paramters a
            calibration model and a yield curve handle
    -option_type            
Optional parameters:
    -transformation: a preprocessing function
    -inverse_transformation: a postprocessing function
'''
he_analytic = {'name' : 'Heston',
               'process' : ql.HestonProcess,
               'model' : ql.HestonModel, 
               'engine' : ql.AnalyticHestonEngine,
               'option_type' : ql.Option.Call,
               'transformation' : np.log, 
               'inverse_transformation' : np.exp}
In [4]:
# Helper functions
def create_imp_vol_skeleton(strike_struct, expiration_dates_struct, 
                            calculation_date, spot_price):
    '''
    Create the structure (skeleton) on which it is possible to price all options.
    '''
    strikes = [spot_price/m for m in strike_struct]
    expiries = [calculation_date+d_time for d_time in expiration_dates_struct]
    ttm_days = [(d-calculation_date) for d in expiries] # time to maturity
    ttm_year = [day_count.yearFraction(calculation_date, d) for d in expiries]
    
    new_array = np.array((ttm_days,strikes))
    cartesian_product_vola_surface = list(product(*new_array))
    df = pd.DataFrame(cartesian_product_vola_surface, 
                        columns=['ttm','strikes'])
    return strikes, np.array((ttm_year)), expiries, df

def set_risk_free_rate(calculation_date, risk_free_rate):
    return ql.YieldTermStructureHandle(
            ql.FlatForward(calculation_date,risk_free_rate,day_count))

def set_dividend_rate(calculation_date, dividend_rate):
    return ql.YieldTermStructureHandle(
            ql.FlatForward(calculation_date,dividend_rate,day_count))

def set_spot(spot_price):
    return ql.QuoteHandle(ql.SimpleQuote(spot_price))

def create_european_option(calculation_date, opt_type, strike_price, ttm):
    # Create European options
    payoff = ql.PlainVanillaPayoff(opt_type, strike_price)
    maturity = calculation_date+int(ttm)
    exercise = ql.EuropeanExercise(maturity)
    return ql.VanillaOption(payoff, exercise)

Heston model class

The following class is used to produce an object of type Heston_Model. Every object of this type has its own attributes: observables and model parameters.

Every object has also a dataframe associated, called "df", which contains all the prices of the options linked to the fixed strikes and maturities.

The input of the class are a dictionary with some general information (QuantLib commands to generate Heston prices, type of options, etc...) and the parameters discussed above.

In [5]:
class Heston_Model:
    def __init__(self, model_dict, spot_price=100., risk_free_rate=0.01, dividend_rate=0.0,
                 inst_var=0.1, calculation_date=ql.Date(8,11,2015),  
                 expiration_dates_struct=delta_times, strike_struct=moneyness,
                 mean_rev_speed=None, eq_var=None, vol_of_vol=None, correlation=None):
        '''
        This class implements the Heston model for a series of given parameters.
        The output will be a dataframe where the columns are parameters, strikes
        time_to_maturities, prices and values of the volatility surface.
        '''
        self._model_dict = model_dict
        if ('process' not in self._model_dict 
            or 'model' not in self._model_dict
            or 'engine' not in self._model_dict
            or 'name' not in self._model_dict
            or 'option_type' not in self._model_dict):
            raise RuntimeError('Missing parameters in the dictionary')
        
        self.option_type = self._model_dict['option_type']
        self.calculation_date = calculation_date
        ql.Settings.instance().evaluationDate = self.calculation_date
        
        #heston model parameters
        self.nb_params = 4
        self.sigma = vol_of_vol
        self.rho = correlation
        self.theta = eq_var
        self.kappa = mean_rev_speed
        self.heston_param = np.array((self.kappa,self.theta,self.sigma,self.rho))
        
        # volatility surface structure
        self.strikes, self.ttm, self.expiries, self.df = create_imp_vol_skeleton(
                                    strike_struct, expiration_dates_struct, 
                                    calculation_date, spot_price)
        
        # Yield curve, dividends, spot and instantaneous volatility
        self.risk_free_rate = risk_free_rate
        self.dividend_rate = dividend_rate
        self.spot_price = spot_price
        self.v0 = inst_var
        
        self.ircurve = set_risk_free_rate(self.calculation_date, self.risk_free_rate)
        self.dividend = set_dividend_rate(self.calculation_date, self.dividend_rate)
        self.spot = set_spot(self.spot_price)
        
        process = self.__create_process(self.kappa,self.theta,self.sigma,self.rho)
        model = self._model_dict['model'](process)
        engine = self._model_dict['engine'](model)
        
        eu_options = [create_european_option(self.calculation_date,self.option_type,s,t) 
                   for s,t in zip(self.df['strikes'], self.df['ttm'])]
        [opt.setPricingEngine(engine) for opt in eu_options]
        self.df['price'] = [o.NPV() for o in eu_options]
#        self.df['analytical_price'] = self.df['price'][:] #####

    def __create_process(self,kappa,theta,sigma,rho):
        # Creation of the object Process
        return self._model_dict['process'](self.ircurve,self.dividend,self.spot,
                self.v0,kappa,theta,sigma,rho)
In [6]:
# Helper functions for the class HestonGroup
def datetime_to_ql(d):
    return ql.Date(d.day,d.month,d.year)

def ql_to_datetime(dateql):
    return dt.datetime(dateql.year(), dateql.month(), dateql.dayOfMonth())

def ql_to_datetime_settings_dict(dates_ql, dictionary):
    if bool(dictionary):
        for dateql in dates_ql:
            helper = ql_to_datetime(dateql)
            dictionary[helper]=dictionary.pop(dateql)                                                                
                
def datetime_to_ql_settings_dict(dates, dictionary):
    if bool(dictionary):
        dates = sorted(dates)
        for date in dates:
            helper = date_to_ql(date)
            dictionary[helper] = dictionary.pop(date)
            
def save_dictionary(dictionary, name_dict, path=data_dir):
        filehandler = open(path+ '/' +name_dict+'.pkl','wb')
        pickle.dump(dictionary,filehandler)
        filehandler.close()

def load_dictionary(name_dict, path=data_dir):
        with open(path+ '/' +name_dict+'.pkl', 'rb') as handle:
            dictionary = pickle.load(handle)
        return dictionary
In [7]:
# Bounds for the calibration of the parameters (in implied_vola_generation.py)
he_generation_bounds = [(0.5,10.), (0.05,0.8), (0.05,0.8), (-0.99,0.99)] #kappa,theta,sigma,rho
he_calibration_bounds = [(0.001,15.), (0.001,6.), (0.005,4.), (-0.999,0.999)]
he_mean_as_initial_point = [5., 1.5, 1., 0.]
In [8]:
# Function to plot the volatility surface
def plot_surface(Z, z_label, main_title, string1='', string2='', W=None, string3='', **kwargs):
    times = kwargs.get('delta_times', delta_times)
    money = kwargs.get('moneyness', moneyness)
    X = times; Y = money
    X, Y = np.meshgrid(X, Y)
    X = np.transpose(X); Y = np.transpose(Y)
    if Z.shape != (X.shape[0],Y.shape[1]):
        Z = Z.reshape((len(times),len(money)))
    if W is not None and W.shape != (X.shape[0],Y.shape[1]):
        W = W.reshape((len(times),len(money)))
    
    if W is None:
        from mpl_toolkits.mplot3d import Axes3D
        from matplotlib import cm
        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1, projection='3d')
        surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=True)
        fig.colorbar(surf, shrink=0.6, aspect=20, ax=ax)
        ax.set_xlabel('time to maturity (days)'); ax.set_ylabel('moneyness (%)'); ax.set_zlabel(z_label)
        ax.text2D(0.06, 0.98, string1, transform=ax.transAxes)
        ax.text2D(0.06, 0.94, string2, transform=ax.transAxes)
        fig.suptitle(main_title)
        plt.show()
    else:
# COMMENT IF MAYAVI NOT AVAILABLE
#        from mayavi import mlab
#        fig = mlab.figure(figure=None, bgcolor=(1,1,1), fgcolor=(0,0,0), size=(1200,600))
#        ax_ranges = [min(times), max(times), min(money), max(money), 0, max(np.amax(Z),np.amax(W))]
#        surf3 = mlab.surf(X, Y, Z, colormap='Oranges', warp_scale='auto')
#        surf4 = mlab.surf(X, Y, W, colormap='Blues', warp_scale='auto')
#        cb1 = mlab.colorbar(object=surf3, title='Original IVS', orientation='vertical', nb_labels=5, nb_colors=None, label_fmt=None)#Original
#        cb2 = mlab.colorbar(object=surf4, title='Neural net 1', orientation='vertical', nb_labels=5, nb_colors=None, label_fmt=None)#NN1
#        cb1.scalar_bar_representation.position = [0.12, 0.15] #bottom left
#        cb1.scalar_bar_representation.position2 = [0.1, 0.8] # from position!!!
#        cb2.scalar_bar_representation.position = [0.01, 0.15] #bottom left
#        cb2.scalar_bar_representation.position2 = [0.1, 0.8]
##        mlab.view(60, 74, 17, [-2.5, -4.6, -0.3])
#        ax = mlab.axes(surf3, color=(.7, .7, .7), #extent=ax_extent,
#                       ranges=ax_ranges, xlabel='time to maturity (days)', 
#                       ylabel='moneyness (%)', zlabel=z_label, nb_labels=5)
#        ax.axes.font_factor=0.8
#        title = mlab.title(main_title)
#        title.x_position = 0.3
#        title.y_position = 0.8
#        title.width = 0.5
#        print(title.__dict__)
#        mlab.show()
# UNCOMMENT IF MAYAVI NOT AVAILABLE        
        from mpl_toolkits.mplot3d import Axes3D
        from matplotlib import cm
        fig = plt.figure()
        fig.set_figheight(8)
        fig.set_figwidth(16)
        ax = fig.add_subplot(1, 2, 1, projection='3d')
        surf1 = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=True)
        fig.colorbar(surf1, shrink=0.6, aspect=20, ax=ax)
        ax.set_xlabel('time to maturity (days)'); ax.set_ylabel('moneyness (%)'); ax.set_zlabel(z_label)
        ax.text2D(0.06, 0.98, string1, transform=ax.transAxes)
        ax.text2D(0.06, 0.94, string2, transform=ax.transAxes)
        ax = fig.add_subplot(1, 2, 2, projection='3d')
        surf1 = ax.plot_surface(X, Y, Z, cmap=cm.Oranges, linewidth=0, antialiased=True, alpha=0.2)
        surf2 = ax.plot_surface(X, Y, W, cmap=cm.Blues, linewidth=0, antialiased=True)
        fig.colorbar(surf1, shrink=0.6, aspect=20, ax=ax)
        fig.colorbar(surf2, shrink=0.6, aspect=20, ax=ax)
        ax.text2D(0.15, 0.96, string3, transform=ax.transAxes)
        ax.set_xlabel('time to maturity (days)'); ax.set_ylabel('moneyness (%)'); ax.set_zlabel(z_label)
        fig.suptitle(main_title)
        plt.show()
In [9]:
'''
 - Search for the implied volatility - 
Since the differential evolution algorithm is extremly long, I have to use
another method to derive the implied volatility for option prices.
References are Li and Lee (2011) and Stefanica and Radoicic (2017).
'''

def approx_func(x):
    return 0.5 + 0.5*np.sign(x)*np.sqrt(1. - np.exp(-2.*(x**2)/np.pi))

def guess_StefanicaRadoicic(option_type, strike, option_price, spot_price,
                            ir_discount, dr_discount):
    
    forward = spot_price * dr_discount / ir_discount
    ey = forward/strike #ey=exp(y)
    emy = strike/forward #emy=exp(-y)
    y = np.log(ey)
    alpha = option_price/(strike*ir_discount)
    
    if option_type==1:
        #Call
        R = 2.*alpha - ey + 1.
    else:
        #Put
        R = 2.*alpha + ey - 1.
    pi_term = 2./np.pi
    arg_exp_term = (1.-pi_term)*y
    R2 = R**2
    
    a = np.exp(arg_exp_term)
    A = (a - 1./a)**2
    b = np.exp(pi_term*y)
    B = 4.*(b + 1./b) - 2.*emy*(a + 1./a)*(ey**2 + 1. - R2)
    C = (emy**2) * (R2 - (ey - 1.)**2) * ((ey + 1.)**2 - R2)
    beta = 2.*C / (B + np.sqrt(B**2 + 4.*A*C))
    gamma = - np.pi/2.*np.log(beta)
    
    if y>=0.:
        if option_type==1: #call
            O0 = strike*ir_discount*(ey*approx_func(np.sqrt(2.*y)) - 0.5)
        else:
            O0 = strike*ir_discount*(0.5 - ey*approx_func(-np.sqrt(2.*y)))
        if option_price <= O0:
            nu = np.sqrt(gamma+y) - np.sqrt(gamma-y)
        else:
            nu = np.sqrt(gamma+y) + np.sqrt(gamma-y)
    else:
        if option_type==1: #call
            O0 = strike*ir_discount*(0.5*ey - approx_func(-np.sqrt(-2.*y)))
        else:
            O0 = strike*ir_discount*(approx_func(np.sqrt(-2.*y)) - 0.5*ey)
        if option_price <= O0:
            nu = np.sqrt(gamma-y) - np.sqrt(gamma+y)
        else:
            nu = np.sqrt(gamma+y) + np.sqrt(gamma-y)
    return nu

def Phi(x,nu):
    nu2 = nu**2
    abs_term = 2.*np.abs(x)
    return (nu2-abs_term)/(nu2+abs_term)
def N_plus(x,nu):
    return norm.cdf(x/nu + 0.5*nu)
def N_minus(x,nu):
    return np.exp(-x)*norm.cdf(x/nu - 0.5*nu)
def F(nu, x, c_star, omega):
    return c_star + N_minus(x,nu) + omega*N_plus(x,nu)
def G(nu, x, c_star, omega):
    argument = F(nu, x, c_star, omega)/(1.+omega)
    term = norm.ppf(argument)
    return term + np.sqrt(term**2 + 2.*np.abs(x))
    
def SOR_TS(option_type, strike, discount_ir, discount_dr, option_price, 
                     spot_price, guess, omega, accuracy, max_iterations=20):
    
    assert (option_price >= 0.),'Price must be positive.'
    
    forward = spot_price * discount_dr / discount_ir
    x = np.log(forward/strike)
    
    if option_type==1:  #call
        c = option_price/(spot_price*discount_dr)
    else:   #put
        c = option_price/(spot_price*discount_dr) + 1. - strike/forward
    
    if x > 0.:
        # formula in-out duality
        c = c*forward/strike + 1. - forward/strike
        assert (c >= 0.),'Normalized price must be positive.'
        x = -x
    
    if not guess:
        guess = guess_StefanicaRadoicic(option_type, strike, option_price, 
                                       spot_price, discount_ir, discount_dr)
    assert (guess >= 0.),'Initial guess must be positive.'
    
    nIter = 0
    nu_k = nu_kp1 = guess
    difference = 1.
    while (np.abs(difference)>accuracy and nIter<max_iterations):
        nu_k = nu_kp1
        alpha_k = (1.+omega)/(1.+Phi(x,nu_k))
        nu_kp1 = alpha_k*G(nu_k, x, c, omega) + (1.-alpha_k)*nu_k
        difference = nu_kp1 - nu_k
        nIter +=1
    return nu_kp1

VolaSurface Class

The purpose of this class is to get as input the observable parameters and the data frame df with all the prices (listed by strike and maturity) and use these as input for the computation of the implied volatility.

In [10]:
def return_year_fraction(date, ttm):
    return [day_count.yearFraction(date,date+int(nd)) for nd in ttm]

class VolaSurface:
    '''
    This class takes in input some prices and transform them in a volatility surface.
    '''
    def __init__(self, option_type, spot_price=100, risk_free_rate=0.01, dividend_rate=0.0,
                 calculation_date=ql.Date(8,11,2015), df=None):   
        # Main data structure
        self.data = df

        # Check to see if there are the correct columns        
        if not 'ttm' in self.data.columns \
        or not 'strikes' in self.data.columns \
        or not 'price' in self.data.columns:
            raise RuntimeError('Some necessary data is missing')
        
        # Option type, calculation date
        self.option_type = option_type
        self.calculation_date = calculation_date #already a ql Date
        ql.Settings.instance().evaluationDate = self.calculation_date
        
        # Yield curve, dividends, spot
        self.ircurve = set_risk_free_rate(self.calculation_date, risk_free_rate)
        self.dividend = set_dividend_rate(self.calculation_date, dividend_rate)
        self.spot = set_spot(spot_price)
        
        # Tolerance on implied volatility error
        self.tol = 1.e-14
    

    def __implied_vola(self, price, strike, ttm):
        # Find the implied volatility through other algorithms
        discount_ir = self.ircurve.discount(self.calculation_date+int(ttm))
        discount_dr = self.dividend.discount(self.calculation_date+int(ttm))
        sol = 0.0
        try:
            sol = SOR_TS(self.option_type, strike, discount_ir, discount_dr,
                         price, self.spot.value(), guess=None, 
                         omega=1., accuracy=self.tol)
        except (RuntimeWarning, AssertionError) as e:
            print(repr(e))
        yearly_ttm = day_count.yearFraction(self.calculation_date, self.calculation_date+int(ttm))
        sol = sol/np.sqrt(yearly_ttm)
        return sol
        
        
    def get_implied_vola(self):
        ivs = []
        for i in range(len(self.data['price'])):
            ivs.append(self.__implied_vola(self.data['price'][i],
                                           self.data['strikes'][i],
                                           self.data['ttm'][i]))
            if ivs[i]==0.:
                raise ValueError('Zero implied volatility!')
        return ivs

HestonGroup Class

This class is a collection of Heston model objects. Every object is accessible through dictionaries whose keys are dates. The idea is that for every different day, I have a different model/object to calibrate.

In [11]:
class HestonGroup:
    
    def __init__(self, model_dict=he_analytic, first_date=ql.Date(1,1,2018), 
                 end_date=ql.Date(2,1,2018)):
        #Create a collection of object of type Heston_Model
        self._model_dict = model_dict
        if ('model' not in self._model_dict
            or 'process' not in self._model_dict
            or 'engine' not in self._model_dict 
            or 'name' not in self._model_dict 
            or 'option_type' not in self._model_dict):
            raise RuntimeError('Missing parameters in the dictionary')
        self.model_name = self._model_dict['name'].replace("/", "").lower()
        
        # Option type
        self.option_type = self._model_dict['option_type']
        # Dates
        self.dates = pd.date_range(ql_to_datetime(first_date), ql_to_datetime(end_date))
        #print(self.dates)
        # in this way I generate a date for every day (with holidays)
        self.dates_ql = [datetime_to_ql(d) for d in self.dates]
        #print(self.dates_ql)
        
        # Dictionaries
        self.he_observable = {} #contains s0, ir, dr and v0
        self.he_hist_parameters = {} #contains s0, ir, dr + 5 heston params
        self.he_hist_df = {} #contains the entire data_frame with ttm, strikes, prices
        self.he_hist_iv = {} #contains the implied vola surface given by the data_frame
        
        
    # Methods for the generation of the fake volatility surfaces
    def __generate_ir(self):
        return np.clip(np.random.beta(a=1.01, b=60), 0.001, 0.08)
    def __generate_dr(self):
        return np.clip(np.random.beta(a=1.01, b=60), 0.001, 0.03)
    def __generate_sp(self):
        return 100*(np.random.beta(a=8, b=8) + 1.0E-2)
    def __generate_theta(self):
        return np.random.uniform(low=he_generation_bounds[1][0], high=he_generation_bounds[1][1])
    def __generate_v0(self):
        return np.random.uniform(low=0.001, high=0.9)**2
    def __generate_rho(self):
        return np.random.uniform(low=he_generation_bounds[3][0], high=he_generation_bounds[3][1])
    def __generate_kappa(self):
        return np.random.uniform(low=he_generation_bounds[0][0], high=he_generation_bounds[0][1])
    def __generate_sigma(self):
        return np.random.uniform(low=he_generation_bounds[2][0], high=he_generation_bounds[2][1])

    def create_Heston_model(self, date, **kwargs):
        '''
        Use fake (random) datas to create the data structure for the Heston model
        and the volatility surface.
        '''
        ir = kwargs.get('ir', self.__generate_ir())
        dr = kwargs.get('dr', self.__generate_dr())
        sp = kwargs.get('sp', self.__generate_sp())
        v0 = kwargs.get('v0', self.__generate_v0())
        rho = kwargs.get('rho', self.__generate_rho())
        sigma = kwargs.get('sigma', self.__generate_sigma()) #vol of vol! Not implied vola!
        # Loop until the Feller condition is satisfied
        theta = kwargs.get('theta', self.__generate_theta())
        kappa = kwargs.get('kappa', self.__generate_kappa())
        while 2*kappa*theta <= sigma**2:
            theta = self.__generate_theta()
            kappa = self.__generate_kappa()
        
        return Heston_Model(model_dict=he_analytic, spot_price=sp, risk_free_rate=ir,
                            dividend_rate=dr, calculation_date=date, 
                            expiration_dates_struct=delta_times, 
                            strike_struct=moneyness, vol_of_vol=sigma, 
                            correlation=rho, eq_var=theta, inst_var=v0, 
                            mean_rev_speed=kappa)
    
    
    def show_ivs(self, **kwargs):
        '''
        Plot implied volatility surface
        '''
        rd = self.dates_ql[0]
        hest_obj = self.create_Heston_model(date = rd, **kwargs)
#        hest_obj = self.create_Heston_model(date=rd,sp=100,ir=0.1,dr=0.05,v0=0.09,rho=-0.75,kappa=1,theta=0.06,sigma=0.4)
#        https://hpcquantlib.wordpress.com/2016/09/11/the-collocating-local-volatility-model/
#        hest_obj = self.create_Heston_model(date=rd,sp=100,ir=0.1,dr=0.05,v0=1,rho=-0.9,kappa=5.7,theta=1,sigma=0.1)
#        hest_obj = self.create_Heston_model(date=rd,sp=100,ir=0.1,dr=0.05,v0=0.5**2,rho=0.5,kappa=6.2,theta=0.4**2,sigma=0.8)
#        hest_obj = self.create_Heston_model(date=rd,sp=100,ir=0.1,dr=0.05,v0=0.4**2,rho=-0.1,kappa=1.5,theta=0.9**2,sigma=0.8)
        obs_str = ['S0 =', 'ir =', 'dr =', 'v0 =']
        obs = [hest_obj.spot_price, hest_obj.risk_free_rate,
               hest_obj.dividend_rate, hest_obj.v0]
        obs_str = ["{}{:.6}".format(o, str(v)) for o,v in zip(obs_str, obs)]
        params_str = [r'$\kappa = $', r'$\theta = $', r'$\sigma = $', r'$\rho = $']
        params = [hest_obj.kappa,hest_obj.theta, hest_obj.sigma, hest_obj.rho]
        params_str = ["{}{:.5}".format(o, str(v)) for o,v in zip(params_str, params)]
        ivs = VolaSurface(option_type=hest_obj.option_type, 
                          spot_price=hest_obj.spot_price, 
                          risk_free_rate=hest_obj.risk_free_rate, 
                          dividend_rate=hest_obj.dividend_rate,
                          calculation_date=hest_obj.calculation_date, 
                          df=hest_obj.df).get_implied_vola()
        plot_surface(Z=np.array(ivs), z_label='ivs', 
                     main_title='Heston Imp Vola Surface',
                     string1=', '.join(o for o in obs_str), 
                     string2=', '.join(p for p in params_str))
    
    
    def create_historical_process_data(self, **kwargs):
        '''
        Real creation of Heston data: big loop over all dates.
        '''
        k = 0
        while k<len(self.dates_ql):
            datehelper = self.dates_ql[k]
            try:
                print('Date: ',datehelper)
                hest_obj = self.create_Heston_model(datehelper)
                self.he_observable[datehelper] = np.array((hest_obj.spot_price,hest_obj.risk_free_rate,
                               hest_obj.dividend_rate,hest_obj.v0))
                self.he_hist_parameters[datehelper] = np.array((hest_obj.kappa,hest_obj.theta,
                                                          hest_obj.sigma,hest_obj.rho))
                print('Parameters: ',np.array((hest_obj.kappa,hest_obj.theta,
                                                          hest_obj.sigma,hest_obj.rho)))
                self.he_hist_df[datehelper] = hest_obj.df
                ivs = VolaSurface(option_type=hest_obj.option_type, 
                                  spot_price=hest_obj.spot_price, 
                                  risk_free_rate=hest_obj.risk_free_rate, 
                                  dividend_rate=hest_obj.dividend_rate,
                                  calculation_date=hest_obj.calculation_date, 
                                  df=hest_obj.df)
                self.he_hist_iv[datehelper] = ivs.get_implied_vola()
            except (ValueError) as e:    
                print(e)
            else:
                k += 1
        
        if 'file_name' in kwargs and kwargs['file_name'] !='': #needed for manual parallelization
            self.model_name = self.model_name+'_'+kwargs['file_name']
        
        if 'save' in kwargs and kwargs['save'] == True:
            print('Saving historical data')
            #for date in self.he_observable.keys():
            #    self.he_observable[ql_to_datetime(date)]=self.he_observable.pop(date)
            #    print(ql_to_datetime(date))
            ql_to_datetime_settings_dict(dates_ql=self.dates_ql, dictionary=self.he_observable)
            #print('x')
            save_dictionary(dictionary=self.he_observable, name_dict='he_observable')
            
            ql_to_datetime_settings_dict(dates_ql=self.dates_ql, dictionary=self.he_hist_parameters)
            save_dictionary(dictionary=self.he_hist_parameters, name_dict='he_hist_parameters')
            
            ql_to_datetime_settings_dict(dates_ql=self.dates_ql, dictionary=self.he_hist_df)
            save_dictionary(dictionary=self.he_hist_df, name_dict='he_hist_df')
            
            ql_to_datetime_settings_dict(dates_ql=self.dates_ql, dictionary=self.he_hist_iv)
            save_dictionary(dictionary=self.he_hist_iv, name_dict='he_hist_iv')

    
    def training_data_param_to_iv(self, nb_samples, **kwargs):
        '''
        Fake creation of Heston data for TRAINING OF NN1: from parameters to
        implied volatility surfaces.
        '''
        # Set seed
        seed = kwargs.get('seed',0)
        print('Seed: %s'%seed)
        np.random.seed(seed)
        print('NN1-training data are produced')
        
        # Empty structures
        x = []
        y = []
        
        fake_date = self.dates_ql[0]
        ql.Settings.instance().evaluationDate = fake_date
        i = 0
        while i<nb_samples:
            try:
                hest_obj = None
                hest_obj = self.create_Heston_model(fake_date)
                imp_vola_obj = VolaSurface(option_type=hest_obj.option_type, 
                                               spot_price=hest_obj.spot_price, 
                                               risk_free_rate=hest_obj.risk_free_rate, 
                                               dividend_rate=hest_obj.dividend_rate,
                                               calculation_date=hest_obj.calculation_date, 
                                               df=hest_obj.df)
                ivs = imp_vola_obj.get_implied_vola()
            except ValueError as e:
                print(hest_obj.heston_param)
                print('Error: %s, sample %s'%(e,i+1)); print(' ')
            else:
                x.append([hest_obj.spot_price, hest_obj.risk_free_rate, hest_obj.dividend_rate,
                          hest_obj.v0,hest_obj.kappa,hest_obj.theta,hest_obj.sigma,hest_obj.rho])
                y.append(ivs)
                i += 1
                
        # There is no need to clean data afterwards in this way
        if 'file_name' in kwargs and kwargs['file_name'] !='': #needed for manual parallelization
            print('file name %s'%(self.model_name+'_'+kwargs['file_name']))
            self.model_name = self.model_name+'_'+kwargs['file_name']
        
        if 'save' in kwargs and kwargs['save'] == True:
            print('Saving data for training of NN1')
            np.save(data_dir+'/'+self.model_name+'_train_he_find_iv_inputNN1', x) #name: Heston_train_he_inputNN
            np.save(data_dir+'/'+self.model_name+'_train_he_find_iv_outputNN1', y)
        return (x,y)
In [12]:
bat_obj = Heston_Model(model_dict=he_analytic, spot_price=100, 
                       risk_free_rate=0.01, dividend_rate=0.0,
                       inst_var=0.1, calculation_date=ql.Date(8,11,2015),
                       expiration_dates_struct=delta_times, strike_struct=moneyness,
                       mean_rev_speed=5., eq_var=0.2, vol_of_vol=0.5, 
                       correlation=-0.7)
    
he_group = HestonGroup(first_date=ql.Date(1,1,2018), end_date=ql.Date(1,1,2018))

In the sequel one example of a volatility surface is drawn ...

In [13]:
# Plot example of volatility surface
he_group.show_ivs()
#he_group.show_ivs(sp=100,ir=0.1,dr=0.05,v0=0.09,rho=-0.75,kappa=3,theta=0.1,sigma=0.4)

Now some data with random parameters are created in order to look whether the presented methodology is able to reproduce the parameters from the volatility surface.

In [14]:
'''
Create historical data
'''

start_date = dt.datetime(2017,1,1)
end_date = dt.datetime(2017,1,5)

f_date = datetime_to_ql(start_date)
e_date = datetime_to_ql(end_date)
heston_processes = HestonGroup(first_date=f_date, end_date=e_date)
heston_processes.create_historical_process_data(save=True, seed=1)
Date:  January 1st, 2017
Parameters:  [2.90732105 0.12593882 0.33576235 0.02830994]
Date:  January 2nd, 2017
Parameters:  [8.71750427 0.50917857 0.43400964 0.92818448]
Date:  January 3rd, 2017
Parameters:  [ 0.9118351   0.44036557  0.28943401 -0.27115573]
Date:  January 4th, 2017
Parameters:  [ 7.56182785  0.053286    0.05666229 -0.86219075]
Date:  January 5th, 2017
Parameters:  [ 6.50268069  0.40280196  0.68358524 -0.23626066]
Saving historical data
In [402]:
'''
Create training data ---- At least 20k points to obtain an acceptable calibration.
'''
heston_processes = HestonGroup()
heston_processes.training_data_param_to_iv(nb_samples=20000, seed=1, save=True, file_name='heston_trial')
Seed: 1
NN1-training data are produced
AssertionError('Price must be positive.',)
[ 2.43128573  0.47607535  0.77545725 -0.84749093]
Error: Zero implied volatility!, sample 8
 
AssertionError('Price must be positive.',)
[ 0.50381924  0.73895133  0.55930163 -0.15801651]
Error: Zero implied volatility!, sample 12
 
AssertionError('Price must be positive.',)
[ 3.01082609  0.16009428  0.65945794 -0.75347504]
Error: Zero implied volatility!, sample 22
 
AssertionError('Price must be positive.',)
[ 7.76924759  0.21837011  0.51435112 -0.82460227]
Error: Zero implied volatility!, sample 80
 
AssertionError('Price must be positive.',)
[ 4.03873828  0.0831161   0.74197902 -0.69794504]
Error: Zero implied volatility!, sample 94
 
AssertionError('Price must be positive.',)
[ 1.27155757  0.62556764  0.13803269 -0.56388502]
Error: Zero implied volatility!, sample 95
 
AssertionError('Price must be positive.',)
[ 2.36042522  0.12711378  0.57199738 -0.241345  ]
Error: Zero implied volatility!, sample 109
 
AssertionError('Price must be positive.',)
[ 3.96662314  0.10703577  0.20860051 -0.55326048]
Error: Zero implied volatility!, sample 185
 
AssertionError('Price must be positive.',)
[0.59988308 0.17642426 0.06649438 0.4657268 ]
Error: Zero implied volatility!, sample 203
 
AssertionError('Price must be positive.',)
[0.88643606 0.12022463 0.3909085  0.03161993]
Error: Zero implied volatility!, sample 215
 
AssertionError('Price must be positive.',)
[ 2.51919001  0.54072656  0.39787411 -0.89802335]
Error: Zero implied volatility!, sample 216
 
AssertionError('Price must be positive.',)
[ 1.30833413  0.48759693  0.13695159 -0.6342653 ]
Error: Zero implied volatility!, sample 270
 
AssertionError('Price must be positive.',)
[ 9.03317149  0.06428705  0.43638558 -0.95745994]
Error: Zero implied volatility!, sample 274
 
AssertionError('Normalized price must be positive.',)
[3.09218523 0.18011553 0.3312123  0.36025549]
Error: Zero implied volatility!, sample 340
 
AssertionError('Price must be positive.',)
[ 4.66171438  0.05504099  0.42175347 -0.32098294]
Error: Zero implied volatility!, sample 367
 
AssertionError('Price must be positive.',)
[1.52395916 0.16244784 0.27035259 0.28817356]
Error: Zero implied volatility!, sample 387
 
AssertionError('Price must be positive.',)
[ 5.85129337  0.12614409  0.07414707 -0.02532373]
Error: Zero implied volatility!, sample 413
 
AssertionError('Normalized price must be positive.',)
[1.17028065 0.49929139 0.39104585 0.80058488]
Error: Zero implied volatility!, sample 420
 
AssertionError('Price must be positive.',)
[ 1.83297437  0.65607744  0.38213889 -0.80837108]
Error: Zero implied volatility!, sample 438
 
AssertionError('Price must be positive.',)
[ 1.2931666   0.10675313  0.41291343 -0.41070583]
Error: Zero implied volatility!, sample 444
 
AssertionError('Price must be positive.',)
[ 4.51536208  0.34240073  0.62235011 -0.95001996]
Error: Zero implied volatility!, sample 455
 
AssertionError('Price must be positive.',)
[0.68918255 0.24830528 0.30107622 0.61862653]
Error: Zero implied volatility!, sample 522
 
AssertionError('Price must be positive.',)
[ 1.29003847  0.12880261  0.29261154 -0.25275649]
Error: Zero implied volatility!, sample 600
 
AssertionError('Price must be positive.',)
[4.16505815 0.09099962 0.51914094 0.37829549]
Error: Zero implied volatility!, sample 605
 
AssertionError('Normalized price must be positive.',)
[3.92758232 0.21107752 0.45696097 0.76662965]
Error: Zero implied volatility!, sample 624
 
AssertionError('Price must be positive.',)
[ 1.07422676  0.76505026  0.48183009 -0.56052191]
Error: Zero implied volatility!, sample 635
 
AssertionError('Price must be positive.',)
[0.91990197 0.39933355 0.222512   0.04150879]
Error: Zero implied volatility!, sample 654
 
AssertionError('Price must be positive.',)
[1.89771782 0.15396174 0.63321165 0.01354826]
Error: Zero implied volatility!, sample 668
 
AssertionError('Price must be positive.',)
[3.80643969 0.11021775 0.56011218 0.55263914]
Error: Zero implied volatility!, sample 699
 
AssertionError('Price must be positive.',)
[ 1.76400168  0.13445478  0.53566784 -0.62378291]
Error: Zero implied volatility!, sample 721
 
AssertionError('Normalized price must be positive.',)
[7.21785757 0.20383691 0.42994239 0.9447846 ]
Error: Zero implied volatility!, sample 743
 
AssertionError('Price must be positive.',)
[2.23853359 0.1650283  0.35475123 0.67235851]
Error: Zero implied volatility!, sample 752
 
AssertionError('Price must be positive.',)
[4.90262442 0.0777899  0.21896521 0.87189422]
Error: Zero implied volatility!, sample 760
 
AssertionError('Normalized price must be positive.',)
[ 4.07190417  0.11291033  0.13049072 -0.82785039]
Error: Zero implied volatility!, sample 809
 
AssertionError('Normalized price must be positive.',)
[2.25262525 0.32322389 0.29329417 0.48869208]
Error: Zero implied volatility!, sample 823
 
AssertionError('Price must be positive.',)
[ 1.55198506  0.22127265  0.4010291  -0.85951875]
Error: Zero implied volatility!, sample 868
 
AssertionError('Normalized price must be positive.',)
[0.57879031 0.11875715 0.34485654 0.84680491]
Error: Zero implied volatility!, sample 873
 
AssertionError('Price must be positive.',)
[7.49379622 0.0694377  0.13133368 0.14948469]
Error: Zero implied volatility!, sample 877
 
AssertionError('Price must be positive.',)
[ 1.74980473  0.1804384   0.71852748 -0.67670004]
Error: Zero implied volatility!, sample 895
 
AssertionError('Normalized price must be positive.',)
[5.17183367 0.26249439 0.36767465 0.96119798]
Error: Zero implied volatility!, sample 911
 
AssertionError('Normalized price must be positive.',)
[3.11778456 0.06110863 0.05120697 0.64600626]
Error: Zero implied volatility!, sample 1032
 
AssertionError('Normalized price must be positive.',)
[3.58932143 0.16602177 0.33735651 0.810505  ]
Error: Zero implied volatility!, sample 1048
 
AssertionError('Price must be positive.',)
[ 1.09212364  0.40011612  0.4894502  -0.88609802]
Error: Zero implied volatility!, sample 1099
 
AssertionError('Price must be positive.',)
[ 9.80125633  0.33551397  0.73603025 -0.96920176]
Error: Zero implied volatility!, sample 1140
 
AssertionError('Price must be positive.',)
[ 4.8542312   0.36085968  0.27790218 -0.93804422]
Error: Zero implied volatility!, sample 1159
 
AssertionError('Price must be positive.',)
[ 5.59913179  0.39639564  0.74931485 -0.86638685]
Error: Zero implied volatility!, sample 1276
 
AssertionError('Price must be positive.',)
[ 1.32720198  0.72554628  0.15388331 -0.11586937]
Error: Zero implied volatility!, sample 1278
 
AssertionError('Price must be positive.',)
[ 2.24597077  0.29090276  0.49953818 -0.6242545 ]
Error: Zero implied volatility!, sample 1295
 
AssertionError('Price must be positive.',)
[ 5.3815999   0.24049639  0.6773601  -0.86299764]
Error: Zero implied volatility!, sample 1310
 
AssertionError('Price must be positive.',)
[ 4.29359762  0.23463488  0.36829171 -0.90870768]
Error: Zero implied volatility!, sample 1334
 
AssertionError('Normalized price must be positive.',)
[1.97134383 0.70396966 0.74113458 0.98474721]
Error: Zero implied volatility!, sample 1342
 
AssertionError('Price must be positive.',)
[ 0.75583752  0.30454087  0.57696434 -0.81568538]
Error: Zero implied volatility!, sample 1379
 
AssertionError('Price must be positive.',)
[ 8.76582717  0.18159048  0.45915106 -0.9797197 ]
Error: Zero implied volatility!, sample 1411
 
AssertionError('Price must be positive.',)
[ 4.05724582  0.25460309  0.38531952 -0.78434707]
Error: Zero implied volatility!, sample 1413
 
AssertionError('Price must be positive.',)
[ 1.74350121  0.25398477  0.5026267  -0.19796692]
Error: Zero implied volatility!, sample 1459
 
AssertionError('Price must be positive.',)
[ 1.44290942  0.3294125   0.66866228 -0.90568233]
Error: Zero implied volatility!, sample 1489
 
AssertionError('Price must be positive.',)
[ 1.14785234  0.24323721  0.59866926 -0.49838385]
Error: Zero implied volatility!, sample 1501
 
AssertionError('Price must be positive.',)
[ 8.60300527  0.21182547  0.28631094 -0.80598882]
Error: Zero implied volatility!, sample 1522
 
AssertionError('Price must be positive.',)
[ 1.49047218  0.5292082   0.30512318 -0.16030565]
Error: Zero implied volatility!, sample 1523
 
AssertionError('Price must be positive.',)
[ 0.67782536  0.64495852  0.44228844 -0.92631921]
Error: Zero implied volatility!, sample 1552
 
AssertionError('Price must be positive.',)
[ 1.84352638  0.25013348  0.25299539 -0.24055158]
Error: Zero implied volatility!, sample 1581
 
AssertionError('Price must be positive.',)
[2.68534501 0.24563515 0.41784322 0.96254231]
Error: Zero implied volatility!, sample 1633
 
AssertionError('Normalized price must be positive.',)
[9.86740907 0.05472612 0.4646408  0.81209053]
Error: Zero implied volatility!, sample 1643
 
AssertionError('Price must be positive.',)
[ 1.90649046  0.15460415  0.27333119 -0.21891383]
Error: Zero implied volatility!, sample 1686
 
AssertionError('Price must be positive.',)
[ 1.09912859  0.20833288  0.08550977 -0.05723461]
Error: Zero implied volatility!, sample 1688
 
AssertionError('Normalized price must be positive.',)
[4.30788896 0.31390521 0.71497813 0.90510901]
Error: Zero implied volatility!, sample 1740
 
AssertionError('Price must be positive.',)
[5.61988134 0.06908066 0.17436429 0.11021677]
Error: Zero implied volatility!, sample 1749
 
AssertionError('Price must be positive.',)
[ 1.98087054  0.45573535  0.63654285 -0.57549615]
Error: Zero implied volatility!, sample 1788
 
AssertionError('Normalized price must be positive.',)
[6.08590115 0.10355052 0.37633233 0.333519  ]
Error: Zero implied volatility!, sample 1807
 
AssertionError('Price must be positive.',)
[ 2.61880382  0.24665201  0.2256821  -0.25821472]
Error: Zero implied volatility!, sample 1820
 
AssertionError('Price must be positive.',)
[ 4.93195783  0.25265763  0.48979071 -0.72166681]
Error: Zero implied volatility!, sample 1851
 
AssertionError('Price must be positive.',)
[1.24816173 0.20322289 0.15627174 0.12983729]
Error: Zero implied volatility!, sample 1864
 
AssertionError('Price must be positive.',)
[1.00200329 0.20617956 0.43232237 0.69160757]
Error: Zero implied volatility!, sample 1883
 
AssertionError('Price must be positive.',)
[ 6.42704592  0.09593291  0.56325711 -0.96490456]
Error: Zero implied volatility!, sample 1890
 
AssertionError('Price must be positive.',)
[ 3.0837005   0.29948776  0.38475816 -0.79010653]
Error: Zero implied volatility!, sample 1899
 
AssertionError('Normalized price must be positive.',)
[0.90452515 0.79806658 0.39457304 0.95321256]
Error: Zero implied volatility!, sample 1904
 
AssertionError('Normalized price must be positive.',)
[2.85191056 0.24191281 0.42876089 0.6337251 ]
Error: Zero implied volatility!, sample 1918
 
AssertionError('Price must be positive.',)
[ 2.93524068  0.32227324  0.49894795 -0.76057991]
Error: Zero implied volatility!, sample 1938
 
AssertionError('Normalized price must be positive.',)
[1.90901147 0.18935905 0.65042883 0.31205176]
Error: Zero implied volatility!, sample 1945
 
AssertionError('Price must be positive.',)
[ 5.98971503  0.37691287  0.67621743 -0.97458102]
Error: Zero implied volatility!, sample 2037
 
AssertionError('Price must be positive.',)
[ 1.91115555  0.62355264  0.58202678 -0.66951535]
Error: Zero implied volatility!, sample 2038
 
AssertionError('Price must be positive.',)
[4.0212755  0.0896595  0.19661343 0.23138345]
Error: Zero implied volatility!, sample 2038
 
AssertionError('Price must be positive.',)
[ 3.44602921  0.5730685   0.54591979 -0.88669578]
Error: Zero implied volatility!, sample 2051
 
AssertionError('Normalized price must be positive.',)
[2.0013864  0.65547    0.53772552 0.89865068]
Error: Zero implied volatility!, sample 2068
 
AssertionError('Price must be positive.',)
[ 1.69098074  0.37696558  0.32431448 -0.29670654]
Error: Zero implied volatility!, sample 2075
 
AssertionError('Price must be positive.',)
[ 7.77602612  0.07685421  0.18956923 -0.37305951]
Error: Zero implied volatility!, sample 2079
 
AssertionError('Price must be positive.',)
[ 1.75023067  0.53171898  0.17639485 -0.82123035]
Error: Zero implied volatility!, sample 2082
 
AssertionError('Price must be positive.',)
[ 6.98961526  0.06648599  0.25820636 -0.19477166]
Error: Zero implied volatility!, sample 2107
 
AssertionError('Price must be positive.',)
[ 1.9779188   0.54005933  0.62685068 -0.76295009]
Error: Zero implied volatility!, sample 2111
 
AssertionError('Normalized price must be positive.',)
[1.65855668 0.3469144  0.20643642 0.73193287]
Error: Zero implied volatility!, sample 2149
 
AssertionError('Price must be positive.',)
[ 1.06983366  0.54535679  0.72434703 -0.76529579]
Error: Zero implied volatility!, sample 2156
 
AssertionError('Price must be positive.',)
[2.04872858 0.17563244 0.14163506 0.68268573]
Error: Zero implied volatility!, sample 2159
 
AssertionError('Price must be positive.',)
[ 8.69601409  0.06401698  0.42073302 -0.35691032]
Error: Zero implied volatility!, sample 2159
 
AssertionError('Price must be positive.',)
[ 3.78295604  0.35869835  0.59178055 -0.9186695 ]
Error: Zero implied volatility!, sample 2191
 
AssertionError('Price must be positive.',)
[ 3.6826314   0.10096799  0.78434506 -0.54388795]
Error: Zero implied volatility!, sample 2210
 
AssertionError('Price must be positive.',)
[ 5.82847432  0.08954553  0.63342552 -0.86575541]
Error: Zero implied volatility!, sample 2214
 
AssertionError('Price must be positive.',)
[ 2.15196598  0.37269755  0.60655977 -0.98679291]
Error: Zero implied volatility!, sample 2220
 
AssertionError('Price must be positive.',)
[ 4.03612283  0.33980119  0.39806362 -0.87350998]
Error: Zero implied volatility!, sample 2246
 
AssertionError('Price must be positive.',)
[ 8.39314923  0.14915996  0.72346125 -0.66209952]
Error: Zero implied volatility!, sample 2249
 
AssertionError('Price must be positive.',)
[ 1.75631035  0.6066936   0.56617602 -0.65599128]
Error: Zero implied volatility!, sample 2278
 
AssertionError('Price must be positive.',)
[9.87212345 0.05781347 0.37355201 0.23528625]
Error: Zero implied volatility!, sample 2370
 
AssertionError('Price must be positive.',)
[4.67402397 0.10249065 0.17526135 0.66170578]
Error: Zero implied volatility!, sample 2395
 
AssertionError('Price must be positive.',)
[ 2.73215548  0.3103195   0.28966603 -0.93644054]
Error: Zero implied volatility!, sample 2407
 
AssertionError('Price must be positive.',)
[ 1.86867789  0.31724254  0.35663984 -0.74972022]
Error: Zero implied volatility!, sample 2435
 
AssertionError('Price must be positive.',)
[ 1.66874186  0.17792282  0.10084456 -0.50022994]
Error: Zero implied volatility!, sample 2457
 
AssertionError('Price must be positive.',)
[ 5.96220448  0.14343128  0.36808923 -0.64985046]
Error: Zero implied volatility!, sample 2462
 
AssertionError('Price must be positive.',)
[ 3.22028011  0.25938827  0.12638199 -0.64105281]
Error: Zero implied volatility!, sample 2509
 
AssertionError('Price must be positive.',)
[ 3.06763229  0.53415957  0.6862363  -0.82612949]
Error: Zero implied volatility!, sample 2513
 
AssertionError('Price must be positive.',)
[ 0.76265506  0.69033197  0.27802502 -0.28090808]
Error: Zero implied volatility!, sample 2519
 
AssertionError('Price must be positive.',)
[ 0.68236975  0.79780626  0.20395001 -0.46041809]
Error: Zero implied volatility!, sample 2533
 
AssertionError('Price must be positive.',)
[ 1.26223438  0.59946379  0.47077826 -0.68701854]
Error: Zero implied volatility!, sample 2565
 
AssertionError('Price must be positive.',)
[ 5.34607881  0.16874199  0.44676918 -0.95862774]
Error: Zero implied volatility!, sample 2565
 
AssertionError('Price must be positive.',)
[ 0.61016371  0.44579238  0.27448914 -0.33415643]
Error: Zero implied volatility!, sample 2582
 
AssertionError('Price must be positive.',)
[ 7.7347359   0.15976365  0.60628284 -0.91596337]
Error: Zero implied volatility!, sample 2600
 
AssertionError('Price must be positive.',)
[ 6.73343825  0.16440268  0.24273878 -0.68075641]
Error: Zero implied volatility!, sample 2633
 
AssertionError('Price must be positive.',)
[ 0.72520942  0.32210586  0.20814253 -0.65443583]
Error: Zero implied volatility!, sample 2638
 
AssertionError('Price must be positive.',)
[0.72444176 0.34445482 0.09804604 0.18331633]
Error: Zero implied volatility!, sample 2639
 
AssertionError('Normalized price must be positive.',)
[2.51789752 0.2854098  0.57117551 0.86581293]
Error: Zero implied volatility!, sample 2643
 
AssertionError('Price must be positive.',)
[ 2.87393622  0.70248735  0.52491005 -0.8689309 ]
Error: Zero implied volatility!, sample 2710
 
AssertionError('Normalized price must be positive.',)
[1.36622437 0.22043299 0.18813702 0.89863401]
Error: Zero implied volatility!, sample 2718
 
AssertionError('Price must be positive.',)
[ 3.00119257  0.59801989  0.75766742 -0.85486881]
Error: Zero implied volatility!, sample 2787
 
AssertionError('Price must be positive.',)
[ 0.82420787  0.25692547  0.41281103 -0.56135428]
Error: Zero implied volatility!, sample 2808
 
AssertionError('Price must be positive.',)
[ 7.96584107  0.1596279   0.56803189 -0.77797699]
Error: Zero implied volatility!, sample 2813
 
AssertionError('Price must be positive.',)
[5.90983609 0.0540362  0.05027731 0.69379847]
Error: Zero implied volatility!, sample 2842
 
AssertionError('Normalized price must be positive.',)
[0.55408612 0.55638678 0.71427909 0.98640011]
Error: Zero implied volatility!, sample 2848
 
AssertionError('Price must be positive.',)
[ 3.75080763  0.10849685  0.64276063 -0.60782369]
Error: Zero implied volatility!, sample 2857
 
AssertionError('Price must be positive.',)
[ 2.46652545  0.53470618  0.33632989 -0.87950108]
Error: Zero implied volatility!, sample 2857
 
AssertionError('Price must be positive.',)
[3.0210564  0.13961874 0.78619119 0.95359621]
Error: Zero implied volatility!, sample 2870
 
AssertionError('Normalized price must be positive.',)
[3.46795374 0.22600455 0.42587302 0.83259756]
Error: Zero implied volatility!, sample 2871
 
AssertionError('Price must be positive.',)
[ 1.83060671  0.47686402  0.55500809 -0.70360721]
Error: Zero implied volatility!, sample 2895
 
AssertionError('Price must be positive.',)
[6.52870235 0.0870923  0.60705982 0.78770494]
Error: Zero implied volatility!, sample 2906
 
AssertionError('Price must be positive.',)
[1.63804153 0.09757373 0.39180171 0.87438413]
Error: Zero implied volatility!, sample 2912
 
AssertionError('Price must be positive.',)
[ 1.87444279  0.31907721  0.15351186 -0.83034548]
Error: Zero implied volatility!, sample 2959
 
AssertionError('Price must be positive.',)
[ 8.71683838  0.0869303   0.26308977 -0.35985707]
Error: Zero implied volatility!, sample 2961
 
AssertionError('Price must be positive.',)
[ 1.0628228   0.50021489  0.67454831 -0.80099293]
Error: Zero implied volatility!, sample 2991
 
AssertionError('Price must be positive.',)
[ 4.07959653  0.13639701  0.08901788 -0.19947484]
Error: Zero implied volatility!, sample 3026
 
AssertionError('Price must be positive.',)
[1.20299128 0.12180369 0.40138442 0.28916178]
Error: Zero implied volatility!, sample 3026
 
AssertionError('Price must be positive.',)
[ 1.3385152   0.5739714   0.43598231 -0.28135346]
Error: Zero implied volatility!, sample 3040
 
AssertionError('Normalized price must be positive.',)
[1.24747717 0.39519246 0.69079702 0.9173457 ]
Error: Zero implied volatility!, sample 3044
 
AssertionError('Price must be positive.',)
[ 4.39900032  0.34994519  0.75877805 -0.87778464]
Error: Zero implied volatility!, sample 3108
 
AssertionError('Price must be positive.',)
[ 5.77035893  0.08457979  0.40632906 -0.67012616]
Error: Zero implied volatility!, sample 3118
 
AssertionError('Price must be positive.',)
[ 1.35272192  0.17331724  0.52731229 -0.39825212]
Error: Zero implied volatility!, sample 3133
 
AssertionError('Price must be positive.',)
[ 4.73014457  0.09919406  0.28356764 -0.78985276]
Error: Zero implied volatility!, sample 3145
 
AssertionError('Price must be positive.',)
[ 1.65181574  0.32342767  0.22101447 -0.92457885]
Error: Zero implied volatility!, sample 3164
 
AssertionError('Price must be positive.',)
[ 1.40178051  0.09951784  0.51045778 -0.84256718]
Error: Zero implied volatility!, sample 3220
 
AssertionError('Price must be positive.',)
[ 2.80462533  0.11196449  0.43526456 -0.44395221]
Error: Zero implied volatility!, sample 3230
 
AssertionError('Price must be positive.',)
[ 2.84551518  0.53916103  0.61416891 -0.96218289]
Error: Zero implied volatility!, sample 3234
 
AssertionError('Normalized price must be positive.',)
[2.62814488 0.40372429 0.4867756  0.89203527]
Error: Zero implied volatility!, sample 3288
 
AssertionError('Price must be positive.',)
[ 1.38708165  0.36564463  0.31947478 -0.63496163]
Error: Zero implied volatility!, sample 3289
 
AssertionError('Price must be positive.',)
[ 1.68867476  0.59163139  0.28623227 -0.68840027]
Error: Zero implied volatility!, sample 3292
 
AssertionError('Normalized price must be positive.',)
[3.20233298 0.16472396 0.20761443 0.50564021]
Error: Zero implied volatility!, sample 3311
 
AssertionError('Price must be positive.',)
[ 3.71088677  0.05938199  0.6291483  -0.76580342]
Error: Zero implied volatility!, sample 3332
 
AssertionError('Price must be positive.',)
[ 1.44343661  0.49484635  0.55505009 -0.69800376]
Error: Zero implied volatility!, sample 3358
 
AssertionError('Price must be positive.',)
[1.28954942 0.11854815 0.42581095 0.33738595]
Error: Zero implied volatility!, sample 3380
 
AssertionError('Price must be positive.',)
[ 4.34642389  0.12656595  0.64088431 -0.66886247]
Error: Zero implied volatility!, sample 3404
 
AssertionError('Normalized price must be positive.',)
[2.84703468 0.30700411 0.44396601 0.62547476]
Error: Zero implied volatility!, sample 3409
 
AssertionError('Normalized price must be positive.',)
[9.41142717 0.16833985 0.30947942 0.96620146]
Error: Zero implied volatility!, sample 3435
 
AssertionError('Price must be positive.',)
[ 8.32981866  0.11401261  0.2331778  -0.25926876]
Error: Zero implied volatility!, sample 3440
 
AssertionError('Normalized price must be positive.',)
[1.22243957 0.6122872  0.58240247 0.74331499]
Error: Zero implied volatility!, sample 3441
 
AssertionError('Normalized price must be positive.',)
[4.08675254 0.09083636 0.44340964 0.74289669]
Error: Zero implied volatility!, sample 3470
 
AssertionError('Price must be positive.',)
[ 1.30746774  0.63048678  0.76956968 -0.96784255]
Error: Zero implied volatility!, sample 3483
 
AssertionError('Price must be positive.',)
[ 3.28463592  0.18431403  0.75573178 -0.57287015]
Error: Zero implied volatility!, sample 3531
 
AssertionError('Price must be positive.',)
[ 4.65935471  0.06901377  0.77252306 -0.41915613]
Error: Zero implied volatility!, sample 3533
 
AssertionError('Price must be positive.',)
[ 1.23183299  0.15687465  0.29763324 -0.67653427]
Error: Zero implied volatility!, sample 3592
 
AssertionError('Price must be positive.',)
[ 3.37535247  0.44181096  0.54929048 -0.96127216]
Error: Zero implied volatility!, sample 3594
 
AssertionError('Price must be positive.',)
[ 4.48506739  0.55932763  0.722322   -0.98901944]
Error: Zero implied volatility!, sample 3603
 
AssertionError('Price must be positive.',)
[ 3.61629812  0.24781652  0.55776383 -0.92519513]
Error: Zero implied volatility!, sample 3609
 
AssertionError('Price must be positive.',)
[ 5.73307815  0.23980437  0.7824464  -0.93933197]
Error: Zero implied volatility!, sample 3620
 
AssertionError('Normalized price must be positive.',)
[1.40656768 0.67156002 0.26713278 0.54674353]
Error: Zero implied volatility!, sample 3627
 
AssertionError('Price must be positive.',)
[ 0.88132758  0.7883925   0.40043533 -0.48685844]
Error: Zero implied volatility!, sample 3636
 
AssertionError('Price must be positive.',)
[2.80644632 0.08472898 0.49263539 0.85179495]
Error: Zero implied volatility!, sample 3648
 
AssertionError('Price must be positive.',)
[ 9.39530069  0.06336243  0.14856532 -0.73342263]
Error: Zero implied volatility!, sample 3661
 
AssertionError('Price must be positive.',)
[ 1.59688064  0.1635672   0.28201942 -0.61737434]
Error: Zero implied volatility!, sample 3675
 
AssertionError('Price must be positive.',)
[0.6115512  0.30018858 0.1077467  0.83033961]
Error: Zero implied volatility!, sample 3710
 
AssertionError('Price must be positive.',)
[ 6.22850215  0.09020419  0.78262833 -0.79667295]
Error: Zero implied volatility!, sample 3741
 
AssertionError('Price must be positive.',)
[ 3.97099578  0.38342959  0.41140382 -0.90788924]
Error: Zero implied volatility!, sample 3768
 
AssertionError('Normalized price must be positive.',)
[5.63182541 0.15791587 0.47545715 0.91335412]
Error: Zero implied volatility!, sample 3782
 
AssertionError('Price must be positive.',)
[ 1.04235846  0.62745735  0.17293253 -0.17708398]
Error: Zero implied volatility!, sample 3792
 
AssertionError('Price must be positive.',)
[ 1.05926256  0.4515381   0.42639122 -0.7589004 ]
Error: Zero implied volatility!, sample 3804
 
AssertionError('Price must be positive.',)
[ 6.57568446  0.14582473  0.32736893 -0.87887503]
Error: Zero implied volatility!, sample 3836
 
AssertionError('Normalized price must be positive.',)
[3.51181413 0.49381397 0.32704371 0.94817903]
Error: Zero implied volatility!, sample 3865
 
AssertionError('Normalized price must be positive.',)
[0.61472041 0.52611287 0.09432251 0.76432848]
Error: Zero implied volatility!, sample 3880
 
AssertionError('Price must be positive.',)
[ 1.43290878  0.48845286  0.1415061  -0.75944714]
Error: Zero implied volatility!, sample 3893
 
AssertionError('Normalized price must be positive.',)
[4.46375061 0.066046   0.12737773 0.68169635]
Error: Zero implied volatility!, sample 3898
 
AssertionError('Price must be positive.',)
[ 0.86995725  0.72416799  0.29304239 -0.89847412]
Error: Zero implied volatility!, sample 3901
 
AssertionError('Price must be positive.',)
[ 1.16375105  0.68036707  0.37129426 -0.56948363]
Error: Zero implied volatility!, sample 3922
 
AssertionError('Price must be positive.',)
[ 6.11763849  0.32415894  0.56013714 -0.83729651]
Error: Zero implied volatility!, sample 3977
 
AssertionError('Price must be positive.',)
[ 2.05380106  0.15495713  0.78553731 -0.62429021]
Error: Zero implied volatility!, sample 3977
 
AssertionError('Normalized price must be positive.',)
[9.52636182 0.12886042 0.34446629 0.92454359]
Error: Zero implied volatility!, sample 3988
 
AssertionError('Price must be positive.',)
[ 0.50069669  0.51777264  0.20183012 -0.9448792 ]
Error: Zero implied volatility!, sample 4003
 
AssertionError('Price must be positive.',)
[ 5.77431232  0.09708292  0.21591949 -0.23578625]
Error: Zero implied volatility!, sample 4004
 
AssertionError('Price must be positive.',)
[ 4.85382342  0.49004041  0.38694708 -0.97208606]
Error: Zero implied volatility!, sample 4009
 
AssertionError('Price must be positive.',)
[ 5.98163663  0.07656015  0.16732012 -0.13531691]
Error: Zero implied volatility!, sample 4034
 
AssertionError('Normalized price must be positive.',)
[0.54022422 0.62706669 0.20306738 0.88451797]
Error: Zero implied volatility!, sample 4043
 
AssertionError('Price must be positive.',)
[ 2.63618673  0.19405374  0.1838506  -0.40895921]
Error: Zero implied volatility!, sample 4047
 
AssertionError('Price must be positive.',)
[ 5.05212907  0.3387222   0.55427845 -0.89451742]
Error: Zero implied volatility!, sample 4051
 
AssertionError('Normalized price must be positive.',)
[0.58898498 0.3262311  0.52645944 0.45995781]
Error: Zero implied volatility!, sample 4061
 
AssertionError('Normalized price must be positive.',)
[7.66043289 0.10959347 0.78410422 0.94838103]
Error: Zero implied volatility!, sample 4063
 
AssertionError('Normalized price must be positive.',)
[5.27280749 0.06888876 0.41870297 0.90919035]
Error: Zero implied volatility!, sample 4072
 
AssertionError('Price must be positive.',)
[ 2.73174443  0.21831725  0.71082417 -0.92414388]
Error: Zero implied volatility!, sample 4078
 
AssertionError('Price must be positive.',)
[ 4.59443511  0.1986017   0.63426591 -0.78983522]
Error: Zero implied volatility!, sample 4088
 
AssertionError('Price must be positive.',)
[ 7.87662503  0.15535794  0.54061255 -0.8968539 ]
Error: Zero implied volatility!, sample 4121
 
AssertionError('Price must be positive.',)
[ 6.77953098  0.08369463  0.27631186 -0.35009832]
Error: Zero implied volatility!, sample 4130
 
AssertionError('Price must be positive.',)
[ 0.59335282  0.7264282   0.37852137 -0.70549091]
Error: Zero implied volatility!, sample 4160
 
AssertionError('Normalized price must be positive.',)
[0.78651397 0.75423911 0.51509958 0.79648985]
Error: Zero implied volatility!, sample 4170
 
AssertionError('Price must be positive.',)
[3.41111519 0.21606288 0.12614875 0.01211539]
Error: Zero implied volatility!, sample 4195
 
AssertionError('Normalized price must be positive.',)
[7.82333022 0.09594563 0.21717617 0.94817439]
Error: Zero implied volatility!, sample 4230
 
AssertionError('Normalized price must be positive.',)
[1.38602766 0.6130733  0.41414521 0.98725613]
Error: Zero implied volatility!, sample 4232
 
AssertionError('Price must be positive.',)
[ 9.53151706  0.24407     0.71727252 -0.77956329]
Error: Zero implied volatility!, sample 4240
 
AssertionError('Price must be positive.',)
[ 1.88492872  0.32411814  0.30654754 -0.38186159]
Error: Zero implied volatility!, sample 4244
 
AssertionError('Normalized price must be positive.',)
[4.00560394 0.24345383 0.72049732 0.6995635 ]
Error: Zero implied volatility!, sample 4244
 
AssertionError('Price must be positive.',)
[ 2.76724577  0.09139227  0.3098766  -0.8935263 ]
Error: Zero implied volatility!, sample 4252
 
AssertionError('Price must be positive.',)
[ 0.86561839  0.41771248  0.09074535 -0.56349294]
Error: Zero implied volatility!, sample 4267
 
AssertionError('Normalized price must be positive.',)
[1.22863113 0.29916962 0.7181743  0.84597346]
Error: Zero implied volatility!, sample 4335
 
AssertionError('Price must be positive.',)
[ 2.18450863  0.18460844  0.72685086 -0.27513042]
Error: Zero implied volatility!, sample 4342
 
AssertionError('Normalized price must be positive.',)
[2.38696815 0.28557158 0.31070475 0.98081448]
Error: Zero implied volatility!, sample 4346
 
AssertionError('Price must be positive.',)
[ 2.22235091  0.29248887  0.25577793 -0.68779283]
Error: Zero implied volatility!, sample 4360
 
AssertionError('Normalized price must be positive.',)
[4.47608157 0.14219561 0.43212364 0.78963102]
Error: Zero implied volatility!, sample 4373
 
AssertionError('Price must be positive.',)
[ 4.73413968  0.11181826  0.44661985 -0.64372442]
Error: Zero implied volatility!, sample 4390
 
AssertionError('Normalized price must be positive.',)
[1.86471254 0.38425517 0.16879074 0.97507844]
Error: Zero implied volatility!, sample 4433
 
AssertionError('Price must be positive.',)
[ 4.04802268  0.19732878  0.64535251 -0.89577008]
Error: Zero implied volatility!, sample 4583
 
AssertionError('Price must be positive.',)
[ 1.24017017  0.17376671  0.6027496  -0.20908708]
Error: Zero implied volatility!, sample 4660
 
AssertionError('Price must be positive.',)
[ 0.73411078  0.08962528  0.147002   -0.88312282]
Error: Zero implied volatility!, sample 4678
 
AssertionError('Price must be positive.',)
[ 5.98022571  0.34099147  0.79217041 -0.91218674]
Error: Zero implied volatility!, sample 4729
 
AssertionError('Price must be positive.',)
[ 1.62836718  0.79156589  0.36830741 -0.76947021]
Error: Zero implied volatility!, sample 4758
 
AssertionError('Price must be positive.',)
[ 1.27812768  0.66538884  0.47116041 -0.96847939]
Error: Zero implied volatility!, sample 4759
 
AssertionError('Price must be positive.',)
[ 0.95988413  0.08690014  0.23847846 -0.72342089]
Error: Zero implied volatility!, sample 4788
 
AssertionError('Price must be positive.',)
[ 3.50397307  0.16318772  0.47547673 -0.78848369]
Error: Zero implied volatility!, sample 4794
 
AssertionError('Price must be positive.',)
[ 5.45196119  0.15606779  0.29717043 -0.95645146]
Error: Zero implied volatility!, sample 4800
 
AssertionError('Normalized price must be positive.',)
[4.78355882 0.11129938 0.40186927 0.94038885]
Error: Zero implied volatility!, sample 4822
 
AssertionError('Normalized price must be positive.',)
[8.10470714 0.05846542 0.69473846 0.95435131]
Error: Zero implied volatility!, sample 4848
 
AssertionError('Price must be positive.',)
[ 0.70430716  0.59992543  0.06355554 -0.09824438]
Error: Zero implied volatility!, sample 4872
 
AssertionError('Price must be positive.',)
[9.15470443 0.0642824  0.51517321 0.64070084]
Error: Zero implied volatility!, sample 4874
 
AssertionError('Normalized price must be positive.',)
[0.94530416 0.59147568 0.22885666 0.92483828]
Error: Zero implied volatility!, sample 4884
 
AssertionError('Price must be positive.',)
[ 1.99885929  0.57872497  0.33700518 -0.37241523]
Error: Zero implied volatility!, sample 4898
 
AssertionError('Price must be positive.',)
[ 1.83605996  0.7046666   0.3682693  -0.79007198]
Error: Zero implied volatility!, sample 4911
 
AssertionError('Price must be positive.',)
[3.06359375 0.15548091 0.1300288  0.38899644]
Error: Zero implied volatility!, sample 4986
 
AssertionError('Price must be positive.',)
[ 5.89754443  0.63412048  0.79615943 -0.98159477]
Error: Zero implied volatility!, sample 5002
 
AssertionError('Price must be positive.',)
[5.45775699 0.11715666 0.50415077 0.96605559]
Error: Zero implied volatility!, sample 5038
 
AssertionError('Price must be positive.',)
[ 1.12059136  0.55295114  0.61182588 -0.75161174]
Error: Zero implied volatility!, sample 5049
 
AssertionError('Price must be positive.',)
[ 1.44595054  0.08037542  0.28709814 -0.49908471]
Error: Zero implied volatility!, sample 5049
 
AssertionError('Price must be positive.',)
[ 1.77198039  0.25553609  0.18636151 -0.73151537]
Error: Zero implied volatility!, sample 5054
 
AssertionError('Price must be positive.',)
[ 5.36843187  0.08959405  0.69655943 -0.53154901]
Error: Zero implied volatility!, sample 5055
 
AssertionError('Price must be positive.',)
[5.54178387 0.10540282 0.08683881 0.12418309]
Error: Zero implied volatility!, sample 5055
 
AssertionError('Price must be positive.',)
[ 4.98728311  0.29469945  0.62239798 -0.90956392]
Error: Zero implied volatility!, sample 5059
 
AssertionError('Price must be positive.',)
[ 1.47420526  0.28597497  0.32266751 -0.49201441]
Error: Zero implied volatility!, sample 5073
 
AssertionError('Price must be positive.',)
[ 4.4086108   0.06264982  0.69216864 -0.84749866]
Error: Zero implied volatility!, sample 5073
 
AssertionError('Price must be positive.',)
[0.85475181 0.20102424 0.3021255  0.80416107]
Error: Zero implied volatility!, sample 5075
 
AssertionError('Normalized price must be positive.',)
[3.31250385 0.08602906 0.72965146 0.96697228]
Error: Zero implied volatility!, sample 5085
 
AssertionError('Price must be positive.',)
[ 6.24282593  0.23763984  0.65175681 -0.74775938]
Error: Zero implied volatility!, sample 5085
 
AssertionError('Normalized price must be positive.',)
[5.87760804 0.36505116 0.4232568  0.98037092]
Error: Zero implied volatility!, sample 5092
 
AssertionError('Normalized price must be positive.',)
[1.47915706 0.58505678 0.76536753 0.8347003 ]
Error: Zero implied volatility!, sample 5117
 
AssertionError('Normalized price must be positive.',)
[1.77241247 0.36303129 0.16817402 0.83394524]
Error: Zero implied volatility!, sample 5135
 
AssertionError('Price must be positive.',)
[ 3.27602532  0.27113392  0.33623547 -0.91417576]
Error: Zero implied volatility!, sample 5154
 
AssertionError('Price must be positive.',)
[ 0.90984646  0.15265244  0.19920468 -0.06841381]
Error: Zero implied volatility!, sample 5158
 
AssertionError('Price must be positive.',)
[2.3185992  0.10479628 0.54393517 0.8462685 ]
Error: Zero implied volatility!, sample 5189
 
AssertionError('Price must be positive.',)
[1.36610783 0.24524059 0.65159815 0.07954606]
Error: Zero implied volatility!, sample 5197
 
AssertionError('Normalized price must be positive.',)
[1.3011418  0.47566175 0.54942584 0.61936162]
Error: Zero implied volatility!, sample 5207
 
AssertionError('Price must be positive.',)
[ 1.63107258  0.15107581  0.66219985 -0.87708654]
Error: Zero implied volatility!, sample 5236
 
AssertionError('Price must be positive.',)
[ 6.14547588  0.19519581  0.2809543  -0.34964656]
Error: Zero implied volatility!, sample 5242
 
AssertionError('Price must be positive.',)
[ 2.45446859  0.57241762  0.42042656 -0.96838286]
Error: Zero implied volatility!, sample 5253
 
AssertionError('Price must be positive.',)
[ 5.57604602  0.06997026  0.23058807 -0.7733413 ]
Error: Zero implied volatility!, sample 5276
 
AssertionError('Price must be positive.',)
[ 6.1380641   0.11077509  0.28559722 -0.59685576]
Error: Zero implied volatility!, sample 5313
 
AssertionError('Price must be positive.',)
[ 8.73944171  0.13156832  0.42190554 -0.50820084]
Error: Zero implied volatility!, sample 5342
 
AssertionError('Price must be positive.',)
[ 1.18209141  0.16161814  0.37790394 -0.33845377]
Error: Zero implied volatility!, sample 5343
 
AssertionError('Price must be positive.',)
[4.50987996 0.17939538 0.53115082 0.96216878]
Error: Zero implied volatility!, sample 5347
 
AssertionError('Price must be positive.',)
[ 0.75357508  0.4626954   0.56710552 -0.35216429]
Error: Zero implied volatility!, sample 5365
 
AssertionError('Price must be positive.',)
[ 7.7558399   0.06661641  0.7903809  -0.8880735 ]
Error: Zero implied volatility!, sample 5395
 
AssertionError('Price must be positive.',)
[2.79902372 0.24075097 0.11381328 0.15715693]
Error: Zero implied volatility!, sample 5397
 
AssertionError('Price must be positive.',)
[ 2.83372999  0.44723326  0.4679267  -0.92612329]
Error: Zero implied volatility!, sample 5403
 
AssertionError('Normalized price must be positive.',)
[9.22592504 0.22470413 0.65808351 0.94588892]
Error: Zero implied volatility!, sample 5416
 
AssertionError('Price must be positive.',)
[2.25810006 0.05059489 0.11171759 0.08843123]
Error: Zero implied volatility!, sample 5464
 
AssertionError('Normalized price must be positive.',)
[5.29280728 0.22517205 0.6315839  0.92272063]
Error: Zero implied volatility!, sample 5515
 
AssertionError('Price must be positive.',)
[ 2.93848811  0.21935138  0.34484146 -0.58823599]
Error: Zero implied volatility!, sample 5524
 
AssertionError('Normalized price must be positive.',)
[1.85720318 0.55482923 0.77004841 0.93204754]
Error: Zero implied volatility!, sample 5561
 
AssertionError('Price must be positive.',)
[ 4.07615884  0.05770415  0.26577998 -0.7103063 ]
Error: Zero implied volatility!, sample 5562
 
AssertionError('Price must be positive.',)
[2.13738934 0.23030477 0.1288111  0.89748696]
Error: Zero implied volatility!, sample 5585
 
AssertionError('Normalized price must be positive.',)
[1.80566425 0.55867658 0.32340762 0.82426218]
Error: Zero implied volatility!, sample 5618
 
AssertionError('Price must be positive.',)
[ 1.22783776  0.09269559  0.38879769 -0.91913365]
Error: Zero implied volatility!, sample 5636
 
AssertionError('Price must be positive.',)
[ 1.48960046  0.59634394  0.381039   -0.38228372]
Error: Zero implied volatility!, sample 5658
 
AssertionError('Price must be positive.',)
[ 3.90589612  0.22167051  0.19760262 -0.89626278]
Error: Zero implied volatility!, sample 5664
 
AssertionError('Normalized price must be positive.',)
[7.81401351 0.16351551 0.42038936 0.77393669]
Error: Zero implied volatility!, sample 5666
 
AssertionError('Price must be positive.',)
[ 8.48780579  0.0887415   0.22959164 -0.97911561]
Error: Zero implied volatility!, sample 5666
 
AssertionError('Price must be positive.',)
[ 0.58247754  0.28589241  0.22943167 -0.31857278]
Error: Zero implied volatility!, sample 5678
 
AssertionError('Price must be positive.',)
[ 2.16673846  0.44433309  0.31305778 -0.56132769]
Error: Zero implied volatility!, sample 5697
 
AssertionError('Price must be positive.',)
[ 0.82691754  0.55915705  0.67420756 -0.39335303]
Error: Zero implied volatility!, sample 5750
 
AssertionError('Normalized price must be positive.',)
[2.18792174 0.39427929 0.29845488 0.95519319]
Error: Zero implied volatility!, sample 5765
 
AssertionError('Price must be positive.',)
[ 5.64543531  0.09649081  0.42607974 -0.94942095]
Error: Zero implied volatility!, sample 5856
 
AssertionError('Price must be positive.',)
[0.80628293 0.1839373  0.24756167 0.44437881]
Error: Zero implied volatility!, sample 5865
 
AssertionError('Price must be positive.',)
[ 6.40520276  0.25196306  0.42712554 -0.5757535 ]
Error: Zero implied volatility!, sample 5875
 
AssertionError('Price must be positive.',)
[ 5.43963224  0.18302005  0.59265755 -0.91867816]
Error: Zero implied volatility!, sample 5899
 
AssertionError('Price must be positive.',)
[ 1.05419331  0.59838856  0.13969806 -0.89111696]
Error: Zero implied volatility!, sample 5924
 
AssertionError('Normalized price must be positive.',)
[0.82774562 0.26763779 0.16728874 0.44635924]
Error: Zero implied volatility!, sample 5946
 
AssertionError('Normalized price must be positive.',)
[5.80519828 0.11867849 0.67326623 0.65070056]
Error: Zero implied volatility!, sample 5973
 
AssertionError('Price must be positive.',)
[ 3.5855541   0.57910493  0.60181284 -0.80281024]
Error: Zero implied volatility!, sample 5982
 
AssertionError('Price must be positive.',)
[ 8.46884337  0.11152324  0.24194399 -0.49225531]
Error: Zero implied volatility!, sample 5992
 
AssertionError('Normalized price must be positive.',)
[0.58891911 0.49195859 0.24014883 0.13910417]
Error: Zero implied volatility!, sample 5995
 
AssertionError('Price must be positive.',)
[ 2.6936595   0.19740496  0.46419347 -0.64338753]
Error: Zero implied volatility!, sample 6001
 
AssertionError('Price must be positive.',)
[ 4.68754954  0.06610623  0.4409242  -0.7750367 ]
Error: Zero implied volatility!, sample 6028
 
AssertionError('Normalized price must be positive.',)
[1.93197429 0.496114   0.63355747 0.97929664]
Error: Zero implied volatility!, sample 6078
 
AssertionError('Price must be positive.',)
[ 1.73937564  0.11232475  0.08720956 -0.34326797]
Error: Zero implied volatility!, sample 6109
 
AssertionError('Price must be positive.',)
[ 3.51225008  0.15277491  0.09362007 -0.65841394]
Error: Zero implied volatility!, sample 6167
 
AssertionError('Price must be positive.',)
[ 4.438557    0.39807023  0.55818056 -0.98836248]
Error: Zero implied volatility!, sample 6249
 
AssertionError('Price must be positive.',)
[ 2.02897795  0.49080845  0.49258277 -0.59433255]
Error: Zero implied volatility!, sample 6259
 
AssertionError('Price must be positive.',)
[3.35616619 0.17294792 0.30285287 0.93656002]
Error: Zero implied volatility!, sample 6272
 
AssertionError('Normalized price must be positive.',)
[2.15147105 0.42450959 0.42522358 0.84341346]
Error: Zero implied volatility!, sample 6274
 
AssertionError('Price must be positive.',)
[ 3.1116645   0.17802088  0.64390212 -0.75728314]
Error: Zero implied volatility!, sample 6296
 
AssertionError('Price must be positive.',)
[ 4.57996585  0.18634148  0.41788386 -0.71842699]
Error: Zero implied volatility!, sample 6306
 
AssertionError('Price must be positive.',)
[3.81898642 0.05827654 0.28225776 0.94292818]
Error: Zero implied volatility!, sample 6345
 
AssertionError('Price must be positive.',)
[ 7.77771598  0.12212227  0.21813224 -0.58161393]
Error: Zero implied volatility!, sample 6356
 
AssertionError('Price must be positive.',)
[0.50052186 0.49559193 0.51299038 0.68134062]
Error: Zero implied volatility!, sample 6364
 
AssertionError('Price must be positive.',)
[ 1.19343704  0.23482852  0.17060972 -0.27283521]
Error: Zero implied volatility!, sample 6374
 
AssertionError('Price must be positive.',)
[ 4.08443914  0.3837715   0.33472536 -0.66970664]
Error: Zero implied volatility!, sample 6384
 
AssertionError('Price must be positive.',)
[ 1.14092636  0.29483573  0.08386616 -0.45807709]
Error: Zero implied volatility!, sample 6390
 
AssertionError('Price must be positive.',)
[ 3.19171273  0.63802072  0.64946775 -0.90520991]
Error: Zero implied volatility!, sample 6430
 
AssertionError('Price must be positive.',)
[2.19159969 0.15171792 0.2760232  0.48951726]
Error: Zero implied volatility!, sample 6441
 
AssertionError('Price must be positive.',)
[4.30913868 0.05761824 0.24856959 0.6740107 ]
Error: Zero implied volatility!, sample 6463
 
AssertionError('Price must be positive.',)
[ 7.82382979  0.12500678  0.71049647 -0.87761839]
Error: Zero implied volatility!, sample 6520
 
AssertionError('Price must be positive.',)
[ 4.20902321  0.37627728  0.24722807 -0.72294735]
Error: Zero implied volatility!, sample 6552
 
AssertionError('Normalized price must be positive.',)
[6.47299897 0.23434202 0.6363599  0.87314595]
Error: Zero implied volatility!, sample 6613
 
AssertionError('Price must be positive.',)
[ 2.77616632  0.11155417  0.14239591 -0.68107481]
Error: Zero implied volatility!, sample 6614
 
AssertionError('Price must be positive.',)
[ 1.02387479  0.27438394  0.18445713 -0.24621396]
Error: Zero implied volatility!, sample 6628
 
AssertionError('Price must be positive.',)
[ 4.92278098  0.20309782  0.11515759 -0.84255137]
Error: Zero implied volatility!, sample 6649
 
AssertionError('Price must be positive.',)
[0.81556387 0.6230676  0.28797705 0.07544786]
Error: Zero implied volatility!, sample 6705
 
AssertionError('Price must be positive.',)
[ 2.95674254  0.30139284  0.7870356  -0.96939048]
Error: Zero implied volatility!, sample 6718
 
AssertionError('Price must be positive.',)
[ 3.49986088  0.55501558  0.24911571 -0.98354441]
Error: Zero implied volatility!, sample 6722
 
AssertionError('Price must be positive.',)
[1.88288797 0.22102509 0.56236736 0.46128619]
Error: Zero implied volatility!, sample 6774
 
AssertionError('Price must be positive.',)
[ 1.19156176  0.15135757  0.18306614 -0.8235623 ]
Error: Zero implied volatility!, sample 6774
 
AssertionError('Price must be positive.',)
[2.77448618 0.13152097 0.53356919 0.13565295]
Error: Zero implied volatility!, sample 6801
 
AssertionError('Price must be positive.',)
[ 3.46460465  0.46226302  0.36987289 -0.59084423]
Error: Zero implied volatility!, sample 6801
 
AssertionError('Normalized price must be positive.',)
[2.84948158 0.30806643 0.66274017 0.60754143]
Error: Zero implied volatility!, sample 6838
 
AssertionError('Initial guess must be positive.',)
[1.37812518 0.55580968 0.50592715 0.83404305]
Error: Zero implied volatility!, sample 6843
 
AssertionError('Price must be positive.',)
[ 1.64176187  0.23261356  0.43667087 -0.14036903]
Error: Zero implied volatility!, sample 6879
 
AssertionError('Price must be positive.',)
[1.65671757 0.07903181 0.19577831 0.00859609]
Error: Zero implied volatility!, sample 6903
 
AssertionError('Normalized price must be positive.',)
[1.15249982 0.42915548 0.63671454 0.9727642 ]
Error: Zero implied volatility!, sample 6928
 
AssertionError('Price must be positive.',)
[ 3.27631856  0.41726622  0.62654062 -0.8456589 ]
Error: Zero implied volatility!, sample 6937
 
AssertionError('Price must be positive.',)
[4.92775773 0.09922715 0.78704525 0.09486082]
Error: Zero implied volatility!, sample 6937
 
AssertionError('Price must be positive.',)
[ 1.84485223  0.10435686  0.29625375 -0.52309046]
Error: Zero implied volatility!, sample 6939
 
AssertionError('Price must be positive.',)
[ 2.11053733  0.30466146  0.46230676 -0.98746181]
Error: Zero implied volatility!, sample 7018
 
AssertionError('Price must be positive.',)
[ 8.11235399  0.056687    0.27975312 -0.63039167]
Error: Zero implied volatility!, sample 7020
 
AssertionError('Normalized price must be positive.',)
[1.24234589 0.58510471 0.55082116 0.59484646]
Error: Zero implied volatility!, sample 7041
 
AssertionError('Price must be positive.',)
[ 0.783075    0.13695044  0.30334266 -0.40897955]
Error: Zero implied volatility!, sample 7057
 
AssertionError('Price must be positive.',)
[ 0.59431379  0.56015495  0.09456361 -0.64642691]
Error: Zero implied volatility!, sample 7078
 
AssertionError('Price must be positive.',)
[ 4.68537827  0.139443    0.0544149  -0.45752206]
Error: Zero implied volatility!, sample 7104
 
AssertionError('Price must be positive.',)
[ 1.46404672  0.15004922  0.16167524 -0.88522261]
Error: Zero implied volatility!, sample 7125
 
AssertionError('Price must be positive.',)
[ 6.77693725  0.13200682  0.34823425 -0.70054049]
Error: Zero implied volatility!, sample 7144
 
AssertionError('Price must be positive.',)
[ 4.30663299  0.05186394  0.2233967  -0.55544498]
Error: Zero implied volatility!, sample 7149
 
AssertionError('Normalized price must be positive.',)
[1.71300799 0.31649022 0.45363834 0.76956601]
Error: Zero implied volatility!, sample 7166
 
AssertionError('Price must be positive.',)
[ 1.69547772  0.27593486  0.25248394 -0.49701495]
Error: Zero implied volatility!, sample 7166
 
AssertionError('Normalized price must be positive.',)
[2.69709047 0.63920174 0.75743895 0.95636475]
Error: Zero implied volatility!, sample 7250
 
AssertionError('Price must be positive.',)
[ 9.31511979  0.16840663  0.7373459  -0.932264  ]
Error: Zero implied volatility!, sample 7253
 
AssertionError('Price must be positive.',)
[ 6.51338822  0.36310452  0.67674604 -0.86795639]
Error: Zero implied volatility!, sample 7263
 
AssertionError('Price must be positive.',)
[ 7.62347274  0.27368436  0.73975466 -0.79202375]
Error: Zero implied volatility!, sample 7281
 
AssertionError('Price must be positive.',)
[1.23497742 0.19613404 0.65304365 0.94838462]
Error: Zero implied volatility!, sample 7293
 
AssertionError('Price must be positive.',)
[ 2.59398793  0.16997582  0.10485563 -0.68108974]
Error: Zero implied volatility!, sample 7301
 
AssertionError('Price must be positive.',)
[ 2.39164463  0.13102833  0.24282565 -0.95597935]
Error: Zero implied volatility!, sample 7322
 
AssertionError('Price must be positive.',)
[ 4.27325929  0.06116189  0.18498964 -0.464067  ]
Error: Zero implied volatility!, sample 7328
 
AssertionError('Price must be positive.',)
[4.06537188 0.05373048 0.53450941 0.62130852]
Error: Zero implied volatility!, sample 7332
 
AssertionError('Price must be positive.',)
[1.51041653 0.08235533 0.11378285 0.58224789]
Error: Zero implied volatility!, sample 7339
 
AssertionError('Normalized price must be positive.',)
[1.32297868 0.18250118 0.06248005 0.1703717 ]
Error: Zero implied volatility!, sample 7357
 
AssertionError('Price must be positive.',)
[ 5.0581934   0.08813137  0.56113849 -0.79812138]
Error: Zero implied volatility!, sample 7391
 
AssertionError('Price must be positive.',)
[ 6.08098483  0.16528138  0.28112619 -0.3472484 ]
Error: Zero implied volatility!, sample 7395
 
AssertionError('Normalized price must be positive.',)
[7.05658853 0.22247275 0.79322468 0.9400334 ]
Error: Zero implied volatility!, sample 7433
 
AssertionError('Price must be positive.',)
[3.43893706 0.06122162 0.40067822 0.17682909]
Error: Zero implied volatility!, sample 7472
 
AssertionError('Price must be positive.',)
[ 1.66226791  0.46357734  0.60385334 -0.6705465 ]
Error: Zero implied volatility!, sample 7477
 
AssertionError('Price must be positive.',)
[ 1.90069269  0.34233286  0.1172013  -0.54794071]
Error: Zero implied volatility!, sample 7478
 
AssertionError('Normalized price must be positive.',)
[0.93542706 0.47631264 0.75186176 0.6256293 ]
Error: Zero implied volatility!, sample 7493
 
AssertionError('Price must be positive.',)
[ 8.26131597  0.05914208  0.5383544  -0.82699537]
Error: Zero implied volatility!, sample 7568
 
AssertionError('Price must be positive.',)
[ 4.08496487  0.13905527  0.69182593 -0.49479363]
Error: Zero implied volatility!, sample 7621
 
AssertionError('Price must be positive.',)
[ 3.90671741  0.42860513  0.54716842 -0.76864616]
Error: Zero implied volatility!, sample 7631
 
AssertionError('Price must be positive.',)
[ 1.16903587  0.38285378  0.40396316 -0.62418521]
Error: Zero implied volatility!, sample 7648
 
AssertionError('Price must be positive.',)
[ 3.39546877  0.17049171  0.14800843 -0.32933398]
Error: Zero implied volatility!, sample 7709
 
AssertionError('Price must be positive.',)
[ 1.02461544  0.61276698  0.24657732 -0.3500557 ]
Error: Zero implied volatility!, sample 7739
 
AssertionError('Price must be positive.',)
[ 8.84348207  0.12191729  0.22219139 -0.39088656]
Error: Zero implied volatility!, sample 7814
 
AssertionError('Price must be positive.',)
[ 8.24946032  0.0899839   0.69792272 -0.8738538 ]
Error: Zero implied volatility!, sample 7842
 
AssertionError('Price must be positive.',)
[ 2.24969265  0.2976998   0.54793131 -0.76593842]
Error: Zero implied volatility!, sample 7851
 
AssertionError('Price must be positive.',)
[3.5984231  0.1357696  0.64849032 0.46693588]
Error: Zero implied volatility!, sample 7862
 
AssertionError('Price must be positive.',)
[1.05723545 0.15012228 0.29430808 0.77969826]
Error: Zero implied volatility!, sample 7891
 
AssertionError('Normalized price must be positive.',)
[8.22765712 0.15747555 0.51991753 0.75396814]
Error: Zero implied volatility!, sample 7898
 
AssertionError('Price must be positive.',)
[ 1.15962341  0.43571893  0.46404633 -0.77684651]
Error: Zero implied volatility!, sample 7957
 
AssertionError('Price must be positive.',)
[ 1.02629295  0.23259667  0.68616419 -0.60201602]
Error: Zero implied volatility!, sample 7976
 
AssertionError('Price must be positive.',)
[1.97929766 0.28307446 0.7733613  0.8519539 ]
Error: Zero implied volatility!, sample 8006
 
AssertionError('Price must be positive.',)
[ 4.79233789  0.14382586  0.391954   -0.43032139]
Error: Zero implied volatility!, sample 8024
 
AssertionError('Price must be positive.',)
[ 5.48337962  0.08353988  0.09293279 -0.12788825]
Error: Zero implied volatility!, sample 8034
 
AssertionError('Price must be positive.',)
[ 7.62057779  0.0978448   0.39253476 -0.45270384]
Error: Zero implied volatility!, sample 8104
 
AssertionError('Price must be positive.',)
[ 9.42454519  0.09759323  0.55047411 -0.9691182 ]
Error: Zero implied volatility!, sample 8116
 
AssertionError('Normalized price must be positive.',)
[1.54193403 0.42317415 0.31213044 0.3814477 ]
Error: Zero implied volatility!, sample 8195
 
AssertionError('Price must be positive.',)
[2.63408602 0.06044406 0.19938324 0.83371329]
Error: Zero implied volatility!, sample 8199
 
AssertionError('Normalized price must be positive.',)
[3.81345797 0.09048382 0.72269429 0.66154351]
Error: Zero implied volatility!, sample 8220
 
AssertionError('Price must be positive.',)
[5.78813896 0.0722469  0.11433265 0.5981637 ]
Error: Zero implied volatility!, sample 8234
 
AssertionError('Price must be positive.',)
[ 1.65869929  0.46782016  0.38009799 -0.54457312]
Error: Zero implied volatility!, sample 8286
 
AssertionError('Price must be positive.',)
[ 2.00025016  0.10350341  0.18416864 -0.59754457]
Error: Zero implied volatility!, sample 8301
 
AssertionError('Price must be positive.',)
[ 3.61563968  0.24322464  0.38086087 -0.77933515]
Error: Zero implied volatility!, sample 8327
 
AssertionError('Normalized price must be positive.',)
[2.80996929 0.09980802 0.13869248 0.39131586]
Error: Zero implied volatility!, sample 8330
 
AssertionError('Price must be positive.',)
[ 2.56918466  0.09273558  0.06569571 -0.66714165]
Error: Zero implied volatility!, sample 8336
 
AssertionError('Normalized price must be positive.',)
[4.47139146 0.08886153 0.42161796 0.54201411]
Error: Zero implied volatility!, sample 8374
 
AssertionError('Price must be positive.',)
[ 2.40623892  0.08264043  0.56038829 -0.94804091]
Error: Zero implied volatility!, sample 8390
 
AssertionError('Normalized price must be positive.',)
[0.68416993 0.11439547 0.21604334 0.90272975]
Error: Zero implied volatility!, sample 8405
 
AssertionError('Price must be positive.',)
[ 1.86317872  0.2438335   0.6075238  -0.8029218 ]
Error: Zero implied volatility!, sample 8435
 
AssertionError('Price must be positive.',)
[ 3.70754324  0.08926139  0.72855132 -0.85772001]
Error: Zero implied volatility!, sample 8459
 
AssertionError('Price must be positive.',)
[ 6.2823156   0.14984124  0.29107265 -0.48879767]
Error: Zero implied volatility!, sample 8500
 
AssertionError('Normalized price must be positive.',)
[1.89680195 0.12045054 0.47622837 0.92877189]
Error: Zero implied volatility!, sample 8500
 
AssertionError('Price must be positive.',)
[ 1.73402473  0.62013062  0.34206765 -0.84324054]
Error: Zero implied volatility!, sample 8503
 
AssertionError('Price must be positive.',)
[ 3.24040589  0.24089446  0.59213581 -0.40554548]
Error: Zero implied volatility!, sample 8543
 
AssertionError('Price must be positive.',)
[ 3.20533074  0.34847636  0.22455623 -0.90416958]
Error: Zero implied volatility!, sample 8546
 
AssertionError('Price must be positive.',)
[ 3.26550279  0.37878542  0.47373108 -0.84741627]
Error: Zero implied volatility!, sample 8548
 
AssertionError('Price must be positive.',)
[0.86489008 0.33234447 0.58397981 0.88217856]
Error: Zero implied volatility!, sample 8556
 
AssertionError('Price must be positive.',)
[ 3.40751325  0.12817665  0.18633927 -0.75643011]
Error: Zero implied volatility!, sample 8576
 
AssertionError('Price must be positive.',)
[ 7.92921839  0.06415016  0.09377027 -0.26584414]
Error: Zero implied volatility!, sample 8597
 
AssertionError('Price must be positive.',)
[ 6.23565358  0.23737244  0.23685222 -0.69790232]
Error: Zero implied volatility!, sample 8618
 
AssertionError('Price must be positive.',)
[ 2.46836534  0.18565411  0.39782224 -0.52102932]
Error: Zero implied volatility!, sample 8648
 
AssertionError('Price must be positive.',)
[ 4.79029427  0.45246816  0.60017569 -0.84797393]
Error: Zero implied volatility!, sample 8697
 
AssertionError('Price must be positive.',)
[ 3.15436144  0.13489005  0.14508338 -0.85453764]
Error: Zero implied volatility!, sample 8717
 
AssertionError('Normalized price must be positive.',)
[5.62179111 0.07305957 0.17714148 0.50014433]
Error: Zero implied volatility!, sample 8804
 
AssertionError('Price must be positive.',)
[ 8.31403832  0.20897116  0.79428305 -0.88674128]
Error: Zero implied volatility!, sample 8816
 
AssertionError('Price must be positive.',)
[1.76204949 0.30100289 0.16975918 0.43255421]
Error: Zero implied volatility!, sample 8874
 
AssertionError('Price must be positive.',)
[ 2.30279515  0.62968758  0.73265503 -0.94203708]
Error: Zero implied volatility!, sample 8881
 
AssertionError('Price must be positive.',)
[0.68582759 0.33778025 0.1738617  0.59055855]
Error: Zero implied volatility!, sample 8885
 
AssertionError('Price must be positive.',)
[ 0.96902952  0.74948074  0.6493919  -0.95287968]
Error: Zero implied volatility!, sample 8885
 
AssertionError('Price must be positive.',)
[ 1.66237823  0.40939706  0.55467243 -0.90850279]
Error: Zero implied volatility!, sample 8901
 
AssertionError('Price must be positive.',)
[ 4.44083989  0.11051277  0.23847935 -0.989835  ]
Error: Zero implied volatility!, sample 8912
 
AssertionError('Price must be positive.',)
[ 1.97078459  0.14174351  0.07136744 -0.40870235]
Error: Zero implied volatility!, sample 8922
 
AssertionError('Normalized price must be positive.',)
[4.04904605 0.15409967 0.66730175 0.39887216]
Error: Zero implied volatility!, sample 8932
 
AssertionError('Price must be positive.',)
[ 5.83064262  0.05264209  0.62456783 -0.95658165]
Error: Zero implied volatility!, sample 8969
 
AssertionError('Price must be positive.',)
[ 4.1796806   0.22858986  0.28643975 -0.76059422]
Error: Zero implied volatility!, sample 8969
 
AssertionError('Price must be positive.',)
[5.38249739 0.09294309 0.6407804  0.18925366]
Error: Zero implied volatility!, sample 9008
 
AssertionError('Normalized price must be positive.',)
[3.45330145 0.64888149 0.65807733 0.91824805]
Error: Zero implied volatility!, sample 9040
 
AssertionError('Price must be positive.',)
[ 4.69261501  0.29492966  0.21406691 -0.53019838]
Error: Zero implied volatility!, sample 9069
 
AssertionError('Price must be positive.',)
[ 0.53907981  0.33221662  0.17050596 -0.6289542 ]
Error: Zero implied volatility!, sample 9100
 
AssertionError('Normalized price must be positive.',)
[1.93885761 0.21575738 0.15727897 0.93404125]
Error: Zero implied volatility!, sample 9108
 
AssertionError('Price must be positive.',)
[ 9.38923624  0.05484279  0.49890356 -0.39923195]
Error: Zero implied volatility!, sample 9119
 
AssertionError('Price must be positive.',)
[ 2.08446706  0.35379042  0.05831099 -0.07846974]
Error: Zero implied volatility!, sample 9121
 
AssertionError('Normalized price must be positive.',)
[8.23894625 0.07631069 0.1247183  0.76952284]
Error: Zero implied volatility!, sample 9128
 
AssertionError('Price must be positive.',)
[ 6.66334525  0.14042984  0.3758052  -0.55883559]
Error: Zero implied volatility!, sample 9135
 
AssertionError('Price must be positive.',)
[ 3.43853734  0.07421873  0.59920395 -0.55757034]
Error: Zero implied volatility!, sample 9278
 
AssertionError('Normalized price must be positive.',)
[8.79803883 0.11202574 0.71353201 0.71906867]
Error: Zero implied volatility!, sample 9284
 
AssertionError('Price must be positive.',)
[2.24009807 0.19992198 0.14379537 0.63849824]
Error: Zero implied volatility!, sample 9289
 
AssertionError('Normalized price must be positive.',)
[7.24284708 0.13015435 0.27766405 0.73204668]
Error: Zero implied volatility!, sample 9376
 
AssertionError('Price must be positive.',)
[ 0.81073563  0.41937392  0.14066627 -0.7586526 ]
Error: Zero implied volatility!, sample 9391
 
AssertionError('Price must be positive.',)
[ 8.03859912  0.146069    0.46878833 -0.85826096]
Error: Zero implied volatility!, sample 9408
 
AssertionError('Price must be positive.',)
[ 3.2606079   0.38371816  0.30981784 -0.4449452 ]
Error: Zero implied volatility!, sample 9463
 
AssertionError('Price must be positive.',)
[0.84038391 0.28322991 0.30977266 0.91364322]
Error: Zero implied volatility!, sample 9467
 
AssertionError('Normalized price must be positive.',)
[3.00571764 0.39110463 0.42081591 0.68211402]
Error: Zero implied volatility!, sample 9469
 
AssertionError('Price must be positive.',)
[ 8.50889621  0.06123177  0.32656992 -0.45706114]
Error: Zero implied volatility!, sample 9510
 
AssertionError('Normalized price must be positive.',)
[8.24373216 0.11367227 0.25020119 0.69214235]
Error: Zero implied volatility!, sample 9524
 
AssertionError('Price must be positive.',)
[ 3.30725022  0.5289199   0.33532907 -0.86109584]
Error: Zero implied volatility!, sample 9531
 
AssertionError('Price must be positive.',)
[ 3.80403983  0.24290497  0.38827048 -0.25027043]
Error: Zero implied volatility!, sample 9537
 
AssertionError('Normalized price must be positive.',)
[1.2140237  0.38791482 0.37895974 0.69217369]
Error: Zero implied volatility!, sample 9546
 
AssertionError('Normalized price must be positive.',)
[3.54026727 0.27848426 0.39241236 0.64476729]
Error: Zero implied volatility!, sample 9560
 
AssertionError('Price must be positive.',)
[ 2.86787858  0.25063965  0.48958498 -0.95959592]
Error: Zero implied volatility!, sample 9563
 
AssertionError('Normalized price must be positive.',)
[3.13880202 0.70807037 0.65401536 0.92306126]
Error: Zero implied volatility!, sample 9564
 
AssertionError('Price must be positive.',)
[ 9.27331731  0.05444078  0.43924027 -0.94551691]
Error: Zero implied volatility!, sample 9567
 
AssertionError('Price must be positive.',)
[ 5.84066292  0.20331999  0.47438549 -0.54585996]
Error: Zero implied volatility!, sample 9577
 
AssertionError('Price must be positive.',)
[1.7791125  0.41469543 0.27792968 0.1945494 ]
Error: Zero implied volatility!, sample 9612
 
AssertionError('Normalized price must be positive.',)
[9.54265924 0.14235121 0.51888528 0.73536313]
Error: Zero implied volatility!, sample 9613
 
AssertionError('Price must be positive.',)
[ 4.03551508  0.30859313  0.05238981 -0.96868142]
Error: Zero implied volatility!, sample 9637
 
AssertionError('Price must be positive.',)
[1.19298549 0.36203966 0.32390621 0.00163809]
Error: Zero implied volatility!, sample 9669
 
AssertionError('Price must be positive.',)
[ 0.93932659  0.44079898  0.53960654 -0.96183617]
Error: Zero implied volatility!, sample 9738
 
AssertionError('Normalized price must be positive.',)
[1.56573007 0.64568328 0.24927138 0.74921079]
Error: Zero implied volatility!, sample 9786
 
AssertionError('Price must be positive.',)
[ 0.74581273  0.59864143  0.5422906  -0.38119731]
Error: Zero implied volatility!, sample 9820
 
AssertionError('Price must be positive.',)
[ 1.257083    0.74003905  0.35324982 -0.62300859]
Error: Zero implied volatility!, sample 9822
 
AssertionError('Price must be positive.',)
[ 5.24972739e-01  6.80058470e-01  3.83815671e-01 -6.56611833e-04]
Error: Zero implied volatility!, sample 9834
 
AssertionError('Price must be positive.',)
[ 3.34032932  0.65896698  0.75157885 -0.89478417]
Error: Zero implied volatility!, sample 9894
 
AssertionError('Normalized price must be positive.',)
[1.68414605 0.18898706 0.09239281 0.55584841]
Error: Zero implied volatility!, sample 9897
 
AssertionError('Price must be positive.',)
[ 0.54182586  0.31933292  0.07828528 -0.96949922]
Error: Zero implied volatility!, sample 9902
 
AssertionError('Price must be positive.',)
[0.68444583 0.15411532 0.35134274 0.7515181 ]
Error: Zero implied volatility!, sample 9952
 
AssertionError('Price must be positive.',)
[1.33977801 0.27867937 0.33188113 0.59073414]
Error: Zero implied volatility!, sample 9964
 
AssertionError('Price must be positive.',)
[ 1.58365899  0.1277772   0.1415619  -0.74056649]
Error: Zero implied volatility!, sample 10005
 
AssertionError('Price must be positive.',)
[ 0.70090526  0.31032682  0.0869253  -0.0704813 ]
Error: Zero implied volatility!, sample 10005
 
AssertionError('Price must be positive.',)
[ 6.95507985  0.22951757  0.55968868 -0.82090598]
Error: Zero implied volatility!, sample 10043
 
AssertionError('Price must be positive.',)
[ 1.20130206  0.3117206   0.6250939  -0.73590518]
Error: Zero implied volatility!, sample 10090
 
AssertionError('Price must be positive.',)
[ 5.25054236  0.1444553   0.25675705 -0.55347249]
Error: Zero implied volatility!, sample 10097
 
AssertionError('Price must be positive.',)
[ 3.01232519  0.77838178  0.45834592 -0.96677269]
Error: Zero implied volatility!, sample 10106
 
AssertionError('Price must be positive.',)
[ 4.34520757  0.27596453  0.36077588 -0.98182581]
Error: Zero implied volatility!, sample 10118
 
AssertionError('Normalized price must be positive.',)
[3.98943918 0.10342197 0.73679764 0.55259187]
Error: Zero implied volatility!, sample 10119
 
AssertionError('Price must be positive.',)
[ 2.92546844  0.40896192  0.61406605 -0.97487121]
Error: Zero implied volatility!, sample 10135
 
AssertionError('Price must be positive.',)
[ 1.15327396  0.46215615  0.20067534 -0.75378914]
Error: Zero implied volatility!, sample 10155
 
AssertionError('Normalized price must be positive.',)
[2.174488   0.14658632 0.74479459 0.7174565 ]
Error: Zero implied volatility!, sample 10181
 
AssertionError('Price must be positive.',)
[ 4.22237548  0.14842009  0.64477052 -0.91081058]
Error: Zero implied volatility!, sample 10237
 
AssertionError('Initial guess must be positive.',)
[3.23213465 0.1306595  0.20472679 0.97839293]
Error: Zero implied volatility!, sample 10247
 
AssertionError('Price must be positive.',)
[ 0.73235624  0.79802086  0.7106143  -0.87482095]
Error: Zero implied volatility!, sample 10271
 
AssertionError('Price must be positive.',)
[ 5.36404698  0.36486667  0.7241596  -0.95519417]
Error: Zero implied volatility!, sample 10324
 
AssertionError('Price must be positive.',)
[ 1.77478149  0.58462849  0.41061877 -0.83369998]
Error: Zero implied volatility!, sample 10327
 
AssertionError('Normalized price must be positive.',)
[4.56605937 0.11113135 0.76561848 0.94994269]
Error: Zero implied volatility!, sample 10349
 
AssertionError('Price must be positive.',)
[ 0.80457377  0.20677239  0.24777657 -0.98532549]
Error: Zero implied volatility!, sample 10354
 
AssertionError('Price must be positive.',)
[ 9.75632479  0.10070207  0.52420237 -0.97406881]
Error: Zero implied volatility!, sample 10374
 
AssertionError('Price must be positive.',)
[ 2.35209685  0.28026099  0.38289364 -0.88949079]
Error: Zero implied volatility!, sample 10411
 
AssertionError('Price must be positive.',)
[1.20491846 0.28588089 0.50260883 0.15581931]
Error: Zero implied volatility!, sample 10411
 
AssertionError('Normalized price must be positive.',)
[0.80041618 0.49776716 0.37720264 0.91204686]
Error: Zero implied volatility!, sample 10433
 
AssertionError('Normalized price must be positive.',)
[1.49395978 0.46567888 0.10888842 0.29334513]
Error: Zero implied volatility!, sample 10434
 
AssertionError('Price must be positive.',)
[ 1.63623453  0.60708018  0.49881736 -0.56732298]
Error: Zero implied volatility!, sample 10436
 
AssertionError('Price must be positive.',)
[ 5.1083847   0.07772696  0.15748725 -0.70796046]
Error: Zero implied volatility!, sample 10452
 
AssertionError('Normalized price must be positive.',)
[4.03111178 0.24478722 0.1301641  0.68569405]
Error: Zero implied volatility!, sample 10468
 
AssertionError('Price must be positive.',)
[ 0.83697622  0.24187722  0.12761068 -0.64187545]
Error: Zero implied volatility!, sample 10479
 
AssertionError('Price must be positive.',)
[1.63493751 0.28641644 0.06291224 0.34246017]
Error: Zero implied volatility!, sample 10485
 
AssertionError('Normalized price must be positive.',)
[2.41711514 0.20143912 0.54487451 0.51043201]
Error: Zero implied volatility!, sample 10510
 
AssertionError('Price must be positive.',)
[ 4.04521787  0.19313672  0.34503918 -0.38543472]
Error: Zero implied volatility!, sample 10528
 
AssertionError('Price must be positive.',)
[ 0.9969049   0.32922893  0.45709557 -0.76209457]
Error: Zero implied volatility!, sample 10566
 
AssertionError('Price must be positive.',)
[ 1.46825049  0.08691968  0.4156627  -0.1853857 ]
Error: Zero implied volatility!, sample 10588
 
AssertionError('Normalized price must be positive.',)
[1.639958   0.58742845 0.15535369 0.87018656]
Error: Zero implied volatility!, sample 10602
 
AssertionError('Price must be positive.',)
[ 1.5711429   0.19125207  0.5927103  -0.61471263]
Error: Zero implied volatility!, sample 10644
 
AssertionError('Normalized price must be positive.',)
[9.49914775 0.07986203 0.42037533 0.72913189]
Error: Zero implied volatility!, sample 10657
 
AssertionError('Price must be positive.',)
[ 5.0975476   0.20317833  0.15457243 -0.89973211]
Error: Zero implied volatility!, sample 10676
 
AssertionError('Price must be positive.',)
[1.88346448 0.11003686 0.56162868 0.14680405]
Error: Zero implied volatility!, sample 10681
 
AssertionError('Normalized price must be positive.',)
[1.61973911 0.24080419 0.64255034 0.18768702]
Error: Zero implied volatility!, sample 10681
 
AssertionError('Normalized price must be positive.',)
[7.25924492 0.09647502 0.24889071 0.78994562]
Error: Zero implied volatility!, sample 10693
 
AssertionError('Normalized price must be positive.',)
[5.80435219 0.05205356 0.41409751 0.77533265]
Error: Zero implied volatility!, sample 10703
 
AssertionError('Normalized price must be positive.',)
[0.61148327 0.71846237 0.23922599 0.9378203 ]
Error: Zero implied volatility!, sample 10726
 
AssertionError('Price must be positive.',)
[3.6946733  0.11321029 0.36127836 0.35910804]
Error: Zero implied volatility!, sample 10776
 
AssertionError('Price must be positive.',)
[ 3.64105637  0.1208551   0.23492397 -0.53183444]
Error: Zero implied volatility!, sample 10796
 
AssertionError('Price must be positive.',)
[9.59452075 0.05419215 0.3140485  0.79437462]
Error: Zero implied volatility!, sample 10825
 
AssertionError('Price must be positive.',)
[ 3.96279849  0.14736318  0.59093394 -0.52505607]
Error: Zero implied volatility!, sample 10862
 
AssertionError('Normalized price must be positive.',)
[4.37330191 0.19979056 0.27913499 0.80683019]
Error: Zero implied volatility!, sample 10873
 
AssertionError('Price must be positive.',)
[ 2.84498224  0.07178999  0.43406785 -0.00354599]
Error: Zero implied volatility!, sample 10898
 
AssertionError('Price must be positive.',)
[ 4.15463433  0.17306117  0.6245984  -0.81534203]
Error: Zero implied volatility!, sample 10902
 
AssertionError('Normalized price must be positive.',)
[2.2932542  0.14764206 0.35955198 0.95853704]
Error: Zero implied volatility!, sample 10910
 
AssertionError('Price must be positive.',)
[ 1.12597612  0.4263488   0.22703755 -0.09172019]
Error: Zero implied volatility!, sample 10929
 
AssertionError('Price must be positive.',)
[ 5.0280249   0.10231334  0.25151683 -0.5133993 ]
Error: Zero implied volatility!, sample 10946
 
AssertionError('Price must be positive.',)
[2.51962756 0.14611652 0.53131442 0.48426675]
Error: Zero implied volatility!, sample 10952
 
AssertionError('Price must be positive.',)
[ 2.12226837  0.67781018  0.50284791 -0.98283674]
Error: Zero implied volatility!, sample 11054
 
AssertionError('Price must be positive.',)
[ 3.44788034  0.22828659  0.42770623 -0.26328272]
Error: Zero implied volatility!, sample 11085
 
AssertionError('Price must be positive.',)
[ 3.3449685   0.38039738  0.65774322 -0.5906787 ]
Error: Zero implied volatility!, sample 11130
 
AssertionError('Price must be positive.',)
[ 4.21306529  0.15147632  0.64599635 -0.69187074]
Error: Zero implied volatility!, sample 11133
 
AssertionError('Normalized price must be positive.',)
[1.38352743 0.40416476 0.28691571 0.94566244]
Error: Zero implied volatility!, sample 11167
 
AssertionError('Price must be positive.',)
[ 1.13856846  0.59876201  0.13842636 -0.12101921]
Error: Zero implied volatility!, sample 11178
 
AssertionError('Price must be positive.',)
[ 1.58534274  0.46760571  0.18128029 -0.07827864]
Error: Zero implied volatility!, sample 11181
 
AssertionError('Price must be positive.',)
[ 5.29901413  0.11744091  0.24582763 -0.95195147]
Error: Zero implied volatility!, sample 11207
 
AssertionError('Price must be positive.',)
[0.88937817 0.62595345 0.23531238 0.33233853]
Error: Zero implied volatility!, sample 11207
 
AssertionError('Price must be positive.',)
[ 6.92875262  0.17570381  0.2706731  -0.43665752]
Error: Zero implied volatility!, sample 11207
 
AssertionError('Price must be positive.',)
[ 6.47717059  0.20538398  0.73742271 -0.83319538]
Error: Zero implied volatility!, sample 11207
 
AssertionError('Normalized price must be positive.',)
[1.21610642 0.61072701 0.5025619  0.65198305]
Error: Zero implied volatility!, sample 11230
 
AssertionError('Normalized price must be positive.',)
[7.54971922 0.11977518 0.24379813 0.93454763]
Error: Zero implied volatility!, sample 11242
 
AssertionError('Normalized price must be positive.',)
[5.85370123 0.20733942 0.27115065 0.73450909]
Error: Zero implied volatility!, sample 11250
 
AssertionError('Price must be positive.',)
[1.06094947 0.41184458 0.18989617 0.44626   ]
Error: Zero implied volatility!, sample 11320
 
AssertionError('Price must be positive.',)
[ 1.19644762  0.35879779  0.23382752 -0.73094079]
Error: Zero implied volatility!, sample 11385
 
AssertionError('Price must be positive.',)
[ 1.20968717  0.43726821  0.22778162 -0.66338474]
Error: Zero implied volatility!, sample 11406
 
AssertionError('Price must be positive.',)
[5.99511093 0.12291363 0.40232907 0.91753211]
Error: Zero implied volatility!, sample 11414
 
AssertionError('Price must be positive.',)
[ 3.53113536  0.3498591   0.47669388 -0.44605195]
Error: Zero implied volatility!, sample 11447
 
AssertionError('Normalized price must be positive.',)
[4.56930246 0.06567297 0.76899356 0.93304013]
Error: Zero implied volatility!, sample 11468
 
AssertionError('Price must be positive.',)
[ 2.19811384  0.32523533  0.76750663 -0.78338068]
Error: Zero implied volatility!, sample 11468
 
AssertionError('Price must be positive.',)
[ 3.46225467  0.07085664  0.69652663 -0.85961065]
Error: Zero implied volatility!, sample 11490
 
AssertionError('Price must be positive.',)
[ 1.29630294  0.69982962  0.74126057 -0.46870673]
Error: Zero implied volatility!, sample 11516
 
AssertionError('Price must be positive.',)
[ 3.67593228  0.16069483  0.53836482 -0.74077273]
Error: Zero implied volatility!, sample 11529
 
AssertionError('Normalized price must be positive.',)
[3.00972606 0.48119933 0.49102512 0.79831168]
Error: Zero implied volatility!, sample 11559
 
AssertionError('Price must be positive.',)
[ 0.99880694  0.50438035  0.53144081 -0.81498894]
Error: Zero implied volatility!, sample 11569
 
AssertionError('Normalized price must be positive.',)
[1.10674301 0.08857824 0.28016474 0.93285684]
Error: Zero implied volatility!, sample 11648
 
AssertionError('Price must be positive.',)
[ 2.63482495  0.66788201  0.52433868 -0.88375184]
Error: Zero implied volatility!, sample 11650
 
AssertionError('Normalized price must be positive.',)
[5.60131493 0.18866794 0.71894298 0.93642834]
Error: Zero implied volatility!, sample 11650
 
AssertionError('Price must be positive.',)
[ 1.48075327  0.47210614  0.3458007  -0.69439973]
Error: Zero implied volatility!, sample 11669
 
AssertionError('Price must be positive.',)
[7.86911033 0.10592654 0.07654913 0.22310014]
Error: Zero implied volatility!, sample 11677
 
AssertionError('Price must be positive.',)
[ 3.33875146  0.15079852  0.57845187 -0.89898389]
Error: Zero implied volatility!, sample 11685
 
AssertionError('Price must be positive.',)
[ 3.45380252  0.49286161  0.72832336 -0.92081251]
Error: Zero implied volatility!, sample 11696
 
AssertionError('Price must be positive.',)
[ 7.49237527  0.14028995  0.72539431 -0.894826  ]
Error: Zero implied volatility!, sample 11729
 
AssertionError('Normalized price must be positive.',)
[5.31134152 0.28992846 0.79251678 0.9120859 ]
Error: Zero implied volatility!, sample 11773
 
AssertionError('Normalized price must be positive.',)
[1.31015738 0.51312809 0.62671807 0.6625562 ]
Error: Zero implied volatility!, sample 11821
 
AssertionError('Price must be positive.',)
[4.53519852 0.21477489 0.11029453 0.41579656]
Error: Zero implied volatility!, sample 11833
 
AssertionError('Price must be positive.',)
[ 6.32645155  0.13224666  0.64447715 -0.88125287]
Error: Zero implied volatility!, sample 11838
 
AssertionError('Price must be positive.',)
[ 1.47540803  0.60636475  0.26629115 -0.66074217]
Error: Zero implied volatility!, sample 11865
 
AssertionError('Price must be positive.',)
[ 7.67635595  0.12108938  0.34078039 -0.64778051]
Error: Zero implied volatility!, sample 11877
 
AssertionError('Price must be positive.',)
[ 3.68392921  0.62547727  0.74876415 -0.98246492]
Error: Zero implied volatility!, sample 11883
 
AssertionError('Price must be positive.',)
[ 4.9958812   0.58653556  0.52989391 -0.94009571]
Error: Zero implied volatility!, sample 11904
 
AssertionError('Normalized price must be positive.',)
[3.4313951  0.18060193 0.32903132 0.29271486]
Error: Zero implied volatility!, sample 11917
 
AssertionError('Price must be positive.',)
[ 0.62030218  0.48319479  0.55431424 -0.22166486]
Error: Zero implied volatility!, sample 11924
 
AssertionError('Normalized price must be positive.',)
[1.00248984 0.05813386 0.2634762  0.8460018 ]
Error: Zero implied volatility!, sample 11941
 
AssertionError('Price must be positive.',)
[ 2.22017154  0.37734452  0.18976435 -0.01234918]
Error: Zero implied volatility!, sample 12000
 
AssertionError('Price must be positive.',)
[ 3.40463509  0.12823711  0.46434322 -0.53680362]
Error: Zero implied volatility!, sample 12015
 
AssertionError('Normalized price must be positive.',)
[3.97943015 0.32777431 0.34132272 0.79669607]
Error: Zero implied volatility!, sample 12020
 
AssertionError('Normalized price must be positive.',)
[3.39984918 0.22879038 0.05889538 0.73610388]
Error: Zero implied volatility!, sample 12052
 
AssertionError('Price must be positive.',)
[ 2.61364747  0.45210555  0.32522291 -0.64176802]
Error: Zero implied volatility!, sample 12109
 
AssertionError('Normalized price must be positive.',)
[7.77419096 0.06612126 0.34737924 0.98383055]
Error: Zero implied volatility!, sample 12134
 
AssertionError('Price must be positive.',)
[6.80358161 0.08133174 0.26366108 0.2902851 ]
Error: Zero implied volatility!, sample 12166
 
AssertionError('Normalized price must be positive.',)
[0.55061504 0.62331216 0.3020163  0.8482937 ]
Error: Zero implied volatility!, sample 12200
 
AssertionError('Normalized price must be positive.',)
[4.6199349  0.21890488 0.29024757 0.74610424]
Error: Zero implied volatility!, sample 12211
 
AssertionError('Price must be positive.',)
[ 3.98509833  0.41985215  0.71533503 -0.91060267]
Error: Zero implied volatility!, sample 12244
 
AssertionError('Price must be positive.',)
[ 2.54833222  0.15656548  0.05555714 -0.92073931]
Error: Zero implied volatility!, sample 12258
 
AssertionError('Price must be positive.',)
[ 1.91857044  0.47357014  0.29479174 -0.56905067]
Error: Zero implied volatility!, sample 12269
 
AssertionError('Price must be positive.',)
[ 1.7127965   0.07318015  0.21436476 -0.62172265]
Error: Zero implied volatility!, sample 12281
 
AssertionError('Price must be positive.',)
[ 1.81825026  0.45266788  0.20203932 -0.30957406]
Error: Zero implied volatility!, sample 12283
 
AssertionError('Price must be positive.',)
[ 6.62818395  0.240653    0.39997106 -0.7798819 ]
Error: Zero implied volatility!, sample 12284
 
AssertionError('Normalized price must be positive.',)
[1.72449001 0.41112325 0.05569845 0.89614698]
Error: Zero implied volatility!, sample 12292
 
AssertionError('Normalized price must be positive.',)
[6.85655985 0.18636242 0.66429874 0.91653745]
Error: Zero implied volatility!, sample 12308
 
AssertionError('Price must be positive.',)
[ 1.95750898  0.21992212  0.15171869 -0.58008464]
Error: Zero implied volatility!, sample 12318
 
AssertionError('Normalized price must be positive.',)
[1.99851737 0.77627601 0.38626472 0.90835711]
Error: Zero implied volatility!, sample 12319
 
AssertionError('Price must be positive.',)
[ 5.95499848  0.18389726  0.54829437 -0.83818528]
Error: Zero implied volatility!, sample 12352
 
AssertionError('Price must be positive.',)
[ 8.05264929  0.18040862  0.74834444 -0.75491506]
Error: Zero implied volatility!, sample 12357
 
AssertionError('Price must be positive.',)
[ 0.76529965  0.71060831  0.69506697 -0.90103208]
Error: Zero implied volatility!, sample 12359
 
AssertionError('Price must be positive.',)
[1.23398301 0.50959797 0.16107189 0.04641036]
Error: Zero implied volatility!, sample 12375
 
AssertionError('Price must be positive.',)
[ 2.28472745  0.38644066  0.76121749 -0.8531115 ]
Error: Zero implied volatility!, sample 12375
 
AssertionError('Price must be positive.',)
[ 1.40383454  0.71948487  0.29685996 -0.44752032]
Error: Zero implied volatility!, sample 12403
 
AssertionError('Normalized price must be positive.',)
[9.57354605 0.07394437 0.38409421 0.72036042]
Error: Zero implied volatility!, sample 12497
 
AssertionError('Normalized price must be positive.',)
[1.8335724  0.39969453 0.22501484 0.36199048]
Error: Zero implied volatility!, sample 12514
 
AssertionError('Price must be positive.',)
[ 1.57418837  0.6368454   0.78034881 -0.76147786]
Error: Zero implied volatility!, sample 12527
 
AssertionError('Price must be positive.',)
[ 2.39984347  0.68578798  0.2977211  -0.74808591]
Error: Zero implied volatility!, sample 12542
 
AssertionError('Price must be positive.',)
[ 9.96665297  0.16115177  0.48658368 -0.88370923]
Error: Zero implied volatility!, sample 12545
 
AssertionError('Price must be positive.',)
[ 1.66820261  0.68406562  0.75708251 -0.90154991]
Error: Zero implied volatility!, sample 12578
 
AssertionError('Normalized price must be positive.',)
[3.50464694 0.1224573  0.35013731 0.88428401]
Error: Zero implied volatility!, sample 12611
 
AssertionError('Normalized price must be positive.',)
[4.87178538 0.24424376 0.48415515 0.67814425]
Error: Zero implied volatility!, sample 12660
 
AssertionError('Normalized price must be positive.',)
[5.61753552 0.13889459 0.32794036 0.61444135]
Error: Zero implied volatility!, sample 12681
 
AssertionError('Normalized price must be positive.',)
[0.79835829 0.67301698 0.40359516 0.62584311]
Error: Zero implied volatility!, sample 12699
 
AssertionError('Price must be positive.',)
[ 3.6608215   0.10255896  0.65814979 -0.9542276 ]
Error: Zero implied volatility!, sample 12710
 
AssertionError('Price must be positive.',)
[ 2.28566833  0.17631246  0.20299767 -0.32580261]
Error: Zero implied volatility!, sample 12716
 
AssertionError('Price must be positive.',)
[ 0.91046273  0.53210874  0.22753812 -0.53736991]
Error: Zero implied volatility!, sample 12741
 
AssertionError('Normalized price must be positive.',)
[1.13590557 0.52088584 0.4279023  0.65731012]
Error: Zero implied volatility!, sample 12752
 
AssertionError('Price must be positive.',)
[ 1.42380748  0.67286225  0.07715424 -0.0927636 ]
Error: Zero implied volatility!, sample 12775
 
AssertionError('Normalized price must be positive.',)
[8.83209799 0.10430306 0.60172949 0.54687567]
Error: Zero implied volatility!, sample 12833
 
AssertionError('Price must be positive.',)
[ 5.83311969  0.08254766  0.37211086 -0.72211843]
Error: Zero implied volatility!, sample 12845
 
AssertionError('Price must be positive.',)
[3.03785775 0.33961369 0.7770269  0.9814369 ]
Error: Zero implied volatility!, sample 12861
 
AssertionError('Price must be positive.',)
[ 2.27707526  0.31616043  0.70169217 -0.97849458]
Error: Zero implied volatility!, sample 12877
 
AssertionError('Normalized price must be positive.',)
[4.85921484 0.12871309 0.55871498 0.60294301]
Error: Zero implied volatility!, sample 12906
 
AssertionError('Price must be positive.',)
[2.15278062 0.12508429 0.06270236 0.06153258]
Error: Zero implied volatility!, sample 12917
 
AssertionError('Price must be positive.',)
[ 7.56030575  0.19871043  0.54902071 -0.87600481]
Error: Zero implied volatility!, sample 12949
 
AssertionError('Price must be positive.',)
[2.31267863 0.30864489 0.19236692 0.15317934]
Error: Zero implied volatility!, sample 12956
 
AssertionError('Price must be positive.',)
[ 1.84132385  0.33676001  0.63094396 -0.33258267]
Error: Zero implied volatility!, sample 12987
 
AssertionError('Price must be positive.',)
[ 4.51934307  0.1144      0.053869   -0.75926311]
Error: Zero implied volatility!, sample 13015
 
AssertionError('Price must be positive.',)
[ 5.17273662  0.39479504  0.77919243 -0.85297193]
Error: Zero implied volatility!, sample 13021
 
AssertionError('Price must be positive.',)
[ 2.11393717  0.75297106  0.69629466 -0.79521142]
Error: Zero implied volatility!, sample 13033
 
AssertionError('Price must be positive.',)
[ 7.10368833  0.09050866  0.53567269 -0.83776168]
Error: Zero implied volatility!, sample 13067
 
AssertionError('Normalized price must be positive.',)
[4.30670886 0.22437151 0.35325518 0.84025176]
Error: Zero implied volatility!, sample 13088
 
AssertionError('Price must be positive.',)
[0.52716064 0.26874947 0.48901767 0.9865257 ]
Error: Zero implied volatility!, sample 13091
 
AssertionError('Price must be positive.',)
[ 3.88711609  0.39119337  0.19128552 -0.78839072]
Error: Zero implied volatility!, sample 13093
 
AssertionError('Price must be positive.',)
[ 4.12932397  0.06052034  0.41845478 -0.2734906 ]
Error: Zero implied volatility!, sample 13104
 
AssertionError('Price must be positive.',)
[1.57554621 0.21169221 0.29413697 0.29354883]
Error: Zero implied volatility!, sample 13138
 
AssertionError('Normalized price must be positive.',)
[1.5988876  0.10282001 0.19624675 0.13731598]
Error: Zero implied volatility!, sample 13139
 
AssertionError('Price must be positive.',)
[ 5.82055225  0.08662492  0.3160102  -0.67338922]
Error: Zero implied volatility!, sample 13150
 
AssertionError('Price must be positive.',)
[ 2.33875606  0.05850151  0.13472587 -0.02599674]
Error: Zero implied volatility!, sample 13155
 
AssertionError('Normalized price must be positive.',)
[1.61788046 0.64844775 0.16427007 0.95709294]
Error: Zero implied volatility!, sample 13221
 
AssertionError('Price must be positive.',)
[ 7.00790019  0.18809521  0.20592261 -0.83550563]
Error: Zero implied volatility!, sample 13234
 
AssertionError('Normalized price must be positive.',)
[5.8768158  0.31356995 0.47209497 0.92245728]
Error: Zero implied volatility!, sample 13249
 
AssertionError('Price must be positive.',)
[ 8.96507549  0.0602903   0.15764858 -0.01725271]
Error: Zero implied volatility!, sample 13259
 
AssertionError('Price must be positive.',)
[3.11417774 0.13286027 0.11508958 0.68299823]
Error: Zero implied volatility!, sample 13283
 
AssertionError('Normalized price must be positive.',)
[1.68566058 0.39864361 0.53936689 0.84940566]
Error: Zero implied volatility!, sample 13304
 
AssertionError('Price must be positive.',)
[ 1.12830872  0.19594907  0.65164217 -0.68788324]
Error: Zero implied volatility!, sample 13315
 
AssertionError('Price must be positive.',)
[ 1.11556192  0.54081348  0.71431067 -0.27378705]
Error: Zero implied volatility!, sample 13332
 
AssertionError('Price must be positive.',)
[ 1.99402998  0.05570324  0.16368814 -0.71520382]
Error: Zero implied volatility!, sample 13351
 
AssertionError('Normalized price must be positive.',)
[2.59337066 0.3009653  0.61806101 0.8878206 ]
Error: Zero implied volatility!, sample 13356
 
AssertionError('Price must be positive.',)
[ 2.13418427  0.51298597  0.28491726 -0.88955002]
Error: Zero implied volatility!, sample 13367
 
AssertionError('Price must be positive.',)
[ 0.81952293  0.35708323  0.27455729 -0.62436441]
Error: Zero implied volatility!, sample 13405
 
AssertionError('Price must be positive.',)
[ 7.47237274  0.42284016  0.5979238  -0.97565287]
Error: Zero implied volatility!, sample 13425
 
AssertionError('Normalized price must be positive.',)
[6.67879177 0.12396041 0.36125257 0.47368587]
Error: Zero implied volatility!, sample 13489
 
AssertionError('Price must be positive.',)
[ 2.49012695  0.08782633  0.41779891 -0.61324641]
Error: Zero implied volatility!, sample 13494
 
AssertionError('Price must be positive.',)
[ 0.53698016  0.64535598  0.79371865 -0.76544319]
Error: Zero implied volatility!, sample 13496
 
AssertionError('Price must be positive.',)
[ 0.90273199  0.73315507  0.45482342 -0.97176137]
Error: Zero implied volatility!, sample 13511
 
AssertionError('Price must be positive.',)
[ 9.24156646  0.10815916  0.11697924 -0.66757347]
Error: Zero implied volatility!, sample 13511
 
AssertionError('Normalized price must be positive.',)
[6.28505573 0.09520276 0.07900039 0.62490501]
Error: Zero implied volatility!, sample 13519
 
AssertionError('Normalized price must be positive.',)
[2.48508078 0.12616321 0.25682772 0.50067365]
Error: Zero implied volatility!, sample 13529
 
AssertionError('Price must be positive.',)
[ 1.04524564  0.37018574  0.18660954 -0.7595962 ]
Error: Zero implied volatility!, sample 13570
 
AssertionError('Normalized price must be positive.',)
[0.56403271 0.37353043 0.23186795 0.57578184]
Error: Zero implied volatility!, sample 13571
 
AssertionError('Price must be positive.',)
[ 8.97111384  0.08018339  0.23856422 -0.87870333]
Error: Zero implied volatility!, sample 13604
 
AssertionError('Price must be positive.',)
[ 1.92979901  0.1761748   0.71711065 -0.48274532]
Error: Zero implied volatility!, sample 13630
 
AssertionError('Price must be positive.',)
[1.36801866 0.08978921 0.24661781 0.68116538]
Error: Zero implied volatility!, sample 13668
 
AssertionError('Price must be positive.',)
[ 1.50647701  0.57693462  0.27341477 -0.54561879]
Error: Zero implied volatility!, sample 13690
 
AssertionError('Normalized price must be positive.',)
[3.12467406 0.24945903 0.72110447 0.62713765]
Error: Zero implied volatility!, sample 13696
 
AssertionError('Price must be positive.',)
[ 2.78097067  0.25513022  0.37974017 -0.21965323]
Error: Zero implied volatility!, sample 13705
 
AssertionError('Normalized price must be positive.',)
[0.67148771 0.05604219 0.16727496 0.30569336]
Error: Zero implied volatility!, sample 13811
 
AssertionError('Price must be positive.',)
[1.63013076 0.35025443 0.09164613 0.63307373]
Error: Zero implied volatility!, sample 13830
 
AssertionError('Price must be positive.',)
[ 0.50162528  0.7513287   0.3818848  -0.73544108]
Error: Zero implied volatility!, sample 13873
 
AssertionError('Price must be positive.',)
[2.2891396  0.05636789 0.33881848 0.69931463]
Error: Zero implied volatility!, sample 13878
 
AssertionError('Price must be positive.',)
[ 7.15190061  0.1030479   0.51888716 -0.68648323]
Error: Zero implied volatility!, sample 13885
 
AssertionError('Price must be positive.',)
[ 1.85664307  0.57592935  0.76011666 -0.94498485]
Error: Zero implied volatility!, sample 13918
 
AssertionError('Price must be positive.',)
[ 4.66232709  0.47073056  0.5968854  -0.75835923]
Error: Zero implied volatility!, sample 13954
 
AssertionError('Price must be positive.',)
[5.42939972 0.12085366 0.5419567  0.98652783]
Error: Zero implied volatility!, sample 13984
 
AssertionError('Price must be positive.',)
[1.33156328 0.45302292 0.23646285 0.15852674]
Error: Zero implied volatility!, sample 14011
 
AssertionError('Price must be positive.',)
[ 0.72652778  0.18653286  0.3624866  -0.96881595]
Error: Zero implied volatility!, sample 14043
 
AssertionError('Price must be positive.',)
[ 2.38335947  0.38975433  0.7254528  -0.72746902]
Error: Zero implied volatility!, sample 14047
 
AssertionError('Price must be positive.',)
[ 8.03980883  0.19787247  0.74401836 -0.71889856]
Error: Zero implied volatility!, sample 14062
 
AssertionError('Price must be positive.',)
[ 4.28964792  0.07639144  0.73613454 -0.93552286]
Error: Zero implied volatility!, sample 14079
 
AssertionError('Price must be positive.',)
[ 1.30287995  0.22099592  0.25095015 -0.44808077]
Error: Zero implied volatility!, sample 14093
 
AssertionError('Price must be positive.',)
[ 7.49670971  0.16690408  0.52898908 -0.93664783]
Error: Zero implied volatility!, sample 14094
 
AssertionError('Price must be positive.',)
[ 5.34616687  0.1804978   0.47925249 -0.63509873]
Error: Zero implied volatility!, sample 14100
 
AssertionError('Price must be positive.',)
[ 1.07824489  0.74299118  0.12921206 -0.67671339]
Error: Zero implied volatility!, sample 14110
 
AssertionError('Price must be positive.',)
[ 1.63142071  0.29970165  0.6646638  -0.49059018]
Error: Zero implied volatility!, sample 14137
 
AssertionError('Price must be positive.',)
[ 1.91067386  0.22094426  0.55637095 -0.60876882]
Error: Zero implied volatility!, sample 14141
 
AssertionError('Price must be positive.',)
[ 7.20818249  0.19631847  0.79716871 -0.85391042]
Error: Zero implied volatility!, sample 14147
 
AssertionError('Price must be positive.',)
[ 2.34912352  0.21437439  0.48979919 -0.69042804]
Error: Zero implied volatility!, sample 14156
 
AssertionError('Price must be positive.',)
[ 2.10703556  0.34428143  0.18064852 -0.42821874]
Error: Zero implied volatility!, sample 14175
 
AssertionError('Price must be positive.',)
[5.25691699 0.12318226 0.24208897 0.32622458]
Error: Zero implied volatility!, sample 14203
 
AssertionError('Normalized price must be positive.',)
[6.83743151 0.06314168 0.77356909 0.21535812]
Error: Zero implied volatility!, sample 14217
 
AssertionError('Price must be positive.',)
[ 1.5333092   0.63916987  0.3838976  -0.78744547]
Error: Zero implied volatility!, sample 14217
 
AssertionError('Price must be positive.',)
[ 3.10171254  0.60332751  0.3859442  -0.95446152]
Error: Zero implied volatility!, sample 14224
 
AssertionError('Price must be positive.',)
[ 0.84428298  0.18170446  0.17837972 -0.30316986]
Error: Zero implied volatility!, sample 14225
 
AssertionError('Price must be positive.',)
[2.4384961  0.0736894  0.13377005 0.65760186]
Error: Zero implied volatility!, sample 14233
 
AssertionError('Price must be positive.',)
[ 6.35109494  0.22162718  0.3817763  -0.84144788]
Error: Zero implied volatility!, sample 14247
 
AssertionError('Price must be positive.',)
[2.03747828 0.16928751 0.43302817 0.79732289]
Error: Zero implied volatility!, sample 14270
 
AssertionError('Price must be positive.',)
[6.58345281 0.13989896 0.05833002 0.22220031]
Error: Zero implied volatility!, sample 14276
 
AssertionError('Price must be positive.',)
[0.59401327 0.66422127 0.07684607 0.16678605]
Error: Zero implied volatility!, sample 14300
 
AssertionError('Normalized price must be positive.',)
[1.08978233 0.46038063 0.75229607 0.87393929]
Error: Zero implied volatility!, sample 14378
 
AssertionError('Price must be positive.',)
[ 1.0126268   0.71054579  0.16703817 -0.88533979]
Error: Zero implied volatility!, sample 14391
 
AssertionError('Price must be positive.',)
[ 1.22595565  0.52835269  0.69634503 -0.71272754]
Error: Zero implied volatility!, sample 14404
 
AssertionError('Normalized price must be positive.',)
[1.86234129 0.36703174 0.17816553 0.72203631]
Error: Zero implied volatility!, sample 14455
 
AssertionError('Price must be positive.',)
[ 4.27622812  0.49383118  0.63231278 -0.8006371 ]
Error: Zero implied volatility!, sample 14464
 
AssertionError('Normalized price must be positive.',)
[9.8922429  0.12099035 0.35188523 0.78370951]
Error: Zero implied volatility!, sample 14475
 
AssertionError('Normalized price must be positive.',)
[2.83858366 0.62149108 0.49892648 0.95356703]
Error: Zero implied volatility!, sample 14475
 
AssertionError('Price must be positive.',)
[2.82792948 0.05237915 0.209823   0.87076604]
Error: Zero implied volatility!, sample 14484
 
AssertionError('Normalized price must be positive.',)
[0.95300816 0.64581219 0.72507017 0.88351844]
Error: Zero implied volatility!, sample 14515
 
AssertionError('Price must be positive.',)
[ 1.97442542  0.59816652  0.23170807 -0.46772349]
Error: Zero implied volatility!, sample 14558
 
AssertionError('Price must be positive.',)
[ 8.29607694  0.19548221  0.53243022 -0.96416777]
Error: Zero implied volatility!, sample 14562
 
AssertionError('Normalized price must be positive.',)
[1.77536734 0.373416   0.53531289 0.78372861]
Error: Zero implied volatility!, sample 14573
 
AssertionError('Price must be positive.',)
[ 0.74322242  0.17080238  0.39746005 -0.59426094]
Error: Zero implied volatility!, sample 14587
 
AssertionError('Normalized price must be positive.',)
[1.43494105 0.61408798 0.34501324 0.67306807]
Error: Zero implied volatility!, sample 14591
 
AssertionError('Price must be positive.',)
[ 2.24385109  0.16494494  0.18887823 -0.96994475]
Error: Zero implied volatility!, sample 14605
 
AssertionError('Normalized price must be positive.',)
[9.06856903 0.13153785 0.2074354  0.91037195]
Error: Zero implied volatility!, sample 14619
 
AssertionError('Normalized price must be positive.',)
[2.78383235 0.51943666 0.5274055  0.96666484]
Error: Zero implied volatility!, sample 14623
 
AssertionError('Price must be positive.',)
[ 2.43753215  0.25087424  0.43003969 -0.92246891]
Error: Zero implied volatility!, sample 14626
 
AssertionError('Price must be positive.',)
[ 5.85598016  0.35284201  0.77953606 -0.8107788 ]
Error: Zero implied volatility!, sample 14632
 
AssertionError('Normalized price must be positive.',)
[5.19891785 0.06200183 0.7631811  0.40082502]
Error: Zero implied volatility!, sample 14638
 
AssertionError('Normalized price must be positive.',)
[9.62999064 0.27129813 0.68082078 0.97952213]
Error: Zero implied volatility!, sample 14660
 
AssertionError('Price must be positive.',)
[3.31716763 0.18103807 0.21833428 0.38399709]
Error: Zero implied volatility!, sample 14673
 
AssertionError('Price must be positive.',)
[1.33260709 0.22910347 0.53970887 0.46268367]
Error: Zero implied volatility!, sample 14696
 
AssertionError('Price must be positive.',)
[ 0.85748456  0.05850333  0.09201705 -0.17678364]
Error: Zero implied volatility!, sample 14733
 
AssertionError('Price must be positive.',)
[ 1.39895797  0.12116518  0.46153919 -0.85386459]
Error: Zero implied volatility!, sample 14733
 
AssertionError('Price must be positive.',)
[ 1.92038613  0.33780419  0.74699909 -0.92744888]
Error: Zero implied volatility!, sample 14758
 
AssertionError('Price must be positive.',)
[ 5.24206753  0.20021024  0.36773238 -0.97895788]
Error: Zero implied volatility!, sample 14758
 
AssertionError('Price must be positive.',)
[ 1.37261781  0.14033115  0.12579721 -0.3816447 ]
Error: Zero implied volatility!, sample 14767
 
AssertionError('Price must be positive.',)
[ 5.39543314  0.57633829  0.69958063 -0.9133412 ]
Error: Zero implied volatility!, sample 14776
 
AssertionError('Normalized price must be positive.',)
[2.013799   0.26103734 0.75511089 0.81141435]
Error: Zero implied volatility!, sample 14784
 
AssertionError('Normalized price must be positive.',)
[9.49104759 0.07969309 0.16354153 0.98913287]
Error: Zero implied volatility!, sample 14788
 
AssertionError('Price must be positive.',)
[1.14905178 0.2373698  0.28427939 0.63578626]
Error: Zero implied volatility!, sample 14813
 
AssertionError('Price must be positive.',)
[ 1.05575253  0.40489189  0.19544029 -0.57117462]
Error: Zero implied volatility!, sample 14837
 
AssertionError('Normalized price must be positive.',)
[5.68708199 0.08396307 0.10838545 0.03074333]
Error: Zero implied volatility!, sample 14859
 
AssertionError('Price must be positive.',)
[ 7.6034091   0.0632474   0.61124089 -0.78168972]
Error: Zero implied volatility!, sample 14859
 
AssertionError('Price must be positive.',)
[ 9.36977945  0.16157354  0.65760734 -0.81169993]
Error: Zero implied volatility!, sample 14872
 
AssertionError('Price must be positive.',)
[ 2.46732314  0.23739839  0.26013506 -0.24990763]
Error: Zero implied volatility!, sample 14885
 
AssertionError('Price must be positive.',)
[ 5.63307291  0.10737392  0.20601326 -0.25793236]
Error: Zero implied volatility!, sample 14896
 
AssertionError('Price must be positive.',)
[ 5.85809445  0.08065515  0.28836859 -0.03305904]
Error: Zero implied volatility!, sample 14901
 
AssertionError('Normalized price must be positive.',)
[0.94847434 0.41714524 0.2805232  0.30729277]
Error: Zero implied volatility!, sample 14921
 
AssertionError('Price must be positive.',)
[ 1.55410317  0.61856848  0.31139726 -0.29463438]
Error: Zero implied volatility!, sample 14971
 
AssertionError('Normalized price must be positive.',)
[0.97265174 0.55700795 0.23542456 0.67835441]
Error: Zero implied volatility!, sample 14999
 
AssertionError('Price must be positive.',)
[ 7.09983907  0.19457498  0.63111791 -0.79714868]
Error: Zero implied volatility!, sample 15025
 
AssertionError('Normalized price must be positive.',)
[3.78939998 0.32523596 0.7747235  0.83177338]
Error: Zero implied volatility!, sample 15033
 
AssertionError('Price must be positive.',)
[ 2.42506651  0.11026766  0.50043054 -0.80481009]
Error: Zero implied volatility!, sample 15079
 
AssertionError('Price must be positive.',)
[ 9.9596983   0.10104939  0.29633592 -0.96996194]
Error: Zero implied volatility!, sample 15089
 
AssertionError('Price must be positive.',)
[ 8.64313261  0.08824739  0.79285259 -0.4992942 ]
Error: Zero implied volatility!, sample 15107
 
AssertionError('Normalized price must be positive.',)
[5.99046052 0.18602046 0.36080403 0.78673107]
Error: Zero implied volatility!, sample 15123
 
AssertionError('Price must be positive.',)
[ 3.60079057  0.1509892   0.59152717 -0.97182227]
Error: Zero implied volatility!, sample 15144
 
AssertionError('Normalized price must be positive.',)
[2.53188254 0.34437626 0.23975694 0.6081838 ]
Error: Zero implied volatility!, sample 15153
 
AssertionError('Normalized price must be positive.',)
[6.05250575 0.34791488 0.46375939 0.96767133]
Error: Zero implied volatility!, sample 15172
 
AssertionError('Price must be positive.',)
[ 1.97690777  0.39247967  0.57681746 -0.96014936]
Error: Zero implied volatility!, sample 15182
 
AssertionError('Normalized price must be positive.',)
[2.26114327 0.21359212 0.22992687 0.92170001]
Error: Zero implied volatility!, sample 15202
 
AssertionError('Price must be positive.',)
[ 2.30777375  0.12707798  0.58615483 -0.6837126 ]
Error: Zero implied volatility!, sample 15227
 
AssertionError('Price must be positive.',)
[ 2.38426514  0.32918703  0.53726672 -0.4466097 ]
Error: Zero implied volatility!, sample 15235
 
AssertionError('Normalized price must be positive.',)
[1.41586198 0.45306249 0.31324095 0.87234003]
Error: Zero implied volatility!, sample 15244
 
AssertionError('Price must be positive.',)
[ 6.05913156  0.10593126  0.28788068 -0.83788356]
Error: Zero implied volatility!, sample 15258
 
AssertionError('Price must be positive.',)
[ 9.62695033  0.05874043  0.76513559 -0.43506187]
Error: Zero implied volatility!, sample 15282
 
AssertionError('Price must be positive.',)
[ 2.36746252  0.26437865  0.14326751 -0.0959866 ]
Error: Zero implied volatility!, sample 15287
 
AssertionError('Price must be positive.',)
[4.58668683 0.05426853 0.10440374 0.01450135]
Error: Zero implied volatility!, sample 15304
 
AssertionError('Price must be positive.',)
[ 1.81913644  0.58838905  0.28406423 -0.61673817]
Error: Zero implied volatility!, sample 15306
 
AssertionError('Price must be positive.',)
[ 1.64182861  0.17355694  0.48734411 -0.88784121]
Error: Zero implied volatility!, sample 15319
 
AssertionError('Price must be positive.',)
[ 1.27677696  0.16136554  0.37544862 -0.82989469]
Error: Zero implied volatility!, sample 15334
 
AssertionError('Price must be positive.',)
[ 9.5686462   0.06858763  0.65670645 -0.96516174]
Error: Zero implied volatility!, sample 15336
 
AssertionError('Price must be positive.',)
[0.98146793 0.3719853  0.50621758 0.94341863]
Error: Zero implied volatility!, sample 15377
 
AssertionError('Price must be positive.',)
[ 5.04363561  0.22487212  0.310367   -0.65201103]
Error: Zero implied volatility!, sample 15378
 
AssertionError('Price must be positive.',)
[ 4.59576858  0.2394792   0.28890945 -0.6506565 ]
Error: Zero implied volatility!, sample 15379
 
AssertionError('Price must be positive.',)
[ 1.60857717  0.6552047   0.27123518 -0.78269388]
Error: Zero implied volatility!, sample 15416
 
AssertionError('Normalized price must be positive.',)
[1.49230056 0.23690277 0.25926429 0.66146376]
Error: Zero implied volatility!, sample 15427
 
AssertionError('Price must be positive.',)
[ 0.69901268  0.1520745   0.19357762 -0.87624589]
Error: Zero implied volatility!, sample 15441
 
AssertionError('Price must be positive.',)
[ 3.51138539  0.20985252  0.73229739 -0.74584044]
Error: Zero implied volatility!, sample 15458
 
AssertionError('Price must be positive.',)
[ 2.53869058  0.2429356   0.3854636  -0.95382702]
Error: Zero implied volatility!, sample 15547
 
AssertionError('Price must be positive.',)
[ 5.54559893  0.08421934  0.29682532 -0.666427  ]
Error: Zero implied volatility!, sample 15553
 
AssertionError('Price must be positive.',)
[ 4.15618139  0.15645736  0.18458325 -0.30707616]
Error: Zero implied volatility!, sample 15583
 
AssertionError('Price must be positive.',)
[ 4.87907094  0.06749279  0.22273175 -0.59330217]
Error: Zero implied volatility!, sample 15589
 
AssertionError('Price must be positive.',)
[ 1.56246068  0.45281681  0.45535684 -0.78828112]
Error: Zero implied volatility!, sample 15599
 
AssertionError('Price must be positive.',)
[ 4.68121466  0.1547494   0.20530399 -0.12354102]
Error: Zero implied volatility!, sample 15632
 
AssertionError('Price must be positive.',)
[ 3.82360408  0.41923501  0.73608872 -0.92283844]
Error: Zero implied volatility!, sample 15637
 
AssertionError('Price must be positive.',)
[1.99280007 0.07916428 0.24184219 0.86693709]
Error: Zero implied volatility!, sample 15644
 
AssertionError('Normalized price must be positive.',)
[0.83999526 0.22550846 0.17818559 0.72714025]
Error: Zero implied volatility!, sample 15649
 
AssertionError('Price must be positive.',)
[ 7.402514    0.2000554   0.63870317 -0.90139345]
Error: Zero implied volatility!, sample 15651
 
AssertionError('Normalized price must be positive.',)
[0.93249805 0.79548844 0.49093594 0.51460925]
Error: Zero implied volatility!, sample 15655
 
AssertionError('Normalized price must be positive.',)
[7.29080652 0.07445382 0.17684454 0.90186392]
Error: Zero implied volatility!, sample 15673
 
AssertionError('Price must be positive.',)
[ 4.21998836  0.09508071  0.22526858 -0.00885006]
Error: Zero implied volatility!, sample 15736
 
AssertionError('Price must be positive.',)
[ 3.01677326  0.14995993  0.66863992 -0.37756309]
Error: Zero implied volatility!, sample 15752
 
AssertionError('Price must be positive.',)
[ 2.01866488  0.37504464  0.50323719 -0.60233682]
Error: Zero implied volatility!, sample 15777
 
AssertionError('Price must be positive.',)
[ 3.0680578   0.45605309  0.51362855 -0.98564983]
Error: Zero implied volatility!, sample 15814
 
AssertionError('Normalized price must be positive.',)
[2.44547495 0.18847418 0.76632639 0.77366238]
Error: Zero implied volatility!, sample 15826
 
AssertionError('Price must be positive.',)
[ 4.4921774   0.14190793  0.65436705 -0.92455133]
Error: Zero implied volatility!, sample 15827
 
AssertionError('Normalized price must be positive.',)
[3.10787642 0.10627528 0.66614166 0.82678265]
Error: Zero implied volatility!, sample 15828
 
AssertionError('Normalized price must be positive.',)
[5.09497904 0.11616778 0.76059516 0.45080243]
Error: Zero implied volatility!, sample 15835
 
AssertionError('Price must be positive.',)
[ 1.09040735  0.38973855  0.28154883 -0.57003188]
Error: Zero implied volatility!, sample 15840
 
AssertionError('Normalized price must be positive.',)
[0.52224292 0.24216871 0.24787721 0.53132996]
Error: Zero implied volatility!, sample 15842
 
AssertionError('Price must be positive.',)
[ 1.72048556  0.41605706  0.1962531  -0.44628099]
Error: Zero implied volatility!, sample 15842
 
AssertionError('Normalized price must be positive.',)
[2.6659498  0.11881775 0.32784014 0.66286795]
Error: Zero implied volatility!, sample 15848
 
AssertionError('Price must be positive.',)
[ 1.934275    0.14934948  0.49861431 -0.71837206]
Error: Zero implied volatility!, sample 15887
 
AssertionError('Price must be positive.',)
[0.73450825 0.63776669 0.59466226 0.90793635]
Error: Zero implied volatility!, sample 15914
 
AssertionError('Price must be positive.',)
[ 5.78097783  0.06145428  0.17658898 -0.41482679]
Error: Zero implied volatility!, sample 15943
 
AssertionError('Normalized price must be positive.',)
[4.72425108 0.12557684 0.69754335 0.97250567]
Error: Zero implied volatility!, sample 15999
 
AssertionError('Price must be positive.',)
[1.06886473 0.36127571 0.63501713 0.38197196]
Error: Zero implied volatility!, sample 16066
 
AssertionError('Price must be positive.',)
[ 2.23942126  0.65343561  0.56863499 -0.64410421]
Error: Zero implied volatility!, sample 16081
 
AssertionError('Price must be positive.',)
[ 6.63313436  0.28168835  0.69720219 -0.77238122]
Error: Zero implied volatility!, sample 16083
 
AssertionError('Price must be positive.',)
[ 8.70330637  0.25180065  0.78203348 -0.83753442]
Error: Zero implied volatility!, sample 16084
 
AssertionError('Price must be positive.',)
[ 3.89020504  0.19361693  0.12297347 -0.98197841]
Error: Zero implied volatility!, sample 16128
 
AssertionError('Price must be positive.',)
[ 3.67742298  0.15062525  0.77316108 -0.71819055]
Error: Zero implied volatility!, sample 16150
 
AssertionError('Price must be positive.',)
[ 1.36117099  0.26528631  0.48538318 -0.84270927]
Error: Zero implied volatility!, sample 16164
 
AssertionError('Price must be positive.',)
[6.20565604 0.05454508 0.15955722 0.25991016]
Error: Zero implied volatility!, sample 16171
 
AssertionError('Price must be positive.',)
[ 6.3632277   0.1357387   0.13999906 -0.0337275 ]
Error: Zero implied volatility!, sample 16176
 
AssertionError('Normalized price must be positive.',)
[2.26196111 0.36964325 0.05796558 0.79001748]
Error: Zero implied volatility!, sample 16178
 
AssertionError('Normalized price must be positive.',)
[3.02162016 0.47708394 0.7699129  0.8789821 ]
Error: Zero implied volatility!, sample 16196
 
AssertionError('Normalized price must be positive.',)
[2.13088864 0.5086266  0.42138916 0.90283142]
Error: Zero implied volatility!, sample 16246
 
AssertionError('Price must be positive.',)
[ 1.55746154  0.58703974  0.49058951 -0.75693786]
Error: Zero implied volatility!, sample 16249
 
AssertionError('Price must be positive.',)
[ 1.09940203  0.69068648  0.12065036 -0.90426109]
Error: Zero implied volatility!, sample 16272
 
AssertionError('Price must be positive.',)
[3.18877108 0.05287332 0.09688801 0.73205319]
Error: Zero implied volatility!, sample 16298
 
AssertionError('Price must be positive.',)
[ 2.33397396  0.17002429  0.74457299 -0.55743535]
Error: Zero implied volatility!, sample 16300
 
AssertionError('Price must be positive.',)
[ 2.58104961  0.60467599  0.75532346 -0.89941017]
Error: Zero implied volatility!, sample 16314
 
AssertionError('Normalized price must be positive.',)
[1.05426944 0.52954655 0.30480284 0.724014  ]
Error: Zero implied volatility!, sample 16328
 
AssertionError('Normalized price must be positive.',)
[0.67092854 0.43769797 0.59935406 0.98195942]
Error: Zero implied volatility!, sample 16337
 
AssertionError('Price must be positive.',)
[ 0.74456039  0.35351551  0.71661892 -0.31933384]
Error: Zero implied volatility!, sample 16340
 
AssertionError('Normalized price must be positive.',)
[8.75080146 0.11082271 0.23081663 0.8817697 ]
Error: Zero implied volatility!, sample 16359
 
AssertionError('Price must be positive.',)
[ 1.78863005  0.68637716  0.11371644 -0.78099564]
Error: Zero implied volatility!, sample 16362
 
AssertionError('Price must be positive.',)
[ 1.53772338  0.21663709  0.34235355 -0.76496098]
Error: Zero implied volatility!, sample 16363
 
AssertionError('Price must be positive.',)
[ 1.05492458  0.13108098  0.47600348 -0.19884972]
Error: Zero implied volatility!, sample 16375
 
AssertionError('Price must be positive.',)
[4.39041349 0.09364933 0.72131599 0.55412317]
Error: Zero implied volatility!, sample 16385
 
AssertionError('Normalized price must be positive.',)
[1.14692382 0.66453408 0.27326721 0.54408152]
Error: Zero implied volatility!, sample 16417
 
AssertionError('Normalized price must be positive.',)
[3.15318913 0.15812326 0.25275874 0.42578147]
Error: Zero implied volatility!, sample 16419
 
AssertionError('Price must be positive.',)
[ 2.20773439  0.36874881  0.45869773 -0.82084861]
Error: Zero implied volatility!, sample 16441
 
AssertionError('Price must be positive.',)
[ 3.28423695  0.20462704  0.56695015 -0.95967203]
Error: Zero implied volatility!, sample 16454
 
AssertionError('Price must be positive.',)
[ 0.60073972  0.64316624  0.60805894 -0.75529024]
Error: Zero implied volatility!, sample 16509
 
AssertionError('Price must be positive.',)
[ 3.75943356  0.33757166  0.22149874 -0.66277613]
Error: Zero implied volatility!, sample 16512
 
AssertionError('Price must be positive.',)
[ 7.72631239  0.10867072  0.08942033 -0.14346425]
Error: Zero implied volatility!, sample 16521
 
AssertionError('Normalized price must be positive.',)
[6.18908609 0.25581788 0.73829335 0.8441861 ]
Error: Zero implied volatility!, sample 16560
 
AssertionError('Price must be positive.',)
[ 0.5197833   0.79532972  0.20974139 -0.13673337]
Error: Zero implied volatility!, sample 16579
 
AssertionError('Price must be positive.',)
[ 5.48087434  0.10390844  0.48045596 -0.88348264]
Error: Zero implied volatility!, sample 16584
 
AssertionError('Price must be positive.',)
[0.77715506 0.71460987 0.13779926 0.45282133]
Error: Zero implied volatility!, sample 16597
 
AssertionError('Normalized price must be positive.',)
[3.21936617 0.24578072 0.39690682 0.85874637]
Error: Zero implied volatility!, sample 16621
 
AssertionError('Normalized price must be positive.',)
[0.70917083 0.54660685 0.49565899 0.63745011]
Error: Zero implied volatility!, sample 16631
 
AssertionError('Price must be positive.',)
[1.17080461 0.14021185 0.08495507 0.35884732]
Error: Zero implied volatility!, sample 16664
 
AssertionError('Price must be positive.',)
[ 4.58401727  0.07780331  0.16364523 -0.96191719]
Error: Zero implied volatility!, sample 16685
 
AssertionError('Normalized price must be positive.',)
[1.87411182 0.4852067  0.75035425 0.9407051 ]
Error: Zero implied volatility!, sample 16702
 
AssertionError('Price must be positive.',)
[ 4.57607459  0.10106139  0.46217449 -0.68543734]
Error: Zero implied volatility!, sample 16739
 
AssertionError('Price must be positive.',)
[ 5.5113232   0.15462098  0.22487665 -0.62519327]
Error: Zero implied volatility!, sample 16744
 
AssertionError('Price must be positive.',)
[ 4.90534789  0.19603385  0.17864753 -0.09986638]
Error: Zero implied volatility!, sample 16790
 
AssertionError('Price must be positive.',)
[ 2.61434814  0.48273107  0.21185797 -0.84762587]
Error: Zero implied volatility!, sample 16799
 
AssertionError('Price must be positive.',)
[ 0.97926664  0.06516387  0.17139714 -0.72114794]
Error: Zero implied volatility!, sample 16802
 
AssertionError('Normalized price must be positive.',)
[8.22319058 0.09171531 0.71949337 0.90202259]
Error: Zero implied volatility!, sample 16857
 
AssertionError('Price must be positive.',)
[ 7.60999138  0.12860628  0.51188989 -0.8260077 ]
Error: Zero implied volatility!, sample 16918
 
AssertionError('Price must be positive.',)
[ 6.21190271  0.06831876  0.27000605 -0.10061561]
Error: Zero implied volatility!, sample 16949
 
AssertionError('Normalized price must be positive.',)
[8.16396349 0.20614665 0.58881374 0.94763367]
Error: Zero implied volatility!, sample 16978
 
AssertionError('Price must be positive.',)
[ 2.25966826  0.134698    0.30221598 -0.94625621]
Error: Zero implied volatility!, sample 17039
 
AssertionError('Price must be positive.',)
[ 8.97276624  0.06165897  0.50721123 -0.83072693]
Error: Zero implied volatility!, sample 17057
 
AssertionError('Price must be positive.',)
[ 2.70737694  0.25739781  0.11527507 -0.33480753]
Error: Zero implied volatility!, sample 17090
 
AssertionError('Normalized price must be positive.',)
[5.36369397 0.19644167 0.22132138 0.70228   ]
Error: Zero implied volatility!, sample 17134
 
AssertionError('Normalized price must be positive.',)
[8.8171     0.11074047 0.49704568 0.97435496]
Error: Zero implied volatility!, sample 17142
 
AssertionError('Price must be positive.',)
[1.88332977 0.06404741 0.4147302  0.09419526]
Error: Zero implied volatility!, sample 17155
 
AssertionError('Normalized price must be positive.',)
[6.72643222 0.06152445 0.25098732 0.98628787]
Error: Zero implied volatility!, sample 17161
 
AssertionError('Price must be positive.',)
[ 3.36770408  0.2466227   0.60635937 -0.85753754]
Error: Zero implied volatility!, sample 17182
 
AssertionError('Price must be positive.',)
[ 4.64824472  0.20787704  0.72260191 -0.95924241]
Error: Zero implied volatility!, sample 17215
 
AssertionError('Price must be positive.',)
[ 3.02218794  0.2329017   0.24739117 -0.68381161]
Error: Zero implied volatility!, sample 17260
 
AssertionError('Price must be positive.',)
[ 4.11765785  0.15046528  0.28222456 -0.70610692]
Error: Zero implied volatility!, sample 17287
 
AssertionError('Normalized price must be positive.',)
[2.9611729  0.33467059 0.71987117 0.82772502]
Error: Zero implied volatility!, sample 17294
 
AssertionError('Price must be positive.',)
[ 1.18453103  0.42746855  0.67517712 -0.71766635]
Error: Zero implied volatility!, sample 17300
 
AssertionError('Price must be positive.',)
[ 6.34833893  0.17300655  0.58170861 -0.89835094]
Error: Zero implied volatility!, sample 17313
 
AssertionError('Price must be positive.',)
[ 2.75125464  0.44052389  0.20068269 -0.96620899]
Error: Zero implied volatility!, sample 17325
 
AssertionError('Price must be positive.',)
[ 2.42095198  0.61039208  0.63646829 -0.77805715]
Error: Zero implied volatility!, sample 17327
 
AssertionError('Price must be positive.',)
[ 5.39871881  0.14293056  0.37106098 -0.53808429]
Error: Zero implied volatility!, sample 17334
 
AssertionError('Price must be positive.',)
[ 9.83387343  0.07909495  0.450245   -0.96092549]
Error: Zero implied volatility!, sample 17432
 
AssertionError('Price must be positive.',)
[ 0.77634954  0.37029332  0.30757384 -0.33775411]
Error: Zero implied volatility!, sample 17476
 
AssertionError('Price must be positive.',)
[ 2.78258986  0.63402058  0.31470182 -0.75958194]
Error: Zero implied volatility!, sample 17482
 
AssertionError('Normalized price must be positive.',)
[1.27003813 0.45627141 0.71218466 0.97681209]
Error: Zero implied volatility!, sample 17515
 
AssertionError('Price must be positive.',)
[ 3.43360821  0.13854809  0.21276108 -0.36154904]
Error: Zero implied volatility!, sample 17517
 
AssertionError('Price must be positive.',)
[ 7.87853971  0.120042    0.23990896 -0.23035869]
Error: Zero implied volatility!, sample 17527
 
AssertionError('Price must be positive.',)
[ 2.32866057  0.13981695  0.63661403 -0.49233101]
Error: Zero implied volatility!, sample 17541
 
AssertionError('Price must be positive.',)
[ 0.72828035  0.24032705  0.43509252 -0.40498712]
Error: Zero implied volatility!, sample 17552
 
AssertionError('Price must be positive.',)
[ 6.7143596   0.06157525  0.21317796 -0.97195523]
Error: Zero implied volatility!, sample 17557
 
AssertionError('Price must be positive.',)
[ 4.98899002  0.3218227   0.19133006 -0.82523967]
Error: Zero implied volatility!, sample 17561
 
AssertionError('Price must be positive.',)
[ 2.43022812  0.59261717  0.57087676 -0.73462293]
Error: Zero implied volatility!, sample 17628
 
AssertionError('Price must be positive.',)
[ 7.31053454  0.0993585   0.49083027 -0.62296687]
Error: Zero implied volatility!, sample 17628
 
AssertionError('Price must be positive.',)
[ 1.20653014  0.70633724  0.30390389 -0.78999632]
Error: Zero implied volatility!, sample 17657
 
AssertionError('Price must be positive.',)
[ 0.83512665  0.25317792  0.25604344 -0.87103141]
Error: Zero implied volatility!, sample 17664
 
AssertionError('Price must be positive.',)
[ 7.55077651  0.26559755  0.39058594 -0.76022212]
Error: Zero implied volatility!, sample 17699
 
AssertionError('Normalized price must be positive.',)
[4.57830518 0.07838931 0.65227736 0.98804473]
Error: Zero implied volatility!, sample 17733
 
AssertionError('Price must be positive.',)
[ 1.26690835  0.53928103  0.29417928 -0.28237845]
Error: Zero implied volatility!, sample 17751
 
AssertionError('Price must be positive.',)
[ 2.92939515  0.18082053  0.3011734  -0.50358567]
Error: Zero implied volatility!, sample 17836
 
AssertionError('Price must be positive.',)
[ 8.51000708  0.05717587  0.5025851  -0.72770898]
Error: Zero implied volatility!, sample 17847
 
AssertionError('Normalized price must be positive.',)
[2.18654732 0.23798867 0.1873436  0.7479992 ]
Error: Zero implied volatility!, sample 17910
 
AssertionError('Normalized price must be positive.',)
[3.78359056 0.17570373 0.45144092 0.77707233]
Error: Zero implied volatility!, sample 17916
 
AssertionError('Price must be positive.',)
[ 2.13468409  0.29642722  0.79463707 -0.61163384]
Error: Zero implied volatility!, sample 17934
 
AssertionError('Price must be positive.',)
[ 9.43032354  0.13160069  0.59155651 -0.95576733]
Error: Zero implied volatility!, sample 17943
 
AssertionError('Price must be positive.',)
[ 7.51912534  0.05937681  0.53015221 -0.96721449]
Error: Zero implied volatility!, sample 17964
 
AssertionError('Price must be positive.',)
[1.85584816 0.16222886 0.48649622 0.36495379]
Error: Zero implied volatility!, sample 17982
 
AssertionError('Price must be positive.',)
[ 6.6248274   0.12032516  0.5598952  -0.77247378]
Error: Zero implied volatility!, sample 17997
 
AssertionError('Price must be positive.',)
[ 1.6940956   0.37766783  0.12245635 -0.56299236]
Error: Zero implied volatility!, sample 18002
 
AssertionError('Price must be positive.',)
[ 1.47633305  0.1946806   0.31157649 -0.93349824]
Error: Zero implied volatility!, sample 18087
 
AssertionError('Price must be positive.',)
[ 2.07723815  0.67919213  0.51913954 -0.85373653]
Error: Zero implied volatility!, sample 18101
 
AssertionError('Price must be positive.',)
[ 2.35592969  0.40962997  0.159678   -0.02700262]
Error: Zero implied volatility!, sample 18130
 
AssertionError('Price must be positive.',)
[ 4.68864843  0.68822918  0.64514614 -0.96162469]
Error: Zero implied volatility!, sample 18189
 
AssertionError('Price must be positive.',)
[0.55709055 0.47990822 0.38975136 0.34765471]
Error: Zero implied volatility!, sample 18228
 
AssertionError('Price must be positive.',)
[ 8.39717242  0.22976888  0.51917732 -0.81294304]
Error: Zero implied volatility!, sample 18241
 
AssertionError('Price must be positive.',)
[1.94360975 0.12239564 0.41948811 0.78105594]
Error: Zero implied volatility!, sample 18259
 
AssertionError('Price must be positive.',)
[ 5.07596571  0.36415973  0.5198631  -0.73512287]
Error: Zero implied volatility!, sample 18261
 
AssertionError('Price must be positive.',)
[ 0.65310265  0.54182308  0.75950616 -0.76619027]
Error: Zero implied volatility!, sample 18326
 
AssertionError('Price must be positive.',)
[ 3.85224064  0.1103598   0.67422987 -0.22781919]
Error: Zero implied volatility!, sample 18330
 
AssertionError('Price must be positive.',)
[ 9.17295261  0.06037185  0.11451589 -0.76492341]
Error: Zero implied volatility!, sample 18359
 
AssertionError('Normalized price must be positive.',)
[2.63200353 0.26924807 0.52281198 0.67115347]
Error: Zero implied volatility!, sample 18375
 
AssertionError('Price must be positive.',)
[ 5.82971266  0.29151287  0.3871838  -0.9122792 ]
Error: Zero implied volatility!, sample 18400
 
AssertionError('Price must be positive.',)
[ 1.91206431  0.13631306  0.39787211 -0.19978624]
Error: Zero implied volatility!, sample 18440
 
AssertionError('Price must be positive.',)
[ 2.18105492  0.14380099  0.45484366 -0.65200676]
Error: Zero implied volatility!, sample 18485
 
AssertionError('Price must be positive.',)
[7.30146984 0.0701353  0.22765761 0.69040993]
Error: Zero implied volatility!, sample 18497
 
AssertionError('Normalized price must be positive.',)
[6.12607926 0.21771113 0.36827355 0.87334903]
Error: Zero implied volatility!, sample 18500
 
AssertionError('Price must be positive.',)
[2.84477666 0.1109375  0.59585765 0.04232556]
Error: Zero implied volatility!, sample 18568
 
AssertionError('Price must be positive.',)
[ 6.97868725  0.24607175  0.57589379 -0.7482147 ]
Error: Zero implied volatility!, sample 18590
 
AssertionError('Price must be positive.',)
[ 8.74349378  0.22391499  0.57030044 -0.98899707]
Error: Zero implied volatility!, sample 18622
 
AssertionError('Price must be positive.',)
[ 1.33536483  0.26831073  0.18252719 -0.00841576]
Error: Zero implied volatility!, sample 18628
 
AssertionError('Price must be positive.',)
[ 1.79993653  0.45486741  0.70220235 -0.74269326]
Error: Zero implied volatility!, sample 18634
 
AssertionError('Price must be positive.',)
[ 2.21315144  0.17727478  0.57055739 -0.35556766]
Error: Zero implied volatility!, sample 18638
 
AssertionError('Price must be positive.',)
[1.65450483 0.27188866 0.16139146 0.22023893]
Error: Zero implied volatility!, sample 18643
 
AssertionError('Price must be positive.',)
[ 0.98603147  0.26059924  0.53120537 -0.73323266]
Error: Zero implied volatility!, sample 18679
 
AssertionError('Normalized price must be positive.',)
[1.24151369 0.29789121 0.24388816 0.81677212]
Error: Zero implied volatility!, sample 18683
 
AssertionError('Price must be positive.',)
[ 0.86210114  0.57566317  0.51525129 -0.4465761 ]
Error: Zero implied volatility!, sample 18694
 
AssertionError('Price must be positive.',)
[ 0.89788577  0.11448996  0.05156487 -0.95608999]
Error: Zero implied volatility!, sample 18798
 
AssertionError('Price must be positive.',)
[ 2.55556961  0.1419436   0.3635883  -0.24148681]
Error: Zero implied volatility!, sample 18845
 
AssertionError('Price must be positive.',)
[ 2.26086141  0.6003488   0.34724368 -0.86926485]
Error: Zero implied volatility!, sample 18901
 
AssertionError('Price must be positive.',)
[1.87133812 0.07910503 0.39913433 0.48386837]
Error: Zero implied volatility!, sample 18928
 
AssertionError('Price must be positive.',)
[ 7.89112297  0.13199318  0.34628208 -0.92413703]
Error: Zero implied volatility!, sample 18931
 
AssertionError('Price must be positive.',)
[ 8.53968874  0.20491097  0.13345916 -0.80599825]
Error: Zero implied volatility!, sample 18946
 
AssertionError('Price must be positive.',)
[0.76470595 0.31033739 0.32306715 0.60436921]
Error: Zero implied volatility!, sample 18951
 
AssertionError('Price must be positive.',)
[ 1.89475758  0.11576079  0.24925197 -0.62111397]
Error: Zero implied volatility!, sample 18953
 
AssertionError('Normalized price must be positive.',)
[1.24938684 0.3966863  0.6683241  0.94743861]
Error: Zero implied volatility!, sample 19011
 
AssertionError('Normalized price must be positive.',)
[8.53265842 0.09677808 0.49284021 0.69106415]
Error: Zero implied volatility!, sample 19120
 
AssertionError('Price must be positive.',)
[ 7.2030014   0.0696972   0.36899322 -0.4543813 ]
Error: Zero implied volatility!, sample 19220
 
AssertionError('Price must be positive.',)
[ 3.22199075  0.20696604  0.67049948 -0.95396357]
Error: Zero implied volatility!, sample 19256
 
AssertionError('Price must be positive.',)
[ 1.17981756  0.69256677  0.17350675 -0.91887255]
Error: Zero implied volatility!, sample 19262
 
AssertionError('Price must be positive.',)
[ 1.44882744  0.36173196  0.44596414 -0.4608546 ]
Error: Zero implied volatility!, sample 19279
 
AssertionError('Price must be positive.',)
[ 0.68378504  0.76942373  0.29969656 -0.71392698]
Error: Zero implied volatility!, sample 19295
 
AssertionError('Price must be positive.',)
[ 3.0765584   0.42039006  0.41216979 -0.7823778 ]
Error: Zero implied volatility!, sample 19376
 
AssertionError('Price must be positive.',)
[ 0.57179098  0.56279654  0.11608516 -0.72679213]
Error: Zero implied volatility!, sample 19383
 
AssertionError('Initial guess must be positive.',)
[7.16284767 0.13211544 0.34659243 0.93816159]
Error: Zero implied volatility!, sample 19399
 
AssertionError('Normalized price must be positive.',)
[8.45122694 0.05212838 0.67690189 0.92195029]
Error: Zero implied volatility!, sample 19420
 
AssertionError('Normalized price must be positive.',)
[3.04021603 0.26508467 0.39565529 0.65367183]
Error: Zero implied volatility!, sample 19425
 
AssertionError('Price must be positive.',)
[ 5.87100622  0.27621615  0.14068786 -0.7321797 ]
Error: Zero implied volatility!, sample 19453
 
AssertionError('Price must be positive.',)
[2.42075444 0.11535313 0.16304034 0.84845946]
Error: Zero implied volatility!, sample 19481
 
AssertionError('Price must be positive.',)
[ 1.16929161  0.64478035  0.45971205 -0.47068086]
Error: Zero implied volatility!, sample 19486
 
AssertionError('Normalized price must be positive.',)
[0.55935725 0.49673995 0.418304   0.9690466 ]
Error: Zero implied volatility!, sample 19555
 
AssertionError('Price must be positive.',)
[ 3.48526948  0.08653069  0.36043862 -0.36178791]
Error: Zero implied volatility!, sample 19557
 
AssertionError('Price must be positive.',)
[ 8.87873548  0.0858846   0.35536616 -0.23898886]
Error: Zero implied volatility!, sample 19587
 
AssertionError('Normalized price must be positive.',)
[1.51876901 0.46588506 0.18078712 0.6888526 ]
Error: Zero implied volatility!, sample 19590
 
AssertionError('Price must be positive.',)
[ 0.97409241  0.43744954  0.08593729 -0.11061439]
Error: Zero implied volatility!, sample 19591
 
AssertionError('Price must be positive.',)
[ 9.39396411  0.33206457  0.56375826 -0.93422284]
Error: Zero implied volatility!, sample 19605
 
AssertionError('Price must be positive.',)
[ 0.90083119  0.21974377  0.13118949 -0.4257609 ]
Error: Zero implied volatility!, sample 19610
 
AssertionError('Price must be positive.',)
[ 0.86828949  0.2323853   0.39775994 -0.50189822]
Error: Zero implied volatility!, sample 19611
 
AssertionError('Price must be positive.',)
[ 0.95144403  0.60783379  0.22716428 -0.74240933]
Error: Zero implied volatility!, sample 19617
 
AssertionError('Normalized price must be positive.',)
[4.30893014 0.39408499 0.57300249 0.91305907]
Error: Zero implied volatility!, sample 19641
 
AssertionError('Price must be positive.',)
[ 1.06548611  0.60564996  0.31934081 -0.73765682]
Error: Zero implied volatility!, sample 19686
 
AssertionError('Price must be positive.',)
[ 2.85802809  0.33593851  0.11756888 -0.2341541 ]
Error: Zero implied volatility!, sample 19699
 
AssertionError('Price must be positive.',)
[ 2.1195755   0.53938401  0.69430007 -0.70837091]
Error: Zero implied volatility!, sample 19703
 
AssertionError('Price must be positive.',)
[ 1.11854466  0.46648778  0.1043004  -0.4074702 ]
Error: Zero implied volatility!, sample 19712
 
AssertionError('Price must be positive.',)
[ 3.93233027  0.40864185  0.20670239 -0.76021507]
Error: Zero implied volatility!, sample 19740
 
AssertionError('Price must be positive.',)
[ 0.51319738  0.5714788   0.36545817 -0.62413994]
Error: Zero implied volatility!, sample 19787
 
AssertionError('Price must be positive.',)
[ 9.20150925  0.05151045  0.10701014 -0.48999291]
Error: Zero implied volatility!, sample 19810
 
AssertionError('Price must be positive.',)
[2.89640669 0.19949045 0.08599827 0.86697825]
Error: Zero implied volatility!, sample 19835
 
AssertionError('Price must be positive.',)
[ 1.68843521  0.22507754  0.49851381 -0.66811131]
Error: Zero implied volatility!, sample 19867
 
AssertionError('Price must be positive.',)
[ 4.69517163  0.75201969  0.78565438 -0.97862864]
Error: Zero implied volatility!, sample 19868
 
AssertionError('Price must be positive.',)
[ 4.21079919  0.20003069  0.44978798 -0.27324304]
Error: Zero implied volatility!, sample 19918
 
AssertionError('Price must be positive.',)
[ 2.2106304   0.40511539  0.24480311 -0.11356662]
Error: Zero implied volatility!, sample 19979
 
file name heston_heston_trial
Saving data for training of NN1
IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.
In [15]:
'''
Function to transform input and output data to give the NN
'''

class MyTransformer(BaseEstimator, TransformerMixin):
    def __init__(self, func=None, inv_func=None, validate=True,
                 accept_sparse=False, pass_y=False):
        self.func = func
        self.inv_func = inv_func
        self.validate = validate
        self.accept_sparse = accept_sparse
        self.pass_y = pass_y
        
    def fit(self, X, y=None):
        if self.validate:
            check_array(X, self.accept_sparse)
        return self

    def transform(self, X, y=None):
        if self.validate:
            X = check_array(X, self.accept_sparse)
        if self.func is None:
            return X
        return self.func(X)
        
    def inverse_transform(self, X, y=None):
        if self.validate:
            X = check_array(X, self.accept_sparse)    
        if self.inv_func is None:
            return X
        return self.inv_func(X)

def transf_heston_parameters_in(y, transformation):
    len_y = y.shape[0]
    aux_vec = (y[:,:7]) #rho
    aux_vec = transformation(aux_vec)
    y = np.concatenate((aux_vec[:,:7],y[:,7].reshape(len_y,1)),axis=1)
    return  y

def transf_heston_prices(y, transformation):
    '''
    Also used for imp vola surfaces as data are all positive.
    '''
    y = transformation(y)
    return y

def transf_heston_parameters_out(y, transformation):
    len_y = y.shape[0]
    aux_vec = (y[:,:3]) #rho
    aux_vec = transformation(aux_vec)
    y = np.concatenate((aux_vec[:,:3],y[:,3].reshape(len_y,1)),axis=1)
    return  y

def preprocess_heston_param_NN1(x, func=he_analytic['transformation'], 
                            inv_func=he_analytic['inverse_transformation']):
    trm = MyTransformer(func=func, inv_func=inv_func)
#   scaler = StandardScaler()
    scaler = MinMaxScaler(copy=True, feature_range=(-1, 1))
    pipeline = Pipeline([('trm', trm), ('scaler', scaler)])
    x = transf_heston_parameters_in(x, pipeline.fit_transform)
    return x, pipeline

def preprocess_prices_or_vola(y):
    func = he_analytic['transformation']
    inv_func = he_analytic['inverse_transformation']
    trm = MyTransformer(func=func, inv_func=inv_func)
#   scaler = StandardScaler()
    scaler = MinMaxScaler(copy=True, feature_range=(-1, 1))
    pipeline = Pipeline([('trm', trm), ('scaler', scaler)])
    try:
        y = pipeline.fit_transform(y)
    except (TypeError,ValueError) as e:
        print(e)
        print('Max value: %s'%np.max(np.array(y)))
        print('Min value: %s'%np.min(np.array(y)))
    return y, pipeline
In [16]:
'''
Retrieving data for NN1 and related functions.
'''
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.pipeline import Pipeline

reorder_seed = 1027
tot_s = 1.
val_s = 0.1
test_s = 0.1

def split_database(total_size, valid_size, test_size, total_sample):
    train_size = total_size - valid_size - test_size
    train_sample = int(round(total_sample*train_size))
    valid_sample = int(round(total_sample*valid_size))
    test_sample = int(round(total_sample*test_size))
    print(train_sample, valid_sample, test_sample)
    if train_sample < 1 or train_sample > total_sample or \
        valid_sample < 0 or valid_sample > total_sample or \
        test_sample < 0 or test_sample > total_sample:
        total_sample -= train_sample
        if total_sample - valid_sample < 0:
            valid_sample = 0
            test_sample = 0
        else:
            total_sample -= valid_sample
            if total_sample - test_sample < 0:
                test_sample = 0
    return train_sample, valid_sample, test_sample


def cleaning_data_eliminate_version(db, indexes=None):
    '''
    Find the rows of the database that have negative elements and eliminate them.
    '''
    db = np.array(db)
    if indexes is None:
        indexes = (db<=0).sum(axis=1) #positions of rows with at least 1 non-positive element
    db = db[indexes==0] #eliminates these rows from the database
    return db, indexes #returns the array and the indexes

def retrieve_trainingset_NN(file_name, fin_model, 
                            force_positive_in=True, force_positive_out=True, 
                            whiten_in=False, whiten_out=False, seed=reorder_seed,
                            total_size=tot_s, valid_size=val_s, test_size=test_s):
    '''
    Function to load and preprocess Heston/Bates data from file in order to make
    them suitable for the Neural Networks.
    '''
    if force_positive_in and whiten_in:
        raise RuntimeError('Choose only one preprocessing type for input values!')
    if force_positive_out and whiten_out:
        raise RuntimeError('Choose only one preprocessing type for output values!')
    
    #To make it reproducible
    np.random.seed(seed)
    print('Reordering seed: %s '%seed)
    
    file_name = data_dir + '/' + file_name
    print('Current working dir: %s;  file name: %s'%(os.getcwd(), file_name))
    
    x = np.load(file_name+'_train_'+fin_model+'_find_iv_inputNN1.npy')
    y = np.load(file_name+'_train_'+fin_model+'_find_iv_outputNN1.npy')
    
    # Cleaning data
    y, indexes = cleaning_data_eliminate_version(db=y)
    print('Number of rejected samples: %s'%sum(indexes))
    x, _ = cleaning_data_eliminate_version(db=x, indexes=indexes)
    
    total_sample = y.shape[0]
    train_sample, valid_sample, test_sample = split_database(total_size, valid_size, 
                                                             test_size, total_sample)
    # INPUT
    if force_positive_in:
        print('--> Force positive input')
        x, pipeline_in = preprocess_heston_param_NN1(x)
    elif whiten_in:
        print('--> PCA & whitening used in input')
        x, pipeline_in = preprocess_prices_or_vola_pca(x)
    else:
        pipeline_in = None
        
    # OUTPUT
    if force_positive_out:
        print('--> Force positive output')
        y, pipeline_out = preprocess_prices_or_vola(y)
    elif whiten_out:
        print('--> PCA & whitening used in output')
        y, pipeline_out = preprocess_prices_or_vola_pca(y)
    else:
        pipeline_out = None
    
    index = np.arange(total_sample)
    np.random.shuffle(index)
    
    x_total, y_total = x[index], y[index]
    x_train, y_train = x_total, y_total
    if test_sample > 0:
        x_train, x_test, y_train, y_test =\
          train_test_split(x_train, y_train, test_size=test_sample, random_state=None)
    else:
        x_test = None
        y_test = None
    
    x_train, x_valid, y_train, y_valid =\
        train_test_split(x_train, y_train, test_size=valid_sample, random_state=None)
    
    return {'x_train': x_train, 'y_train': y_train, 
            'x_valid': x_valid, 'y_valid': y_valid, 
            'x_test': x_test, 'y_test': y_test, 
            'preprocess_in': pipeline_in, 'transform_out': pipeline_out}
In [28]:
'''
Neural Network implementation
'''
from os.path import isfile
from os import getcwd
from copy import deepcopy, copy
import matplotlib.pyplot as plt
from six import string_types
import dill
from functools import partial
import tensorflow as tf

from keras.models import Sequential, Model
from keras.layers.core import Dense, Dropout, Activation
from keras.layers import BatchNormalization, Input
from keras.layers.merge import add
#from keras.regularizers import l2
from keras.layers.advanced_activations import ELU
from keras.optimizers import RMSprop, Adagrad, Adadelta, Adam, Adamax, Nadam
from keras.models import model_from_json
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau, \
                            Callback, TensorBoard
from keras import backend as K
from keras.constraints import maxnorm
#from keras import initializations
from keras.initializers import VarianceScaling, RandomUniform
#from keras.utils import Sequence
from keras.utils.vis_utils import plot_model


'''
Callbacks
'''
reduceLR = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=80, min_lr=9e-10, verbose=1)
earlyStopping = EarlyStopping(monitor='val_loss', patience=200)


'''
NN class
'''
def split_dataset(data):
    x_train = data['x_train']
    x_valid = data['x_valid']
    x_test = data['x_test']
    y_train = data['y_train']
    y_valid = data['y_valid']
    y_test = data['y_test']
    return x_train, x_valid, x_test, y_train, y_valid, y_test

def proper_name(name):
    name = name.replace(" ", "_")
    name = name.replace("(", "")
    name = name.replace(")", "")
    name = name.replace(",", "_")
    name = name.replace("-", "_")
    return name 

def reshape_fnn(x):
    if len(x[0].shape) == 1:
        p = np.concatenate(x)
        p.shape = (1, p.shape[0])
    else:
        p=x[None,:]
    return p

class NeuralNetwork(object):
    def __init__(self, model_name, model_callback, train_file_name,
                 lr=0.0005, loss='mean_squared_error', re_shape=reshape_fnn,
                 prefix='', postfix='', fin_model='he',
                 method=Nadam, checkPoint=True):
       
        self.model_name = model_name.lower()
        self.name = prefix + model_name
        self.postfix = postfix
        if self.postfix != '':
            self.postfix = '_'+self.postfix
        self.train_file_name = train_file_name
        self._fin_model = fin_model
        self._data = self.__get_data(fin_model=fin_model)
        self.x_train = None
        self.x_valid = None
        self.x_test = None
        self.y_train = None
        self.y_valid = None
        self.y_test = None
        self.model = None
        self.history = None
        self.method = method
        self._transform = self._data['transform_out']
        self._model_callback = model_callback
        self.lr = lr
        self.loss = loss
        self._reshape = re_shape
        self._preprocessing = self._data['preprocess_in']
        self.checkPoint = checkPoint
        print('Class NeuralNetwork, __init__ finished.')
        
        
    def __get_data(self, fin_model):
        # File name is h5_model_node + _ + risk factor + '_' + self.name
        self.train_file_name = proper_name(self.train_file_name)
        self.train_file_name = self.train_file_name.replace('/', '_')
        print(self.train_file_name) 
        return retrieve_trainingset_NN(self.train_file_name, fin_model=fin_model)

    def file_names(self):
        # File name is self.name + _nn
        file_name = proper_name(self.name) + '_nn' + self.postfix
        file_name = file_name.lower().replace('/', '_')
        return (data_dir + '/' + file_name, file_name)

    def __tofile(self):
        file_name, _ = self.file_names()
        if self.model is not None:
            json_file = file_name + '.json' #saving architecture
            json = self.model.to_json()
            open(json_file, 'w').write(json)
            if not self.checkPoint:
                print('Saving weights...')
                h5_file = file_name + '.h5' #saving weights
                self.model.save_weights(h5_file, overwrite=True)

    def __fromfile(self):
        file_name, _ = self.file_names()
        json_file = file_name + '.json'
        if isfile(json_file):
            self.model = model_from_json(open(json_file).read())
            h5_file = file_name + '.h5'
            self.model.load_weights(h5_file)
            print('Reading NN from file and setting learning rate to ',self.lr)
            method = self.method(lr=self.lr, clipnorm=1.)
            self.model.compile(optimizer=method, loss=self.loss)
        else:
            self.model = None

    def __getstate__(self):
        print("I'm being pickled!")
        self.__tofile()
        model = self.model
        del self.__dict__['model']
        d = deepcopy(self.__dict__)
        self.model = model
        del d['_data']
        del d['x_train']
        del d['x_valid']
        del d['x_test']
        del d['y_train']
        del d['y_valid']
        del d['y_test']
        return d

#   def __setstate__(self, d):
#       print "I'm being unpickled with these values: "#, d #Values taken from .p file
#       self.__dict__ = d
#       self._data = None
#       history = self.history
#       self.train(nb_epochs=0)
#       self.history = history
#       self.__fromfile()

    def train(self, nb_epochs):
        print('Train function of class NeuralNetwork called with %s epochs.'%nb_epochs)
        if nb_epochs > 0: 
            self.y_train = self._data['y_train'] 
            self.y_valid = self._data['y_valid']
            self.y_test = self._data['y_test'] 
            method = self.method(lr=self.lr, clipnorm=1.) #gradient clipnorm
            cp_file_name, simple_file_name = self.file_names()
            self.x_train, self.x_valid, self.x_test, self.model, self.history = \
                self._model_callback(self._data, method, self.loss, 
                                     nb_epochs=nb_epochs, CP=self.checkPoint,
                                     CP_name=cp_file_name, model_name=simple_file_name)
            if len(self.y_test)>0:
                print(' '); print ('   -- NN on Test set --')
#                print self.model.evaluate(self.x_test, self.y_test, batch_size=128)
                print(self.model.evaluate(self.x_test, self.y_test, 
                                          batch_size=self.history['params']['batch_size']))
                print(' ')


    def fit(self, nb_epochs):
        if self.model is None:
            raise RuntimeError('Model not yet instantiated')
        print('Now fitting the neural network model...')
        batch_size = self.history['params']['batch_size']
        history2 = self.model.fit(self.x_train, self.y_train, batch_size=batch_size, 
                          nb_epoch=nb_epochs, verbose=1, 
                          validation_data=(self.x_valid, self.y_valid))
        self.history = {'history': history2.history,
                        'params': history2.params}

    def predict(self, data):
        if self.model is None:
            raise RuntimeError('Model not yet instantiated')
        if self._reshape is not None:
            data = self._reshape(data)
        if self._preprocessing is not None:
            # Preprocessing applied in predict phase
            data = transf_heston_parameters_in(y=data, transformation=self._preprocessing.transform)
        y = self.model.predict(data)
        if self._transform is not None:
            # Postprocessing applied in predict phase
            y = transf_heston_prices(y=y, transformation=self._transform.inverse_transform)
        return y
In [29]:
'''
Functions to generate different neural networks
'''
def design_model(method, activation, exponent, init, layers, loss='mse', 
                 dropout_first=None, dropout_middle=None, dropout_last=None, 
                 neurons_first=None, neurons_last=None, weight_constraint=None,
                 dropout=None, tuning=False, **kwargs):
    
    c = weight_constraint
    
    if type(exponent)==str:
        exponent = eval(exponent)
    nb_unit = int(2**exponent)
    
    if dropout_first is None:
        dropout_first = dropout
    if dropout_middle is None:
        dropout_middle = dropout_first
    if dropout_last is None:
        dropout_last = dropout_middle
        
    act_idx = 1
    ## Input of model
    inp = Input(shape=(neurons_first,))
    ## Output of model
    # First layer
    ly = Dense(nb_unit, kernel_initializer=init,
               kernel_constraint=maxnorm(c, axis=0),
               use_bias=False)(inp)
    ly = BatchNormalization()(ly)
    act = copy(activation)
    act.name = act.name+'_'+str(act_idx)
    act_idx += 1
    ly = act(ly)
    ly = Dropout(dropout_first)(ly)
    
    # Middle layers
    for i in range(layers-1):
        middle = Dense(nb_unit, kernel_initializer=init, 
                       kernel_constraint=maxnorm(c, axis=0),
                       use_bias=False)(ly)
        middle = BatchNormalization()(middle)
        act = copy(activation)
        act.name = act.name+'_'+str(act_idx)
        act_idx += 1
        middle = act(middle)
        middle = Dropout(dropout_middle)(middle)
        ly = add([ly, middle])
        act = copy(activation)
        act.name = act.name+'_'+str(act_idx)
        act_idx += 1
        ly = act(ly)
        #BN after addition messes up infotmation: (see link below)
        # https://github.com/abhshkdz/papers/blob/master/reviews/identity-mappings-in-deep-residual-networks.md
    # Last layer
    ly = Dense(neurons_last, kernel_initializer=init, 
               kernel_constraint=maxnorm(c, axis=0),
               use_bias=False)(ly)
    ly = BatchNormalization()(ly)
    act_idx += 1
    ly = Activation('linear')(ly)
    ly = Dropout(dropout_last)(ly)
    
    ## Put together input and output
    nn = Model(inputs=inp, outputs=ly)
    
    # Compile
    nn.compile(optimizer=method, loss=loss)
    
    return (nn, nb_unit, act_idx)
In [30]:
'''
Feedforward NN
'''
def fnn_model(data, method, loss='mse', exponent=8, nb_epochs=0, 
              batch_size=128, activation='tanh', layers=4, 
              init='he_uniform', dropout=0.5, dropout_first=None, 
              dropout_middle=None, dropout_last=None,
              neurons_first=None, neurons_last=None, CP=True,
              CP_name=None, **kwargs):
    
#    print activation
    assert(isinstance(activation, string_types))
    
    if activation == 'elu':
        alpha = kwargs.get('alpha',1.0)
        activation = ELU(alpha)
    else:
        activation = Activation(activation)
    
    x_train, x_valid, x_test, y_train, y_valid, y_test = split_dataset(data)
    
    if not(neurons_first):
        neurons_first = x_train.shape[1]
    if not(neurons_last):
        neurons_last = y_train.shape[1]
        
    c = kwargs.get('c', None)
    
    nn, nb_unit, _ = design_model(method, activation, exponent, init, layers, loss,
                                     dropout_first, dropout_middle, dropout_last,
                                     neurons_first, neurons_last, 
                                     weight_constraint=c, **kwargs)
    if nb_epochs > 0:
        callbacks_list = [earlyStopping]
        callbacks_list.append(reduceLR)
        
        if CP:
            filepath = CP_name + '.h5'
            checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, 
                                         save_best_only=True, mode='min')
            callbacks_list.append(checkpoint)
        print('Callbacks: ' + ', '.join([str(cb) for cb in callbacks_list]))
        
        history2 = nn.fit(x_train, y_train, batch_size=batch_size, 
                          epochs=nb_epochs, verbose=2, callbacks=callbacks_list,
                          validation_data=(x_valid, y_valid))

        min_index, min_value = min(enumerate(history2.history['val_loss']), 
                                   key=lambda p: p[1])
        
        print('Min losses - epoch %s and val_loss: %s, training loss: %s'%(
                min_index+1, min_value, history2.history['loss'][min_index]))
        
        history = {'history': history2.history,
                   'params': history2.params}
    else:
        history = {'history': [],
                   'params': []}
    return (x_train, x_valid, x_test, nn, history)
In [31]:
'''
Function to be called to generate the NN-model
'''
def generate_nn(exponent=8, batch_size=512, lr=5e-5, layers=6, loss='mse',
                activation='tanh', prefix='', postfix='', dropout=0.5, 
                dropout_first=None, dropout_middle=None, dropout_last=None, 
                residual_cells=0, **kwargs):
    
    init = kwargs.get('init', 'glorot_uniform')
    c = kwargs.get('c', None)
    fin_model = kwargs.get('fin_model','')
    train_file_name = kwargs.get('train_file_name', 
                                 he_analytic['name'].lower()) 
    check_point = kwargs.get('check_point',True)
    
    callb = partial(fnn_model, exponent=exponent, batch_size=batch_size, 
                    activation=activation, layers=layers, dropout=dropout, 
                    dropout_first=dropout_first, dropout_middle=dropout_middle,
                    dropout_last=dropout_last, lr=lr, c=c, init=init)
    
    model = NeuralNetwork(he_analytic['name'].lower(), 
                          model_callback=callb, train_file_name=train_file_name, 
                          fin_model=fin_model, lr=lr, loss=loss, 
                          re_shape=reshape_fnn, prefix=prefix, 
                          postfix=postfix, checkPoint=check_point)
    return model
In [32]:
#'''
#Functions to save a model to file or load it back
#'''
def write_model(model):
    model_file_name, _ = model.file_names()
    file_name = model_file_name + '.p'
    print('Saving model to file: %s' % file_name)
    dill.dump(model, open(file_name, 'wb'))

#def read_model(file_name):
#    file_name = file_name + '.p'
#    print('Reading model from file: %s' % file_name)
#    model = dill.load(open(file_name, 'rb'))
#    return model

Training of the neural network (called model here)

In the sequel the model is trained over 250 epochs.

In [33]:
model = generate_nn(exponent=8, activation='elu',
                    train_file_name='heston_heston_trial',
                    layers=6, lr=5e-4,
                    prefix='', postfix='',
                    dropout_first=0, dropout_middle=0,
                    dropout_last=0, batch_size=1024, 
                    fin_model='he',c=5, check_point = True)
heston_heston_trial
Reordering seed: 1027 
Current working dir: /scratch/users/jteichma/Desktop/lecture_ML;  file name: /scratch/users/jteichma/Desktop/lecture_ML/heston_heston_trial
Number of rejected samples: 0
16000 2000 2000
--> Force positive input
--> Force positive output
Class NeuralNetwork, __init__ finished.
In [34]:
model.train(nb_epochs=250)
write_model(model)
Train function of class NeuralNetwork called with 250 epochs.
WARNING:tensorflow:From /scratch/users/jteichma/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:1290: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead
Callbacks: <keras.callbacks.EarlyStopping object at 0x7f045b954160>, <keras.callbacks.ReduceLROnPlateau object at 0x7f045b9540f0>, <keras.callbacks.ModelCheckpoint object at 0x7f045ae30e10>
WARNING:tensorflow:From /scratch/users/jteichma/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:1188: calling reduce_sum (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead
Train on 16000 samples, validate on 2000 samples
Epoch 1/250
Epoch 00000: val_loss improved from inf to 0.53167, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
3s - loss: 1.0370 - val_loss: 0.5317
Epoch 2/250
Epoch 00001: val_loss did not improve
1s - loss: 0.7603 - val_loss: 0.6080
Epoch 3/250
Epoch 00002: val_loss improved from 0.53167 to 0.36262, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.7086 - val_loss: 0.3626
Epoch 4/250
Epoch 00003: val_loss improved from 0.36262 to 0.15373, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.6836 - val_loss: 0.1537
Epoch 5/250
Epoch 00004: val_loss improved from 0.15373 to 0.08365, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.6649 - val_loss: 0.0837
Epoch 6/250
Epoch 00005: val_loss improved from 0.08365 to 0.08223, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.6455 - val_loss: 0.0822
Epoch 7/250
Epoch 00006: val_loss did not improve
1s - loss: 0.6281 - val_loss: 0.1074
Epoch 8/250
Epoch 00007: val_loss did not improve
1s - loss: 0.6109 - val_loss: 0.1358
Epoch 9/250
Epoch 00008: val_loss did not improve
1s - loss: 0.5955 - val_loss: 0.1644
Epoch 10/250
Epoch 00009: val_loss did not improve
1s - loss: 0.5782 - val_loss: 0.1877
Epoch 11/250
Epoch 00010: val_loss did not improve
1s - loss: 0.5628 - val_loss: 0.1981
Epoch 12/250
Epoch 00011: val_loss did not improve
1s - loss: 0.5473 - val_loss: 0.2150
Epoch 13/250
Epoch 00012: val_loss did not improve
1s - loss: 0.5321 - val_loss: 0.2200
Epoch 14/250
Epoch 00013: val_loss did not improve
1s - loss: 0.5177 - val_loss: 0.2253
Epoch 15/250
Epoch 00014: val_loss did not improve
1s - loss: 0.5029 - val_loss: 0.2331
Epoch 16/250
Epoch 00015: val_loss did not improve
2s - loss: 0.4888 - val_loss: 0.2328
Epoch 17/250
Epoch 00016: val_loss did not improve
1s - loss: 0.4752 - val_loss: 0.2332
Epoch 18/250
Epoch 00017: val_loss did not improve
1s - loss: 0.4617 - val_loss: 0.2347
Epoch 19/250
Epoch 00018: val_loss did not improve
1s - loss: 0.4488 - val_loss: 0.2331
Epoch 20/250
Epoch 00019: val_loss did not improve
1s - loss: 0.4359 - val_loss: 0.2298
Epoch 21/250
Epoch 00020: val_loss did not improve
1s - loss: 0.4240 - val_loss: 0.2341
Epoch 22/250
Epoch 00021: val_loss did not improve
1s - loss: 0.4114 - val_loss: 0.2245
Epoch 23/250
Epoch 00022: val_loss did not improve
1s - loss: 0.3994 - val_loss: 0.2129
Epoch 24/250
Epoch 00023: val_loss did not improve
1s - loss: 0.3875 - val_loss: 0.2236
Epoch 25/250
Epoch 00024: val_loss did not improve
1s - loss: 0.3760 - val_loss: 0.2179
Epoch 26/250
Epoch 00025: val_loss did not improve
1s - loss: 0.3652 - val_loss: 0.2146
Epoch 27/250
Epoch 00026: val_loss did not improve
1s - loss: 0.3544 - val_loss: 0.2150
Epoch 28/250
Epoch 00027: val_loss did not improve
1s - loss: 0.3438 - val_loss: 0.2151
Epoch 29/250
Epoch 00028: val_loss did not improve
1s - loss: 0.3331 - val_loss: 0.2249
Epoch 30/250
Epoch 00029: val_loss did not improve
1s - loss: 0.3232 - val_loss: 0.2422
Epoch 31/250
Epoch 00030: val_loss did not improve
1s - loss: 0.3133 - val_loss: 0.2215
Epoch 32/250
Epoch 00031: val_loss did not improve
1s - loss: 0.3037 - val_loss: 0.2414
Epoch 33/250
Epoch 00032: val_loss did not improve
1s - loss: 0.2945 - val_loss: 0.2315
Epoch 34/250
Epoch 00033: val_loss did not improve
1s - loss: 0.2851 - val_loss: 0.2153
Epoch 35/250
Epoch 00034: val_loss did not improve
1s - loss: 0.2764 - val_loss: 0.2236
Epoch 36/250
Epoch 00035: val_loss did not improve
1s - loss: 0.2677 - val_loss: 0.2099
Epoch 37/250
Epoch 00036: val_loss did not improve
1s - loss: 0.2589 - val_loss: 0.2064
Epoch 38/250
Epoch 00037: val_loss did not improve
1s - loss: 0.2510 - val_loss: 0.2188
Epoch 39/250
Epoch 00038: val_loss did not improve
1s - loss: 0.2428 - val_loss: 0.2086
Epoch 40/250
Epoch 00039: val_loss did not improve
1s - loss: 0.2349 - val_loss: 0.2078
Epoch 41/250
Epoch 00040: val_loss did not improve
1s - loss: 0.2275 - val_loss: 0.2057
Epoch 42/250
Epoch 00041: val_loss did not improve
1s - loss: 0.2201 - val_loss: 0.1954
Epoch 43/250
Epoch 00042: val_loss did not improve
1s - loss: 0.2127 - val_loss: 0.1954
Epoch 44/250
Epoch 00043: val_loss did not improve
1s - loss: 0.2061 - val_loss: 0.1825
Epoch 45/250
Epoch 00044: val_loss did not improve
1s - loss: 0.1990 - val_loss: 0.1731
Epoch 46/250
Epoch 00045: val_loss did not improve
1s - loss: 0.1923 - val_loss: 0.1829
Epoch 47/250
Epoch 00046: val_loss did not improve
1s - loss: 0.1859 - val_loss: 0.1882
Epoch 48/250
Epoch 00047: val_loss did not improve
1s - loss: 0.1797 - val_loss: 0.1798
Epoch 49/250
Epoch 00048: val_loss did not improve
1s - loss: 0.1735 - val_loss: 0.1579
Epoch 50/250
Epoch 00049: val_loss did not improve
1s - loss: 0.1675 - val_loss: 0.1561
Epoch 51/250
Epoch 00050: val_loss did not improve
1s - loss: 0.1617 - val_loss: 0.1477
Epoch 52/250
Epoch 00051: val_loss did not improve
1s - loss: 0.1561 - val_loss: 0.1476
Epoch 53/250
Epoch 00052: val_loss did not improve
1s - loss: 0.1506 - val_loss: 0.1385
Epoch 54/250
Epoch 00053: val_loss did not improve
1s - loss: 0.1455 - val_loss: 0.1379
Epoch 55/250
Epoch 00054: val_loss did not improve
1s - loss: 0.1405 - val_loss: 0.1326
Epoch 56/250
Epoch 00055: val_loss did not improve
1s - loss: 0.1353 - val_loss: 0.1365
Epoch 57/250
Epoch 00056: val_loss did not improve
1s - loss: 0.1304 - val_loss: 0.1186
Epoch 58/250
Epoch 00057: val_loss did not improve
1s - loss: 0.1258 - val_loss: 0.1209
Epoch 59/250
Epoch 00058: val_loss did not improve
1s - loss: 0.1212 - val_loss: 0.1091
Epoch 60/250
Epoch 00059: val_loss did not improve
1s - loss: 0.1168 - val_loss: 0.1095
Epoch 61/250
Epoch 00060: val_loss did not improve
1s - loss: 0.1126 - val_loss: 0.1013
Epoch 62/250
Epoch 00061: val_loss did not improve
2s - loss: 0.1085 - val_loss: 0.1015
Epoch 63/250
Epoch 00062: val_loss did not improve
1s - loss: 0.1042 - val_loss: 0.0988
Epoch 64/250
Epoch 00063: val_loss did not improve
1s - loss: 0.1004 - val_loss: 0.0977
Epoch 65/250
Epoch 00064: val_loss did not improve
1s - loss: 0.0967 - val_loss: 0.0909
Epoch 66/250
Epoch 00065: val_loss did not improve
1s - loss: 0.0931 - val_loss: 0.0890
Epoch 67/250
Epoch 00066: val_loss did not improve
1s - loss: 0.0896 - val_loss: 0.0824
Epoch 68/250
Epoch 00067: val_loss did not improve
1s - loss: 0.0864 - val_loss: 0.0862
Epoch 69/250
Epoch 00068: val_loss did not improve
1s - loss: 0.0827 - val_loss: 0.0856
Epoch 70/250
Epoch 00069: val_loss improved from 0.08223 to 0.07600, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0797 - val_loss: 0.0760
Epoch 71/250
Epoch 00070: val_loss did not improve
1s - loss: 0.0765 - val_loss: 0.0808
Epoch 72/250
Epoch 00071: val_loss improved from 0.07600 to 0.07405, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0736 - val_loss: 0.0741
Epoch 73/250
Epoch 00072: val_loss improved from 0.07405 to 0.06707, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0706 - val_loss: 0.0671
Epoch 74/250
Epoch 00073: val_loss improved from 0.06707 to 0.06249, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0678 - val_loss: 0.0625
Epoch 75/250
Epoch 00074: val_loss improved from 0.06249 to 0.06224, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0650 - val_loss: 0.0622
Epoch 76/250
Epoch 00075: val_loss improved from 0.06224 to 0.05940, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0626 - val_loss: 0.0594
Epoch 77/250
Epoch 00076: val_loss improved from 0.05940 to 0.05112, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0600 - val_loss: 0.0511
Epoch 78/250
Epoch 00077: val_loss did not improve
1s - loss: 0.0576 - val_loss: 0.0557
Epoch 79/250
Epoch 00078: val_loss did not improve
1s - loss: 0.0550 - val_loss: 0.0565
Epoch 80/250
Epoch 00079: val_loss improved from 0.05112 to 0.04879, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0529 - val_loss: 0.0488
Epoch 81/250
Epoch 00080: val_loss did not improve
1s - loss: 0.0507 - val_loss: 0.0497
Epoch 82/250
Epoch 00081: val_loss improved from 0.04879 to 0.04628, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0487 - val_loss: 0.0463
Epoch 83/250
Epoch 00082: val_loss improved from 0.04628 to 0.04440, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0465 - val_loss: 0.0444
Epoch 84/250
Epoch 00083: val_loss improved from 0.04440 to 0.04168, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0445 - val_loss: 0.0417
Epoch 85/250
Epoch 00084: val_loss improved from 0.04168 to 0.03890, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0426 - val_loss: 0.0389
Epoch 86/250
Epoch 00085: val_loss did not improve
1s - loss: 0.0408 - val_loss: 0.0398
Epoch 87/250
Epoch 00086: val_loss improved from 0.03890 to 0.03654, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0392 - val_loss: 0.0365
Epoch 88/250
Epoch 00087: val_loss improved from 0.03654 to 0.03417, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0375 - val_loss: 0.0342
Epoch 89/250
Epoch 00088: val_loss improved from 0.03417 to 0.03227, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0359 - val_loss: 0.0323
Epoch 90/250
Epoch 00089: val_loss did not improve
2s - loss: 0.0342 - val_loss: 0.0343
Epoch 91/250
Epoch 00090: val_loss improved from 0.03227 to 0.03135, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0327 - val_loss: 0.0313
Epoch 92/250
Epoch 00091: val_loss improved from 0.03135 to 0.03001, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0313 - val_loss: 0.0300
Epoch 93/250
Epoch 00092: val_loss did not improve
1s - loss: 0.0299 - val_loss: 0.0310
Epoch 94/250
Epoch 00093: val_loss improved from 0.03001 to 0.02934, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0285 - val_loss: 0.0293
Epoch 95/250
Epoch 00094: val_loss improved from 0.02934 to 0.02586, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0273 - val_loss: 0.0259
Epoch 96/250
Epoch 00095: val_loss did not improve
1s - loss: 0.0261 - val_loss: 0.0260
Epoch 97/250
Epoch 00096: val_loss improved from 0.02586 to 0.02451, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
2s - loss: 0.0247 - val_loss: 0.0245
Epoch 98/250
Epoch 00097: val_loss improved from 0.02451 to 0.02178, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0238 - val_loss: 0.0218
Epoch 99/250
Epoch 00098: val_loss did not improve
1s - loss: 0.0226 - val_loss: 0.0226
Epoch 100/250
Epoch 00099: val_loss improved from 0.02178 to 0.01993, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0215 - val_loss: 0.0199
Epoch 101/250
Epoch 00100: val_loss improved from 0.01993 to 0.01922, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0206 - val_loss: 0.0192
Epoch 102/250
Epoch 00101: val_loss improved from 0.01922 to 0.01874, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0196 - val_loss: 0.0187
Epoch 103/250
Epoch 00102: val_loss improved from 0.01874 to 0.01566, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0188 - val_loss: 0.0157
Epoch 104/250
Epoch 00103: val_loss did not improve
1s - loss: 0.0177 - val_loss: 0.0159
Epoch 105/250
Epoch 00104: val_loss did not improve
1s - loss: 0.0169 - val_loss: 0.0169
Epoch 106/250
Epoch 00105: val_loss did not improve
1s - loss: 0.0161 - val_loss: 0.0159
Epoch 107/250
Epoch 00106: val_loss improved from 0.01566 to 0.01525, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0152 - val_loss: 0.0152
Epoch 108/250
Epoch 00107: val_loss improved from 0.01525 to 0.01379, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0146 - val_loss: 0.0138
Epoch 109/250
Epoch 00108: val_loss improved from 0.01379 to 0.01359, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0139 - val_loss: 0.0136
Epoch 110/250
Epoch 00109: val_loss improved from 0.01359 to 0.01201, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0131 - val_loss: 0.0120
Epoch 111/250
Epoch 00110: val_loss did not improve
2s - loss: 0.0126 - val_loss: 0.0137
Epoch 112/250
Epoch 00111: val_loss improved from 0.01201 to 0.01126, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
2s - loss: 0.0119 - val_loss: 0.0113
Epoch 113/250
Epoch 00112: val_loss did not improve
1s - loss: 0.0113 - val_loss: 0.0114
Epoch 114/250
Epoch 00113: val_loss improved from 0.01126 to 0.00983, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0108 - val_loss: 0.0098
Epoch 115/250
Epoch 00114: val_loss did not improve
1s - loss: 0.0103 - val_loss: 0.0106
Epoch 116/250
Epoch 00115: val_loss improved from 0.00983 to 0.00838, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0096 - val_loss: 0.0084
Epoch 117/250
Epoch 00116: val_loss did not improve
1s - loss: 0.0091 - val_loss: 0.0088
Epoch 118/250
Epoch 00117: val_loss did not improve
1s - loss: 0.0087 - val_loss: 0.0087
Epoch 119/250
Epoch 00118: val_loss did not improve
1s - loss: 0.0082 - val_loss: 0.0086
Epoch 120/250
Epoch 00119: val_loss did not improve
1s - loss: 0.0079 - val_loss: 0.0084
Epoch 121/250
Epoch 00120: val_loss improved from 0.00838 to 0.00731, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0074 - val_loss: 0.0073
Epoch 122/250
Epoch 00121: val_loss improved from 0.00731 to 0.00658, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0070 - val_loss: 0.0066
Epoch 123/250
Epoch 00122: val_loss improved from 0.00658 to 0.00635, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0066 - val_loss: 0.0063
Epoch 124/250
Epoch 00123: val_loss did not improve
1s - loss: 0.0063 - val_loss: 0.0072
Epoch 125/250
Epoch 00124: val_loss improved from 0.00635 to 0.00605, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0060 - val_loss: 0.0061
Epoch 126/250
Epoch 00125: val_loss improved from 0.00605 to 0.00589, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0056 - val_loss: 0.0059
Epoch 127/250
Epoch 00126: val_loss improved from 0.00589 to 0.00541, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0054 - val_loss: 0.0054
Epoch 128/250
Epoch 00127: val_loss improved from 0.00541 to 0.00507, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0050 - val_loss: 0.0051
Epoch 129/250
Epoch 00128: val_loss improved from 0.00507 to 0.00494, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0048 - val_loss: 0.0049
Epoch 130/250
Epoch 00129: val_loss improved from 0.00494 to 0.00468, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0045 - val_loss: 0.0047
Epoch 131/250
Epoch 00130: val_loss improved from 0.00468 to 0.00386, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0042 - val_loss: 0.0039
Epoch 132/250
Epoch 00131: val_loss did not improve
1s - loss: 0.0042 - val_loss: 0.0050
Epoch 133/250
Epoch 00132: val_loss improved from 0.00386 to 0.00365, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0039 - val_loss: 0.0037
Epoch 134/250
Epoch 00133: val_loss did not improve
1s - loss: 0.0036 - val_loss: 0.0040
Epoch 135/250
Epoch 00134: val_loss did not improve
1s - loss: 0.0034 - val_loss: 0.0039
Epoch 136/250
Epoch 00135: val_loss improved from 0.00365 to 0.00327, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0033 - val_loss: 0.0033
Epoch 137/250
Epoch 00136: val_loss improved from 0.00327 to 0.00315, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0030 - val_loss: 0.0032
Epoch 138/250
Epoch 00137: val_loss improved from 0.00315 to 0.00313, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0028 - val_loss: 0.0031
Epoch 139/250
Epoch 00138: val_loss improved from 0.00313 to 0.00284, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0027 - val_loss: 0.0028
Epoch 140/250
Epoch 00139: val_loss improved from 0.00284 to 0.00259, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0025 - val_loss: 0.0026
Epoch 141/250
Epoch 00140: val_loss improved from 0.00259 to 0.00252, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0024 - val_loss: 0.0025
Epoch 142/250
Epoch 00141: val_loss improved from 0.00252 to 0.00213, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0023 - val_loss: 0.0021
Epoch 143/250
Epoch 00142: val_loss did not improve
1s - loss: 0.0022 - val_loss: 0.0022
Epoch 144/250
Epoch 00143: val_loss did not improve
1s - loss: 0.0020 - val_loss: 0.0022
Epoch 145/250
Epoch 00144: val_loss improved from 0.00213 to 0.00199, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0019 - val_loss: 0.0020
Epoch 146/250
Epoch 00145: val_loss did not improve
1s - loss: 0.0020 - val_loss: 0.0023
Epoch 147/250
Epoch 00146: val_loss improved from 0.00199 to 0.00189, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
2s - loss: 0.0019 - val_loss: 0.0019
Epoch 148/250
Epoch 00147: val_loss improved from 0.00189 to 0.00187, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0017 - val_loss: 0.0019
Epoch 149/250
Epoch 00148: val_loss improved from 0.00187 to 0.00174, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0016 - val_loss: 0.0017
Epoch 150/250
Epoch 00149: val_loss did not improve
1s - loss: 0.0015 - val_loss: 0.0018
Epoch 151/250
Epoch 00150: val_loss improved from 0.00174 to 0.00170, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
2s - loss: 0.0014 - val_loss: 0.0017
Epoch 152/250
Epoch 00151: val_loss improved from 0.00170 to 0.00133, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0014 - val_loss: 0.0013
Epoch 153/250
Epoch 00152: val_loss did not improve
1s - loss: 0.0013 - val_loss: 0.0014
Epoch 154/250
Epoch 00153: val_loss did not improve
1s - loss: 0.0012 - val_loss: 0.0016
Epoch 155/250
Epoch 00154: val_loss improved from 0.00133 to 0.00110, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
2s - loss: 0.0012 - val_loss: 0.0011
Epoch 156/250
Epoch 00155: val_loss did not improve
2s - loss: 0.0011 - val_loss: 0.0012
Epoch 157/250
Epoch 00156: val_loss improved from 0.00110 to 0.00104, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 0.0010 - val_loss: 0.0010
Epoch 158/250
Epoch 00157: val_loss did not improve
1s - loss: 0.0012 - val_loss: 0.0011
Epoch 159/250
Epoch 00158: val_loss improved from 0.00104 to 0.00100, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 9.7100e-04 - val_loss: 9.9995e-04
Epoch 160/250
Epoch 00159: val_loss did not improve
1s - loss: 9.3957e-04 - val_loss: 0.0010
Epoch 161/250
Epoch 00160: val_loss did not improve
1s - loss: 9.3487e-04 - val_loss: 0.0010
Epoch 162/250
Epoch 00161: val_loss did not improve
1s - loss: 7.4279e-04 - val_loss: 0.0011
Epoch 163/250
Epoch 00162: val_loss improved from 0.00100 to 0.00095, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
2s - loss: 8.3039e-04 - val_loss: 9.5398e-04
Epoch 164/250
Epoch 00163: val_loss did not improve
2s - loss: 8.3009e-04 - val_loss: 0.0011
Epoch 165/250
Epoch 00164: val_loss improved from 0.00095 to 0.00082, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
2s - loss: 7.1608e-04 - val_loss: 8.1802e-04
Epoch 166/250
Epoch 00165: val_loss improved from 0.00082 to 0.00082, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
2s - loss: 7.3440e-04 - val_loss: 8.1655e-04
Epoch 167/250
Epoch 00166: val_loss improved from 0.00082 to 0.00079, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
2s - loss: 6.7902e-04 - val_loss: 7.8530e-04
Epoch 168/250
Epoch 00167: val_loss improved from 0.00079 to 0.00066, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 7.0549e-04 - val_loss: 6.5837e-04
Epoch 169/250
Epoch 00168: val_loss did not improve
1s - loss: 5.7814e-04 - val_loss: 6.5973e-04
Epoch 170/250
Epoch 00169: val_loss did not improve
1s - loss: 6.1084e-04 - val_loss: 9.1632e-04
Epoch 171/250
Epoch 00170: val_loss did not improve
1s - loss: 6.1152e-04 - val_loss: 6.6724e-04
Epoch 172/250
Epoch 00171: val_loss did not improve
1s - loss: 4.9364e-04 - val_loss: 6.8600e-04
Epoch 173/250
Epoch 00172: val_loss improved from 0.00066 to 0.00056, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 5.2951e-04 - val_loss: 5.6306e-04
Epoch 174/250
Epoch 00173: val_loss improved from 0.00056 to 0.00052, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 5.6085e-04 - val_loss: 5.1757e-04
Epoch 175/250
Epoch 00174: val_loss improved from 0.00052 to 0.00049, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 5.3428e-04 - val_loss: 4.9002e-04
Epoch 176/250
Epoch 00175: val_loss did not improve
1s - loss: 5.2940e-04 - val_loss: 8.1608e-04
Epoch 177/250
Epoch 00176: val_loss did not improve
1s - loss: 5.2200e-04 - val_loss: 5.0349e-04
Epoch 178/250
Epoch 00177: val_loss did not improve
1s - loss: 4.6295e-04 - val_loss: 4.9540e-04
Epoch 179/250
Epoch 00178: val_loss improved from 0.00049 to 0.00045, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 4.2052e-04 - val_loss: 4.5425e-04
Epoch 180/250
Epoch 00179: val_loss improved from 0.00045 to 0.00043, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
2s - loss: 4.4109e-04 - val_loss: 4.2748e-04
Epoch 181/250
Epoch 00180: val_loss did not improve
2s - loss: 5.0160e-04 - val_loss: 5.5395e-04
Epoch 182/250
Epoch 00181: val_loss did not improve
2s - loss: 4.2380e-04 - val_loss: 4.8937e-04
Epoch 183/250
Epoch 00182: val_loss improved from 0.00043 to 0.00036, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
2s - loss: 4.2791e-04 - val_loss: 3.6065e-04
Epoch 184/250
Epoch 00183: val_loss did not improve
1s - loss: 3.9595e-04 - val_loss: 5.3922e-04
Epoch 185/250
Epoch 00184: val_loss improved from 0.00036 to 0.00035, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 4.3823e-04 - val_loss: 3.4584e-04
Epoch 186/250
Epoch 00185: val_loss improved from 0.00035 to 0.00034, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 3.9804e-04 - val_loss: 3.3831e-04
Epoch 187/250
Epoch 00186: val_loss did not improve
1s - loss: 4.7914e-04 - val_loss: 3.8099e-04
Epoch 188/250
Epoch 00187: val_loss did not improve
1s - loss: 4.1846e-04 - val_loss: 5.7510e-04
Epoch 189/250
Epoch 00188: val_loss improved from 0.00034 to 0.00032, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 3.7700e-04 - val_loss: 3.1848e-04
Epoch 190/250
Epoch 00189: val_loss did not improve
1s - loss: 4.1477e-04 - val_loss: 3.2212e-04
Epoch 191/250
Epoch 00190: val_loss did not improve
1s - loss: 4.2718e-04 - val_loss: 4.3261e-04
Epoch 192/250
Epoch 00191: val_loss did not improve
1s - loss: 2.9078e-04 - val_loss: 5.0140e-04
Epoch 193/250
Epoch 00192: val_loss did not improve
1s - loss: 3.1275e-04 - val_loss: 4.4043e-04
Epoch 194/250
Epoch 00193: val_loss improved from 0.00032 to 0.00026, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 3.5063e-04 - val_loss: 2.6331e-04
Epoch 195/250
Epoch 00194: val_loss did not improve
1s - loss: 3.1492e-04 - val_loss: 4.2203e-04
Epoch 196/250
Epoch 00195: val_loss did not improve
1s - loss: 2.8567e-04 - val_loss: 3.0877e-04
Epoch 197/250
Epoch 00196: val_loss did not improve
1s - loss: 3.8002e-04 - val_loss: 3.0638e-04
Epoch 198/250
Epoch 00197: val_loss improved from 0.00026 to 0.00024, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 3.9044e-04 - val_loss: 2.3723e-04
Epoch 199/250
Epoch 00198: val_loss did not improve
1s - loss: 3.3733e-04 - val_loss: 3.3727e-04
Epoch 200/250
Epoch 00199: val_loss did not improve
2s - loss: 3.1897e-04 - val_loss: 2.9124e-04
Epoch 201/250
Epoch 00200: val_loss improved from 0.00024 to 0.00023, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 4.2054e-04 - val_loss: 2.2892e-04
Epoch 202/250
Epoch 00201: val_loss did not improve
1s - loss: 2.6174e-04 - val_loss: 2.4017e-04
Epoch 203/250
Epoch 00202: val_loss did not improve
1s - loss: 3.2191e-04 - val_loss: 2.3565e-04
Epoch 204/250
Epoch 00203: val_loss did not improve
1s - loss: 3.7933e-04 - val_loss: 3.4225e-04
Epoch 205/250
Epoch 00204: val_loss did not improve
2s - loss: 3.4313e-04 - val_loss: 2.6763e-04
Epoch 206/250
Epoch 00205: val_loss improved from 0.00023 to 0.00022, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 4.0263e-04 - val_loss: 2.1564e-04
Epoch 207/250
Epoch 00206: val_loss did not improve
1s - loss: 3.9355e-04 - val_loss: 2.7532e-04
Epoch 208/250
Epoch 00207: val_loss did not improve
1s - loss: 3.2377e-04 - val_loss: 2.7081e-04
Epoch 209/250
Epoch 00208: val_loss did not improve
1s - loss: 3.0164e-04 - val_loss: 2.7913e-04
Epoch 210/250
Epoch 00209: val_loss did not improve
1s - loss: 3.3155e-04 - val_loss: 2.6646e-04
Epoch 211/250
Epoch 00210: val_loss did not improve
1s - loss: 2.7503e-04 - val_loss: 2.9524e-04
Epoch 212/250
Epoch 00211: val_loss did not improve
1s - loss: 2.7638e-04 - val_loss: 2.4975e-04
Epoch 213/250
Epoch 00212: val_loss improved from 0.00022 to 0.00021, saving model to /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.h5
1s - loss: 3.2735e-04 - val_loss: 2.1446e-04
Epoch 214/250
Epoch 00213: val_loss did not improve
2s - loss: 2.8482e-04 - val_loss: 3.7097e-04
Epoch 215/250
Epoch 00214: val_loss did not improve
2s - loss: 3.2278e-04 - val_loss: 2.2878e-04
Epoch 216/250
Epoch 00215: val_loss did not improve
2s - loss: 3.0686e-04 - val_loss: 3.2429e-04
Epoch 217/250
Epoch 00216: val_loss did not improve
2s - loss: 3.5268e-04 - val_loss: 2.8292e-04
Epoch 218/250
Epoch 00217: val_loss did not improve
2s - loss: 3.9976e-04 - val_loss: 2.2257e-04
Epoch 219/250
Epoch 00218: val_loss did not improve
2s - loss: 3.1366e-04 - val_loss: 3.5199e-04
Epoch 220/250
Epoch 00219: val_loss did not improve
1s - loss: 3.2870e-04 - val_loss: 2.7203e-04
Epoch 221/250
Epoch 00220: val_loss did not improve
1s - loss: 2.6724e-04 - val_loss: 2.2549e-04
Epoch 222/250
Epoch 00221: val_loss did not improve
1s - loss: 3.3657e-04 - val_loss: 2.4261e-04
Epoch 223/250
Epoch 00222: val_loss did not improve
1s - loss: 3.5364e-04 - val_loss: 4.7642e-04
Epoch 224/250
Epoch 00223: val_loss did not improve
1s - loss: 2.7218e-04 - val_loss: 2.2475e-04
Epoch 225/250
Epoch 00224: val_loss did not improve
2s - loss: 3.3290e-04 - val_loss: 2.5707e-04
Epoch 226/250
Epoch 00225: val_loss did not improve
1s - loss: 3.2857e-04 - val_loss: 3.0085e-04
Epoch 227/250
Epoch 00226: val_loss did not improve
2s - loss: 3.4643e-04 - val_loss: 2.6848e-04
Epoch 228/250
Epoch 00227: val_loss did not improve
2s - loss: 3.3717e-04 - val_loss: 3.9585e-04
Epoch 229/250
Epoch 00228: val_loss did not improve
2s - loss: 3.8144e-04 - val_loss: 3.0756e-04
Epoch 230/250
Epoch 00229: val_loss did not improve
2s - loss: 4.0304e-04 - val_loss: 3.0359e-04
Epoch 231/250
Epoch 00230: val_loss did not improve
2s - loss: 3.8084e-04 - val_loss: 3.0691e-04
Epoch 232/250
Epoch 00231: val_loss did not improve
1s - loss: 2.9709e-04 - val_loss: 2.2068e-04
Epoch 233/250
Epoch 00232: val_loss did not improve
1s - loss: 3.4406e-04 - val_loss: 3.9630e-04
Epoch 234/250
Epoch 00233: val_loss did not improve
1s - loss: 3.3661e-04 - val_loss: 2.7291e-04
Epoch 235/250
Epoch 00234: val_loss did not improve
1s - loss: 3.0565e-04 - val_loss: 2.9654e-04
Epoch 236/250
Epoch 00235: val_loss did not improve
1s - loss: 3.3401e-04 - val_loss: 2.5760e-04
Epoch 237/250
Epoch 00236: val_loss did not improve
1s - loss: 3.0354e-04 - val_loss: 2.6158e-04
Epoch 238/250
Epoch 00237: val_loss did not improve
1s - loss: 4.2698e-04 - val_loss: 4.2329e-04
Epoch 239/250
Epoch 00238: val_loss did not improve
1s - loss: 3.3807e-04 - val_loss: 3.0989e-04
Epoch 240/250
Epoch 00239: val_loss did not improve
1s - loss: 3.1237e-04 - val_loss: 4.5860e-04
Epoch 241/250
Epoch 00240: val_loss did not improve
1s - loss: 4.2840e-04 - val_loss: 2.6485e-04
Epoch 242/250
Epoch 00241: val_loss did not improve
1s - loss: 3.2917e-04 - val_loss: 3.8054e-04
Epoch 243/250
Epoch 00242: val_loss did not improve
1s - loss: 3.2360e-04 - val_loss: 3.9150e-04
Epoch 244/250
Epoch 00243: val_loss did not improve
1s - loss: 4.0341e-04 - val_loss: 3.6286e-04
Epoch 245/250
Epoch 00244: val_loss did not improve
1s - loss: 2.7971e-04 - val_loss: 3.0927e-04
Epoch 246/250
Epoch 00245: val_loss did not improve
1s - loss: 3.2873e-04 - val_loss: 2.6477e-04
Epoch 247/250
Epoch 00246: val_loss did not improve
1s - loss: 2.8804e-04 - val_loss: 3.0810e-04
Epoch 248/250
Epoch 00247: val_loss did not improve
1s - loss: 2.9848e-04 - val_loss: 2.6640e-04
Epoch 249/250
Epoch 00248: val_loss did not improve
1s - loss: 4.0529e-04 - val_loss: 3.2349e-04
Epoch 250/250
Epoch 00249: val_loss did not improve
1s - loss: 2.7739e-04 - val_loss: 2.7790e-04
Min losses - epoch 213 and val_loss: 0.00021446336642839015, training loss: 0.00032735323254019024
 
   -- NN on Test set --
1024/2000 [==============>...............] - ETA: 0s0.00030152150616049765
 
Saving model to file: /scratch/users/jteichma/Desktop/lecture_ML/heston_nn.p
I'm being pickled!

This the essential step: the trained neural network is used for the formulation of an inverse problem.

In [35]:
'''
Calibration of the model through the map NN1.
'''
from scipy.optimize import minimize, differential_evolution



def cost_function_calibr_with_NN1(obj, pred_model1, observables, l2=False, **kwargs):
    # obj will be our ivs
    def cost_function(params):
        params = params.flatten().tolist()
        input_params = observables[:]
        input_params.extend(params)
        predicted_obj = (pred_model1.predict(np.array(input_params))).ravel()
#        diff = predicted_obj/obj - 1.0 #relative error
        diff = predicted_obj - obj #absolute error
        if l2:
            return np.sum(diff**2)
        else:
            return np.sum(np.abs(diff))
    return cost_function

def heston_handle_v0_as_param(observables, params):
    v0 = observables[3]
    parameters = params.tolist()
    parameters.insert(0,v0)
    return parameters

def calibration_through_nn1(pred_model1, fin_model, method):
    '''
    Alternative way of parametrizing the process (e.g. Heston) using NN1 as a 
    pricing map.
    '''
    #Dictionaries
    hist_prices_df = load_dictionary(name_dict=fin_model+'_hist_df')
    dates = sorted(hist_prices_df.keys())
    hist_observable = load_dictionary(name_dict=fin_model+'_observable')
    hist_prices = {}
    for k in sorted(hist_prices_df.keys()):
        hist_prices[k] = hist_prices_df[k]['price']
    
    hist_variables = load_dictionary(name_dict=fin_model+'_hist_iv')
    hist_params = load_dictionary(name_dict=fin_model+'_hist_parameters')
    
    cal_params = {}
    max_it = 100
    
    for date in dates:
        print(datetime_to_ql(date))
        target_obj = np.array((hist_variables[date]))
        observables = hist_observable[date]
        observables = observables.flatten().tolist()
        cost_function = cost_function_calibr_with_NN1(target_obj, pred_model1,  
                                        observables, l2=True)
        
        if method == 'slsqp':
            initial_guess = he_mean_as_initial_point
            sol = minimize(cost_function, initial_guess,
                           bounds=he_calibration_bounds, 
                           method='SLSQP', options={'maxiter':max_it})
        elif method == 'diff_ev':
            sol = differential_evolution(func=cost_function,
                                         bounds=he_calibration_bounds, 
                                         maxiter=max_it)
        cal_params[date] = sol.x
        print('Calibrated parameters: %s'%cal_params[date])
        print('Historical parameters: %s'%hist_params[date])
        print('Final value: ', sol.fun)
        print('Iterations: ',sol.nit); print(' ')
        parameters = heston_handle_v0_as_param(observables=observables, params=cal_params[date])
        
    # Saving dictionaries
    name_dict = 'nn1_calibrated_params_he'
    save_dictionary(dictionary=cal_params, name_dict=name_dict)
In [36]:
calibration_through_nn1(pred_model1=model, fin_model='he', method='diff_ev')
January 1st, 2017
Calibrated parameters: [3.72070355 0.12052912 0.40027683 0.0263045 ]
Historical parameters: [2.90732105 0.12593882 0.33576235 0.02830994]
Final value:  0.001315898500304548
Iterations:  37
 
January 2nd, 2017
Calibrated parameters: [10.97253394  0.50655822  2.85816566  0.2630448 ]
Historical parameters: [8.71750427 0.50917857 0.43400964 0.92818448]
Final value:  0.011169824644885027
Iterations:  22
 
January 3rd, 2017
Calibrated parameters: [ 0.81376379  0.46382957  0.20274838 -0.40928435]
Historical parameters: [ 0.9118351   0.44036557  0.28943401 -0.27115573]
Final value:  0.000635455582044444
Iterations:  48
 
January 4th, 2017
Calibrated parameters: [11.17440201  0.05425979  0.23059584 -0.30317411]
Historical parameters: [ 7.56182785  0.053286    0.05666229 -0.86219075]
Final value:  0.0012018907881217221
Iterations:  35
 
January 5th, 2017
Calibrated parameters: [ 9.07791734  0.39392831  0.26682668 -0.76720618]
Historical parameters: [ 6.50268069  0.40280196  0.68358524 -0.23626066]
Final value:  0.0036936308743339653
Iterations:  25
 
In [37]:
dispatch_names = {}
dispatch_names['he'] = 'Heston'

dispatch_string2 = {}
dispatch_string2['he'] = [r'$\kappa = $', r'$\theta = $', r'$\sigma = $', r'$\rho = $']

def params_to_string(obj, params_h, params_c, fin_model):
    obs_str = [r'$S_0 = $', 'ir = ', 'dr = ', r'$v_0 = $']
    obs = [obj[0],obj[1],obj[2],obj[3]]
    obs_str = ["{}{:.6}".format(o, str(v)) for o,v in zip(obs_str, obs)]

    params_str_h = ["{}{:.5}".format(o, str(v)) 
                            for o,v in zip(dispatch_string2[fin_model], params_h)]
    params_str_c = ["{}{:.5}".format(o, str(v)) 
                            for o,v in zip(dispatch_string2[fin_model], params_c)]
    return obs_str, params_str_h, params_str_c


def nn1_calibration_plot(pred_model1, date_index=0):
    
    hist_observable = load_dictionary(name_dict='he_observable')
    dates = sorted(hist_observable.keys())
    date = dates[date_index]
    
    hist_params = load_dictionary(name_dict='he_hist_parameters')
    hist_ivs = load_dictionary(name_dict='he_hist_iv')
    cal_params = load_dictionary(name_dict='nn1_calibrated_params_he')

    obj = hist_observable[date]
    obj = obj.tolist()
    obs_str, params_h_str, params_c_str = params_to_string(obj, 
                hist_params[date].tolist(), cal_params[date].tolist(), 'he')
    nn1_input = np.concatenate((np.array(obj),cal_params[date]))
    W = pred_model1.predict(nn1_input)
    plot_surface(Z=np.array(hist_ivs[date]), z_label='ivs', 
                 main_title=dispatch_names['he']+' - Calibrated IVS with NN1', 
                 string1=', '.join(o for o in obs_str), 
                 string2='Hist params: '+', '.join(o for o in params_h_str), 
                 W=W, string3='Cal params: '+', '.join(o for o in params_c_str))

def nn1_illustration_plot(pred_model1, date_index=0):
    
    hist_observable = load_dictionary(name_dict='he_observable')
    dates = sorted(hist_observable.keys())
    date = dates[date_index]
    
    hist_params = load_dictionary(name_dict='he_hist_parameters')
    hist_ivs = load_dictionary(name_dict='he_hist_iv')
    #cal_params = load_dictionary(name_dict='nn1_calibrated_params_he')

    obj = hist_observable[date]
    obj = obj.tolist()
    obs_str, params_h_str, params_c_str = params_to_string(obj, 
                hist_params[date].tolist(), hist_params[date].tolist(), 'he')
    nn1_input = np.concatenate((np.array(obj),hist_params[date]))
    W = pred_model1.predict(nn1_input)
    plot_surface(Z=np.array(hist_ivs[date]), z_label='ivs', 
                 main_title=dispatch_names['he']+' - Calibrated IVS with NN1', 
                 string1=', '.join(o for o in obs_str), 
                 string2='Hist params: '+', '.join(o for o in params_h_str), 
                 W=W, string3='Hist params: '+', '.join(o for o in params_c_str))

In the sequel we see two plots: the first one compares the real volatility surface (given some Heston) parameters with the one from the trained neural network. The second looks what the inverse problem given by means of the neural network would choose as parameters to reproduce the given volatility surface on the left hand side.

In [38]:
hist_observable = load_dictionary(name_dict='he_observable')
N = len(hist_observable.keys())
for i in range(N):
    nn1_illustration_plot(pred_model1=model, date_index=i)
In [39]:
hist_observable = load_dictionary(name_dict='he_observable')
N = len(hist_observable.keys())
for i in range(N):
    nn1_calibration_plot(pred_model1=model, date_index=i)

The following example shows how this calibration exercise would look in classical Quantlib style, I follow here notes from the great blog entry of Goutham Balaraman.

In [40]:
import QuantLib as ql
from math import pow, sqrt
import numpy as np
from scipy.optimize import root
In [41]:
day_count = ql.Actual365Fixed()
calendar = ql.UnitedStates()
calculation_date = ql.Date(6, 11, 2015)

spot = 659.37
ql.Settings.instance().evaluationDate = calculation_date

risk_free_rate = 0.01
dividend_rate = 0.0
yield_ts = ql.YieldTermStructureHandle(
    ql.FlatForward(calculation_date, risk_free_rate, day_count))
dividend_ts = ql.YieldTermStructureHandle(
    ql.FlatForward(calculation_date, dividend_rate, day_count))
In [42]:
expiration_dates = [ql.Date(6,12,2015), ql.Date(6,1,2016), ql.Date(6,2,2016),
                    ql.Date(6,3,2016), ql.Date(6,4,2016), ql.Date(6,5,2016), 
                    ql.Date(6,6,2016), ql.Date(6,7,2016), ql.Date(6,8,2016),
                    ql.Date(6,9,2016), ql.Date(6,10,2016), ql.Date(6,11,2016), 
                    ql.Date(6,12,2016), ql.Date(6,1,2017), ql.Date(6,2,2017),
                    ql.Date(6,3,2017), ql.Date(6,4,2017), ql.Date(6,5,2017), 
                    ql.Date(6,6,2017), ql.Date(6,7,2017), ql.Date(6,8,2017),
                    ql.Date(6,9,2017), ql.Date(6,10,2017), ql.Date(6,11,2017)]
strikes = [527.50, 560.46, 593.43, 626.40, 659.37, 692.34, 725.31, 758.28]
data = [
[0.37819, 0.34177, 0.30394, 0.27832, 0.26453, 0.25916, 0.25941, 0.26127],
[0.3445, 0.31769, 0.2933, 0.27614, 0.26575, 0.25729, 0.25228, 0.25202],
[0.37419, 0.35372, 0.33729, 0.32492, 0.31601, 0.30883, 0.30036, 0.29568],
[0.37498, 0.35847, 0.34475, 0.33399, 0.32715, 0.31943, 0.31098, 0.30506],
[0.35941, 0.34516, 0.33296, 0.32275, 0.31867, 0.30969, 0.30239, 0.29631],
[0.35521, 0.34242, 0.33154, 0.3219, 0.31948, 0.31096, 0.30424, 0.2984],
[0.35442, 0.34267, 0.33288, 0.32374, 0.32245, 0.31474, 0.30838, 0.30283],
[0.35384, 0.34286, 0.33386, 0.32507, 0.3246, 0.31745, 0.31135, 0.306],
[0.35338, 0.343, 0.33464, 0.32614, 0.3263, 0.31961, 0.31371, 0.30852],
[0.35301, 0.34312, 0.33526, 0.32698, 0.32766, 0.32132, 0.31558, 0.31052],
[0.35272, 0.34322, 0.33574, 0.32765, 0.32873, 0.32267, 0.31705, 0.31209],
[0.35246, 0.3433, 0.33617, 0.32822, 0.32965, 0.32383, 0.31831, 0.31344],
[0.35226, 0.34336, 0.33651, 0.32869, 0.3304, 0.32477, 0.31934, 0.31453],
[0.35207, 0.34342, 0.33681, 0.32911, 0.33106, 0.32561, 0.32025, 0.3155],
[0.35171, 0.34327, 0.33679, 0.32931, 0.3319, 0.32665, 0.32139, 0.31675],
[0.35128, 0.343, 0.33658, 0.32937, 0.33276, 0.32769, 0.32255, 0.31802],
[0.35086, 0.34274, 0.33637, 0.32943, 0.3336, 0.32872, 0.32368, 0.31927],
[0.35049, 0.34252, 0.33618, 0.32948, 0.33432, 0.32959, 0.32465, 0.32034],
[0.35016, 0.34231, 0.33602, 0.32953, 0.33498, 0.3304, 0.32554, 0.32132],
[0.34986, 0.34213, 0.33587, 0.32957, 0.33556, 0.3311, 0.32631, 0.32217],
[0.34959, 0.34196, 0.33573, 0.32961, 0.3361, 0.33176, 0.32704, 0.32296],
[0.34934, 0.34181, 0.33561, 0.32964, 0.33658, 0.33235, 0.32769, 0.32368],
[0.34912, 0.34167, 0.3355, 0.32967, 0.33701, 0.33288, 0.32827, 0.32432],
[0.34891, 0.34154, 0.33539, 0.3297, 0.33742, 0.33337, 0.32881, 0.32492]]
In [82]:
def setup_helpers(engine, expiration_dates, strikes, 
                  data, ref_date, spot, yield_ts, 
                  dividend_ts):
    heston_helpers = []
    grid_data = []
    for i, date in enumerate(expiration_dates):
        for j, s in enumerate(strikes):
            t = (date - ref_date )
            p = ql.Period(t, ql.Days)
            vols = data[i][j]
            helper = ql.HestonModelHelper(
                p, calendar, spot, s, 
                ql.QuoteHandle(ql.SimpleQuote(vols)),
                yield_ts, dividend_ts)
            helper.setPricingEngine(engine)
            heston_helpers.append(helper)
            grid_data.append((date, s))
    return heston_helpers, grid_data

def cost_function_generator(model, helpers,norm=False):
    def cost_function(params):
        params_ = ql.Array(list(params))
        model.setParams(params_)
        error = [h.calibrationError() for h in helpers]
        if norm:
            return np.sqrt(np.sum(np.abs(error)))
        else:
            return error
    return cost_function

def calibration_report(helpers, grid_data, detailed=False):
    avg = 0.0
    if detailed:
        print("%15s %25s %15s %15s %20s" % (
            "Strikes", "Expiry", "Market Value", 
             "Model Value", "Relative Error (%)"))
        print("="*100)
    for i, opt in enumerate(helpers):
        err = (opt.modelValue()/opt.marketValue() - 1.0)
        date,strike = grid_data[i]
        if detailed:
            print("%15.2f %25s %14.5f %15.5f %20.7f " % (
                strike, str(date), opt.marketValue(), 
                opt.modelValue(), 
                100.0*(opt.modelValue()/opt.marketValue() - 1.0)))
        avg += abs(err)
    avg = avg*100.0/len(helpers)
    if detailed: print("-"*100)
    summary = "Average Abs Error (%%) : %5.9f" % (avg)
    print(summary)
    return avg
    
def setup_model(_yield_ts, _dividend_ts, _spot, 
                init_condition=(0.02,0.2,0.5,0.1,0.01)):
    theta, kappa, sigma, rho, v0 = init_condition
    process = ql.HestonProcess(_yield_ts, _dividend_ts, 
                           ql.QuoteHandle(ql.SimpleQuote(_spot)), 
                           v0, kappa, theta, sigma, rho)
    model = ql.HestonModel(process)
    engine = ql.AnalyticHestonEngine(model) 
    return model, engine
summary= []
In [83]:
#theta, kappa, sigma, rho, v0 = (0.02, 0.2, 0.5, 0.1, 0.01)
#theta, kappa, sigma, rho, v0 = (0.07, 0.5, 0.1, 0.1, 0.1)
In [84]:
model1, engine1 = setup_model(
    yield_ts, dividend_ts, spot, 
    init_condition=(0.02,0.2,0.5,0.1,0.01))
heston_helpers1, grid_data1 = setup_helpers(
    engine1, expiration_dates, strikes, data, 
    calculation_date, spot, yield_ts, dividend_ts
)
initial_condition = list(model1.params())
In [85]:
%%time
lm = ql.LevenbergMarquardt(1e-8, 1e-8, 1e-8)
model1.calibrate(heston_helpers1, lm, 
                 ql.EndCriteria(500, 300, 1.0e-8,1.0e-8, 1.0e-8))
theta, kappa, sigma, rho, v0 = model1.params()
print("theta = %f, kappa = %f, sigma = %f, rho = %f, v0 = %f" %
      (theta, kappa, sigma, rho, v0))
error = calibration_report(heston_helpers1, grid_data1)
summary.append(["QL LM1", error] + list(model1.params()))
theta = 0.125748, kappa = 7.915000, sigma = 1.887854, rho = -0.364942, v0 = 0.055397
Average Abs Error (%) : 3.015268051
CPU times: user 2.37 s, sys: 0 ns, total: 2.37 s
Wall time: 2.37 s
In [86]:
model1, engine1 = setup_model(
    yield_ts, dividend_ts, spot, 
    init_condition=(0.07,0.5,0.1,0.1,0.1))
heston_helpers1, grid_data1 = setup_helpers(
    engine1, expiration_dates, strikes, data, 
    calculation_date, spot, yield_ts, dividend_ts
)
initial_condition = list(model1.params())
In [87]:
%%time
lm = ql.LevenbergMarquardt(1e-8, 1e-8, 1e-8)
model1.calibrate(heston_helpers1, lm, 
                 ql.EndCriteria(500, 300, 1.0e-8,1.0e-8, 1.0e-8))
theta, kappa, sigma, rho, v0 = model1.params()
print("theta = %f, kappa = %f, sigma = %f, rho = %f, v0 = %f" % \
    (theta, kappa, sigma, rho, v0))
error = calibration_report(heston_helpers1, grid_data1)
summary.append(["QL LM2", error] + list(model1.params()))
theta = 0.084523, kappa = 0.000000, sigma = 0.132289, rho = -0.514278, v0 = 0.099928
Average Abs Error (%) : 11.007433024
CPU times: user 2.38 s, sys: 0 ns, total: 2.38 s
Wall time: 2.38 s
In [88]:
model2, engine2 = setup_model(
    yield_ts, dividend_ts, spot, 
    init_condition=(0.02,0.2,0.5,0.1,0.01))
heston_helpers2, grid_data2 = setup_helpers(
    engine2, expiration_dates, strikes, data,
    calculation_date, spot, yield_ts, dividend_ts
)
initial_condition = list(model2.params())
In [89]:
%%time
cost_function = cost_function_generator(model2, heston_helpers2)
sol = root(cost_function, initial_condition, method='lm')
theta, kappa, sigma, rho, v0 = model2.params()
print("theta = %f, kappa = %f, sigma = %f, rho = %f, v0 = %f" % \
    (theta, kappa, sigma, rho, v0))
error = calibration_report(heston_helpers2, grid_data2)
summary.append(["Scipy LM1", error] + list(model2.params()))
theta = 0.125747, kappa = 7.915687, sigma = 1.887934, rho = -0.364944, v0 = 0.055394
Average Abs Error (%) : 3.015252651
CPU times: user 2.33 s, sys: 0 ns, total: 2.33 s
Wall time: 2.33 s
In [90]:
model2, engine2 = setup_model(
    yield_ts, dividend_ts, spot,
    init_condition=(0.07,0.5,0.1,0.1,0.1))
heston_helpers2, grid_data2 = setup_helpers(
    engine2, expiration_dates, strikes, data,
    calculation_date, spot, yield_ts, dividend_ts
)
initial_condition = list(model2.params())
In [91]:
%%time
cost_function = cost_function_generator(model2, heston_helpers2)
sol = root(cost_function, initial_condition, method='lm')
theta, kappa, sigma, rho, v0 = model2.params()
print("theta = %f, kappa = %f, sigma = %f, rho = %f, v0 = %f" % \
    (theta, kappa, sigma, rho, v0))
error = calibration_report(heston_helpers2, grid_data2)
summary.append(["Scipy LM2", error] + list(model2.params()))
theta = 0.048184, kappa = -0.548903, sigma = 0.197958, rho = -0.999547, v0 = 0.090571
Average Abs Error (%) : 7.019499509
CPU times: user 15.1 s, sys: 0 ns, total: 15.1 s
Wall time: 15.1 s
In [92]:
from scipy.optimize import least_squares

model3, engine3 = setup_model(
    yield_ts, dividend_ts, spot, 
    init_condition=(0.02,0.2,0.5,0.1,0.01))
heston_helpers3, grid_data3 = setup_helpers(
    engine3, expiration_dates, strikes, data,
    calculation_date, spot, yield_ts, dividend_ts
)
initial_condition = list(model3.params())
In [93]:
%%time
cost_function = cost_function_generator(model3, heston_helpers3)
sol = least_squares(cost_function, initial_condition)
theta, kappa, sigma, rho, v0 = model3.params()
print("theta = %f, kappa = %f, sigma = %f, rho = %f, v0 = %f" % \
    (theta, kappa, sigma, rho, v0))
error = calibration_report(heston_helpers3, grid_data3)
summary.append(["Scipy LS1", error] + list(model3.params()))
theta = 0.125748, kappa = 7.915255, sigma = 1.887887, rho = -0.364942, v0 = 0.055396
Average Abs Error (%) : 3.015263920
CPU times: user 3.64 s, sys: 0 ns, total: 3.64 s
Wall time: 3.65 s
In [94]:
model3, engine3 = setup_model(
    yield_ts, dividend_ts, spot, 
    init_condition=(0.07,0.5,0.1,0.1,0.1))
heston_helpers3, grid_data3 = setup_helpers(
    engine3, expiration_dates, strikes, data,
    calculation_date, spot, yield_ts, dividend_ts
)
initial_condition = list(model3.params())
In [95]:
%%time
cost_function = cost_function_generator(model3, heston_helpers3)
sol = least_squares(cost_function, initial_condition)
theta, kappa, sigma, rho, v0 = model3.params()
print("theta = %f, kappa = %f, sigma = %f, rho = %f, v0 = %f" % \
    (theta, kappa, sigma, rho, v0))
error = calibration_report(heston_helpers3, grid_data3)
summary.append(["Scipy LS2", error] + list(model3.params()))
theta = 3.134502, kappa = 0.000005, sigma = -0.000245, rho = -0.000010, v0 = 1.596768
Average Abs Error (%) : 5.096409898
CPU times: user 17.9 s, sys: 0 ns, total: 17.9 s
Wall time: 17.9 s
In [62]:
from scipy.optimize import differential_evolution

model4, engine4 = setup_model(yield_ts, dividend_ts, spot)
heston_helpers4, grid_data4 = setup_helpers(
    engine4, expiration_dates, strikes, data,
    calculation_date, spot, yield_ts, dividend_ts
)
initial_condition = list(model4.params())
bounds = [(0,1),(0.01,15), (0.01,1.), (-1,1), (0,1.0) ]
In [63]:
%%time
cost_function = cost_function_generator(
    model4, heston_helpers4, norm=True)
sol = differential_evolution(cost_function, bounds, maxiter=100)
theta, kappa, sigma, rho, v0 = model4.params()
print("theta = %f, kappa = %f, sigma = %f, rho = %f, v0 = %f" % \
    (theta, kappa, sigma, rho, v0))
error = calibration_report(heston_helpers4, grid_data4)
summary.append(["Scipy DE1", error] + list(model4.params()))
theta = 0.122699, kappa = 5.318954, sigma = 0.999916, rho = -0.577105, v0 = 0.079550
Average Abs Error (%) : 2.854176631
CPU times: user 1min 31s, sys: 0 ns, total: 1min 31s
Wall time: 1min 31s
In [64]:
from scipy.optimize import differential_evolution

model4, engine4 = setup_model(yield_ts, dividend_ts, spot)
heston_helpers4, grid_data4 = setup_helpers(
    engine4, expiration_dates, strikes, data,
    calculation_date, spot, yield_ts, dividend_ts
)
initial_condition = list(model4.params())
bounds = [(0,1),(0.01,15), (0.01,1.), (-1,1), (0,1.0) ]
In [65]:
%%time
cost_function = cost_function_generator(
    model4, heston_helpers4, norm=True)
sol = differential_evolution(cost_function, bounds, maxiter=100)
theta, kappa, sigma, rho, v0 = model4.params()
print("theta = %f, kappa = %f, sigma = %f, rho = %f, v0 = %f" % \
    (theta, kappa, sigma, rho, v0))
error = calibration_report(heston_helpers4, grid_data4)
summary.append(["Scipy DE1", error] + list(model4.params()))
theta = 0.122040, kappa = 4.480588, sigma = 0.661518, rho = -0.736864, v0 = 0.078674
Average Abs Error (%) : 2.893280499
CPU times: user 1min 13s, sys: 0 ns, total: 1min 13s
Wall time: 1min 14s
In [66]:
from scipy.optimize import basinhopping

class MyBounds(object):
     def __init__(self, xmin=[0.,0.01,0.01,-1,0], xmax=[1,15,1,1,1.0] ):
         self.xmax = np.array(xmax)
         self.xmin = np.array(xmin)
     def __call__(self, **kwargs):
         x = kwargs["x_new"]
         tmax = bool(np.all(x <= self.xmax))
         tmin = bool(np.all(x >= self.xmin))
         return tmax and tmin
bounds = [(0,1),(0.01,15), (0.01,1.), (-1,1), (0,1.0) ]
In [67]:
model5, engine5 = setup_model(
    yield_ts, dividend_ts, spot,
    init_condition=(0.02,0.2,0.5,0.1,0.01))
heston_helpers5, grid_data5 = setup_helpers(
    engine5, expiration_dates, strikes, data,
    calculation_date, spot, yield_ts, dividend_ts
)
initial_condition = list(model5.params())
In [68]:
%%time
mybound = MyBounds()
minimizer_kwargs = {"method": "L-BFGS-B", "bounds": bounds }
cost_function = cost_function_generator(
    model5, heston_helpers5, norm=True)
sol = basinhopping(cost_function, initial_condition, niter=5,
                   minimizer_kwargs=minimizer_kwargs,
                   stepsize=0.005,
                   accept_test=mybound,
                   interval=10)
theta, kappa, sigma, rho, v0 = model5.params()
print("theta = %f, kappa = %f, sigma = %f, rho = %f, v0 = %f" % \
    (theta, kappa, sigma, rho, v0))
error = calibration_report(heston_helpers5, grid_data5)
summary.append(["Scipy BH1", error] + list(model5.params()))
theta = 0.123293, kappa = 5.126383, sigma = 0.994797, rho = -0.558719, v0 = 0.078612
Average Abs Error (%) : 2.851924537
CPU times: user 1min 9s, sys: 0 ns, total: 1min 9s
Wall time: 1min 9s
In [69]:
model5, engine5 = setup_model(
    yield_ts, dividend_ts, spot,
    init_condition=(0.07,0.5,0.1,0.1,0.1))
heston_helpers5, grid_data5 = setup_helpers(
    engine5, expiration_dates, strikes, data,
    calculation_date, spot, yield_ts, dividend_ts
)
initial_condition = list(model5.params())
In [70]:
%%time
mybound = MyBounds()
minimizer_kwargs = {"method": "L-BFGS-B", "bounds": bounds}
cost_function = cost_function_generator(
    model5, heston_helpers5, norm=True)
sol = basinhopping(cost_function, initial_condition, niter=5,
                   minimizer_kwargs=minimizer_kwargs,
                   stepsize=0.005,
                   accept_test=mybound,
                   interval=10)
theta, kappa, sigma, rho, v0 = model5.params()
print("theta = %f, kappa = %f, sigma = %f, rho = %f, v0 = %f" % \
    (theta, kappa, sigma, rho, v0))
error = calibration_report(heston_helpers5, grid_data5)
summary.append(["Scipy BH2", error] + list(model5.params()))
theta = 0.123174, kappa = 5.164211, sigma = 0.993950, rho = -0.561678, v0 = 0.078687
Average Abs Error (%) : 2.852014730
CPU times: user 1min 22s, sys: 0 ns, total: 1min 22s
Wall time: 1min 22s
In [96]:
from pandas import DataFrame
DataFrame(
    summary,
    columns=["Name", "Avg Abs Error","Theta", "Kappa", "Sigma", "Rho", "V0"],
    index=['']*len(summary))
Out[96]:
Name Avg Abs Error Theta Kappa Sigma Rho V0
QL LM1 3.015268 0.125748 7.915000e+00 1.887854 -0.364942 0.055397
QL LM2 11.007433 0.084523 1.625740e-08 0.132289 -0.514278 0.099928
Scipy LM1 3.015253 0.125747 7.915687e+00 1.887934 -0.364944 0.055394
Scipy LM2 7.019500 0.048184 -5.489029e-01 0.197958 -0.999547 0.090571
Scipy LS1 3.015264 0.125748 7.915255e+00 1.887887 -0.364942 0.055396
Scipy LS2 5.096410 3.134502 4.903959e-06 -0.000245 -0.000010 1.596768
In [73]:
print(theta, kappa, sigma, rho, v0)
0.12317361519594697 5.164210537890706 0.9939499200746806 -0.5616784083137235 0.07868677127642666