def test_nelson_siegel_ols_calibration(self):
     '''Test ols based calibration of Nelson-Siegel model'''
     t = np.linspace(0, 30)
     y_target = self.y(t)
     y_hat, opt_res = calibrate_ns_ols(t, y_target, tau0=self.y.tau)
     places = 12
     self.assertAlmostEqual(self.y.beta0, y_hat.beta0, places=places)
     self.assertAlmostEqual(self.y.beta1, y_hat.beta1, places=places)
     self.assertAlmostEqual(self.y.beta2, y_hat.beta2, places=places)
     self.assertAlmostEqual(self.y.tau, y_hat.tau, places=places)
     # The following does not work in general, but in this case the
     # error function (of tau) is well-behaving
     places_beta = 3
     places_tau = 2
     y_hat, opt_res = calibrate_ns_ols(t, y_target)  # default start value
     self.assertAlmostEqual(self.y.beta0, y_hat.beta0, places=places_beta)
     self.assertAlmostEqual(self.y.beta1, y_hat.beta1, places=places_beta)
     self.assertAlmostEqual(self.y.beta2, y_hat.beta2, places=places_beta)
     self.assertAlmostEqual(self.y.tau, y_hat.tau, places=places_tau)
     y_hat, opt_res = calibrate_ns_ols(t, y_target, tau0=0.5)
     self.assertAlmostEqual(self.y.beta0, y_hat.beta0, places=places_beta)
     self.assertAlmostEqual(self.y.beta1, y_hat.beta1, places=places_beta)
     self.assertAlmostEqual(self.y.beta2, y_hat.beta2, places=places_beta)
     self.assertAlmostEqual(self.y.tau, y_hat.tau, places=places_tau)
     y_hat, opt_res = calibrate_ns_ols(t, y_target, tau0=5)
     self.assertAlmostEqual(self.y.beta0, y_hat.beta0, places=places_beta)
     self.assertAlmostEqual(self.y.beta1, y_hat.beta1, places=places_beta)
     self.assertAlmostEqual(self.y.beta2, y_hat.beta2, places=places_beta)
     self.assertAlmostEqual(self.y.tau, y_hat.tau, places=places_tau)
     y_hat, opt_res = calibrate_ns_ols(t, y_target, tau0=10)
     self.assertAlmostEqual(self.y.beta0, y_hat.beta0, places=places_beta)
     self.assertAlmostEqual(self.y.beta1, y_hat.beta1, places=places_beta)
     self.assertAlmostEqual(self.y.beta2, y_hat.beta2, places=places_beta)
     self.assertAlmostEqual(self.y.tau, y_hat.tau, places=places_tau)
     y_hat, opt_res = calibrate_ns_ols(t, y_target, tau0=20)
     self.assertAlmostEqual(self.y.beta0, y_hat.beta0, places=places_beta)
     self.assertAlmostEqual(self.y.beta1, y_hat.beta1, places=places_beta)
     self.assertAlmostEqual(self.y.beta2, y_hat.beta2, places=places_beta)
     self.assertAlmostEqual(self.y.tau, y_hat.tau, places=places_tau)
# reset
b0 = 0
b1 = 0
b2 = 0
tau = 0
idx = 0
error = 0

for idx in range(0, length):
    y = timeseries[idx:idx + 1]
    y = y.to_numpy()
    y = y.reshape((19, ))

    try:
        curve, status = calibrate_ns_ols(
            t, y,
            tau0=1.0)  # starting value of 1.0 for the optimisation of tau
        if status.success == 1:
            b0 = curve.beta0
            b1 = curve.beta1
            b2 = curve.beta2
            tau = curve.tau
            iteration = np.array([b0, b1, b2, tau])
            iteration = iteration.reshape(1, 4)
            parameters[idx] = iteration
        else:
            iteration = np.array([0, 0, 0, 0])
            parameters[idx] = iteration
            error = error + 1
    except:
        iteration = np.array([0, 0, 0, 0])
Exemple #3
0
rates = [
    elem.text for elem in rows[len(rows) - 1].find_elements_by_xpath(
        "//td[@class = 'text_view_data']")
]

# End the chromium driver
driver.close()

# Convert the rates we obtained to float.
# Also convert to a numpy array - this is the input format for the NSS
rates = np.array(rates[-12:]).astype(float) / 100
maturities = np.array(
    [1 / 12, 2 / 12, 3 / 12, 6 / 12, 1, 2, 3, 5, 7, 10, 20, 30])

# Apply the nelson siegel svennson model
curve, status = calibrate_ns_ols(maturities, rates)

# <br>
# <br>
# We can have a quick view of the shape of the curve before moving forward

# In[6]:

# Plot the graph to visualize the shape
mats = np.linspace(0.05, 20, 101)
rates = curve(mats)
fig = go.Figure(
    go.Scatter(x=mats, y=rates, line=dict(color='#ffc93c', width=2.5)))
fig.update_layout(
    font_color="#0f4c75",
    title_text='Interpolated Rate Curve',