%\documentclass{jss}
\documentclass[nojss]{jss}
\usepackage[OT1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,lifecon,thumbpdf,actuarialsymbol}
%\usepackage{myVignette}
%\VignetteIndexEntry{An introduction to lifecontingencies package}
%\VignetteKeywords{vig1}
%\VignettePackage{lifecontingencies}
\usepackage{Sweave}
%\SweaveOpts{prefix.string=Figures/fig}
\author{Giorgio Alfredo Spedicato\\StatisticalAdvisor}
\Plainauthor{Giorgio Alfredo Spedicato}
\title{The \pkg{lifecontingencies} Package: Performing Financial and Actuarial Mathematics Calculations in \proglang{R}}
\Plaintitle{The lifecontingencies Package: Performing Financial and Actuarial Mathematics Calculations in R}
\Shorttitle{\pkg{lifecontingencies}: Financial and Actuarial Mathematics Calculations in \proglang{R}}
\Abstract{
It is possible to model life contingency insurances with the
\pkg{lifecontingencies} \proglang{R} package, which is capable of
performing financial and actuarial mathematics calculations. Its
functions permit one to determine both the expected value and the
stochastic distribution of insured benefits. Therefore, life
insurance coverage can be priced and portfolios risk-based capital
requirements can be assessed. This paper briefly summarizes the
theory regarding life contingencies that is based on financial
mathematics and demographic concepts. Then, with the aid of applied
examples, it shows how the \pkg{lifecontingencies} package can be a
useful tool for executing routine, deterministic, or stochastic
calculations for life-contingencies actuarial mathematics.
}
\Keywords{life tables, financial mathematics, actuarial mathematics, life insurance}
\Plainkeywords{life tables, financial mathematics, actuarial mathematics, life insurance} %% without formatting
%% at least one keyword must be supplied
\Volume{55}
\Issue{10}
\Month{November}
\Year{2013}
\Submitdate{2012-04-10}
\Acceptdate{2013-04-03}
%% The address of (at least) one author should be given
%% in the following format:
\Address{
Giorgio Alfredo Spedicato\\
Ph.D ACAS C.STAT\\
Via Firenze 11
20037 Italy\\
E-mail: \email{spedygiorgio@gmail.com}\\
URL: \url{https://github.com/spedygiorgio/lifecontingencies}
}
%% for those who use Sweave please include the following line (with % symbols):
%% need no \usepackage{Sweave.sty}
%% end of declarations %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\SweaveOpts{concordance=TRUE}
<>=
options(prompt = "R> ", continue = "+ ", width = 70, useFancyQuotes = FALSE)
set.seed(123)
numSim=100
@
\maketitle
\section{Introduction}
This vignette is based on the package's paper published in JSS,
\cite{spedLifecon}. Apart of keeping track of package's updates, a major
difference with respect to JSS publication is that parallel features are turned
off to cope with CRAN packages' submission policies. As of March 2014, the
\pkg{lifecontingencies} package
\citep{spedLifecon} appears as the first \proglang{R} package that deals with life contingent actuarial mathematics. The \proglang{R}
statistical programming environment \citep{rSoftware} has become the
primary reference software for academics. Even in a business context,
\proglang{R} is now considered a valid alternative to affirmed
proprietary packages for statistics and data analysis, namely
\proglang{SAS} \citep{SAS}, \proglang{MATLAB} \citep{MATLAB} and
\proglang{SPSS} \citep{SPSS}. Some packages for actuarial applications
have already been developed within \proglang{R}. However, most of them
mainly focus on non-life insurance. In fact, non-life insurance
modeling involves more data analysis and applied statistical modeling
than that of life insurance. Functions allowing one to fit loss
distributions and perform credibility analysis are provided within the
package \pkg{actuar} \citep{Dutang2008}. This package represents the
computational side of the classical actuarial textbook on loss
distribution \citep{klugman2009loss}. The package \pkg{ChainLadder}
\citep{chainLadder} provides functions that are capable of estimating
loss reserves for non-life insurance. Generalized linear models
(GLMs), widely used in non-life insurance rate-making, can be fit by
functions bundled within base \proglang{R} distributions. The
generalized additive models for location, shape and scale (GAMLSS) and
tweedie regression, which are both more advanced predictive models
used by actuaries, are handled by specifically developed packages such
as \pkg{gamlss} \citep{gamlssPkg,gamlsspkg2} and \pkg{cplm} \citep{cplmPkg}.
Life insurance actuarial work deals mainly with demographic and
financial data. The CRAN task view ``Empirical Finance''
\citep{taskview} lists several packages tailored specifically for
financial analysis. Packages \pkg{YieldCurve} \citep{YieldCurveR} and
\pkg{termstrc} \citep{termstrcR} are capable of financial modeling for
interest rates. Among the few packages that handle demographic data,
\pkg{demography} \citep{demographyR} and \pkg{LifeTables}
\citep{LifeTableR} can be used to manage demographic projections and
life tables.
On the other hand, many commercial software packages tailored
specifically for the actuarial analysis of life insurance are already
available. \pkg{MoSes} \citep{moses} and \pkg{Prophet}
\citep{prophet} are currently the leading actuarial software packages
for life insurance modeling. The \pkg{lifecontingencies} package aims
to represent the computational \proglang{R} companion of the
theoretical concepts exposed in textbooks like the classical
\cite{bowers1997actuarial} and \cite{dickson2009actuarial} for
actuarial mathematics and \cite{broverman2008mathematics} for
financial mathematics. Package \pkg{lifecontingencies} is available from
the Comprehensive \proglang{R} Archive Network at
\url{http://CRAN.R-project.org/package=lifecontingencies}. The examples used throughout this
paper have been taken from \cite{mathFinAct} and \cite{finanMLC}, both
freely available financial and actuarial mathematics textbooks. The
paper is structured as follows: Section~\ref{sec:statistics} outlines
the statistical and financial mathematical theory regarding life
contingencies, Section~\ref{sec:structure} overviews the structure of
the \pkg{lifecontingencies} package, Section~\ref{sec:examples} gives
a wide choice of applied \pkg{lifecontingencies} examples, and
finally, Section~\ref{sec:discussion} discusses the packages current
and future development as well as its known limitations.
\section[Statistical and financial foundations of life
contingencies]{Statistical and financial foundations of life
contingencies}\label{sec:statistics}
The actuarial pricing and reserving of life contingent insurances
involves the calculation of statistics regarding occurrences and
amounts of future cash flows. For example, the insurance pure premium
(also known as benefit premium) can be regarded as the expected value
of the prospective benefits cash flow distribution, valued at time zero
for a given interest rate structure. The probabilities of the
prospective benefits cash flow are based on the occurrence of the
policyholder's life events (life contingencies). In addition, the
theory of interest is used to find the present value of these amounts
that will occur in the future. Therefore, life insurance actuarial
mathematics is based on concepts derived from demography and theory of
interest.
A life table (also called a mortality table or actuarial table) is a
table that shows how mortality affects subjects of a cohort across
different ages. For each age $x$, it reports the number of $l_x$
individuals living at the beginning of age $x$. It is a sequence of
$l_0, l_1, \ldots, l_{\omega}$, where $l_{\omega}$, the terminal age,
represents the latest age that a subject of the cohort can survive
until. Life tables are typically distinguished according to gender,
year of birth and nationality, with different categories being used
depending on the type of life contingency (i.e., assurance or
annuity). As another example, businesses may have different customers
with different underlying mortalities, so they will be in need of
different life tables.
From a statistical point of view, a life table allows one to deduce
the probability distribution of the future lifetime for a policyholder
aged $x$. In particular, a life table also permits one to derive two
key probability distributions: $\tilde T_x$, the complete future
lifetime for a policyholder aged $x$, and its curtate form, $\tilde
K_x$, the number of future years completed before death. Therefore,
many demographic statistics can be derived from a life table, of which
a non-exhaustive list follows:
\begin{itemize}
\item $_t{p_x} = \frac{l_{x + t}}{l_x}$, the probability that a policyholder
alive at age $x$ will reach age $x+t$.
\item $_t{q_x}$, the complementary probability of $_t{p_x}$.
\item $_t{d_x}$, the number of deaths between age $x$ and $x+t$.
\item $_t{L_x} = \int_0^t {l_{x + y}}dy$, the expected number of years
lived by the cohort between ages $x$ and $x+t$.
\item $_t{m_x} = \frac{{_t{d_x}}}{{_t{L_x}}}$, the central mortality
rate between ages $x$ and $x+t$.
\item $e_x$, the curtate expectation of life for a subject aged $x$,
$e_x = \E[ \tilde K_x] = \sum\limits_{k = 1}^\infty
{_k{p_x}} $.
\end{itemize}
The Keyfitz textbook \citep{keyfitz2005applied} provides an exhaustive
coverage of life table theory and practice. Life tables are usually
published by institutions that have access to large amounts of
reliable historical data, like social security bureaus. It is common
practice for actuaries to start from these life tables and adapt them
to the insurer's portfolio actual experience.
Classical financial mathematics deals with monetary amounts that can
be available at different times. The present value of a series of cash
flows, expressed by Equation~\ref{eq:CF}, is probably the most
important concept. The present value (PV) can be considered as the
value -- in current money -- of a series of financial cash flows,
$\text{CF}_t$, that are available at different periods of time.
%
\begin{equation}
\text{PV} = \sum\limits_{t \in T}^{} \text{CF}_{t} {\left( 1 + i_t \right)^{-t}}
\label{eq:CF}
\end{equation}
%
The interest rate, $i$, represents the measure of the price of money
available in future times. Like the interest rate, the time value of
money can be expressed by the discount rate $d = \frac{i}{1 +
i}$. This paper will use the $i$ symbol to express effective
compound interest, with money invested once per period. In the case
where money is invested more frequently, say $m$ times per period,
each fractional period is named the interest conversion period. During
each interest conversion period, the real interest rate
$\frac{i^{\left( m \right)}}{m}$ is earned, where the $i^{\left( m
\right)}$ expression defines the convertible (also known as
``nominal'') rate of interest payable $m$ times per period.
Equation~\ref{eq:interest} combines the various notations for interest
and discount rates, both on an effective and convertible basis, to
express what an amount of \$1 invested now will grow to at time $t$.
%
\begin{equation}
A \left( t \right) =\left( 1 + i \right)^{t}=\left( 1 - d \right )^{-t} = v^{-t} = \left( 1 + \frac{i^{m}}{m} \right)^{tm} = \left( 1 - \frac{d^m}{m} \right)^{-tm}
\label{eq:interest}
\end{equation}
%
All financial mathematics functions (like annuities, $a_{\lcroof{n}}$, or
accumulated values, $s_{\lcroof{n}}$) can be rewritten as particular expressions
of Equation~\ref{eq:CF}, as shown in classical actuarial mathematics textbooks.
Actuaries use the probabilities inherent in life tables to price life
contingent insurances. In fact, life contingencies are stochastic
variables themselves. A life contingent insurance can be represented
by a sequence of one or more payments whose occurrence, timing, and
consequent present value are not certain. In fact, their eventual
occurrence and timing depend on events in the life of the
policyholder; for this reason, they are named ``life
contingencies''. Since the actuary focuses on the present value of such
uncertain payments, future payments of life contingent insurances need
to be discounted using interest rates that may also be considered
stochastic. The \pkg{lifecontingencies} package provides functions
that model most of the standard life contingent random variables,
$\tilde{Z}$, and in particular their expected value, the actuarial
present value (APV). Of all the statistics used by actuaries, the APV
is certainly the most important. In fact, it represents the average
cost of the benefits guaranteed to the policyholder by the insurer. In
a non-life insurance context, it would be named pure premium. The
policyholder pays out the gross premium, $G$, which is a sum of
benefit premiums, loading for expense, profits and taxes. Life
contingencies can be either continuous or discrete, as the cited actuarial
mathematics textbooks explain. Directly, the \pkg{lifecontingencies}
package can only model discrete life contingencies with a
non-stochastic interest rate. Nevertheless, most continuous time life
contingent insurances can be easily derived from their discrete form
under broad assumptions that can be found in the cited textbooks.
A few examples of life contingent insurances follow:
\begin{enumerate}
\item An $n$-year term life insurance provides a payment, if the
insured dies within $n$ years from issue. If the payment is performed
at the end of the year of death, $\tilde Z$ can be written
as
$$\tilde Z = \left\{ \begin{array}{ll}
v^{K + 1},\,&\tilde K_x = 0,1, \ldots ,n - 1,\\
0,\,&\tilde K_x \ge n.
\end{array} \right.$$ Its APV expression is $\termins{x}{n}$.
\item A life annuity consists of a sequence of benefits paid out as
long as the insured life survives. In particular, a temporary life
annuity due pays a benefit at the beginning of each period as long
as the annuitant aged $x$ survives, for up to a total of $n$ years,
or $n$ payments. $\tilde Z$ can be written as $$\tilde Z =
\left\{ \begin{array}{ll} {\ddot a_{\left. {\overline {\,
{K + 1} \,}}\! \right| }},\,&\tilde K_x < n,\\
{\ddot a_{\left. {\overline {\, n \,}}\! \right| }},\,&\tilde K_x
\ge n.
\end{array} \right.$$ Its APV expression is $\anndue{x}{n}$.
\item An $n$-year pure endowment insurance grants a benefit payable at
the end of $n$ years if the insured survives at least $n$ years from
issue. $\tilde Z$ can be written as $$\tilde Z
= \left\{ \begin{array}{ll} {0},\,&\tilde K_x < n,\\
{v^n},\,&\tilde K_x \ge n.
\end{array} \right.$$ Its APV expression is $\pureend{x}{n}$ (or
$\pureendc{x}{n}$).
\item An $n$-year endowment insurance will pay a benefit at the
year of death or at the end of the $n$-th year, whichever occurs
earlier. $\tilde Z$ can be written
as $$\tilde Z = \left\{ \begin{array}{ll} v^{K + 1},\,&\tilde K_x = 0,1, \ldots ,n - 1,\\
{v^n},\,&\tilde K_x \ge n.
\end{array} \right.$$ Its APV expression is $\insend{x}{n}$.
\end{enumerate}
Interested readers could see the cited references for formulas regarding
other life contingent insurances like $\lcterm{(DA)}{x}{n}$, the
decreasing term life insurance, or $\lcterm{(IA)}{x}{n}$, the
increasing term life insurance. There are also common variations of
payments that form arrangements like deferment or fractional
payments. Similarly, it is possible to define insurances and annuities
depending on the survival status of two or more lives. For example,
$A_{xy}$ and $a_{\overline{xy}}$ represent, respectively, the APV
symbols for the two lives joint-live insurance and the two lives
last-survivor annuity immediate.
The \pkg{lifecontingencies} package provides functions that allow an actuary
to perform classical financial and actuarial mathematics calculations. In
addition to standard deterministic modeling, a peculiar feature of
\pkg{lifecontingencies} is that it allows one to generate variates from the
stochastic distribution of the present value of future benefits, $\tilde Z$, for most life contingent
insurances. This feature allows for a deeper assessment of the insurance
liabilities variability.
\section{The structure of the package}\label{sec:structure}
The package \pkg{lifecontingencies} contains classes and methods that
handle life tables and actuarial tables in a convenient manner.
The package is loaded within the \proglang{R} command line interace as follows:
<>=
library("lifecontingencies")
@
Two main \proglang{S}4 classes have been defined within the
\pkg{lifecontingencies} package: the `\code{lifetable}' class and the
`\code{actuarialtable}' class. The `\code{lifetable}' class is defined
as follows:
<>=
showClass("lifetable")
@
Class `\code{actuarialtable}' inherits from the `\code{lifetable}' class; it
is different from the `\code{lifetable}' class because it has one more
slot accounting for the interest rate.
<>=
showClass("actuarialtable")
@
The following methods have been defined for the `\code{lifetable}' and
`\code{actuarialtable}' classes.
<>=
showMethods(classes=c("actuarialtable","lifetable"))
@
The computation of financial, demographic and actuarial quantities is
based on dedicated functions that use objects of classes
`\code{lifetable}' and `\code{actuarialtable}' when
required. Table~\ref{tab:pars} shows the naming convention for common
input parameters used within the package. The sections that follow
briefly present such functions with the aid of examples.
\begin{table}[t!]
\centering
\begin{tabular}{lll}
\hline
Parameter & Significance \\
\hline
\code{x} & the policyholder's age\\
\code{n} & the coverage duration or payment duration\\
\code{i} & interest rate (could be varying)\\
\code{k} & the frequency of payments\\
\hline
\end{tabular}
\caption{\pkg{lifecontingencies} functions parameters naming conventions.
\label{tab:pars}}
\end{table}
Finally, the \pkg{lifecontingencies} package depends on the
\pkg{methods} package, which defines its classes, and the
\pkg{parallel} package, which is used to speed up computations. As
detailed in Section~\ref{sec:discussion}, implementation of
\proglang{C} or \proglang{C++} code snippets is expected to shorten
computational times in the future versions of the package.
\section{Code and examples}\label{sec:examples}
This section is structured as follows: Section~\ref{ss:mathFin} shows
classical financial mathematics examples, Section~\ref{ss:lfActT}
deals with life tables and actuarial table management,
Section~\ref{ss:actMath} shows classical actuarial mathematics
examples, and Section~\ref{ss:stochastic} presents functions in the
\pkg{lifecontingencies} package that perform simulation analysis.
\subsection{Classical financial mathematics example}\label{ss:mathFin}
\begin{table}[b!]
\centering
\begin{tabular}{ll}
\hline
Function & Purpose\\
\hline
\code{presentValue} & present value of a series of cash flows\\
\code{annuity} & present value of an annuity-certain, $a_{\lcroof{n}}$\\
\code{accumulatedValue} & future value of a series of cash flows, $s_{\lcroof{n}}$\\
\code{increasingAnnuity} & present value of an increasing annuity -- certain, $IA_{n}$\\
\code{decreasingAnnuity} & present value of a decreasing annuity -- certain,
$DA_{\lcroof{n}}$\\
\code{convertible2Effective} & conversion from convertible to effective
interest (discount) rates\\
\code{effective2Convertible} & \code{convertible2Effective} inverse\\
\code{intensity2Interest} & conversion from force of interest to the
interest rate\\
\code{interest2Intensity} & \code{intensity2Interest} inverse\\
\code{duration} & dollar / Macaulay duration of a series of cash flows\\
\code{convexity} & convexity of a series of cash flows\\
\hline
\end{tabular}
\caption{\pkg{lifecontingencies} functions for financial mathematics.
\label{tab:finfun}}
\end{table}
The \pkg{lifecontingencies} package provides functions that perform
classical financial mathematics calculations listed in
Table~\ref{tab:finfun}.
Some of these implement closed form formulas and their inverses; this
is also shown in financial mathematics textbooks. A broader
discussion, however, shall be dedicated to the \code{presentValue}
function. In fact, the \code{presentValue} function is internally
called by most other financial and actuarial functions within the
\pkg{lifecontingencies} package. This function calculates present
values or APVs by computing Equation~\ref{eq:pv}.
%
\begin{equation}
\text{PV}=\sum\limits_{i = 1}^n {c_i\cdot v^{t_i}\cdot p_i}
\label{eq:pv}
\end{equation}
%
The terms in Equation~\ref{eq:pv} are the cash flows vector, $c_i$,
the corresponding discount factors vector, $v^{t_i}$, and the
occurrence probabilities vector, $p_i$, respectively. Many
\pkg{lifecontingencies} package functions, like \code{axn} or
\code{annuity}, work by first defining the pattern vectors of cash
flows, interest rate and probabilities (in case of actuarial
functions), which are then passed as arguments to the
\code{presentValue} function.
Examples that follow show how to handle interest and discount rates
with different compounding frequencies, how to perform present value,
annuity, and future value analysis, and how to deal with loan
amortization and bond pricing.
\subsubsection{Interest rate functions}\label{sss:subsubInterest}
Interest rates represent the time-value of money. Different
types of rates can be found in literature. As a remark,
Equation~\ref{eq:intdisc} displays the relationship between the effective interest
rate, the convertible interest rate, the discount factor, the force of interest, the effective
discount rate and the convertible discount rate.
%
\begin{equation}
\left( 1 + i \right)^t = \left( 1 + \frac{i^{\left( m \right)}}{m} \right)^t = v^{-t} =
\exp \left( \delta t \right) = \left( 1 - d \right)^{ - t} =
\left( 1 - \frac{d^{\left( m \right)}}{m} \right)^{ - t}
\label{eq:intdisc}
\end{equation}
%
The functions \code{interest2Discount}, \code{discount2Interest},
\code{convertible2Effective}, \linebreak
\code{effective2Convertible}, \code{interest2Intensity},
\code{intensity2Interest} have all been based on
Equation~\ref{eq:intdisc}; their inverse formulas are implied
therein. Throughout the paper, an effective interest rate is used
unless otherwise stated.
As examples, functions \code{interest2Discount} and
\code{discount2Interest} represent a convenient way to switch from
interest to discount rates and vice versa.
<>=
interest2Discount(0.03)
discount2Interest(interest2Discount(0.03))
@
The function \code{convertible2Effective} allows one to find the
effective interest rate implied in a consumer - credit loan that offers a 10\%
convertible (nominal) interest rate with quarterly compounding.
<>=
convertible2Effective(i=0.10,k=4)
@
\subsubsection{Analysis of present value and internal rate of return}\label{sss:pva}
Performing a project appraisal means evaluating the net present value (NPV) of all projected cash flows.
The code below shows an example of NPV analysis.
<>=
capitals <- c(-1000,200,500,700)
times <- c(0,1,2,5)
presentValue(cashFlows=capitals, timeIds=times, interestRates=0.03)
@
When interest rates vary and cash flows are uncertain, the
\code{probabilities} parameter can be
properly set as the following code shows:
<>=
presentValue(cashFlows=capitals, timeIds=times,
interestRates=c( 0.04, 0.02, 0.03, 0.05),
probabilities=c(1,1,1,0.5))
@
The internal rate of return (IRR) is defined as the interest rate that makes the
NPV zero. It is an alternative NPV that allows financial investment projects to be ranked
by the timing and amount of their cash flows.
The following example displays how the
\pkg{lifecontingencies} package can use base \proglang{R} functions to calculate the IRR.
<>=
getIrr <- function(p) (presentValue(cashFlows=capitals, timeIds=times,
interestRates=p) - 0)^2
nlm(f=getIrr, p=0.1)$estimate
@
\subsubsection{Annuities and future values}\label{sss:annfv}
An annuity (certain) is a sequence of payments with a specified amount that is
present valued. If it is valued at the end of the term of payment
is is called future value (or accumulated value). The code below shows examples of annuity, $a_{\left.
{\overline {\, n \,}}\! \right| }$, and accumulated value, $s_{\left. {\overline {\, n \,}}\! \right| }$, evaluations.\\
The PV of an annuity immediate \$100 payable at the end of each year for the next 5 years at an interest rate of 3\% is:
<>=
100 * annuity(i=0.03, n=5)
@
while the corresponding future value is:
<>=
100 * accumulatedValue(i=0.03, n=5)
@
Annuities and future values payable $k$-thly (where fractional payments of $\frac{1}{k}$
are received for each $k$-th of a period) can be evaluated properly by setting the
functions' parameters as follows:
<>=
ann1 <- annuity(i=0.03, n=5, k=1, type="immediate")
ann2 <- annuity(i=0.03, n=5, k=12, type="immediate")
c(ann1,ann2)
@
\code{increasingAnnuity} and \code{decreasingAnnuity} functions handle
increasing and decreasing annuities, whose symbols are $IA_{x}$, $DA_{x}$
respectively. Assuming a ten-year term and a 3\% interest rate,
examples of increasing and decreasing annuities follow.
<>=
incrAnn <- increasingAnnuity(i=0.03, n=10, type="due")
decrAnn <- decreasingAnnuity(i=0.03, n=10, type="immediate")
c(incrAnn, decrAnn)
@
The last example within this section displays the calculation of the present
value of a geometrically increasing annuity. As known by classical financial and actuarial mathematic, geometric
annuities are priced using a synthetic discount rate $j=i-g$ being $g$ the geometric growth rate and
$i$ the interest rate used to discount future payments. So, if payment amounts increase by
3\%, the interest rate is 4\%, and its term is 10 years, the implied present
value is:
<>=
annuity(i=((1+0.04)/(1+0.03)-1), n=10)
@
\subsubsection{Loan amortization}\label{sss:finloan}
As this section exemplifies, \pkg{lifecontingencies} financial mathematics functions allow one to define the
repayment schedule of any loan arrangement. Let $C$ denote the loaned capital (principal). Assuming an interest rate $i$, the amount due to the lender at each installment is $R =\frac{C}{a_{\left. {\overline {\, n \,}}\! \right| }}$.
Therefore, the $R$ amount repays $I_t = C_{t-1} * i$ as interest, while $C_t = R - I_t$ is the amount of the loan still outstanding after installment t has been paid. The periodic installment of loan repayment , $R$, is
calculated as follows:
<>=
capital <- 100000
interest <- 0.05
payments_per_year <- 2
rate_per_period <- (1+interest)^(1/payments_per_year)-1
years <- 30
R <- 1/payments_per_year *
capital/annuity(i=interest, n=years,
k=payments_per_year)
R
@
Then the balance due at end of the period (EoP) is calculated as follows:
<>=
balanceDue <- numeric(years * payments_per_year)
balanceDue[1] <- capital * (1+rate_per_period) - R
for(i in 2:length(balanceDue)) balanceDue[i]<-
balanceDue[i-1] * (1+rate_per_period) - R
@
Figure~\ref{fig:LoanAmort} shows the EoP balance due for a 30 year loan, assuming a 5\% interest rate on a principal of \$ 100,000.
\begin{figure}
\begin{center}
<