Professional Documents
Culture Documents
# EX 1 p.131 #
##############
> x <- 5.684342e-14
> y <- 1.92875e-22
> x > y
[1] TRUE
##############
# EX 2 p.131 #
##############
> l <- function(x) x-1
> p <- function(x) x^2+x-3
> curve(l,-1,4)
> curve(p,-1,4, col="red", add=T)
> optimize(l,c(-1,4))
$minimum
[1] -0.999922
$objective
[1] -1.999922
$objective
[1] -3.25
##############
# EX 4 p.131 #
##############
> x <- seq(0.4,3,len=11)
> y <- 10+sin(10*x)
> g <- function(x) exp(0.9*x)
> h <- function(x) 1.6*x+5
> curve(g,0,3)
> curve(h,0,3,add=T)
> points(x,y,pch=19,col="red")
##############
# EX 5 p.131 #
##############
> set.seed(143)
> x <- rnorm(n=100,mean=-4,sd=1)
> sum(x[21:27])
[1] -29.55734
##############
# EX 6 p.131 #
##############
> f <- function(x) x^4-x^3+2*x^2-2*x-3
> optimize(f,c(-3,0), maximum=T) #ATTENTION,
Y'ARE LOOKIN 4 MAX
$maximum
[1] -2.999924
$objective
[1] 128.9887
##############
# EX 7 p.131 #
##############
> f <- function(x) x/100+0.17*exp(0.079*x)
#Samsung
> g <- function(x) 0.32+0.024*x #
Apple
> t <- function(x) (((x/100)+0.17*exp(0.079*x))-1)
> uniroot(t,c(0,1000))
$root
[1] 19.65906
$f.root
[1] -2.602349e-08
$iter
[1] 17
$init.it
[1] NA
$estim.prec
[1] 6.103516e-05
##############
# EX 8 p.131 #
##############
> f <- function(x,y) 5*x^2+2*x*y+4*y^2+4*x-2*y-2
> fb <- function(x) f(x[1],x[2])
> x <- seq(-3,3,length=31)
> y <- seq(-3,3, length=31)
> fz <- outer(x,y,f)
> image(x,y,fz)
> contour(x,y,fz,add=T)
> persp(x,y,fz,phi=30,theta=30,ticktype="detailed")
#DEMO OF WHAT BELOW
##############
# EX 9 p.131 #
##############
price <- 101.51
mon <- 1 # distance in months between May and June
net <- function(r) -price + 3.5/2*(1+r)^(-mon/
12)+3.5/2*(1+r)^(-(mon+6)/12)+3.5/2*(1+r)^(-(mon+12)/
12)+100*(1+r)^(-(mon+12)/12)
sol <- uniroot(net,c(0,0.9))
sol$root
[1] 0.03478652 #FINAL SOLUTION
###############
# EX 10 p.131 #
###############
> set.seed(165)
> y <- rnorm(10)
> x <- 1:10
> a <- function(x) -x+3
> b <- function(x) x-5
> curve(a,3.5,4.5)
> curve(b,3.5,4.5,add=T)
> abline(h=-1)
> points(x,y,pch=19,col="red")
###############
# EX 11 p.131 #
###############
> f <- function(x,y) (x^2+x*y+y^2-y)/6+exp(-
x^2)+exp(-y^2)
> fb <- function(x) f(x[1],x[2])
> x <- seq(-3,3,length=31)
> y <- seq(-3,3, length=31)
> fz <- outer(x,y,f)
> image(x,y,fz)
> contour(x,y,fz, add=T)
> optim(c(-2,2),fb)
$par
[1] -1.609192 1.768892
$value
[1] 0.3026699
$counts
function gradient
51 NA
$convergence
[1] 0
$message
NULL
###############
# EX 12 p.131 #
###############
> f <- function(l,k) 0.75*l^0.6*k^0.4
????????????????
###############
# EX 13 p.131 #
###############
> profit <- function(p) 4.5*exp(1.5*p)-0.7*exp(3*p)
> price <- 0.65
> curve(profit,-1,1)
> optimize(profit,c(0.5,1),maximum=T)
$maximum
[1] 0.7784127
$objective
[1] 7.232143
> ((0.7784127-price)*100)/price
[1] 19.7558
###############
# EX 14 p.131 #
###############
> A <- matrix(c(104,104,104,110,120,103),3,2)
> b <- c(105.2,107.2,103.8)
> qr(A)$rank
[1] 2
> qr(cbind(A,b))$rank #OK!
[1] 2
> require(MASS)
> invA <- ginv(A)
> portfolio <- invA%*%b
> portfolio
[,1]
[1,] 0.8
[2,] 0.2
> A%*%portfolio #IT'S REPLICABLE
[,1]
[1,] 105.2
[2,] 107.2
[3,] 103.8
> pai <- c(93.60,97.35,94.350)
> sum(pai[1:2]*portfolio)
[1] 94.35
> pai[3] #NO ARBITRAGE
OPPORTUNITY, ANS D
[1] 94.35
###############
# EX 15 p.131 #
###############
> A <- matrix(c(1,2,1.5,-1,0,-0.5,1,4,2.5),
3,3,byrow=T)
> b <- c(1,1,4)
> library(MASS)
> qr(A)$rank
[1] 2
> qr(A)$rank == qr(cbind(A,b))$rank # Ranks
of the two matrices are DIFFERENT, SO IMP!
[1] FALSE
###############
# EX 16 p.131 #
###############
> sum <- 0
samples <- 100000
for(i in 1:samples){
a <- rnorm(1)
b <- rnorm(1)
if((a+b)/2 > 0.3) sum <- sum + 1
}
> est_prob <- sum/samples
[1] 0.33635
###############
# EX 17 p.131 #
###############
> payoff <- sample(c(200,100,3),10000,replace=T)
> gain <- sum(payoff)/10000 # ANS: D,
CLOSEST RESULT
> gain - 110
[1] -8.6455
> payoff <- sample(c(200,100,3),10000,replace=T)
> gain <- sum(payoff)/10000
> gain - 110
[1] -10.0305
> payoff <- sample(c(200,100,3),10000,replace=T)
> gain <- sum(payoff)/10000
> gain - 110
[1] -9.9154