Sie sind auf Seite 1von 5

##############

# EX 1 P.132 #
##############
> x <- 2.392993e+23
> y <- 5.184706e+21
> x>y
[1] TRUE

##############
# EX 2 P.132 #
##############
> f <- function(x) -2*x+3
> g <- function(x) x^2+x+1
> curve(f,-2,2)
> curve(g,-2,2,add=T)
> optimize(g,c(-2,2))
$minimum
[1] -0.5

$objective
[1] 0.75

> optimize(f,c(-2,2))
$minimum
[1] 1.99994

$objective # LOWEST POINT BELONGING TO THE GRAPHS


[1] -0.9998794

##############
# EX 3 P.132 #
##############
> log(1)
[1] 0

##############
# EX 4 P.132 #
##############
A distribution is a description of a random experiment

##############
# EX 5 P.132 #
##############
> set.seed(187)
> A <- matrix(c(runif(6)),2,3,byrow=F)
> b <- c(0,2,1)
> u <- A%*%b
> sum(u)
[1] 3.52271

##############
# EX 6 P.132 #
##############
> f <- function(x) -x^4+3*x^3+3*x^2-x-2
> optimize(f,c(-3,1))
$minimum
[1] -2.99994

$objective
[1] -133.9898
##############
# EX 7 P.132 #
##############
> f <- function(x) x^3+x^2-4*x+3
> curve(f,-10,10)
> abline(h=0, col="red")
> curve(f,-5,0)
> abline(h=0, col="red")
> uniroot(f,c(-3,-2))
$root #RIGHTMOST ROOT OF THE EQUATION
[1] -2.8063

$f.root
[1] 7.951592e-06

$iter
[1] 4

$init.it
[1] NA

$estim.prec
[1] 0.0001020187

##############
# EX 8 P.132 #
##############
> f <- function(x,y) 3*x+2*y-exp(-x^2)+5
> g <- function(x,y) 4*x-5*y-exp(-y^2)+5
> x <- seq(-5,5,len=101)
> y <- seq(-5,5,len=101)
> z <- outer(x,y,g)
> contour(x,y,z,levels=0,col=2)
> w <- outer(x,y,f)
> contour(x,y,w,levels=0,col="blue",add=T)
> grid()

???????????????????????????

##############
# EX 9 P.132 # RIVEDERE!!!!
##############
> price <- 101.24
> mon <- 5
> net <- function(r) -price + rate/2*(1+r)^(-mon/12) + rate/2*(1+r)^(-(mon+6)/12) + rate/
2*(1+r)^(-(mon+12)/12) + 100*(1+r)^(-(mon+12)/12)
> sol <- uniroot(net,c(0,0.9))
> sol$root
[1] 0.002985041
###############
# EX 10 P.132 #
###############
> set.seed(193)
> x <- 1:10
> y <- rnorm(10)
> a <- function(x) -0.5*x+3.5
> b <- function(x) 0.5*x-4.5
> curve(a,-5,5)
> curve(b,-5,5,add=T)
> abline(h=0)
> points(x,y,pch=19, col="red")

###############
# EX 11 P.132 #
###############
> f <- function(x,y) (x^2+x*y+y^2-y)/3+exp(-x^2+2*x)+exp(-y^2-2*y)
> 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] -0.9398604 1.1454484

$value
[1] 0.0814683 #SOL

$counts
function gradient
57 NA

$convergence
[1] 0

$message
NULL
###############
# EX 12 P.132 #
###############
> f <- function(x,y) -x^2+2*x*y+y^2+x-4*y-4
> fb <- function(x) f(x[1],x[2])
> x <- seq(-10,10,length=1001)
> y <- seq(-10,10,length=1001)
> fz <- outer(x,y,f)
> image(x,y,fz)
> contour(x,y,fz,add=T)
> abline(v=3,col="blue")
> abline(h=4,col="blue")
> abline(a=4,b=-2,col="blue")
> A <- matrix(c(-1,0,0,-1,2,1),3,2,byrow=T)
> b <- c(-3,-4,4)
> constrOptim(c(2,2),fb,NULL,A,b)
$par
[1] 3.0000000 -0.9999417

$value
[1] -11

$counts
function gradient
248 NA

$convergence
[1] 0

$message
NULL

$outer.iterations
[1] 3

$barrier.value
[1] -0.0005047095

###############
# EX 13 P.132 #
###############
> f <- function(p) 4.5*exp(2*p)-0.6*exp(4*p)
> price <- 0.56
> curve(f,-1,1)
> optimize(f,c(0.5,1),maximum=T)
$maximum
[1] 0.6608817

$objective
[1] 8.4375
> ((0.6608817-price)*100)/price
[1] 18.01459
#SOL: A
###############
# EX 14 P.132 #
###############
> A <- matrix(c(111,100,99,100,94,105),3,2,byrow=T)
> b <- c(147.8,136.4,143,2)
> qr(A)$rank
[1] 2
> qr(A)$rank == qr(cbind(A,b))$rank
[1] FALSE
> qr(cbind(A,b))$rank
[1] 3
> require(MASS)
Carico il pacchetto richiesto: MASS
> invA <- ginv(A)
> portfolio <- invA%*%b
Error in invA %*% b : gli argomenti non sono compatibili

SOL: A, NO REPLICATION NO ARBITRAGE OPPORTUNITY

###############
# EX 15 P.132 #
###############
> A <- matrix(c(1,2,0,1,4,-2,1,4,-2),3,3,byrow=T)
> b <- c(4,6,6)
> qr(A)$rank
[1] 2
> qr(cbind(A,b))$rank # RC THEOREM, rk(A)=rk(A|b), THERE IS A SOL!!!
[1] 2

> library(MASS)
> invA <- ginv(A). # SOLVE THROUGH THE INVERSE OR GENERALIZED INVERSE OF A
> sum(invA%*%b)
[1] 3

###############
# EX 16 P.132 #
###############
> count <-0
> expp <- 10000
> for (i in 1:expp){
+ a <- runif(1)
+ b <- runif(1)
+ if (max(a,b)>0.5) {count <- count+1}
+ }
> sol <- count/expp
> sol
[1] 0.7554

###############
# EX 17 P.132 #
###############
#Simulation is used to verify that common birthdays are frequent in a groupo of about 30
people

Das könnte Ihnen auch gefallen