2

Looking at some code I grabbed from https://www.quantopian.com/posts/python-black-and-scholes-pde-finite-difference-method - Only thing is I'm not sure why the graph isn't showing up. I'm using Canopy. Code:

import math
import numpy as np
import pandas as pd
from scipy import stats
import matplotlib as plt

T = 1  #Time to Expiry in Years
E = 100  #Strike
r = .05  #Risk Free Rate
SIGMA = .20  #Volatility
Type = True   #Type of Option True=Call False=Put
NAS = 40  #Number of Asset Steps - Higher is more accurate, but more time consuming

ds = 2 * E / NAS  #Asset Value Step Size
dt = (0.9/NAS/NAS/SIGMA/SIGMA)  #Time Step Size
NTS = int(T / dt) + 1  #Number of 

value_matrix = np.zeros((int(NAS+1), int(NTS)))
asset_price = np.arange(NAS*ds,-1,-ds)
value_matrix[:,-1]= np.maximum(asset_price - E,0)

for x in range(1,NTS):
    value_matrix[-1,-x-1] = value_matrix[-1,-x]* math.exp(-r*dt)
for x in range(1,int(NTS)):
    for y in range(1,int(NAS)):
        #Evaluate Option Greeks
        Delta = (value_matrix[y-1,-x] - value_matrix[y+1,-x]) / 2 / ds
        value_matrix[y+1,-x]
        Gamma = (value_matrix[y-1,-x] - (2 * value_matrix[y,-x]) + value_matrix[y+1,-x]) / ds / ds
        Theta = (-.5 * SIGMA**2 * asset_price[y]**2 * Gamma) - (r * asset_price[y] * Delta) + (r * value_matrix[y,-x])
        value_matrix[y,-x-1] = value_matrix[y,-x] - Theta * dt
        value_matrix[0,-x-1] = 2 * value_matrix[1,-x-1] - value_matrix[2,-x-1]
value_df = pd.DataFrame(value_matrix)
value_df = value_df.set_index(asset_price)
print(value_df[0])

plot_df = value_df.sort_index(ascending=True)
plot_df[0].plot()
plot_df[NTS-1].plot()
1
  • works for me in ipython. maybe tried adding a plt.show()? Commented Apr 24, 2018 at 20:17

3 Answers 3

3

You have imported matplotlib itself as plt. This is not commonly done. Instead you want to import matplotlib.pyplot. This then allows you to do all the conventional plotting in matplotlib such as plt.plot() etc., in addition to showing any figures that have been generated using pandas plotting functionality which is what seems to be being used in the question

The solution to your problem is to add plt.show() at the end of your code using the correct module imports at the top of the code:

import matplotlib
import matplotlib.pyplot as plt

# the rest of your code

plt.show()
Sign up to request clarification or add additional context in comments.

Comments

1
%matplotlib inline

If you're using jupyter notebook, you need to add the code above.

2 Comments

I'm not using iPython
Missed the Canopy part. My apologies.
1

When importing matplotlib in python you want to use the pyplot submodule. So you're going to want to change your import statement to

import matplotlib.pyplot as plt

and then to view what is plotted you have to add

plt.show()

to the bottom of your code

2 Comments

If I do that I get an error - 'module' object has no attribute 'show'
@undergrad Your import is also wrong... replace it with import matplotlib.pyplot as plt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.