Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to graphically represent this function?
Hi everyone,

I've tried multiple times to represent this function in Python, but I am still unsuccessful.

This is the function:
def CON_call_price(spot,strike,tau,sigma,r,q,phi,x):
    d1 = (log(spot/strike)+(r-q+0.5*sigma**2)*tau)/(sigma*sqrt(tau))
    d2 = (d1-sigma*sqrt(tau))
    y = x*exp(-r*tau)*norm.cdf(d2)
    return y
And this is how I tried to represent it:
from math import exp, log, sqrt
from scipy.stats import norm
import matplotlib.pyplot as plt'ggplot')
plt.rcParams['xtick.labelsize'] = 14
plt.rcParams['ytick.labelsize'] = 14
plt.rcParams['figure.titlesize'] = 18
plt.rcParams['figure.titleweight'] = 'medium'
plt.rcParams['lines.linewidth'] = 2.5

def CON_call_price(spot,strike,tau,sigma,r,q,phi,x):
    d1 = (log(spot/strike)+(r-q+0.5*sigma**2)*tau)/(sigma*sqrt(tau))
    d2 = (d1-sigma*sqrt(tau))
    y = x*exp(-r*tau)*norm.cdf(d2)
    return y

    P = list(map(lambda x:  x if spot > strike else 0))
    return P

S = [t/5 for t in range(0,1000)] 

fig, ax = plt.subplots(nrows=1, ncols=1, sharex=True, sharey=True, figsize = (20,15))
fig.suptitle('Payoff Function for CON CALL', fontsize=20, fontweight='bold')

bc_P = CON_call_price(120,100, 1, 0.25, 0.2, 0.03, 1, 50)
plt.plot(S, bc_P, 'b')
plt.legend(["Binary Call"])
This is the result I get:
File "<ipython-input-41-b68d66dfa3e5>", line 1, in <module> runfile('C:/Users/Admin/Desktop/', wdir='C:/Users/Admin/Desktop') File "C:\Users\Admin\Anaconda3\lib\site-packages\spyder_kernels\customize\", line 704, in runfile execfile(filename, namespace) File "C:\Users\Admin\Anaconda3\lib\site-packages\spyder_kernels\customize\", line 108, in execfile exec(compile(, filename, 'exec'), namespace) File "C:/Users/Admin/Desktop/", line 30, in <module> plt.plot(S, bc_P, 'b') File "C:\Users\Admin\Anaconda3\lib\site-packages\matplotlib\", line 2813, in plot is not None else {}), **kwargs) File "C:\Users\Admin\Anaconda3\lib\site-packages\matplotlib\", line 1810, in inner return func(ax, *args, **kwargs) File "C:\Users\Admin\Anaconda3\lib\site-packages\matplotlib\axes\", line 1611, in plot for line in self._get_lines(*args, **kwargs): File "C:\Users\Admin\Anaconda3\lib\site-packages\matplotlib\axes\", line 393, in _grab_next_args yield from self._plot_args(this, kwargs) File "C:\Users\Admin\Anaconda3\lib\site-packages\matplotlib\axes\", line 370, in _plot_args x, y = self._xy_from_xy(x, y) File "C:\Users\Admin\Anaconda3\lib\site-packages\matplotlib\axes\", line 231, in _xy_from_xy "have shapes {} and {}".format(x.shape, y.shape)) ValueError: x and y must have same first dimension, but have shapes (1000,) and (1,) 
I also get a figure, but no graph in it.

I've seen some plot functions, but for some reason I can't get this to work, unfortunately.

Thank you in advance.
You need to compute values of your function for all values in S. Just replace line #28 in your code with the following:

bc_P = [CON_call_price(120,100, 1, 0.25, 0.2, 0.03, 1, s) for s in S]

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)