Pages

Showing posts with label regression. Show all posts
Showing posts with label regression. Show all posts

Saturday, January 14, 2017

Pair Trading using Quantmod - HUL & ITC


In my earlier post, I have explained that very first step for stat Arb Strategies is to find out the relation between two stocks. To analyze the whole data and run the relations could be very time consuming if done using downloading data and running calculations separately.

In this blog we will discuss Quantmod which is a package in R language and can help us to get it done easily. It helps to download the data from Yahoo finance and run quick calculations since the data gets downloaded in the form of vectors.

This blog will be using example of Indian Market's stock prices to understand basic use of quantmod.

TO INSTALL PACKAGE

>install.packages("quantmod")
>library(quantmod)
>require(quantmod)

TO RETRIEVE DATA

> symbolsHUL<-c("HINDUNILVR.NS")
> getSymbols(symbolsHUL)
[1] "HINDUNILVR.NS"
> startT <- "2016-01-01"
> endT <- "2017-01-01"
> rangeT <- paste(startT,"::",endT,sep="")
> HUL <- HINDUNILVR.NS[,6][rangeT]
> symbolsITC<-c("ITC.NS")
> getSymbols(symbolsITC)
[1] "ITC.NS"
> HUL <- HINDUNILVR.NS[,6][rangeT]
> ITC <- ITC.NS[,6][rangeT]

ANALYZE DATA & RUNNING REGRESSION

> pdtHUL <- diff(HUL)[-1]
> pdtITC <- diff(ITC)[-1]
> model <- lm(pdtHUL ~ pdtITC -1)
> hr <- as.numeric(model$coefficients)
 [1] 0.1332138

PLOTTING DATA

> plot(ITC)



Friday, June 17, 2011

Regression Analysis in R

We will perform regression analysis on few banking stocks and nifty index to find out their beta and then make decision on validity of these figures. Regression analysis is done using t test to find out validity of coefficient while standard error and degrees of freedom to find out t score.

To get all the data into R we have download csv files (from nifty website) and following command will be further used to find out return series.

>sbitemp <- read.csv("01-04-2010-TO-01-04-2011SBINALLN.csv")
> sbi <- sbitemp[[9]]

> pnbtemp <- read.csv("01-04-2010-TO-01-04-2011PNBALLN.csv")

> pnb <- pnbtemp[[9]]

> hdfctemp <- read.csv("01-04-2010-TO-01-04-2011HDFCBANKALLN.csv")

> hdfc <- hdfctemp[[9]]
> icicitemp <- read.csv("01-04-2010-TO-01-04-2011ICICIBANKALLN.csv")
> icici <- icicitemp[[9]]

> sbiR <- bankl(sbi,sbi)

> niftyR <- bankl(nifty,nifty)

> pnbR <- bankl(pnb,pnb)
> hdfcR <- bankl(hdfc,hdfc)

> iciciR <- bankl(icici,icici)

Where function bankl has following source code
> bankl function (first,second) { a= first
a[1]=0
k=length(second)

i=2
while(i<=k)
{ a[i]= log(second[i]/first[i-1])
i=i+1 }
a }
>

Let's plot returns of NIFTY as compare to returns of SBI.

Next step is to perform pair wise regression to find out beta for each of these securities. glm function in R language help us to do regression analysis and output can be saved.
> glm(sbiR ~ niftyR) Call: glm(formula = sbiR ~ niftyR) Coefficients: (Intercept) niftyR 0.0005735 1.1515344 Degrees of Freedom: 254 Total (i.e. Null); 253 Residual Null Deviance: 0.09468 Residual Deviance: 0.05277 AIC: -1434

Storing the output of regression into a linear object for (SBI & NIFTY)

> sbi.linear <- glm(sbiR ~ niftyR)
> summary(sbi.linear)

Call:
glm(formula = sbiR ~ niftyR)
Deviance Residuals:
Min 1Q Median 3Q Max -0.051342 -0.008509 -0.000283 0.008339 0.068983
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.0005735 0.0009049 0.634 0.527
niftyR 1.1515344 0.0812352 14.175 <2e-16 *** ---

T value of 14.175 clearly shows the Beta for SBI is 1.151 at more than 99.99% confidence interval. Similarly we will run regression on all the stocks.
> sbiB <- sbi.linear[[1]][[2]]
> sbiB
[1] 1.151534
> pnbB <- pnb.linear[[1]][[2]]
> hdfcB <- hdfc.linear[[1]][[2]]
> iciciB <- icici.linear[[1]][[2]]


(Please change all &gt to >(in symbol) and &lt tp <(in symbol) , I am trying to convert this but his revert back in code)

Reference: