Vectors and Effective Rates
Why R thinks in vectors: one line of code instead of nine, built on a compound-interest homework. Then the rate arithmetic every economist needs: compound vs simple interest, effective annual rates, the rule of 70, and percentage points vs percent.
- Compute future values with compound interest in R, for one rate and for several years at once
- Create vectors with c(), :, seq() and explain element-wise arithmetic
- Fix the four common R mistakes from Homework 1
- Convert a nominal rate into an effective annual rate
- Estimate doubling time with the rule of 70
- Tell a change in percentage points from a percent change
The week had three blocks: Homework 1 written together in class, vectors (Irizarry, Introduction to Data Science, chapter 2, free online at rafalab.dfci.harvard.edu/dsbook-part-1), and effective rates, the rule of 70, and points vs percent. All interest and policy rates used this week are illustrative, not real market data.
Homework 1: compound interest in R
The problem: you deposit a principal of 50,000 TL at an annual interest rate, and interest is added to the balance every year. How much do you have after 1, 3 and 5 years?
- future value
- principal (the starting amount)
- interest rate per year as a decimal (40% = 0.40)
- number of years
Use it when interest is paid on the growing balance, not just on the original principal.
In class it was typed one variable at a time:
principal <- 50000
rate <- 0.50 # interest rate is 50%
one.year.fv <- principal * (1 + rate)^1
three.year.fv <- principal * (1 + rate)^3
five.year.fv <- principal * (1 + rate)^5
At 50% this gives 75,000, 168,750 and 379,687.5. The full answer key from the slide:
| Rate | 1 year | 3 years | 5 years |
|---|---|---|---|
| 30% | 65,000 | 109,850 | 185,646.5 |
| 40% | 70,000 | 137,200 | 268,912 |
| 50% | 75,000 | 168,750 | 379,687.5 |
What went wrong? The four common slips
| Symptom | Cause |
|---|---|
| Result is far too small | principal * 1 + 0.40^5: missing parentheses. R does ^ first, then *, then +. Write principal * (1 + 0.40)^5. |
object 'principal' not found | The chunk that defines principal was not run, or the name is spelled differently (R is case-sensitive). |
Error at 0,40 | A decimal comma. R needs a point: 0.40. |
| Document will not render | An unfinished chunk, e.g. principal <- with nothing after it. |
Vectors: one line instead of nine
The slide “Count the lines” made the point: for three rates and three horizons you write the formula nine times, and only the exponent changes. What if the exponent held three numbers at once?
years <- c(1, 3, 5)
principal * (1 + 0.40)^years
[1] 70000 137200 268912
There are three basic kinds of vector you will meet first:
| Kind | Example | Holds |
|---|---|---|
| numeric | c(0.3, 0.4, 0.5) | numbers |
| character | c("Ankara", "İzmir") | text, in quotes |
| logical | c(TRUE, FALSE, TRUE) | true/false values |
Element-wise arithmetic
Arithmetic on vectors works element by element: the first element with the first, the second with the second, and so on. A single number (a vector of length 1) is reused for every element.
int.rate.vector <- c(0.3, 0.4, 0.5)
years <- c(1, 3, 5)
(1 + int.rate.vector)^years
[1] 1.30000 2.74400 7.59375
principal * (1 + int.rate.vector)^years
[1] 65000.0 137200.0 379687.5
Building sequences
| Code | Result | Use |
|---|---|---|
1:5 | 1 2 3 4 5 | consecutive integers |
seq(1, 5, 2) | 1 3 5 | from, to, by |
seq(0, 1, 0.25) | 0.00 0.25 0.50 0.75 1.00 | steps that are not whole numbers |
length(x) | how many elements x has | checking your vector |
seq(from, to, by) is the one you will use most. With it the homework becomes: principal * (1 + 0.30)^(1:5) gives the balance for every year from 1 to 5 in one line.
Effective rates
A rate is often quoted per year but paid more often: monthly, quarterly, daily. If 40% a year is paid as every month, interest starts earning interest within the year, so you end up with more than 40%.
- quoted (nominal) annual rate as a decimal
- number of compounding periods per year
Use it when two offers quote rates with different payment frequencies and you need to compare what they really pay.
The rule of 70
How long does it take for something growing at a year to double? The exact answer solves , which gives . For small , and , which gives a rule you can do in your head:
- growth rate in percent per year (write 7, not 0.07)
- years to double
Use it when you need a quick doubling time for GDP, prices, population or an investment growing at a steady rate.
Points vs percent
When a rate changes, there are two ways to describe it, and they give different numbers.
- Percentage points are a plain subtraction: a policy rate going from 40% to 50% rises by 10 points.
- Percent change divides by the starting value: . The rate rose by 25 percent.
Exam practice
Summary and review
- Compound interest: . Simple interest: . At 40% for 5 years on 50,000 TL: 268,912 vs 150,000.
- Use parentheses:
principal * (1 + rate)^years. Decimal point, not comma. Run a chunk before using its objects. - A vector is an ordered collection of values of one type;
c()concatenates. Kinds: numeric, character, logical. - Vector arithmetic is element-wise; two vectors pair up by position.
1:5,seq(from, to, by),length().- Effective annual rate: . More frequent compounding means a higher effective rate.
- Rule of 70: doubling time is about ; accurate for small .
- Percentage points are a difference; percent change divides by the starting value.