-
Black-Scholes 2
-
Lecture1.1
-
Lecture1.2
-
-
Binomial Model 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Monte Carlo 3
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Binomial Model Part 1
In this part of the course we will re-create the binomial model for use in an interactive dashboard. The first step is creating widgets we will use to pick the binomial tree parameters. In this case we are using a couple of numeric fields but will also have our time increment field be a dropdown for picking between days, months, quarters or years. This will be converted in the formula to the correct numeric value. If we wrap it into a function it will make life easier when we want to recreate the widgets with just a line.
from ipywidgets import widgets, interact, Button, HBox, VBox
from IPython.display import display
def create_simulation_parameters_box():
#Start with an empty list for the simulations parameter box
simulation_parameters_box = []
#Stock price
simulation_parameters_box.append(widgets.IntSlider(
value=50,
min=1,
max=100,
step=1,
description='Stock Price:',
readout_format='.1f',
))
#Volatility (sigma) in the binomial tree
simulation_parameters_box.append(widgets.FloatSlider(
value=.08,
min=.01,
max=.80,
step=.01,
description='Volatility:',
readout_format='.1%',
))
#Risk-free rate
simulation_parameters_box.append(widgets.FloatSlider(
value=.04,
min=.01,
max=.30,
step=.01,
description='Risk-free Rate:',
readout_format='.1%',
))
#Number of time steps
simulation_parameters_box.append(widgets.IntSlider(
value=4,
min=1,
max=12,
step=1,
description='Number of Periods:',
readout_format='.0f',
))
#Time increment
simulation_parameters_box.append(widgets.Dropdown(
options=['Trading Day', 'Month', 'Quarter', 'Year'],
value='Month',
description='Time Increment:'))
#Turn the list to a vertical box
simulation_parameters_box = VBox(simulation_parameters_box)
return simulation_parameters_box
simulation_parameters_box = create_simulation_parameters_box()
display(simulation_parameters_box)
Now, close down the widgets.
simulation_parameters_box.close()
The next thing we want is a function that makes parameters for an option. It will let us pick the strike price, whether it is a put or call, and if we are long or short.
def create_option_parameters_box():
#Start with an empty list for the option parameter box
option_parameters_box = []
#Strike price
option_parameters_box.append(widgets.IntSlider(
value=50,
min=1,
max=100,
step=1,
description='Strike Price:',
readout_format='.1f',
))
#Option Type
option_parameters_box.append(widgets.Dropdown(
options=['Call', 'Put'],
value='Call',
description='Option Type:'))
#Either buying the option or selling the option
option_parameters_box.append(widgets.Dropdown(
options=['Long', 'Short'],
value='Long',
description='Side:'))
#Turn the list to a vertical box
option_parameters_box = VBox(option_parameters_box)
return option_parameters_box
option_parameters_box = create_option_parameters_box()
display(option_parameters_box)
Close.
option_parameters_box.close()
Put these two functions together to build the master set of widgets. Using layouts we can make it so there are two columns, one for each set.
def create_widgets():
#Create the two vertical boxes
simulation_parameters_box = create_simulation_parameters_box()
option_parameters_box = create_option_parameters_box()
#Put them together with a horizontal box
return HBox([simulation_parameters_box, option_parameters_box])
widget_box = create_widgets()
display(widget_box)
If we define a dummy function to just print some of the values we can make sure that we are correctly linking the widget to the function. We are going to use interactive output so that we can display the output and widgets as their own parts. As well, we are going to see an alternative way to refer to each widget within the layouts. Our widget box has an attribute children which points to each underlying widget. Because we have two VBoxs in an HBox, we can first index the children for 0 or 1 to decide the column, then index that child’s children to find each component within. Note how each variable in the function is matched to a child in the dictionary below.
#Build a function for valuation
#First fill with code to ensure that the connections work
from ipywidgets import interactive_output
def valuation(S, sigma, rf, T, T_increment, X1, O1, Side1):
print(S, sigma)
output_object = interactive_output(valuation,{"S":widget_box.children[0].children[0],
"sigma":widget_box.children[0].children[1],
"rf":widget_box.children[0].children[2],
"T":widget_box.children[0].children[3],
"T_increment":widget_box.children[0].children[4],
"X1": widget_box.children[1].children[0],
"O1": widget_box.children[1].children[1],
"Side1": widget_box.children[1].children[2]})
display(VBox([widget_box, output_object]))
And close.
widget_box.close()