R Code and examples
for David Ruppert 's book: Statistics and Finance: An Introduction
Shortcut I: Chapter 2, 3 (Returns), 4 (Times Series), 5 (Portfolio Theory), 7 (CAPM), 8 (Option Pricing), 9 (Fixed Income), 10 (Resampling), 12 (Garch); Useful Resources, Link of the book , About this Homepage
Note: Some programming might require additional R packages, please install required packages before running them. Please click Useful Resources for package information .
Chapter 2: Probability and Statistical Models
Fig 2.2 & R Code : Binomial Probability distribution with n=10 and p=0.6
Fig 2.3 & R Code: Example of Normal Probability densities
Fig 2.4 & R Code: Example of lognormal Probability densities
Fig 2.5 & R Code: CDF & Empirical CDF
Fig 2.6&2.7 & R Code:
Fig 2.8 & R Code: Several binomial probability distributions with n=10 and their skew ness
Fig 2.9 & R Code: Comparison of a normal density and a t-density with 5 degrees of freedom. Both densities have mean 0 and standard deviation 1.
Fig 2.10 & R Code:
Fig 2.11 & R Code: Comparison on normal and heavy-tailed distributions.
Fig 2.12 & R Code: Survival function of a Pareto distribution with c=0.25 and a=1.1 and of normal and exp distribution on being greater than 0.25.
Fig 2.13 & R Code: Sample correlation coefficients for eight random samples.
Fig 2.14 & R Code: Prior and posterior densities in Examples 2.17.
Fig 2.15 & R Code: Posterior CDF in Examples 2.17.
Required packages: fSeries nortest
Fig 3.1 & R Code: Comparison of functions log(1+x) and x
Fig 3.2 & R Code: Means and bounds on a random walk with S0=0, mean = 0.5 and standard deviation=1.
Fig 3.3 & R Code: Two independent simulations of a geometric random walk (GRW) with u=0.1 and sigma=0.2
Fig 3.4 & R Code : GE Daily return. The first plot is the prices. The second and third are the net returns and log returns. The fourth plot is a normal probability plot of log returns. The final plot is of the absolute value of log returns.
Fig 3.5 & R Code: Five independent simulations of a geometric random walk( GRW) and GE Daily daily log returns.
Fig 3.6 & R Code: Three independent simulations of Price series. On Left: log prices. On right: prices.
Basic Statistic (including skewness and kurtosis) and Normality Tests for univariate statistics, using GE Daily return data.
An example for downloading Finance data from YAHOO.
Required packages: fSeries tseries
Fig4.1 & R Code: Autocorrelation functions of AR(1) processes with r equal to 0.95 , 0.75,0.2 and -0.9
Fig4.2 & R Code: Simulations of 200 Observations from AR(1) processes with various parameters. The white noise process is the same for all four AR(1) Processes.
Fig4.3 & R Code: Simulations of 30 Observations from AR(1) processes with various parameters. The white noise process is the same for all four AR(1) Processes.
Fig4.4 & R Code: Simulations of 1000 Observations from AR(1) processes with various parameters. The white noise process is the same for all four AR(1) Processes.
Fig4.6 & R Code: AR(1) Process with mu=0.4 and a=0.4, the integral of this AR(1) and second integral of this AR(1).
Fig4.7 & R Code: Time series plot of the 3 month Treasury bill rates, plot of first differences, and ACFs. The data set contains monthly values of the 3 month rates from Jan 1950 until Mar 1996.
Model fit and diagnostics:
Model Fit Examples R Codes: Fit GE Daily log return using AR(1),AR(6), MA(2), ARMA(2,1) and log price using ARIMA(2,1,0) Model
Results: AR(1), AR(6), MA(2), ARMA(2,1), ARIMA(2,1,0)
Residual Plots: AR(1), AR(6), MA(2), ARMA(2,1), ARIMA(2,1,0)
Model Selection:
Forecasting:
Fig4.8 & R Code & Predicts: Time series plot of the daily GE log returns with forecasts from an AR(1).
Fig4.9 & R Code: Time series plot of the daily GE log Prices with forecasts from an ARIMA(1,1,0) Model.
Required packages: quadprog
Fig5.1 & R Code: Expected Return for a portfolio with allocation w to the risky asset and risk free return.
Fig5.2 & R Code: Expected Return Versus risk for Example 5.4.
Fig5.3 & R Code: Expected frontier and tangency portfolio with different r.
Tangency portfolio without the constraints R code
Fig5.4 : Efficient frontier (solid) plotted for N=3 assets.
Fig5.5 : Weights for assets 1,2 and 3 as functions of return.
Fig5.7 : Weights for assets 1,2, and 3 as function of return. The weights for all three assets are constrained to be nonnegative
Fig5.8 : Efficient frontier and line of optimal combination of N=3 risky assets with risk free asset.
Tangency portfolio with the constraints R Code:
Fig5.6 : Efficient frontier for N=3 with and without constraint of no negative weights.
Fig5.9 : Efficient frontier and tangency portfolio with the constraints that the weights are nonnegative.
Chapter 7: The Capital Asset Pricing Model
Required packages: pspline
Example 7.3: R Code, Data & Output, Page 239
An interesting Question: R Code & Data, Page 247-250
Output: 1. Linear regression with an intercept,
2. Broken Line Model: output
3. Linear regression with no intercept: output
Figure 7.4: Monthly excess log return from 03-1996 to 02-2001 for Ford plotted against those for the S&P 500 index. The curve is a a penalized spline.
Figure 7.5: Excess log returns of Ford plotted against those of the S&P 500 with three fits, a penalized spline, broken line model and line with no intercept.
Figure 7.6: Gross k period return of S&P 500 and Ford since 03-1996 plotted against k.
Is Beta Constant? Example 7.4: R Code ,Data & Output, Page 251
R Code Volatility smiles and polynomial regressionpage 283-284
Fig8.9 : A plot of hatV vs K when T=mean(T) for GE options.
Fig8.10: A plot of hatV vs T when K=mean(K) for GE options.
R Code: Page285
Fig8.11: A plot of hatV vs K and T for GE call options.
Fig8.12: A plot of standard error of Hat(V) vs K and T for GE call options.
Fig 8.14 : Evolution of option prices. Stock prices is a Geometric Brownian motion. Page 289.
Fig 8.15: Ratio of Log Return on a call to log return on the underlying stock. Page 291.
Fig 8.16: Ratio of Log Return on a put to log return on the underlying stock. Page 291.
Fig 8.17: Delta and Theta for the call option and simulated data (stock 2). Page 294.
Chapter 9 Fixed Income Securities
Fig 9.1 & R Code : Bond price versus the interest rate r. Page 308
R Code for Fig 9.3 : Prices of the U.S. STRIPS plotted as a function of maturity. Page 322
Fig 9.4 : Polynomial and spline estimates of forward rates of U.S. Treasury bonds.
output: Anova for nonlinear regression.
Resampling and Efficient Portfolios:
Fig 10.2 : Time series plots of returns for the 10 equity markets. Page 333
Fig 10.3: Autocorrelation plots of returns for the 10 equity markets. Page 334
Fig 10.4 : Normal probability plots of returns for the 10 equity markets. Page 335
Fig 10.5 : Actual efficient frontier for the sample (optimal) and bootstrap efficient frontier (achieved) for each of six bootstrap resamples.
Fig 10.6: Results from 400 bootstrap resamples. For each resample, the efficient portfolio with a mean return of 0.012 is estimated. In the upper subplot, the actual mean return and standard deviation of the return are plotted as a small dot. The large dot is the point on the efficient frontier with mean return of 0.012.
Fig 10.7 : Results from 400 bootstrap resamples assuming that the vector of mean returns is known. For each resample, the efficient portfolio with a mean return of 0.012 is estimated using the mean returns from the sample and the covariance matrix of return from the resample.
Required packages: fSeries
Data Simulation:
Fig12.2 & R Code: Simulations of 60 observations from an ARCH (1) process and an AR(1)/ARCH(1) process. Page 368
Fig12.3 & R Code: Simulations of 600 observations from
an ARCH (1) process and an AR(1)/ARCH(1) process. Page 369
Fig12.4 & R Code: Simulations of 600 observations from
an GARCH (1,1) process and an AR(1)/GARCH(1,1) process. Page 371.
Model Fit:
** R Code: GARCH Model Fit, Page 373.
R General: R Project R Package Index standard library Contributed Packages
The R Manuals: An Introduction to R [browse HTML | download PDF ]
The R language definition [browse HTML | download PDF ]
R for Beginners (PDF) R reference card (PDF)
Finance: CRAN Task View: Empirical Finance
Finance Package Rmetrics :
Overall:
Overview
Fact Sheet
Reference Card
Packages:
fBasics
fCalendar
fMultivar
fExtremes
fOptions
fSeries
fPortfolio
Times Series Packages:
|
Time series analysis and computational finance |
Other Packages:
|
Multivariate Dependence with Copula |
|
Functions to solve Quadratic Programming Problems. |
|
Tests for Normality. |
Other Docs:
Emacs
RefCard
ESS RefCard
About :
This homepage is constructed and maintained by Lei Jin and Dr Huang, Jianhua .
Any Suggestion? Please send email to ljin@stat.tamu.edu or jianhua@stat.tamu.edu