Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ECON 3315

Assignment 4

Fall 2023

Remark:

. Please submit both your R script and log file.

. Write a comment for each step to explain your following codes.

. Implement the programming style to your codes from thisblog

This assignment uses Newtons method to calculate the implied interest rate deter-

mined by a price for an annuity.

Suppose we have a function f (x) and that we have code to evaluate f (x) and f (x)  for any x. We have a number a and we want to find x so that f (x) = a.  This is  sometimes called root finding”,because x is a root” of the equation f (x)−a = 0.  The term root” originally applied to polynomials, but now is used for the zeros”

( x values where f (x) = a = 0 ) of any function.

Newton’s method constructs a sequence of iterates xn for n = 1, 2, . . ., that we hope

have the property that

xn xas n → ∞

You stop the code when you think xn is close enough to x .  Newton’s method is iterative, it involves a sequence of iterations.  That means, it does the same thing (with different numbers) over and over. It is derived by imagining that xn is close to x∗ .

We approximate f (x) by Taylor expansion up to the first derivative term around xn. That is

f (x) = f (xn + (xxn)) ≈ f (xn) + f (xn) (xxn) .

We approximate the equation f (x) = a with the approximation

f (xn) + f (xn) (xxn) ≈ a

We choose the next iterate xn+1  by changing this approximation into an equation

f (xn) + f (xn) (xn+1 − xn) = a.

At iteration n, we know xn. We write code to evaluate f (xn) and f (xn). Then we can solve for xn+1. The algebra leads to

            (1)

The core of Newtons method is to apply the formula (1) over and over until you

think xn is close enough to x∗ , or until you decide it is not working.

The first iterate”, which we call x1  (it sometimes is called x0  ) is the initial guess. The programmer has to find some guess at the solution. This isn’t fun. It often in- volves drawing graphs and playing with simple approximations. This is ”problem specific”, which means it depends on the specific formula for f (x) and the shape of the graph and the value of a. The iteration formula (1) is not problem specific.

But if you have a poor initial guess, the method is more likely to fail.

It is a theorem that iff (x) * 0 and if x1  is close enough to x , then xn xas n → ∞. Moreover, xn converges fast. xn+1  is much closer to x∗ than xn is.  But if the initial guess is poor, then xn may not converge to x .  On the contrary, it may happen that xn → ∞ as n → ∞. This is common. It is important to spend the time to find a good initial guess.

But despite your best efforts, your Newton’s method code will sometimes fail to converge.  You need something in your code to make sure the iteration (1) is not an infinite loop.  You can do this by defining a parameter nmax  and stopping the program if n > nmax . If this happens, the program should return an error message saying it didn’t work.  In practice nmax   = 20 is probably fine, but nmax   = 100 also

is OK.

You also need a convergence criterion, a condition that tells you when to stop and declare victory. You can define a small number E and declare convergence if |xn+1 − xn | ≤ E. If you want an interest rate to within one basis point, and ifr has units of interest per year, then you can take E = 104. But this isn’t so small. If the code can get to 104  it probably get to 1010. Try a small E until you learn it’s too small.

Apply your code to calculating the interest rate r implied by the price of an annuity. If r is an interest rate, C is the annual payment rate, mis the number of payments per year, and T is the number of years, then the present value is

           (2)

We are given P, the price of the annuity. We seek the number r so that P is the present value given by (2). The problem has extra parameters C, m, and T.

You should write three functions in your R script for this assignment: f(r, C, m, T) that evaluates f (x), fp(r, C, m, T) that evaluates f (x), and implied rate() taking inputs x0,P, C,m, T, n max, epsilon, verbose, where x0 is the initial guess, n max is nmax , epsilon is E.

implied rate() should apply Newton’s method to solve the problem by using a

while loop for iteration, f(r, C, m, T) and fp(r, C, m, T) for updating values.

This while loop should have a trip count to make sure it isn’t an infinite loop. Also, a convergence criterion should be applied so that the while loop will break if the criterion is met. If the iteration does not converge, then the function should return an error message. (Using stop() to return the error message, check documentation for usage)

verbose is a boolean input, and its default value is TRUE. If verbose is TRUE, the while loop should print a line of output for each iteration.  This line should have n, xn , the difference xn+1−xn that you use to decide whether you’ve converged. This will give you some confidence that your code is correct and solving the equation. If verbose is FALSE, then don’t print the iteration by iteration progress, just the eventual answer.

Use your code to find the implied interest rate for a $20, 000/ year annuity paid monthly for ten years if the price is P =  $170, 000.   Also,  finding  one  set of parameter values that triggers your error message of failing to converge.