You are on page 1of 37

www.wunan.com.

tw
(02)2705-5066
www.wunan.com.tw
(02)2705-5066

R R
R

1 7
1 7 R 7

11 11 1
R 2 7

i
www.wunan.com.tw
(02)2705-5066
R

t
1 7
8
MLE
R
9 10 MLE
MLE
11
9 11
MLE MLE 8
11

R
R
t t
R t
t t
R

R R
SASSPSS EVIEWS
R
R
R
R R

ii
www.wunan.com.tw
(02)2705-5066

R
R

R
R R
R


5 5

R
R 1

R
R
R

R
R R

iii
www.wunan.com.tw
(02)2705-5066
R

*
TEJ
TEJ TEJ
R

TEJ



2016/8/3

iv
www.wunan.com.tw
(02)2705-5066

v
www.wunan.com.tw
(02)2705-5066
R

vi
www.wunan.com.tw
(02)2705-5066

...................................................................................................................................... ii

1 ..............................................................................1
R 2
21
R 34
46
46

2 .....................................................................53
54
63
71
78
91

3 ..........................................................97
97
102
110
123
134

4 ........................................................139
140
150
166
178

vii
www.wunan.com.tw
(02)2705-5066
R

5 ...............................................................183
184
198
203
218
224

6 ........................................................229
230
237
F 251
261
271

7 ...................................................................275
279
298
312
326
328

8 ........................................................333
334
348
365
378
387

viii
www.wunan.com.tw
(02)2705-5066

9 ........................................................389
MLE 391
MLE 400
t 421
GARCH 431
447

10 .........................................................453
453
Newton 469
486
497
503

11 .............................................................511
511
526
546

1 ................................................................................................565
2..................................................................................................................579
3............................................................................................591

ix
www.wunan.com.tw
(02)2705-5066
R

x
www.wunan.com.tw
(02)2705-5066
1

Statistics

Google 2


R R

1


2 Google Yahoo Statistics and Finance

www.wunan.com.tw
(02)2705-5066
R
R 3
package R
R

R
R

R
R
R S S-PLUS R R
R

R
R R
R 4
R R

1.1 R
R R

# ch1 "#"
x = c(2,3,4)# 2,3,4 x
x # x
y = 0.12345 # y 0.12345

3 www.r-project.org
4 Google Yahoo R
R R

2
www.wunan.com.tw
(02)2705-5066
1
y
round(y)# 4 5
round(y,2)# 2
?round # round
y1 = round(y,2)# y 2 y1
log(x)# x
sqrt(x)# x
y1+x #
y1*x #
x/y1 #
x^y1 # x y1
z = x/y1
z
z1 = round(z,2)
z1
z1[2] # z1 2 ( )
length(z1)# z1 z1
sum(z1)#
exp(z1)# e z1 ,e
min(z1)#
max(z1)#
mean(z1)# z1
sort(z1)#

R #

R ch1-1-1.R
R

1. 1313.513.2512.515 17

3
www.wunan.com.tw
(02)2705-5066
R
2. 1 R
)p = c(13,13.5,13.25,12.5,15,17)
pbar = mean(p)
pbar
maxp = max(p)
maxp
minp = min(p)
minp
range = maxp-minp
range
3. 1 R

)pbar1 = round(pbar,2);pbar1 #
4. 1
)p2 = sort(p);p2
5. 4 p2 3 4
) p2[3:4]
6.
)lp = log(p);lp
7. 100,000 10%5
10%8% 12%
)F = 100000
intr = 0.1
disr = c(0.1,0.08,0.12)#
I = F*intr #
dis = 1+disr
P = I/dis + I/dis^2 + I/dis^3 + I/dis^4 +(I+F)/dis^5
P # 100000.00 107985.42 92790.45
8. 5% 1 1 10

)intr = 0.05
n = 10

4
www.wunan.com.tw
(02)2705-5066
1
F =(1+intr)^n
F
9. yield to maturity, YTM 7 111,419

)disr = 0.085 #
dis = 1+disr
tryP = I/dis + I/dis^2 + I/dis^3 + I/dis^4 +(I+F)/
dis^5
tryP
10. 5% 1
10
)r = 0.05
t = 10
F = exp(r*t)
F
11. present value of annuity 2 2
5%
pmt n i

)pmt = 20000
i = 0.05/12
n = 24
pv = pmt*((1/i)-(1/(i*(1+i)^n)))
pv
12. 45
)pmt = 20000
i = 0.08 # # 0.062747
dis = i/12
n = 24

5
www.wunan.com.tw
(02)2705-5066
R
trypv = pmt*((1/dis)-(1/(dis*(1+dis)^n)))
trypv

1.2 R
R 5 R
TSMC 2000/1/4 2014/7/10 6
1-1 1-2
scatter diagram 1-2
7 1-1

1-1 2000/1/4 2014/7/10

#
# 2000/1/4-2014/7/10

5
6 TSMC TEJ
EPS TSE
0.6

6
www.wunan.com.tw
(02)2705-5066
1

1-2 2000/1/4 2014/7/10


# (TSMC) ( ) (TSE)
tsmcpper = read.table("c:\\meiyih\\Finstats\\ch1\\tsmcpper.txt")
# ,
tsmcp = tsmcpper[,1] #
tsmcper = tsmcpper[,2] #
windows()#
par(mfrow=c(2,1))# 2 1
ts.plot(tsmcp, ylab="TSMC ", main=" ")#
ts.plot(tsmcper, ylab="TSMC ", main=" ")
# ( )
tsmcdiv = tsmcper/tsmcp
tsmcdiv = 1/tsmcdiv
windows()
par(mfrow=c(1,2))# 1 2
#
plot(tsmcper,tsmcp,type="l", ylab=" ", xlab=" ", main="
")

7
www.wunan.com.tw
(02)2705-5066
R
# type
abline(lm(tsmcp~tsmcper),col="blue")#
plot(tsmcdiv,tsmcp,type="p", ylab="", xlab="", main="")
abline(lm(tsmcp~tsmcdiv , col="blue" #

ch1-1-2.R

c
R

1-2
1-2

1. LARGAN
2005/1/4 2015/4/28 1-1 1-2
2. A:(x, y) = (1, 2) B:(x, y) = (2, 4)
C:(x, y) = (3, 6) R

8
www.wunan.com.tw
(02)2705-5066
1
)

x = c(1,2,3)
y = c(2,4,6)
windows()
par(mfrow=c(1,2))
plot(x,y,type="p",xlim=c(0,5),ylim=c(0,8),cex=1.5)# 1.5 ,
# x 0-5,y 0-8
plot(x,y,type="l",xlim=c(0,5),ylim=c(0,8),lwd=2)
# ,2
3. A 20 B 35 A B
4. y = 0.5 + 0.3x
)t = 1:10 # 1 10
y = 0.5+0.3*t
windows()
plot(t,y,type="l",main=" ",xlab=" ")
# ,x
5. 1981/1 2015/5 CPI

)CPI = read.table("c:\\meiyih\\nstats\\ex\\ch1\\cpi.txt"
#

9
www.wunan.com.tw
(02)2705-5066
R
cpi = CPI[,1]
windows()
plot(cpi,type="l",xlab="Time",main="CPI ",lwd=2)
T = length(cpi)# T CPI
T
t = 1:T #
abline(lm(cpi~t),lty=2,lwd=2)# lty=2,

t
6. y = e0.05t y1 = (1 + 0.05) t = 1, 2, ,
20

10
www.wunan.com.tw
(02)2705-5066
1
)

t = 1:20
intr = 0.05
y = exp(intr*t)
windows()
plot(t,y,type="l",lwd=2)
y1 =(1+intr)^t
lines(t,y1,lty=2,col="red",lwd=2)
legend("topleft",c(" "," "),lty=1:2,col=
c("black","red"),bty="n",lwd=2)
7. 5 CPI 5
8. 1
1 250
)p = largan[,1];per = largan[,2] #
n = length(p);p1 = p[(n-249):n]
div = per/p; div = 1/div
div1 = div[(n-249):n]; cYTM = 100*div1/p1 #
length(cYTM)
windows()
plot(cYTM,type="l",xlab="Time",ylab="YTM",main="

11
www.wunan.com.tw
(02)2705-5066
R
",lwd=2)
abline(h=mean(cYTM),lty=2,col="blue")# ( )

1.3 R

p t t
t rt

(1-1)

(1-1)
7

12
www.wunan.com.tw
(02)2705-5066
1

1-3TSMC 10

3,624 10
R 1-3
(1-1) 4 3,623
3,620 362
0.5% 1-3 10
2.5% 3% 51
-2% -2.5% 68 1-3

T = length(tsmcp)# tsmcp T
T
#
r = 100*(tsmcp[2:T]-tsmcp[1:(T-1)])/tsmcp[1:(T-1)] #2:T
2 T
T = length(r)
T
r = r[4:T] # 4 r
T = length(r)

13
www.wunan.com.tw
(02)2705-5066
R
T
m = 10
maxr = numeric((T/m))# T/m , 0
minr = numeric((T/m))
for(i in 1:(T/m))# i 1 T/m( 362)
{
h =(i-1)*m + 1
k = i*m
maxr[i] = max(r[h:k])
minr[i] = min(r[h:k])
}
windows()
par(mfrow=c(2,1))
hist(maxr,main=" 10 ",xlab="
",lwd=2)
freqmax = hist(maxr,plot=FALSE)# hist ,
freqmax
segments(2.75,0,2.75,51,col="black")# ,
#
points(2.75,51,cex=2,pch=20)# (2.75,51) 2
hist(minr,main=" 10 ",xlab="
",lwd=2)# title
freqmin = hist(minr,plot=FALSE)
freqmin
segments(-2.25,0,-2.25,68,col="red")# ,
# (-2.25,0), (-2.25,68)
points(-2.25,68,cex=2,pch=18 #

ch1-1-3.Rloop i
1 2 3628

8 R for(i in 1:362) {} i = 2

14
www.wunan.com.tw
(02)2705-5066
1
10 362
simulation

1. 8382.584 84.5

2. 1 R
) p = c(83,82.5,84,84.5)
T = length(p)
r = 100*(p[2:T]-p[1:(T-1)])/p[1:(T-1)] # %
r
3. r TSMC 10
% freqmax
0 r < 0.5 0.5 r < 1 1 r < 1.5 1.5 r < 2 2 r < 2.5
2 14 33 49 43
0.25 0.75 1.25 1.75 2.25

4. CPI CPI

5.
) T = length(cpi)
T
pit = 100*((cpi[13:T]-cpi[1:(T-12)])/cpi[1:(T-12)])
head(pit)# 1-6
tail(pit)# 1-6
windows()
plot(pit,type="l",xlab="Time",ylab=expression(pi[t]),
main=" ")

h = 11 k = 20 r h k maxr
minr i i = 6 i = 30

15
www.wunan.com.tw
(02)2705-5066
R
6.

7.
)p = largan[,1]
T = length(p)
T
rt = 100*((p[2:T]-p[1:(T-1)])/p[1:(T-1)])
windows()
par(mfrow=c(2,1))
plot(rt,type="l",xlab="Time",ylab=" ",main="
")
hist(rt,breaks=100,main=" ",ylab="
",lwd=2,xlab=" ")

8. 3
)histpi = hist(pit,plot=FALSE)
histpi
9. x = 1:5y = 0.3 + 0.5x y
)x = 1:5
y1 = 0.3+0.5*x

16
www.wunan.com.tw
(02)2705-5066
1
y1
#
y = numeric(5)
for(i in 1:5)
{
y[i] = 0.3+0.5*x[i]
}
y # y y1

1.4
R 1-4
tseries quantmod R
1-4 ch1-1-4.R

# yahoo
# R
library("tseries")# tseries Package,

1-4S&P 500TWI

17
www.wunan.com.tw
(02)2705-5066
R
price = get.hist.quote(instrument = "^gspc", start = "2000-01-01",
end = "2016-01-05",
quote="AdjClose")# S&P 500
head(price)# 6
tail(price)# 6
#
price.tw = get.hist.quote(instrument = "^TWII", start = "2000-
01-01", end = "2016-01-05",
quote="AdjClose")# Taiwan Weighted Index
head(price.tw)
tail(price.tw)
## GRED
# quantmod Package
library(quantmod)
ex1 = getFX("USD/TWD",from="2011-01-01",to="2015-06-30")
ex1
tex = USDTWD
head(tex)
tail(tex)
windows()
par(mfrow=c(3,1))
plot(price,main="S&P 500 ",lwd=2)
plot(price.tw,main="TWI ",lwd=2)
plot(tex,main=" ")

1. YAHOO A 2000/1/1
2015/12/14
)library(tseries)
price.ss = get.hist.quote(instrument = "000001.SS",

18
www.wunan.com.tw
(02)2705-5066
1
start = "2000-01-01", end = "2015-12-14",
quote="AdjClose")#
price.SZSA = get.hist.quote(instrument = "^SZSA",
start = "2000-01-01", end = "2015-12-14",
quote="AdjClose")# A
head(price.ss)
tail(price.ss)
head(price.SZSA)
tail(price.SZSA)
windows()
par(mfrow=c(2,1))
plot(price.ss,type="l",ylab=" ",main="
",lwd=2)
plot(price.SZSA,type="l",ylab=" ",main=" A
",lwd=2)
2.
3. A 7

4. 2004/1/2 2015/5/29

)exch = read.table("c:\\meiyih\\FinStats\\exercises\\ch1\\
exch.txt")
tai = exch[,2] #
chi = exch[,3] #
chi_tai = tai/chi #
windows()
par(mfrow=c(3,1))
plot(tai,type="l",xlab="Time",ylab=" ",main="
")
plot(chi,type="l",xlab="Time",ylab=" ",main="
")
plot(chi_tai,type="l",xlab="Time",ylab=" ",main="

19
www.wunan.com.tw
(02)2705-5066
R
")

5. 1
)priceSS = price.ss
priceSZSA = price.SZSA
twoprice = cbind(priceSS,priceSZSA)#
twoprice # , NA ,
#
write.table(twoprice,"x:\\FSR\\ch1\\twoprice.txt")
6. 5
)two = read.table("x:\\FSR\\ch1\\twoprice.txt",header=T)
#
names(two)#
attach(two)# two
x = AdjClose.priceSS #
y = AdjClose.priceSZSA
x # x NA
x = x[!is.na(x)] # x NA
x
windows()

20
www.wunan.com.tw
(02)2705-5066
1
par(mfrow=c(2,1))
plot(x,type="l",lwd=2)# 2
y
y = y[!is.na(y)] # y NA
plot(y,type="l",lwd=2)


3,620

10

2.1
R

21
www.wunan.com.tw
(02)2705-5066
R

random variable


probability
2% 2% 20%

20%

probability distribution

uniform distribution

20 1 20

4%
0%

(1-2)

f(x) (1-2)

22
www.wunan.com.tw
(02)2705-5066
1
0 1
1
(1-2)

1-5
1-5 (a)
100 x 1, x2, , x100
xi 0 < xi < 1 0 1
xi
3 x1 0.2876x2 0.7883 x3 0.4090
3 x77
77

-2% 2% R

1-5

23
www.wunan.com.tw
(02)2705-5066
R
1-5 (a) runif(100) r
unif 0 1 100
100
normal distributionnorm R rnorm(100)

(b) (b) 100
cumulative distribution function, CDFCDF x
0.1 0.2
CDF
20 1% 30 2% 40
3%
CDF (d) (b) x
x quantile(d)
(a) R
punif qunif
(c)
probability density function, PDF9
dunif R 1-5 R ch1-2-1.R

# uniform distribution
?runif
x = runif(10)# ( 0 1 ) 10
x
y = seq(0,1,length=100)# 0 1 100
y
?ecdf # ecdf
windows()
par(mfrow=c(2,2))
set.seed(123)#
x = runif(100)

24
www.wunan.com.tw
(02)2705-5066

: R / .
-- . -- : , 2016.10
;
ISBN 978-957-11-8845-4()

1. 2.

512.4 105017437
1HA7

R









106 339 4
(02)2705-5066
(02)2706-6100
http://www.wunan.com.tw
wunan@wunan.com.tw
01068953


2016 10

850

www.wunan.com.tw
(02)2705-5066

You might also like