1

For my thesis I need the implied volatility of options, I already created the following function for it:

#Implied volatility solver
def Implied_Vol_Solver(s_t,K,t,r_f,option,step_size):
#s_t=Current stock price, K=Strike price, t=time until maturity, r_f=risk-free rate and option=option price,stepsize=is precision in stepsizes
    #sigma set equal to steps to make a step siz equal to the starting point
    sigma=step_size
    while sigma < 1:
        #Regualar BlackScholes formula (current only call option, will also be used to calculate put options)
        d_1=(np.log(s_t/K)+(r_f+(sigma**2)/2)*t)/(sigma*(np.sqrt(t)))
        d_2=d_1-np.square(t)*sigma
        P_implied=s_t*norm.cdf(d_1)-K*np.exp(-r_f*t)*norm.cdf(d_2)
        if option-(P_implied)<step_size:
            #convert stepts to a string to find the decimal point (couldn't be done with a float)
            step_size=str(step_size)
            #rounds sigma equal to the stepsize
            return round(sigma,step_size[::-1].find('.'))
        sigma+=step_size
    return "Could not find the right volatility"

The variables I need are located in a Pandas DataFrame and I already created a loop for it, to test if it works (I will add the other variables when it works correctly):

for x in df_option_data['Settlement_Price']:
    df_option_data['Implied_Volatility']=Implied_Vol_Solver(100,100,1,0.01,x,0.001)

However when I run this loop I will get 0.539 for the whole Implied_Voltality Column and these numbers needs to be different, what do I wrong? Or are there any easier solutions?

I also tried the following:

df_option_data['Implied_Volatility']=Implied_Vol_Solver(100,100,1,0.01,np.array(df_option_data['Settlement_Price']),0.001)

But than I get the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Essentially what I need is the following: A dataframe with 5 columns for the input variables and 1 column with the output variables (the implied volatility) which is calculated by the function.

10uss
  • 47
  • 6

2 Answers2

1

You are replacing the result from Implied_Vol_Solver to the entire column instead of a specific cell.

Try the following:

df_option_data['Implied_Volatility'] = df_option_data['Settlement_Price'].apply(lambda x: Implied_Vol_Solver(100,100,1,0.01,x,0.001))

The apply function can apply a function to all the elements in a data column so that you don't need to do the for loop yourself.

ATK7474
  • 339
  • 2
  • 12
TYZ
  • 8,466
  • 5
  • 29
  • 60
  • First off all thanks for your quick responses, I understand ATK7474 solution but yours is easier to apply and maintain. One small question, the other variables also need to change, how can I do this Lambda function with multiple arguments? Currently I only can adjust one variable but all five need to change (variable in the same row but a different column). – 10uss Apr 17 '19 at 16:25
  • @10uss You can take a look at: https://stackoverflow.com/questions/39814416/pandas-apply-with-args-which-are-dataframe-row-entries – TYZ Apr 17 '19 at 16:38
0

Instead of having the input variables passed into the function, you could pass in the row (as a series) and pluck off the values from that. Then, use an apply function to get your output frame. This would look something like this:

def Implied_Vol_Solver(row):
    s_t = row['s_t']  # or whatever the column is called in the dataframe
    k = row['k']  # and so on and then leave the rest of your logic as is

Once you've modified the function, you can use it like this:

df_option_data['Implied_Volatility'] = df_option_data.apply(Implied_Vol_Solver, axis=1)
ATK7474
  • 339
  • 2
  • 12