You are on page 1of 12

NG DNG PHN MM R TRONG PHN TCH C PHIU

L Vn Tun1 Phng Duy Quang2

R l mt phn mm m ngun m s dng cho phn tch thng k v th, bn c th download min ph t trang ch r-project.org. Phn ln cc k thut phn tch trong kinh doanh u c R h tr - t thng k n hc my hay cc k thut ti u ha. Bng chng cho sc mnh ca R l nhng gii thng v s tn dng t nhng tp ch hay cng ng uy tn trn th gii nh New York Times, Forbes, Intelligent Enterprise, InfoWorld v The Register. C th xem R l mt ngn ng bc cao c thit k cho quant nhng chuyn gia trong lnh vc phn tch ti chnh. Trong bi vit ny, chng ti s trnh by mt s ng dng ca R trong phn tch c phiu, bao gm cc ch s thng k, bin ng Volatility, thc o ri ro Value at Risk v h s beta. 1. c d liu 1.1. Th trng Vit Nam D liu ca th trng chng khon Vit Nam c th download t trang cophieu68.com, d liu lu trong file .csv (c c bng excel), t trong th mc: D:\WorkspaceR\data (D liu t ngy u tin 28/7/2000 n ngy 30/3/2012) D liu ca VN Index k t ngy u tin > setwd("D:/WorkspaceR/data") ##ng dn

> vnindex<-read.csv("excel_^vnindex.csv", header=TRUE) ##c d liu > vnindex ##Hin th d liu

X.Ticker. X.DTYYYYMMDD. X.Open. X.High. X.Low. X.Close. X.Volume. 1 2 3


1 2

^VNINDEX ^VNINDEX ^VNINDEX

20120330 442.6 443.7 437.8 441.0 67123170 20120329 449.0 449.0 439.5 439.6 74524400 20120328 443.2 447.1 437.1 446.3 82025950

ThS. L Vn Tun Trng i hc Thng mi ThS. Phng Duy Quang Trng i hc Ngoi thng

. 2743 ^VNINDEX 2744 ^VNINDEX > attach(vnindex) 20000731 101.5 101.5 101.5 20000728 100.0 100.0 100.0 ##Tch cc ct d liu 101.5 100.0 10300 4200

> vnclose<-rev(X.Close.) ##Ly ct d liu gi ng ca theo chiu tng thi gian D liu ca VN Index trong 100 ngy giao dch gn nht Sa cu lnh read.csv trn thnh, cc lnh khc tng t > vnindex100<-read.csv("excel_^vnindex.csv", header=TRUE,nrows=100) 1.2. Th trng th gii c d liu ca cc th trng trn th gii, ta s s dng packages quantmod, ci t package ny nh sau: > install.pakages(quantmod) c d liu ca ch s Dow Jones ca th trng M t trang finance.yahoo.com, t 28/7/2000 - 30/3/2012 > library(quantmod) ##Gi package quantmod x dng

> getSymbols('^DJI',src='yahoo', from="2000-07-28", to="2012-03-30") > DJI DJI.Open DJI.High DJI.Low DJI.Close DJI.Volume DJI.Adjusted 2000-07-28 10594.97 10732.14 10367.28 10511.17 980000000 2000-07-31 10514.29 10727.09 10374.00 10521.98 952600000 . 2012-03-30 13147.94 13249.12 13127.58 13212.04 3676890000 13212.04 10511.17 10521.98

> djiclose<-data.frame(DJI)$DJI.Close ##Ly chui d liu ng ca ca Dow Jones Ghi ch: ^DJI l k hiu ca ch s Dow Jones trn trang finance.yahoo.com; tng t nh trn, ta c th ly c d liu ca cc ch s th trng cc c phiu ca mt s th trng ln trn th gii (c trn yahoo finance).

2. V th 2.1. Th trng Vit Nam th gi ca VN index k t ngy u tin > plot(vnclose,type="l",main=" th VN index: 28/7/2000 - 30/3/2012",xlab="Ngy", ylab="Gi ng ca", col = "blue")

So snh th ca VN index v c phiu HAG Ly chui gi 100 ngy gn nht ca VN index v HAG vo bin vnclose100 v hagclose100, ta s v th ca VN index v c phiu HAG trn cng mt hnh > matplot(cbind(vnclose100,hagclose100),type="l",col=c("blue","red") ,main=" th ca VNindex & HAG")

2.2. Th trng th gii Do d liu ca mt s th trng th gii c R h tr phn tch k thut nn s dng gi quantmod s cho th p v hu ch hn. ##Bin DJI c trn > chartSeries(DJI, theme = chartTheme("white"), subset='last 3 months')

3. Biu v phn phi xc sut Ta thng s dng biu v hm phn phi ca chui li tc hn l chui gi, chui li sut c tnh nh sau (khi c chui gi ng ca vnclose): > n<-length(vnclose) > vnreturn<-(vnclose[2:n]-vnclose[1:(n-1)])/vnclose[1:(n-1)] 3.1. Biu > hist(vnreturn, breaks=12, col="blue")

3.2. Phn phi xc sut > d <- density(vnreturn) ##Hm mt > plot(d)

## ly cc ch s thng k ta g >d Data: vnreturn (2743 obs.); x Min. :-0.080595 y Min. :7.271e-04 Bandwidth 'bw' = 0.002297

1st Qu.:-0.038603 1st Qu.:1.863e-01 Median : 0.003389 Median :2.111e+00

Mean : 0.003389 Mean :5.948e+00 3rd Qu.: 0.045381 3rd Qu.:6.903e+00

Max. : 0.087373 Max. :3.951e+01 4. bin ng Volatility 4.1. Volatility ca th trng Vit Nam Tnh bin ng Volatility ## Ta c bin vnclose (lu d liu ch s VN Index t 28/7/2000 - 30/3/2012) > n<-length(vnclose) > volatility<-sqrt(252)*sqrt(var(log(vnclose[2:n]/vnclose[1:n-1])))

> volatility [1] 0.2765367 Volatility ca VN Index trong mt s thi k

Tnh ton tng t cho cc thi k: Nm 2007 : Th trng tng trng nng Nm 2008 : Th trng gim mnh Nm 2010: Th trng gn nh i ngang Volatility ca VN-Index (%) 28/7/2000 - 30/3/2012 2007 2008 2010 27.65 28.07 37.23 20.6

Qua bng tnh ton trn, ta thy rng bin ng ca VN-index trong hn 10 nm qua khng cao. (Theo quy c bin ng nh hn 20% c th xem l bin ng mc thp, nn ln hn 40% c th xem l mc cao). Volatility ca nm 2007 (nm tng trng nng) cng khng mc cao, th hin s d dt, thm d trc khi nho . Trong cc s liu, nm 2008 c mc cao nht, y l giai on th trng i xung v bin ng kh mnh, nm 2010 c mc thp nht, phn nh ng giai on i ngang v khng bin ng nhiu ca th trng.

Volatility ca mt s c phiu Tnh ton tng t cho chui d liu 100 ngy giao dch (4/11/2011 30/3/2012), ca cc c phiu HAG (Tp on Hong Anh Giai Lai c phiu blue chip ca ngnh bt ng sn), STB (Ngn hng Sacombank c phiu blue chip ca ngnh ngn hng), HT1 (Cng ty c phn xi mng H Tin 1 c phiu penny stock) ta c bng Volatility sau:

Volatility (%) 100 ngy giao dch VN index HAG STB HT1 21.46 51.59 42.01 45.89

T bng trn, ta thy rng volatility ca VN-index trong khong thi gian qua mc thp, n phn nh ng tnh trng m m ca th trng chng khon Vit Nam hn th biu ca nn kinh t t nc. Xt ring, volatility ca cc c phiu STB, HAG u mc cao, n c th th hin nim tin v li nhun ca nh u t vi 2 ngnh ny trong tng lai nhng cng c th th hin nhng bt n trong c 2 lnh vc xy ra trong nm 2011 v c th cn ko di trong nm 2012. C phiu HAG c bin ng mc cao nht, hn gp i so vi VN-index v hn 20% so vi STB, nguyn nhn chnh c l do tuyn b rt hon ton khi u t trong lnh vc bt ng sn ca ch tch tp on Hong Anh Gia Lai (trong 5 nm ti) gy nn bt n ca c phiu ny. C phiu HT1, mc d l c phiu nh, nhng bin ng khng cao hn nhiu so vi STB v nh hn hn HAG, n th hin s gim hng th ca nh u t vi loi c phiu ny nhng c phiu thng thng c bin ng cao, ph hp lt sng kim li.

4.2. Volatility ca mt s th trng trn th gii Tnh Volatility ca cc ch s Dow Jones & NASDAQ (k hiu ^IXIC) ca M, ch s FTSE 100 (k hiu ^FTSE) ca Anh, ch s Hang Seng (k hiu ^HSI) ca Hng Kng ta c bng sau:

Volatility (%): 28/7/2000 - 30/3/2012 VN Index Dow Jones NASDAQ FTSE 100 Hang Seng 27.65 20.26 27.82 20.87 25.75

Qua bng tnh ton trn, ta thy rng bin ng ca VN Index ch ngang vi NASDAQ v Hang Seng, khng nh nh gi ch quan ca nhiu chuyn gia l th trng chng khon Vit Nam bin ng mnh hn nhiu so vi cc th trng trn th gii. Nh vy, nu ni v ri ro (theo quan im ca Volatility) th u t chng khon Vit Nam khng ri ro hn nhiu so vi cc th trng trn th gii, vo thi im khng hong, c nhng ngy ph Wall cng bc hi hng ngn t USD. Tuy nhin, tr li cu hi: u t th trng chng khon Vit Nam c ri ro hn nhiu so vi cc th trng trn th gii hay khng? l vn phc tp; khng ch l vic nh lng ri ro m cn vn li nhun thc t u t. 5. Value at Risk Value at Risk (gi tr chu ri ro) ca mt ti sn (hoc danh mc, ch s) l mt con s, k hiu l VaR(,), cho bit vi tin cy (1- )100%, sau mt chu k thi gian th li sut ca ti sn st gim khng qu VaR(,)100%

Chu k c th l ngy, thng, nm nhng vi cc chu k thi gian khc u c th quy i v chu k ngy, thng chn l 0.01 hoc 0.05; VaR cng c tnh cho chui gi, tuy nhin t VaR ca chui li sut ta cng tnh c VaR ca chui gi. C nhiu phng php tnh VaR nh phng php lch s, phng php tham s, phng php m phng, s khc nhau nm cc phng php cc nh phn phi ca chui li sut. s dng R tnh VaR, ta ci t packages PerformanceAnalytics > install.packages(PerformanceAnalytics) 5.1. VaR ca th trng Vit Nam Tnh VaR(chu k ngy, = 0.05) ##Ta c chui li sut ca VN index ( mc 3) vnreturn > library(PerformanceAnalytics) ##Gi package ra x dng ##VaR lch s > VaR(vnreturn, p=.95, method="historical") VaR -0.02874779 ##VaR tham s (gi thit phn phi chun) > VaR(vnreturn, p=.95, method="gaussian") VaR -0.02793978 ##VaR Cornish-Fishe hiu chnh (phng php c nh gi l khc phc c nhc im gi thit chun ca chui li sut) > VaR(vnreturn, p=.95, method="modified") VaR -0.02752093 VaR ca mt s c phiu Ta dng phng php VaR lch s tnh VaR theo ngy (ta ch ly chui d liu 800 ngy cc c phiu u c s ngy giao dch: 14/1/2009 30/3/2012) ca cc c

phiu HAG, STB, VT1 v tnh li chi VN index c hai mc tin cy 95% v 99%. Kt qu tnh cho trong bng sau: VaR0.05 (%) VN index HAG STB HT1 -2.83 -4.56 -3.47 -4.44 VaR0.01 (%) -4.01 -4.91 -4.88 -4.90

Nh vy, trong c hai trng hp, ri ro ca c phiu ngn hng STB u nh hn c phiu bt ng sn HAG v c phiu penny stock HT1, iu ny ph hp vi nh gi cao ca nh u t Vit Nam i vi cc c phiu ngn hng. 5.2. VaR ca mt s th trng Tng t mc 1 v mc 3, ta ly c chui li sut ca cc th trng theo ngy, thi gian t 28/7/2000 - 30/3/2012, tnh VaR lch s ta c bng sau: VaR0.05 (%) VN Index HNX index(*) Dow Jones FTSE 100 Hang Seng -2.87 -3.89 -1.99 -2.08 -2.51 VaR0.01 (%) -4.57 -6.54 -3.63 -3.79 -4.43

(*) Chui s liu ca HNX index ly t 4/1/2006 (ngy th trng bt u m ca) 30/3/2012

T bng tnh trn, ta thy ri ro ca th trng Vit Nam qua hai ch s VN index v HNX index u cao hn ri ro ca ch s cc sn chng khon trn th gii. 6. H s beta H s beta l i lng o tc thay i tng i ca mt c phiu (hoc danh mc) vi th trng. Phn ln h s beta l s dng cc c phiu thng bin ng cng chiu th trng h s beta cng ln th tc tng/ gim cng chiu vi th trng cng ln; khi h s beta m, n th hin c phiu bin ng ngc th trng. Sau u, ta s tnh ch s beta ca mt s c phiu trn sn HOSE vi ch s i din th trng VN index, chui d liu 100 ngy giao dch (4/11/2011 30/3/2012) Tnh h s beta ## Tng t mc 1 & 3, ta ly c chui li sut vnreturn v hagreturn gm 100 ngy giao dch > beta<-cov(hagreturn,vnreturn)/var(vnreturn) > beta [1] 1.772361 H s beta ca mt s c phiu Tnh ton tng t cho c phiu STB v HT1 ta c bng Beta HAG STB HT1 1.77 0.45 0.76

So snh vi bng tnh Volatility mc 6, ta thy cc tnh ton th hin ng l thuyt, cc c phiu c Volatility cao th cng c h s beta cao.

Kt lun Bi vit ny trnh by mt s ng dng ca R trong phn tch c phiu. Gi tr s dng ca R trong dy v hc, nghin cu khoa hc cng nh trong cng vic ca nhng chuyn gia phn tch, nghin cu kinh t, l rt ln. Mc d l phn mm min ph v hu dng nhng R vn cha c s dng ph bin Vit Nam, mt phn v ti liu ting Vit cha nhiu, mt phn v hu ht cc trng i hc vn cha s dng R trong ging dy. Hy vng l trong thi gian ti, cc trng i hc, cc nh nghin cu trong nc s s dng R nhiu hn cng nh c nhng ng gp ca ngi Vit cho cng ng pht trin R trn th gii.

Ti liu tham kho [1] . . Thi N. T. Dng. Nhp mn Ton ti chnh (Sp xut bn) [2] H. . Tun. M Hnh Phn Tch V nh Gi Ti Sn Ti Chnh, Tp 1. 2010. NXB KHKT [3] L. V. Tun (ch bin). Hng dn s dng phn mm R cho cc hc phn ton bc i hc (Sp xut bn)