!pip install ipywidgets
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Requirement already satisfied: ipywidgets in /usr/local/lib/python3.7/dist-packages (7.7.1) Requirement already satisfied: widgetsnbextension~=3.6.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets) (3.6.1) Requirement already satisfied: jupyterlab-widgets>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets) (1.1.1) Requirement already satisfied: ipython>=4.0.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets) (5.5.0) Requirement already satisfied: traitlets>=4.3.1 in /usr/local/lib/python3.7/dist-packages (from ipywidgets) (5.1.1) Requirement already satisfied: ipykernel>=4.5.1 in /usr/local/lib/python3.7/dist-packages (from ipywidgets) (4.10.1) Requirement already satisfied: ipython-genutils~=0.2.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets) (0.2.0) Requirement already satisfied: tornado>=4.0 in /usr/local/lib/python3.7/dist-packages (from ipykernel>=4.5.1->ipywidgets) (5.1.1) Requirement already satisfied: jupyter-client in /usr/local/lib/python3.7/dist-packages (from ipykernel>=4.5.1->ipywidgets) (5.3.5) Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.4 in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (1.0.18) Requirement already satisfied: pygments in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (2.6.1) Requirement already satisfied: pickleshare in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (0.7.5) Requirement already satisfied: pexpect in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (4.8.0) Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (57.4.0) Requirement already satisfied: decorator in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (4.4.2) Requirement already satisfied: simplegeneric>0.8 in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (0.8.1) Requirement already satisfied: wcwidth in /usr/local/lib/python3.7/dist-packages (from prompt-toolkit<2.0.0,>=1.0.4->ipython>=4.0.0->ipywidgets) (0.2.5) Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.7/dist-packages (from prompt-toolkit<2.0.0,>=1.0.4->ipython>=4.0.0->ipywidgets) (1.15.0) Requirement already satisfied: notebook>=4.4.1 in /usr/local/lib/python3.7/dist-packages (from widgetsnbextension~=3.6.0->ipywidgets) (5.3.1) Requirement already satisfied: jinja2 in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (2.11.3) Requirement already satisfied: jupyter-core>=4.4.0 in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (4.11.1) Requirement already satisfied: terminado>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.13.3) Requirement already satisfied: Send2Trash in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (1.8.0) Requirement already satisfied: nbformat in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (5.4.0) Requirement already satisfied: nbconvert in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (5.6.1) Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from jupyter-client->ipykernel>=4.5.1->ipywidgets) (2.8.2) Requirement already satisfied: pyzmq>=13 in /usr/local/lib/python3.7/dist-packages (from jupyter-client->ipykernel>=4.5.1->ipywidgets) (23.2.0) Requirement already satisfied: ptyprocess in /usr/local/lib/python3.7/dist-packages (from terminado>=0.8.1->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.7.0) Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/dist-packages (from jinja2->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (2.0.1) Requirement already satisfied: entrypoints>=0.2.2 in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.4) Requirement already satisfied: defusedxml in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.7.1) Requirement already satisfied: testpath in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.6.0) Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (1.5.0) Requirement already satisfied: mistune<2,>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.8.4) Requirement already satisfied: bleach in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (5.0.1) Requirement already satisfied: jsonschema>=2.6 in /usr/local/lib/python3.7/dist-packages (from nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (4.3.3) Requirement already satisfied: fastjsonschema in /usr/local/lib/python3.7/dist-packages (from nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (2.16.1) Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.18.1) Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.7/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (4.12.0) Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (4.1.1) Requirement already satisfied: importlib-resources>=1.4.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (5.9.0) Requirement already satisfied: attrs>=17.4.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (22.1.0) Requirement already satisfied: zipp>=3.1.0 in /usr/local/lib/python3.7/dist-packages (from importlib-resources>=1.4.0->jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (3.8.1) Requirement already satisfied: webencodings in /usr/local/lib/python3.7/dist-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.5.1)
import ipywidgets as widgets
slider = widgets.FloatSlider(
value=8.0,
min=5.0,
max=10.0,
step=0.1,
description='Input:',
)
slider
FloatSlider(value=8.0, description='Input:', max=10.0, min=5.0)
import ipywidgets as ipw
def f(x):
print(x * x)
widgets.interact(f, x=(0, 100, 1));
interactive(children=(IntSlider(value=50, description='x'), Output()), _dom_classes=('widget-interact',))
pip install quandl
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Requirement already satisfied: quandl in /usr/local/lib/python3.7/dist-packages (3.7.0) Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from quandl) (1.15.0) Requirement already satisfied: python-dateutil in /usr/local/lib/python3.7/dist-packages (from quandl) (2.8.2) Requirement already satisfied: more-itertools in /usr/local/lib/python3.7/dist-packages (from quandl) (8.13.0) Requirement already satisfied: requests>=2.7.0 in /usr/local/lib/python3.7/dist-packages (from quandl) (2.23.0) Requirement already satisfied: pandas>=0.14 in /usr/local/lib/python3.7/dist-packages (from quandl) (1.3.5) Requirement already satisfied: numpy>=1.8 in /usr/local/lib/python3.7/dist-packages (from quandl) (1.21.6) Requirement already satisfied: inflection>=0.3.1 in /usr/local/lib/python3.7/dist-packages (from quandl) (0.5.1) Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.14->quandl) (2022.1) Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.7.0->quandl) (2.10) Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.7.0->quandl) (3.0.4) Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.7.0->quandl) (1.24.3) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.7.0->quandl) (2022.6.15)
import matplotlib.pyplot as plt
import pandas as pd
import quandl as ql
%matplotlib inline
yield_ = ql.get("USTREASURY/YIELD")
yield_
1 MO | 2 MO | 3 MO | 6 MO | 1 YR | 2 YR | 3 YR | 5 YR | 7 YR | 10 YR | 20 YR | 30 YR | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Date | ||||||||||||
1990-01-02 | NaN | NaN | 7.83 | 7.89 | 7.81 | 7.87 | 7.90 | 7.87 | 7.98 | 7.94 | NaN | 8.00 |
1990-01-03 | NaN | NaN | 7.89 | 7.94 | 7.85 | 7.94 | 7.96 | 7.92 | 8.04 | 7.99 | NaN | 8.04 |
1990-01-04 | NaN | NaN | 7.84 | 7.90 | 7.82 | 7.92 | 7.93 | 7.91 | 8.02 | 7.98 | NaN | 8.04 |
1990-01-05 | NaN | NaN | 7.79 | 7.85 | 7.79 | 7.90 | 7.94 | 7.92 | 8.03 | 7.99 | NaN | 8.06 |
1990-01-08 | NaN | NaN | 7.79 | 7.88 | 7.81 | 7.90 | 7.95 | 7.92 | 8.05 | 8.02 | NaN | 8.09 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
2022-01-31 | 0.03 | 0.13 | 0.22 | 0.49 | 0.78 | 1.18 | 1.39 | 1.62 | 1.75 | 1.79 | 2.17 | 2.11 |
2022-02-01 | 0.04 | 0.10 | 0.19 | 0.48 | 0.78 | 1.18 | 1.39 | 1.63 | 1.76 | 1.81 | 2.19 | 2.12 |
2022-02-02 | 0.04 | 0.10 | 0.19 | 0.45 | 0.76 | 1.16 | 1.38 | 1.60 | 1.74 | 1.78 | 2.17 | 2.11 |
2022-02-03 | 0.03 | 0.15 | 0.20 | 0.48 | 0.78 | 1.19 | 1.42 | 1.66 | 1.78 | 1.82 | 2.20 | 2.14 |
2022-02-04 | 0.05 | 0.12 | 0.23 | 0.56 | 0.89 | 1.31 | 1.55 | 1.78 | 1.90 | 1.93 | 2.29 | 2.23 |
8032 rows × 12 columns
today = yield_.iloc[-1,:]
month_ago = yield_.iloc[-30,:]
df = pd.concat([today, month_ago], axis=1)
df.columns = ['today', 'month_ago']
df.plot(style={'today': 'ro-', 'month_ago': 'bx--'}
,title='Treasury Yield Curve, %');
A zero-coupon bond is a bond that does not pay any periodic interest except on maturity, where the principal or face value is repaid. Zero-coupon bonds are also called pure discount bonds.
A coupon bond is a bond that pays periodic interests plus par value on maturity. A price of a bond is the sum of all present values of coupon and principal.
https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.newton.html
def bondprice(ytm, period, par, coupon):
price_bond = par*coupon/2*(1-(1+ytm/2)**(-2*period))/(ytm/2) + par/(1+ytm/2)**(2*period)
print("${:.2f}".format(price_bond))
bondprice(0.09, 20, 1000, 0.08)
$907.99
def bondprice2(ytm, period, par, coupon):
price_bond = par*coupon/2*(1-(1+ytm/2)**(-2*period))/(ytm/2) + par/(1+ytm/2)**(2*period)
return price_bond
import pandas as pd
import numpy as np
yield_b = np.linspace(0.03, 0.7, num=69)
#yield_b
price_b = np.zeros([69,1])
price_b.shape
(69, 1)
for ind in range(3, 72):
price_b[ind-3]=(bondprice2(ind/100, period=2, par=1000, coupon=0.08))
#print(price_b)
print(yield_b.shape)
print(price_b.shape)
(69,) (69, 1)
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = [8, 5]
plt.plot(yield_b, price_b)
plt.show()
widgets.interact(bondprice, ytm=(0.05, 0.7, 0.01), period=(1,30, 1), par=(100,1000, 100), coupon=(0.05, 0.7, 0.01))
interactive(children=(FloatSlider(value=0.37, description='ytm', max=0.7, min=0.05, step=0.01), IntSlider(valu…
<function __main__.bondprice>
import scipy.optimize as optimize
def bond_ytm(price, par, T, coup, freq=2, guess=0.05):
freq = float(freq)
periods = T*2
coupon = coup/100.*par
dt = [(i+1)/freq for i in range(int(periods))]
ytm_func = lambda y: \
sum([coupon/freq/(1+y/freq)**(freq*t) for t in dt]) +\
par/(1+y/freq)**(freq*T) - price
return optimize.newton(ytm_func, guess)
# a 5.75% bond that will mature in 1.5 years with a par value of $100. The price is $95.0428. and coupons are paid semiannually. YTM=9.3692%
ytm = bond_ytm(price=100, par=100, T=1.5, coup=5.75, freq=2)
print("{:.4f}%".format(ytm*100))
# %load solutions/intro/intro-example.py
!pip install bqplot
import numpy as np
import bqplot as bq
from google.colab import output
output.enable_custom_widget_manager()
from google.colab import output
output.disable_custom_widget_manager()
xs = bq.LinearScale()
ys = bq.LinearScale()
x = np.arange(100)
y = np.cumsum(np.random.randn(2, 100), axis=1) #two random walks
line = bq.Lines(x=x, y=y, scales={'x': xs, 'y': ys}, colors=['red', 'green'])
xax = bq.Axis(scale=xs, label='x', grid_lines='solid')
yax = bq.Axis(scale=ys, orientation='vertical', tick_format='0.2f', label='y', grid_lines='solid')
fig = bq.Figure(marks=[line], axes=[xax, yax], animation_duration=1000)
display(fig)
# update data of the line mark
line.y = np.cumsum(np.random.randn(2, 100), axis=1)
pip install ipympl
%matplotlib ipympl
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import VBox, FloatSlider
with plt.ioff():
fig, ax = plt.subplots()
x1 = np.linspace(0, 20, 500)
slider = FloatSlider(
value=1.0,
min=0.02,
max=2.0
)
lines = plt.plot(x1, np.sin(slider.value * x1))
def update_lines(change):
lines[0].set_data(x1, np.sin(change.new * x1))
fig.canvas.draw()
slider.observe(update_lines, names='value')
VBox([slider, fig.canvas])
with plt.ioff():
fig, ax = plt.subplots()
#x1 = np.linspace(0, 20, 500)
x1 = np.linspace(0.03, 0.5, num=50)
slider = FloatSlider(
value=1.0,
min=0.02,
max=2.0
)
#lines = plt.plot(x1, np.sin(slider.value * x1))
lines = plt.plot(x1, price_b)
def update_lines(change):
lines[0].set_data(x1, np.sin(change.new * x1))
fig.canvas.draw()
slider.observe(update_lines, names='value')
VBox([slider, fig.canvas])
Behind the scenes, even pure Python widgets are composed of two pieces:
When writing your own widgets, that means making a choice: write only in Python or write in both Python and Javascript.
Which to choose depends on what you are trying to accomplish. This tutorial will focus on writing your own widgets in pure Python. An example of a pure-Python package that includes some interesting interfaces is reducer, a package for calibrating astronomical data.
Jupyter widgets forms a framework for representing python objects interactively. Some large open-source interactive controls based on Jupyter widgets include: