Created on Fri Jul 19 2013
Revised on Fri Aug 16 23:30:51 2013
FinCal – Time Value of Money Calculation and Computational Finance
FinCal is available on CRAN
library(FinCal)
## Loading required package: ggplot2 Loading required package: reshape2
## Loading required package: scales Loading required package: RCurl Loading
## required package: bitops
Functions available:
ls("package:FinCal")
## [1] "bdy" "bdy2mmy"
## [3] "candlestickChart" "coefficient.variation"
## [5] "date.breaks" "discount.rate"
## [7] "ear" "ear.continuous"
## [9] "ear2bey" "ear2hpr"
## [11] "fv" "fv.annuity"
## [13] "fv.simple" "fv.uneven"
## [15] "geometric.mean" "get.ohlc.google"
## [17] "get.ohlc.yahoo" "get.ohlcs.google"
## [19] "get.ohlcs.yahoo" "harmonic.mean"
## [21] "hpr" "hpr2bey"
## [23] "hpr2ear" "hpr2mmy"
## [25] "irr" "lineChart"
## [27] "lineChartMult" "mmy2hpr"
## [29] "n.period" "npv"
## [31] "pmt" "pv"
## [33] "pv.annuity" "pv.perpetuity"
## [35] "pv.simple" "pv.uneven"
## [37] "r.continuous" "r.norminal"
## [39] "r.perpetuity" "sampling.error"
## [41] "SFRatio" "Sharpe.ratio"
## [43] "twrr" "volumeChart"
## [45] "wpr"
Getting help on a function (e.g., pv)
help{pv} # display the documentation for the function
args{pv} # see arguments of the function
example{pv} # see example of using the function
Note: for all examples, cash inflows are positive and outflows are negative.
Using a stated rate of 4.25%, compute EARs for semiannual, quarterly, monthly, daily and continuous compounding.
ear(0.0425, 2)
## [1] 0.04295
ear(0.0425, 4)
## [1] 0.04318
ear(0.0425, 12)
## [1] 0.04334
ear(0.0425, 365)
## [1] 0.04341
ear.continuous(0.0425)
## [1] 0.04342
Calculate the FV of a $500 investment at the end of ten years if it earns an annually compounded rate of return of 6%.
fv.simple(r = 0.06, n = 10, pv = -500)
## [1] 895.4
Given a discount rate of 3%, calculate the PV of a $1,000,000 cash flow that will be received in five years.
pv.simple(r = 0.03, n = 5, fv = 1e+06)
## [1] -862609
What is the future value of an ordinary annuity that pays $15,000 per year at the end of each of the next 25 years, given the investment is expected to earn a 6% rate of return?
fv.annuity(r = 0.06, n = 25, pmt = -15000, type = 0)
## [1] 822968
What is the future value of an annuity that pays $10,000 per year at the beginning of each of the next three years, commencing today, if the cash flows can be invested at an annual rate of 5%?
fv.annuity(r = 0.05, n = 3, pmt = -10000, type = 1)
## [1] 33101
What is the PV of an annuity that pays $20,000 per year at the end of each of the next 25 years, given a 6% discount rate?
pv.annuity(r = 0.06, n = 25, pmt = -20000, type = 0)
## [1] 255667
Given a discount rate of 10%, what is the present value of a 10-year annuity that makes a series of $1000 payments at the beginning of each of the next three years, starting today?
pv.annuity(r = 0.1, n = 10, pmt = -1000, type = 1)
## [1] 6759
A preferred stock that will pay $2.50 per year in annual dividends beginning next year and plans to follow this dividend policy forever. Given an 10% rate of return, what is the value of this preferred stock today?
pv.perpetuity(r = 0.1, pmt = 2.5, type = 0)
## [1] -25
Using the preferred stock described in the preceding example, determine the rate of return that an investor would realize if she paid $75 per share for the stock.
r.perpetuity(pmt = 2.5, pv = -75)
## [1] 0.03333
A bond will make coupon interest payments of 70 HK$ (7% of its face value) at the end of each year and will also pay its face value of 1,000 HK$ at maturity in 10 years. If the appropriate discount rate is 6%, what is the present value of the bond's promised cash flows?
pv(r = 0.06, n = 10, fv = 1000, pmt = 70, type = 0)
## [1] -1074
Using a rate of return of 6%, compute the future value of the 6-year uneven cash flow stream occured at the end of each year. (-10000 -5000 2000 4000 6000 8000)
fv.uneven(r = 0.06, cf = c(-10000, -5000, 2000, 4000, 6000, 8000))
## [1] -1542
Compute the present value of this 6-year uneven cash How stream described above using a 10% rate of return.
pv.uneven(r = 0.1, cf = c(-10000, -5000, 2000, 4000, 6000, 8000))
## [1] 747.1
A company plans to borrow $500,000 for five years. The company's bank will lend the money at a rate of 6% and requires that the loan be paid off in five equal end-of-year payments. Calculate the amount of the payment that the company must make in order to fully amortize this loan in five years.
pmt(r = 0.06, n = 5, pv = 5e+05, fv = 0)
## [1] -118698
How many $1000 end-of-year payments are required to accumulate $10,000 if the discount rate is 9%?
n.period(r = 0.09, pv = 0, fv = 10000, pmt = -1000, type = 0)
## [1] 7.448
Suppose you have the opponunity to invest $1000 at the end of each of the next five years in exchange for $6000 at the end of the fifth year. What is the annual rate of return on this investment?
discount.rate(n = 5, fv = 6000, pmt = -1000, pv = 0, type = 0)
## [1] 0.0913
Calculate the NPV of an investment project with an initial cost of $6 million and positive cash flows of $2.6 million at the end of Year 1, $2.4 million at the end of Year 2, and $3.8 million at the end ofYear 3. Use 8% as the discount rate.
npv(r = 0.08, cf = c(-6, 2.6, 2.4, 3.8))
## [1] 1.482
What is the IRR for the investment described in example 13?
irr(cf = c(-6, 2.6, 2.4, 3.8))
## [1] 0.2033
Suppose a stock is purchased for $3 and is sold for $4 six months later, during which time it paid $0.50 in dividends. What is the holding period return?
hpr(ev = 4, bv = 3, cfr = 0.5)
## [1] 0.5
An investor purchases a share of stock at t = 0 for $10. At the end of the year, t = 1 , the investor buys another share of the same stock for $12. At the end of Year 2, the investor sells both shares for $13 each. At the end of both years 1 and 2, the stock paid a $1 per share dividend. What is the annual time-weighted rate of return for this investment?
twrr(ev = c(12, 26), bv = c(10, 24), cfr = c(1, 2))
## [1] 0.2315
Calculate the bank discount yield for a T-hill priced at $9,850, with a face value of $10,000 and 120 days until maturity.
bdy(d = 150, f = 10000, t = 120)
## [1] 0.045
Compute the EAY using the 120-day HPY of 2.85%.
hpr2ear(hpr = 0.0285, t = 120)
## [1] 0.08923
What is the money market yield for a 120-day T-bill that has a bank discount yield of 4.50%?
bdy2mmy(bdy = 0.045, t = 120)
## [1] 0.04569
Assume the price of a $10,000 T-hill that matures in 150 days is $9,800. The quoted money market yield is 4.898%. Compute the HPY and the EAR.
hpr(ev = 10000, bv = 9800)
## [1] 0.02041
mmy2hpr(mmy = 0.04898, t = 150)
## [1] 0.02041
hpr2ear(hpr = mmy2hpr(mmy = 0.04898, t = 150), t = 150)
## [1] 0.05039
ear2hpr(ear = hpr2ear(hpr = mmy2hpr(mmy = 0.04898, t = 150), t = 150), t = 150)
## [1] 0.02041
What is the yield on a bond-equivalent basis of a 3-month loan has a holding period yield of 4%?
hpr2bey(hpr = 0.04, t = 3)
## [1] 0.1632
What is the yield on a bond-equivalent basis of an investment with 6% effective annual yield?
ear2bey(ear = 0.06)
## [1] 0.05913
A portfolio consists of 40% common stocks, 50% bonds, and 10% cash. If the return on common stocks is 9%, the return on bonds is 6%, and the return on cash is 1%, what is the portfolio return?
wpr(r = c(0.09, 0.06, 0.01), w = c(0.4, 0.5, 0.1))
## [1] 0.067
or
rs = c(0.09, 0.06, 0.01)
ws = c(0.4, 0.5, 0.1)
wpr(r = rs, w = ws)
## [1] 0.067
For the last three years, the returns for Acme Corporation common stock have been -5%, 11%, and 9%. Compute the compound annual rate of return over the 3-year period.
geometric.mean(r = c(-0.05, 0.11, 0.09))
## [1] 0.04751
An investor purchases $10,000 of stock each month, and over the last three months the prices paid per share were $4.5, $5.2, and $4.8. What is the average cost per share for the shares acquired?
harmonic.mean(p = c(4.5, 5.2, 4.8))
## [1] 4.816
Download historical financial data from Yahoo finance
apple <- get.ohlc.yahoo(symbol = "AAPL", start = "2013-07-01", end = "2013-08-01")
head(apple)
## date open high low close volume adjusted
## 23 2013-07-01 402.7 412.3 401.2 409.2 13966200 406.5
## 22 2013-07-02 410.0 421.6 409.5 418.5 16780900 415.7
## 21 2013-07-03 420.9 423.0 417.4 420.8 8604600 418.0
## 20 2013-07-05 420.4 423.3 415.4 417.4 9786600 414.7
## 19 2013-07-08 420.1 421.0 410.6 415.1 10647800 412.3
## 18 2013-07-09 413.6 423.5 410.4 422.4 12592300 419.6
Download historical financial data from Google Finance
google <- get.ohlc.google(symbol = "GOOG", start = "2013-07-01", end = "2013-08-01")
head(google)
## date open high low close volume
## 23 2013-07-01 886.5 892.1 885.0 887.9 1726780
## 22 2013-07-02 890.2 891.0 877.3 882.3 1891812
## 21 2013-07-03 879.9 889.2 878.5 886.4 1048628
## 20 2013-07-05 890.0 895.4 887.3 893.5 1701830
## 19 2013-07-08 899.2 906.3 897.1 905.1 1970157
## 18 2013-07-09 911.0 913.0 898.0 905.2 1979338
Download multiple historical financial data from Yahoo finance
applegoog <- get.ohlcs.yahoo(symbols = c("AAPL", "GOOG"), start = "2013-01-01",
end = "2013-07-31")
head(applegoog$AAPL)
## date open high low close volume adjusted
## 146 2013-01-02 553.8 555.0 541.6 549.0 20018500 538.7
## 145 2013-01-03 547.9 549.7 541.0 542.1 12605900 531.9
## 144 2013-01-04 537.0 538.6 525.8 527.0 21226200 517.1
## 143 2013-01-07 522.0 529.3 515.2 523.9 17291300 514.0
## 142 2013-01-08 529.2 531.9 521.2 525.3 16382400 515.4
## 141 2013-01-09 522.5 525.0 516.0 517.1 14557300 507.4
head(applegoog$GOOG)
## date open high low close volume adjusted
## 146 2013-01-02 719.4 727.0 716.5 723.2 2541300 723.2
## 145 2013-01-03 724.9 731.9 720.7 723.7 2318200 723.7
## 144 2013-01-04 729.3 741.5 727.7 738.0 2763500 738.0
## 143 2013-01-07 735.5 739.4 730.6 734.8 1655700 734.8
## 142 2013-01-08 735.5 736.3 724.4 733.3 1676100 733.3
## 141 2013-01-09 732.3 738.4 728.6 738.1 2024700 738.1
Download multiple historical financial data from Google Finance
all <- get.ohlcs.google(symbols = c("YHOO", "SPY", "SINA"), start = "2013-01-01",
end = "2013-07-31")
head(all$YHOO)
## date open high low close volume
## 146 2013-01-02 20.20 20.32 20.01 20.08 20463033
## 145 2013-01-03 20.05 20.10 19.72 19.78 19599094
## 144 2013-01-04 19.76 19.95 19.72 19.86 12489700
## 143 2013-01-07 19.56 19.58 19.28 19.40 23866609
## 142 2013-01-08 19.32 19.68 19.30 19.66 16932176
## 141 2013-01-09 19.73 19.75 19.22 19.33 21656278
head(all$SPY)
## date open high low close volume
## 146 2013-01-02 145.1 146.2 144.7 146.1 192058911
## 145 2013-01-03 146.0 146.4 145.3 145.7 144761781
## 144 2013-01-04 146.0 146.6 145.7 146.4 116817675
## 143 2013-01-07 145.8 146.4 145.4 146.0 110002444
## 142 2013-01-08 145.7 145.9 145.0 145.6 121265078
## 141 2013-01-09 145.9 146.3 145.6 145.9 90745581
head(all$SINA)
## date open high low close volume
## 146 2013-01-02 52.24 55.19 51.75 52.27 3947513
## 145 2013-01-03 52.34 53.61 51.54 52.77 2200712
## 144 2013-01-04 52.70 52.94 51.70 52.76 1466652
## 143 2013-01-07 51.81 52.67 51.40 52.57 1368991
## 142 2013-01-08 52.20 53.00 51.41 51.81 1381026
## 141 2013-01-09 51.76 52.43 50.61 51.54 2098518
Line chart
apple <- get.ohlc.yahoo(symbol = "AAPL", start = "2013-07-01", end = "2013-08-01")
lineChart(apple)
Candlestick chart
google <- get.ohlc.yahoo("GOOG", start = "2013-07-01", end = "2013-08-01")
candlestickChart(google)
## Warning: Removed 1 rows containing missing values (geom_segment). Warning:
## Removed 1 rows containing missing values (geom_segment). Warning: Removed
## 1 rows containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment). Warning: Removed 1 rows
## containing missing values (geom_segment).
Volume chart
apple <- get.ohlc.google("AAPL")
volumeChart(apple)
Multiple line chart
googapple <- get.ohlcs.yahoo(c("GOOG", "AAPL", "SPY"), start = "2013-01-01",
end = "2013-06-30")
lineChartMult(googapple)