Wednesday, August 22, 2012

R for Financial Engineering Course on Applying R in Quantitative Finance/ Analytics & Projects at

R for Financial Engineering / Quantitative Finance

R is mix of MATLAB and SAS, if I relate to the other 2 tools used in Finance.
R is having mix of maths and data handling powers of SAS and given that it is open source and the best choice for research.

R is MATLAB when it comes to syntax.
When it comes to data handling it uses RMySQL in the very same way as SAS does.
Hence, it does things that SAS+MATLAB do for free.
SQL has inherent limitations of doing quant computations, and MATLAB is heave and requires too much resources. MATLAB is not very good to deal with SQL at system level.

But to start with, one also needs to know VBA for entry level and for easy task, VBA is preferred.

  1. 10 hours on handling data, functions, indexing, structures, data linkages, importing, SQL, etc
  2. 10 hours on Financial Commands, tools, pre-built libraries.

I will discuss in detail about R and its application in this post.

Keep checking it for updates.

Ref: R in a Nutshell

For getting some info you can join my MATLAB Course at:

Join our MATLAB for financial engineering course ( & get 15% discount. Ask for discount code, e-mail:-

Course based on:

Introduction to R
  • Some background on R, how to obtain and how to install it.
  • R documentation and help and web resources available.
  • R Console and R GUI.
  • Data import, data types and variables.
  • Elementary import functions.
  • Data types.
  • Variable generation, inspection and modification.
  • Graphics in R.
  • Command "plot" and where to plot.
  • Setting the par.
  • High-level and low-level plotting functions legends.
  • Math in plots.  
  • Hands-on Session—data handling & graphics.
  • Simulations in R.
  • Simulating from given distributions.
  • Risk management basics.
  • Data Structures in R.
  • Review of types of objects.    
  • Additional object types.
  • Basics of the R Language. 
  • Conditionals & Loops. 
  • Writing R Functions: Basic functions.
  • Object-orientation.
  • Debugging.
  • Application in Finance.
  • Calculating Value at Risk.
  • Packages for Portfolio Optimization. 

Functions of R/R Commands to be covered in the course:
Caution: Always save workspace.
Use “c” when you want to use it a signal in black scholes formula.

read.table function will be used to get the data into R (/\ used opposite
than the one used in windows)

Example 1
> test.txt <- read.table ("C:/Documents and Settings/shivbhakta.joshi/My
Documents/test.txt", header=T)
> print(test.txt)
> column <- test.txt[,c('make','price')].

Example 2
grb <- read.table ("C:/Documents and Settings/shivbhakta.joshi/My
Documents/GRB_afterglow.dat", header=T, skip=1)
x <- (grb[,1]).

Grabbing data from tables into a single dimensional array
x <- (grb[,1])
y <- log(grb[,1]).

Another way to create returns for stock daily return is
f=dft$age/dft$price (using dollar sign extraction)
Deleting column
> add <- add[-2] to delete the element 2nd
Adding data to array busing column command
> add <- c(1,d).

Using Data frames
Data.frame is used to combine the arrays back into table format
people = data.frame (x,y)
Write.CSV command executed

grb <- read.table ("C:/Documents and Settings/satyadhar.joshi/My
Documents/GRB_afterglow.dat", header=T, skip=1)
x <- (grb[,1])
y <- log(grb[,1])
people = data.frame (x,y)

These methods can be used to compute beta from columns of a csv file.

Working formula of Black Scholes
BS <-
function(S, K, T, r, sig, type="C"){
d1 <- (log(S/K) + (r + sig^2/2)*T) / (sig*sqrt(T))
d2 <- d1 - sig*sqrt(T)
value <- S*pnorm(d1) - K*exp(-r*T)*pnorm(d2)
value <- K*exp(-r*T)*pnorm(-d2) - S*pnorm(-d1)

Black Scholes <- function(s, k, r=.1, t=5, sigma=.9,call=TRUE) {
#calculate call/put option
d1 <- (log(s/k)+(r+sigma^2/2)*t)/(sigma*sqrt(t))
d2 <- d1 - sigma * sqrt(t)
ifelse(call==TRUE,s*pnorm(d1) - k*exp(-r*t)*pnorm(d2),k*exp(-r*t)
* pnorm(-d2) - s*pnorm(-d1))

Predefined Black schools model:
black_scholes ("c",5600,5600,.1,.08,.22 )
this depicts use of functions in R.

Merton Distance to Default (different as per needs)
(modified from Black Scholes formula, still
unsure about pnorm() function)
d1 <- (log(s/k)+(sigma^2/2))/(sigma)

3 files are needed to run the demos which are available on internet by various authors:
  1. black_scholes.R
  2. GRB_afterglow.dat
  3. test.txt.

ARIMA and Forecasting Using R


> Quandl.auth("1LkmpypqJskJzKcpd2TV")

>stockData=Quandl("YAHOO/INDEX_GSPC", start_date="2004-01-01", end_date="2014-04-17")


// ran Arima here:
results2=arima(stockData[,7], order = c(3,0,0))

fit <- arima(myts, order=c(p, d, q)

fit <- arima(stockData[,7],order = c(3,0,0))


fit <- arima(stockData[,7],order = c(3,0,0))

predict(arima(stockData[,7], order = c(3,0,0))


x2=(forecast(fit, 100))


Excel and R:

Playing with arrays and matrix

ARIMA and Predict statement

Market check

Other good References

One on One Customized Training: believes in personalized touch so that our clients are completely satisfied with our service. In this regard, we offer One on One Customized Training to our clients.
These Trainings are provided on request by our clients & are customized according to their individual needs.
The course structure & timings for these training are highly flexible, classes are scheduled as per the convenience of our clients. 

Contact Us for More details:


  1. Can you guide me on how to link R to matlab??

    You can create 20% of course on that.