{ "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": "\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" }, "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 }