HSG-MCS-HS21_Julia/Problemsets/PS04_YieldToMaturity.ipynb

259 lines
48 KiB
Plaintext
Raw Normal View History

2021-11-15 20:14:51 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"printyellow (generic function with 1 method)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Printf, Roots\n",
"\n",
"include(\"jlFiles/printmat.jl\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"using Plots\n",
"\n",
"#pyplot(size=(600,400))\n",
"gr(size=(480,320))\n",
"default(fmt = :svg)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Yield to Maturity"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The yield to maturity (ytm) is the $\\theta$ that solves\n",
"\n",
"$P = \\sum_{k=1}^{K} \\frac{cf_{k}}{(1+\\theta) ^{m_{k}}}$,\n",
"\n",
"where $cf_k$ is the cash flow from the bond (portfolio) $k$ periods ahead and $P$ is the current price of the bond (portfolio).\n",
"\n",
"We typically have to find $\\theta$ by a numerical method."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 1\n",
"\n",
"Starting by coding a function that calculates a price of the bond as a function of the cash flows ($cf$, a vector), times to the cash flows ($k$, also a vector) and the discount rate ($\\theta$, a single number).\n",
"\n",
"Assume that the bond portfolio pays 0.2 each year for 10 years (1 year from now, 2 years from now,...). (It pays no face value. This is an annuity.) \n",
"\n",
"(1) What is a fair price if the discount rate $\\theta$ is 0.05?\n",
"(2) Plot the fair price as a function of $\\theta$ (use θ = -0.02:0.005:0.1)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1) Price at θ=0.05: 1.544\n"
]
},
{
"data": {
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"480\" height=\"320\" viewBox=\"0 0 1920 1280\">\n<defs>\n <clipPath id=\"clip170\">\n <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip170)\" d=\"\nM0 1280 L1920 1280 L1920 0 L0 0 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip171\">\n <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip170)\" d=\"\nM181.127 1106.38 L1872.76 1106.38 L1872.76 123.472 L181.127 123.472 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip172\">\n <rect x=\"181\" y=\"123\" width=\"1693\" height=\"984\"/>\n </clipPath>\n</defs>\n<polyline clip-path=\"url(#clip172)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 494.982,1106.38 494.982,123.472 \n \"/>\n<polyline clip-path=\"url(#clip172)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 827.457,1106.38 827.457,123.472 \n \"/>\n<polyline clip-path=\"url(#clip172)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1159.93,1106.38 1159.93,123.472 \n \"/>\n<polyline clip-path=\"url(#clip172)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1492.41,1106.38 1492.41,123.472 \n \"/>\n<polyline clip-path=\"url(#clip172)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1824.88,1106.38 1824.88,123.472 \n \"/>\n<polyline clip-path=\"url(#clip170)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 181.127,1106.38 1872.76,1106.38 \n \"/>\n<polyline clip-path=\"url(#clip170)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 494.982,1106.38 494.982,1087.48 \n \"/>\n<polyline clip-path=\"url(#clip170)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 827.457,1106.38 827.457,1087.48 \n \"/>\n<polyline clip-path=\"url(#clip170)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1159.93,1106.38 1159.93,1087.48 \n \"/>\n<polyline clip-path=\"url(#clip170)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1492.41,1106.38 1492.41,1087.48 \n \"/>\n<polyline clip-path=\"url(#clip170)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1824.88,1106.38 1824.88,1087.48 \n \"/>\n<path clip-path=\"url(#clip170)\" d=\"M442.205 1131.73 Q438.594 1131.73 436.765 1135.3 Q434.96 1138.84 434.96 1145.97 Q434.96 1153.07 436.765 1156.64 Q438.594 1160.18 442.205 1160.18 Q445.839 1160.18 447.645 1156.64 Q449.473 1153.07 449.473 1145.97 Q449.473 1138.84 447.645 1135.3 Q445.839 1131.73 442.205 1131.73 M442.205 1128.03 Q448.015 1128.03 451.071 1132.63 Q454.149 1137.22 454.149 1145.97 Q454.149 1154.69 451.071 1159.3 Q448.015 1163.88 442.205 1163.88 Q436.395 1163.88 433.316 1159.3 Q430.261 1154.69 430.261 1145.97 Q430.261 1137.22 433.316 1132.63 Q436.395 1128.03 442.205 1128.03 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip170)\" d=\"M462.367 1157.33 L467.251 1157.33 L467.251 1163.21 L462.367 1163.21 L462.367 1157.33 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip170)\" d=\"M487.436 1131.73 Q483.825
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# calculate the formula above\n",
"function PV(θ, cf, k) #cf is a vector of all cash flows at times k[1],k[2],...\n",
" cdisc = cf./((1.0.+θ).^k) \n",
" P = sum(cdisc) \n",
" return P\n",
"end\n",
"\n",
"θ = -0.02:0.005:0.1\n",
"k = 1:10\n",
"cf = 0.2*ones(10)\n",
"\n",
"printlnPs(\"(1) Price at θ=0.05: \",PV(0.05,cf,k))\n",
"\n",
"n = length(θ)\n",
"Pfair = fill(NaN, n)\n",
"for i = 1:n\n",
" Pfair[i] = PV(θ[i], cf, k)\n",
"end\n",
"\n",
"# Pfair = PV.(θ, Ref(cf), Ref(k))\n",
"\n",
"plot(θ, Pfair, title=\"Present value of cash flow\", legend=false, xlabel=\"θ\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 2\n",
"\n",
"Assume the price this bond (portfolio) is 1.54. Solve for the ytm. Compare with the previous figure. Repeat with a bond price of 1.71.\n",
"\n",
"Hint: the `find_zero()`command of the `Roots.jl` package."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The price and yield to maturity is \n",
" P θ\n",
" 1.540 0.051\n",
" 1.710 0.030\n",
"\n"
]
}
],
"source": [
"P1 = 1.54\n",
"P2 = 1.71\n",
"\n",
"ytm1 = find_zero(θ->PV(θ,cf,k)-P1,(-0.1,0.1)) \n",
"ytm2 = find_zero(θ->PV(θ,cf,k)-P2,(-0.1,0.1)) \n",
"\n",
"printlnPs(\"The price and yield to maturity is \")\n",
"printmat([P1,P2],[ytm1,ytm2],colNames=[\"P\",\"θ\"])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 3a\n",
"\n",
"Now interpret `cf` as the cash flow to a company where `cf[1]` also incorporates the initial investment and `m[1]=0`. (Alternatively, let `P` represent the initial investment.)\n",
"\n",
"The cash flow process is as follows. Find the IRR (internal rate of return).\n",
"\n",
"Hint: `findzero(fn,0)` would start searching at 0."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.2349421128831387"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cf = [-150,100,0,130]\n",
"m = [0,1,2,3]\n",
"\n",
"iir = find_zero(θ->PV(θ, cf, m), 0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 3b\n",
"\n",
"Change the cash flow as in the cell below. Find the IRR. Could there be several solutions?\n",
"\n",
"(Hint: `find_zeros()`)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2-element Vector{Float64}:\n",
" 0.04650143593633046\n",
" 0.9160439083362444"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cf = [-140,320,0,-190]\n",
"\n",
"iir = find_zeros(θ->PV(θ, cf, m),-1,1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Julia 1.7.0-rc1",
"language": "julia",
"name": "julia-1.7"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 1
}