{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "38c6b854-4605-4f99-a6f0-9a0692b2e025", "metadata": {}, "outputs": [], "source": [ "from rateslib import *" ] }, { "cell_type": "markdown", "id": "6a31e9be-2101-4a15-9efd-2362a240915e", "metadata": {}, "source": [ "# Constructing Curves from (CC) Zero Rates\n", "\n", "A common type of curve definition in quantitative analysis is to construct a `Curve` from continuously compounded zero coupon rates.\n", "\n", "There is a one-to-one equivalence relation between discount factors (DFs) and cc zero rates: " ] }, { "cell_type": "markdown", "id": "e7785e60-76eb-4b5a-96e2-0ebdfbca2d2e", "metadata": {}, "source": [ "$$\n", "v = exp ( - d \\bar{r} )\n", "$$\n", "where $d$ is the day count fraction (DCF) measured between 'today' and the 'maturity' date of the rate, using the ``convention`` assocoiated with the rates. \n", "\n", "In **rateslib** a ``Curve`` is defined by DF nodes, so if one wants to construct a ``Curve`` from zero rates either these have to be manually converted to DFs or the ``Solver`` can be used to determine them via calibration." ] }, { "cell_type": "markdown", "id": "f58cb040-c516-4ae0-9eb4-9bece0d4315f", "metadata": {}, "source": [ "### Direct conversion\n", "\n", "Writing a manual conversion function is not difficult. We just need to use the above formula directly." ] }, { "cell_type": "code", "execution_count": null, "id": "6105c526-0c8d-474b-acc0-bc574c472d63", "metadata": {}, "outputs": [], "source": [ "def curve_from_zero_rates(nodes, convention, calendar):\n", " start = list(nodes.keys())[0]\n", " nodes_ = {\n", " **{date: dual_exp(-dcf(start, date, convention=convention) * r/100.0) \n", " for (date,r) in list(nodes.items())}\n", " }\n", " return Curve(nodes=nodes_, convention=convention, calendar=calendar)" ] }, { "cell_type": "code", "execution_count": null, "id": "bdd95731-050d-4acd-8c47-a42c44ae8529", "metadata": {}, "outputs": [], "source": [ "curve = curve_from_zero_rates(\n", " {dt(2024, 7, 15): 0.0, dt(2025, 7, 15): 5.00, dt(2026, 7, 15): 4.65},\n", " convention=\"act365f\",\n", " calendar=\"nyc\",\n", ")\n", "curve.plot(\"1d\")" ] }, { "cell_type": "code", "execution_count": null, "id": "436743a0-4444-4f2c-818a-fc938df2f7b5", "metadata": {}, "outputs": [], "source": [ "curve.nodes" ] }, { "cell_type": "markdown", "id": "4face5a4-0e12-4632-bdb0-bbbcfa2fbd20", "metadata": {}, "source": [ "If cubic spline interpolation was required this could be included within the ``curve_from_zero_rates`` function using the ``t`` argument from a ``Curve``." ] }, { "cell_type": "markdown", "id": "ab2249f1-4730-4acd-87b6-8dd7abd10e72", "metadata": {}, "source": [ "### Using a Solver" ] }, { "cell_type": "markdown", "id": "f2bc3a56-ff37-468a-a67d-ddbb58f6d401", "metadata": {}, "source": [ "The advantage of using a ``Solver`` is that the ``Curve`` can be calibrated directly without a manually written construction function and derivatives and risk sensitivities are automatically obtained. The easiest way to directly specify this is to use the ``Value`` class. " ] }, { "cell_type": "code", "execution_count": null, "id": "17b52a8f-eaf3-4864-84f8-bc592c0529be", "metadata": {}, "outputs": [], "source": [ "curve = Curve(\n", " {dt(2024, 7, 15): 1.0, dt(2025, 7, 15): 1.0, dt(2026, 7, 15): 1.0},\n", " convention=\"act365f\", calendar=\"nyc\", id=\"ccz_curve\"\n", ")\n", "solver = Solver(\n", " curves=[curve],\n", " instruments=[\n", " Value(dt(2025, 7, 15), \"act365f\" ,metric=\"cc_zero_rate\", curves=curve),\n", " Value(dt(2026, 7, 15), \"act365f\" ,metric=\"cc_zero_rate\", curves=curve),\n", " ],\n", " s=[5.0, 4.65] # <- Same rates to observe same derived discount factors\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "249fd6d5-7714-4b7c-9812-1f2ddfee09f9", "metadata": {}, "outputs": [], "source": [ "curve.plot(\"1d\")" ] }, { "cell_type": "code", "execution_count": null, "id": "82bd302e-0439-4c5f-b717-8ad61b2be8fe", "metadata": {}, "outputs": [], "source": [ "curve.nodes" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }