Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Computational tools command inputs

Ls() Names of unknown


Logic Operator >< greater/smaller
<=>= less/greater than or equal to
& and
| or
! not
== equal (≠ from = to one item is assigned the value of another)
** exponential
TRUE Value 1
FALSE Value 0
Squrt() radice
Log() Basis exp not 10
Log10() Basis 10
Rm(item) Remove item in brackets
Rm(list=ls()) Remove elements represented by a list of elements
%/% Integral part of a ratio
%% Reminder of a ratio
__&__ If both relations TRUE result> True
If 1 wrong result> False
1/0 Inf = not possible
0/0 Nan = not a number
Mode(item) Specifies nature of the item
Length(item) Get or set the length of vectors
<- Assign something to item
Rep(item, number of times it has to be Repetition item
repeted)
Item[Number of place of item]
Item [_:_] Range of indices that are wanted to be selected
Item[-_] Exclude the item selected
Seq() (from=1, to=5, by=3) same as (1,5,3)
Sequence of numbers btw 1 and 34.54 divided by interval 2.34)
Seq(from=1, to=34.54, by=2.34) R calculates the step size itself. You make a vector of nine values going from –2.7 to
1.3:
> seq(from = -2.7, to = 1.3, length.out = 9)
Alternatively, you can specify the length of the [1] -2.7 -2.2 -1.7 -1.2 -0.7 -0.2 0.3 0.8 1.3
sequence by using the argument length.out
Vector <- c(items)
<-rep(_,_) repetition of 1st item for tot times defined by 2nd item
RECYCLING 2 vectors of different lengths, sum btw the 2 happens by repeating items of
> Warning message the shorter vector for the left items of the longer
PMAX(_,_) Compare entries of the 2 items> identifying the max/min btw the 2 vectors
PMIN(_,_)
Matrix(c(items), n° rows, n° columns, TT<-matrix(c(items),number of rows, number of columns, byrow=T)
byrow=T) Order to fill the matrix
>byrow=T/F-> by columns
Consider entire column or row

Matrix [number of rows, number of


columns]

Matrix[1:2, -1] Excluding last column


Computational tools command inputs
Matrix [1:2, 3:4] From 1st to 2nd row, from 3rd to 4th column
Dim(matrix) Dimension of matrix
Matrix [-2,-1] Excluding 2nd row and 1st column

T(matrix) Transposed matrix


Rbind (matrix, vector) Adding row of vector to matrix
(matrix, vector[1:2])> adding only the first 2 items
Runif (number of items wanted) Randomly generated numbers (randomly distributed)
Runif (10, 2, 4)>10 numbers btw 2 and 4
Qr(A) Compute factorization of A (couple of matrices)
Computing the QR decomposition
QR<-qr(A) Rank of the matrix

QR$rank
Solve(A,b) Solve linear system
A%*%sol To verify that A*sol=b
Sum(vector) Sum of the items of the vector
Set.seed(123) Procedure to generate random numbers
L<-list(number=5, greetings= “ciao” List = include several objects within 1 single element
Extract elements > l$number
Diag(pi,4) Diagonal matrix 4x4 with diagonal elements coming from π
Round (2^9,9…) Value close to actual result
1:4 > 3 FALSE 1>3
FALSE 2>3
FALSE 3>3
TRUE 4>3
FUNCTION Solve a*x^2+b*x+c=0

Function(a,b,c)
Quadra<-function (a,b,c){
Delta <- b^2-4*a*c
If (delta>=0){
x_1<-(-b+sqrt(delta))/(2*a)
x_2<-(-b+sqrt(delta))/(2*a)
res<-c(x_1,x_2)} else<-“delta is negative:
complex roots”}res}

f(10:15) Computing f for values from 10 to 15


Quadra(-1,2,5) -1x²+2x+5
Students<-(“Brit”, “Barb”, “DJ”) Create a matrix
Exam<-c(“Maths”, “Comp. Tools”,
“Marketing”)
Grade<-(23, 21, 30)
Gender<-(F, F, M)
stud<- data.frame(exam, grade, gender ,
row.names=students)

{used to identify 1 by 1 the rows}


City<- c(“Venice”, “Wien”) {creates very general structure}
Information<- list(origin=city,
stud_data=stud)
Computational tools command inputs
Hist(name item$name group of elements Create histogram
to register in the histogram)
Example hist(stud$grades)

To insert specific name to x and y axis:


Hist(c$grade, xlab=”name”, ylab=”name”)
C<-read.table(“position of the file/nome Insert different type of document with series of data
file.txt”, header=T) CAMBIARE SLASH DA \ A /

FOR HISTOGRAM
Hist(c$grade, xlab

Name of the item and elements to insert in it


Curve(f(x), value left extreme drawing Drawing of the function defined btw the 2 values defined
function, value right extreme drawing
function)

Curve(f(x), xlim=c(0,10)) Xlim=sets the x-axis limits for the current axes or chart
c(0,10) vector of extremes 0 and 10

Curve(f(x),interval of values in which the function is wanted to be defined)


Curve(x^2,0,5, add=T) Add the function to the graph of the previous one
Abline(h=value, col=”red”) Horizontal line h=coordinate
Abline(v=value, col=”blue”)
Vertical line v=coordinate
Abline(a=0.7, b=-1, col=3) a=… specifies intersection of the line with the y-axis
b=slope of the line/angular coefficient

Points((1:4), F(1:4)) Points((x coordinates), (y coordinates))


Example’s coordinates:
Reminder: if there is no graphical representation Points of coordinates (1,1); (2,2); (3,3); (4,4)
of the graph before typing in the points > Error

Window() Whether if you type


Generally: Window()
By Typing one time hist(c$grade, col=2) ① and then

hist(c$grade, col=3)②
② overlaps/subtitutes ① ②
Window()
They are magically separeted
Tent<-function(x) ifelse(x<0.5, 2*x, 2-2*x) Ifelse (test performed, what happens if the test is realized, what happens if
the test is not realized)

Colour<- ifelse(x<=0, ifelse(y<=, “red”, Colour<- (test if x is smaller or equal to 0, for x<= test y if y<=0 then points will
“green”), ifelse(y<=0, “blue”, “black”)) be red if y >=0 points will be green, for x>=0 and y<=0 points will be blue, for
y>=0 points will be black)
Plot (x,y,t=“l”, xlab= “sampled points”, Command to join the points previously drawn by a line
main= “squared x”)
> # "t" stands for 'type', "l" stands for 'line', 'main' is the title
>plot(x,y,t="l",xlab="Sampled points",
main="Squared x") > # 'l' means 'lines', 'p' means 'points', 'b' means 'both lines and points'
> # 'cex' is an expansion term, size of the dots
>plot(x,y,t="l",cex=2.5,pch=20,col="red") > # 'pch' is the type of points
>plot(x,y,t="p",cex=2.5,pch=20,col="red") > # 'col' is color, can be specified also with numbers

>plot(x,y,t="b",cex=2.5,pch=18,col="red")
Computational tools command inputs

“p”: Points
“l”: Lines
“b”: Both
“c”: The lines part alone of “b”
“o”: Both “overplotted”
“h”: Histogram like (or high-density) vertical lines
“n”: No plotting

lty=… (it’s a small L)

line type (lty) can be specified using either text (“blank”, “solid”, “dashed”,
“dotted”, “dotdash”, “longdash”, “twodash”) or number (0, 1, 2, 3, 4, 5, 6).
Note that lty = “solid” is identical to lty=1.

Polygon(c(2,3,4,2), c(10,20,10,10), Vertices points (2,10); (3,20); (4,10); (2,10)


col=”skyblue”)
Vertices points (1,2) (2,3) (3,4) (4,2)
Polygon(c(2,3,4,2), col=”skyblue”)
Rev(previous command) Reverse previous command

Intersection btw 2 functions f(x)=g(x) written in the form h(x)=f(x)-g(x)


# First Function
func6 <- function(x)
{x * log(x) - sqrt(x)
}

# Second Function
func7 <- function(x)
{
x
}

x <- seq(0,10,length=100)
plot(x, func6(x), col="blue", lwd="1", main="Graph
#1")
lines(x, func7(x), col="red")
func_name<-function(argument){
statement
}
Uniroot (function of which is needed to find the root, The function uniroot searches the interval from lower to upper for a root (i.e., zero) of the
interval in which is needed to investigate the function f with respect to its first argument
solution)

> # ROOT FINDING #>> find the zeros of a function(where the f(x)=0)
> f <- function(x) x^4+3*x^3+3*x^2-2*x-2 > # Consider the following function. Let us plot it and an horizontal line at zero level, i.e.
Computational tools command inputs
> curve(f,-3,3) > # evidently there are two intersections (zeros) between 'f' and the x-axis (i.e. the horizontal
> abline(h=0) line).
> # If roots (zeros) are sought (ricercati), then the command 'uniroot' might be used as follows
> uniroot(f,c(-1,0)) # Note that this will look for a zero of 'f' in the interval [-1,0]
$root # i.e. the interval for search must be given as a vector of two entries
[1] -0.6744252
$f.root
[1] 7.183991e-07
$iter
[1] 4
$init.it
[1] NA
$estim.prec
[1] 6.103516e-05
# Note that this will look for a zero of 'f' in the interval [0,1]
> sol <- uniroot(f,c(0,1))
> sol
$root
[1] 0.7740835
$f.root
[1] 3.222181e-07
$iter
[1] 6
$init.it
[1] NA
$estim.prec
[1] 0.0001199778
> sol$root
[1] 0.7740835
> sol$f.root
[1] 3.222181e-07
> # ...and to zoom and better see the zeros we can type:
> curve(f,-2,1)
> abline(h=0)
SOLVE THE PROBLEM f(x)=g(x) > # write the problem in the form h(x) = f(x) - g(x)
> # then use uniroot.

> curve(x^2, -2, 2)


> curve(log(x+4), add=T, lty=2)
> h <- function(x) x^2 - log(x+4)
>h
function(x) x^2 - log(x+4)
> sol1 <- uniroot(h, c(-1.5, 0.5))
> sol1
$root
[1] -1.041483
$f.root
[1] -1.178135e-06
$iter
[1] 6
$init.it
[1] NA
$estim.prec
[1] 6.103516e-05
> sol2 <- uniroot(h, c(1,2))
> sol2
$root
[1] 1.290718
$f.root
[1] -7.414335e-07
$iter
[1] 5
$init.it
[1] NA
$estim.prec
Computational tools command inputs
[1] 6.103516e-05
> sol2$root
[1] 1.290718
Runif (total number of randomly generated Generates random numbers from a uniform distribution
numbers, interval of randomly generated Runif (100,-1,1)
numbers)
uniform distribution

> y <- x**2+runif(10)/3 # we also generate the vector 'y'


Rnorm(100) Random normal distribution
Rnorm(100,0,1) Generates "n" normal random numbers based on the mean and standard
Rnorm(100, mean of the function, decide deviation arguments passed to the function
how large has to be the interval)

make a vector of random numbers normal


distribution
> x <- rnorm(10,5,2)
[1] 8.721095 2.595320 3.620825 2.076413 7.213503
# chooses 10 numbers in a normal distribution with # mean=5 and standard deviation=2
4.451149 5.998161 2.647196
[9] 5.870546 3.706552
funzione Density, distribution function, quantile function and random generation for
dnorm(x, mean, sd) the normal distribution with mean equal to mean and standard deviation
equal to sd

> dnorm(10,0.5,3) # computes the 'normal distribution' (with mean 0.5 and # standard deviation 3, at the value 10
[1] 0.0008836587
> # We recall that the 'max' or 'min' functions can > max(c(-34,12,5,sqrt(3),.03^3))
select the maximum/minimum [1] 12
> # number among a series of values > min(c(-34,12,5,sqrt(3),.03^3))
[1] -34
> # The command 'seq' can be partially enriched
using also the argument 'length.out', i.e.
> seq(from=2, to=5,length.out=3) # generates a sequence of 3 equal-spaced elements where the first is 2 and the third is 5
[1] 2.0 3.5 5.0
> seq(1,10,length.out=20) # generates a sequence of 20 equal-spaced elements where the first is 1 and the third is 10
[1] 1.000000 1.473684 1.947368 2.421053 2.894737
Shorter ways to write seq(_,_, length.out)
3.368421 3.842105
[8] 4.315789 4.789474 5.263158 5.736842 6.210526 > seq(1,10,length=20)
6.684211 7.157895 > seq(1,10,len=20)
[15] 7.631579 8.105263 8.578947 9.052632 9.526316
10.000000
OPTIMIZATION: to find local minima/maxima (i.e. to optimize a function)
> # when you have just one variable use the command 'optimize'
draw the function to allocate it in the best interval
> f <- function(x) x^4-3*x^3-3*x^2-3*x-2
> curve(f,-5,5)
> abline(h=0,col="blue")
> curve(f,0,5) > # inflection points at x1 = (3-17^(1/2))/4 and x2 = (3+17^(1/2))/4, i.e.
> # Imposing the equation f''(x) = 12x^2- > points((3-17^0.5)/4, f((3-17^0.5)/4),pch=19) # outside the interval [0,5]
18x-6 = 0 we find that 'f' has two > points((3+17^0.5)/4, f((3+17^0.5)/4),pch=19)
> # Imposing the equation f'(x) = 4x^3-9x^2-6x-3 = 0 we can find that 'f' has a
> # minimum point nearby x=3, given by
[1] -0.0001765588
> g <- function(x) 4*x^3-9*x^2-6*x-3
> windows()
> curve(g,2.5,3.5)
> abline(h=0,col="green")
> uniroot(g,c(2.5,3.5))
Computational tools command inputs
> # Now we want to use the command 'optimize' to directly find the minimum
point of 'f':
> optimize(f,c(2,4))
$minimum
[1] 2.864946

$objective
[1] -38.39441

> optimize(f,c(2,4),maximum=F) # does exactly the same of 'optimize(f,c(2,4))'


z <- optimize(f,c(2,4)) <command to find directly minimum
> str(z)
List of 2
$ minimum : num 2.86
$ objective: num -38.4
> z$minimum
[1] 2.864946
>
points(z$minimum,f(z$minimum),pch=19,c
ol=2) # this shows the minimum point

> f <- function(x,y) x^2+y^2+x*y)/6+exp(- Optimization of 2 variable function


x^2+2*x)+exp(-y*2+2*y)
x<-seq(-3,3,len=31)
y<- seq(-3,3,len=31)
z<-outer(x,y,f) 'k'
> k <- function(x) f(x[1],x[2]) # is a function of a 2-vector and not a function of two separate unknowns
> optim(c(0,0),k) generating a sequence of points, starting from the point (0,0). maximizing 'fb'
$par # minimizes 'fb' # is equivalent to minimize '-fb'.
[1] 0.5789234 -1.7893906
TIP: if area has various red areas control all of them by setting the
$value minimizing process for each critical area an then look at the value
[1] -8.842105 the smaller=the one that minimizes more

MINIMIZING/MAXIMIZING with
CONSTRAINTS
x>=0,y>=0, x-2y>=1 transform it into a
matrix of the form Ax=b

> # where we have for the function 'g'

> # Let us plot 'g'. Since it is a function of 2


unknowns, we transform it into a function
of a 2-real vector

1 0 x > 0
Computational tools command inputs
0 1 y = 0
> # Now we consider the introduction of 1 -2 1
'linear constraints', i.e. the feasible set 'D'
defined by the following constraints: KEEP AN EYE ON THE SIGNS!!!
'x >= 0, -y <= 1, x+y <= 2', which must be If one of the constraints has a different sign than the others then CHANGE IT
rearranged into 'x >= 0, y >= 1, -x-y >= -2'. Example
> # Observe that by looking at the plot of min g(x,y)
'g(x,y)' we easily see that the solution of (x,y) in D
the constrained optimization is located at
the right lower corner (1,1) of the triangle g <- function(x,y) x^2+y^2-3*x+y+2
which describes the feasible set gb <- function(x) g(x[1],x[2])
points(1,1,pch=19) > x <- seq(-2, 2, len=1001)
>A > y <- seq(-2,2, len=1001)
[,1] [,2] gz <- outer(x,y,g)
[1,] 1 0 > image(x,y,gz)
[2,] 0 1 > contour(x,y,gz,labcex=1.3)
[3,] -1 -1 > contour(x,y,gz,add=T,labcex=1.8)
>b
[1] 0 1 -2 > abline(v=0,col="blue") # necessary to consider the first constraint
> abline(h=1,col="blue") # necessary to consider the second constraint
> # To solve the constrained optimization > abline(2,-1,col="blue") # necessary to consider the third constraint
problem we type: > polygon(c(0,0,1), c(1,2,1), col="grey", density=c(30,40))
> # where
> # '(0.1,1.1)' is the starting point for > # Now, the feasible set 'D' contains linear constraints in the form 'Ax >=
minimization b', where we have for 'A' and 'b'
> # 'gb' is the function to be minimized > A <-matrix(c(1,0,0,1,-1,-1),3,2,byrow=T)
(must be a function of a 2-real vector)
> # 'NULL' (the third argument) indicates
that the heuristic optimization method
"Nelder-Mead" is used
> # (in case 'NULL' is replaced by the vector
'c(gb_x,gb_y)' of partial derivatives of 'gb',
then > b <- c(0,1,-2)
> # (the exact and more accurate
optimization method "BFGS" is used in
place of "Nelder-Mead" > constrOptim(c(0.1,1.1), gb, NULL,A,b)
> gb_x <- function(x,y) 2*x-3 # partial $par
derivative of 'gb' with respect to 'x' [1] 0.9999394 1.0000000
> gb_y <- function(x,y) 2*y+1 # partial
derivative of 'gb' with respect to 'y' $value
> gbg <- function(x) { c( gb_x(x[1],x[2]) , [1] 2.000061
gb_y(x[1],x[2]) ) } # define the 'gradient'
function of 'gb'. $counts
function gradient
272 NA

$convergence
[1] 0

$message
NULL
Computational tools command inputs
$outer.iterations
[1] 3

$barrier.value
[1] 6.494207e-08

Intersection btw 2 functions f(x)=g(x) written in the form→h(x)=f(x)-g(x)

curve(x^2,-2,2)
curve(log(x+4),add=T)
h<-function(x) x^2- log(x+4)
soll<-uniroot(h,c(-1.5,0.5))
Z<-outer((-5:5),(-10:10),f) Matrix of entries dimension 11x21 being 11=5-(-5)+1) and 21=10-(-10)+1
Plot a 3D graph of the function z(x,y) The entry z(I,j) contains the value of f at the point in position(I,j)
Persp(x,y,z)
> # The command to compute integrals in
R is 'integrate'. For example:
> f <- function(x) cos(x)/(1+sin(x))
>f
function(x) cos(x)/(1+sin(x))
> curve(f,0,10)
> pi
[1] 3.141593
> integrate(f,0,pi/2) > # Let us integrate this function between '0' and 'pi/2'
0.6931472 with absolute error < 7.7e-15
> in_f <- integrate(f,0,pi/2)
> str(in_f)
List of 5
$ value : num 0.693
$ abs.error : num 7.7e-15
$ subdivisions: int 1
$ message : chr "OK"
$ call : language integrate(f = f, lower = 0,
upper = pi/2)
- attr(*, "class")= chr "integrate"
> in_f$value
[1] 0.6931472
Computational tools command inputs

# The command 'curve' just plots a function


> f <- function(x) x^(1/5)
>f
> curve(f(x),0,1)
> # Note that 'f(x)' is the defined function, while '0' and '1' are the
> # limits of the unknown x (i.e. 'xlim'). The next instruction produces
> # exactly the same result:
> curve(f(x),xlim=c(0,1))
>
> f(1:4)
[1] 1.000000 1.148698 1.245731 1.319508
> curve(f(x),0,5) # draws the function when 0 <= x <= 5
> points((1:4),f(1:4)) # draws 4 points with coordinates (x_i,f(x_i))
> points(f(1:4)) # the same effect
> # Observe that 'points' works only in case you have already opened a
> # graphical window, i.e. in case you have right used commands like
> # 'curve' or 'plot'
>
> # As another example let's take a previously solved exercise
> tent <- function(x) ifelse(x<0.5,2*x, 2-2*x)
> curve(tent(x),0,1)
> curve(tent(x),-2,5)
> grid(col="gray")
> grid(col="red")
> grid(col=3)

> tent(1:4)
[1] 0 -2 -4 -6
> points(tent(1:4))

# Let's examine more accurately the 'plot' command


> x <- runif(10)
> y <- x**2+runif(10)/3
> windows()
Computational tools command inputs
> # the next two commands plot the pairs (x_i,y_i) similarly (blue vs. red)
> plot(x,y,col="blue")
> points(x,y,col="red")
> # note once more that the command 'points' can be invoked only after opening
> # a graphical window (e.g. with 'curve' or 'plot')

> plot(x,y,t="l",xlab="Sampled points", main="Squared x")


> # "t" stands for 'type', "l" stands for 'line', 'main' is the title
> plot(x,y,t="l",cex=2.5,pch=20,col="red")
> plot(x,y,t="p",cex=2.5,pch=20,col="red")
> plot(x,y,t="b",cex=2.5,pch=18,col="red")
> # 'l' means 'lines', 'p' means 'points', 'b' means 'both lines and points'
> # 'cex' is an expansion term, size of the dots
> # 'pch' is the type of points
> # 'col' is color, can be specified also with numbers

> plot(x,y,t="b",cex=1.5,pch=18,col="blue",cex.lab=1.25)
> # 'cex.lab' is the size of labels 'x' and 'y' on the axes
> # We can also add a legend whose upper left corner is in position (0.5,0.5)
> legend(0.0,0.8,"Points and lines",col="blue",pch=18)

> # Some examples and additional parameters:


> plot(x,y)
> plot(x,y,t="l")
> windows()
> plot(x,y)
> points(c(0.5,0.2,0.7),c(0.4,0.6,0.8),pch=19,cex=4,col="green")
> lines(c(0.5,0.2,0.7),c(0.4,0.6,0.8),col="red") # adds lines
> windows()
> plot(x,y)
> # The next three commands add lines to a previously drawn plot
> abline(a=1,b=-1,lty=10); abline(h=0.5,lwd=2); abline(v=0.2,lwd=3,col="blue")
> # 'a' is the intercept of the line with y-axis
> # 'b' is the angular coefficient of the line
> # 'lty' is the type of line adopted
> # 'h' stands for horizontal and the subsequent number is the position
> # 'v' stands for vertical and the subsequent number is the position
> # 'lwd' is the line width

> # Examples using 'curve' (observe that the operator '**' is treated as '^')
> f <- function(x) x-x**3
> curve(f,-3,3,lwd=2)
> windows()
> curve(x-x**3,-3,3,lwd=3,lty=2,col="red")
> # ...and note the different label on the axes
> # Now, to overlap different graphs/plots you can use 'add', e.g.
> plot(x,y)
> curve(f,-3,3,add=T) # the same of typing 'curve(f,-3,3,add=TRUE)'
Computational tools command inputs
> plot(x,y,pch=19)
> curve(x-x**5,add=T,col=4)

> price <- 101.74


> NPV <- function(r) -price + 2.5*(1+r)^{-5/12}+2.5*(1+r)^{-11/12}+(100+2.5)*(1+r)^{-17/12}
> NPV
function(r) -price + 2.5*(1+r)^{-5/12}+2.5*(1+r)^{-11/12}+(100+2.5)*(1+r)^{-17/12}
> curve(NPV,xlim=c(0,1))
> curve(NPV,xlim=c(0,.2))
> curve(NPV,xlim=c(0,0.1))
> abline(h=0,col=3)
> uniroot(NPV,c(0.03,0.05))
$root
[1] 0.04067622

$f.root
[1] 2.165849e-06

$iter
[1] 2

$init.it
[1] NA

$estim.prec
[1] 0.0001148596
##################################################

> # EXERCISE: Try with the command "uniroot(f,c(-2,2))" ...and find out why it provides an Error !!!!

> # EXERCISE: compute the actual interest rate, after 1 year, of a BTP whose nominal reward is 3%,
> # it is purchased at April 1st, 2013 at a price of 101.7 and pays 1.5 in each semester
> # (here the investment in the BTP starts in April 1st 2013 and expires in April 1st 2014)
> btp <- function(r,price=101.7) -price + 1.5*1/(1+r)^0.5 + 101.5*1/(1+r) # 'price' is a parameter !!!!
> curve(btp,0,1)
> curve(btp,0,0.1) # zoom in to better locate the root
> curve(btp,0,0.02) # zoom in to better locate the root
> abline(h=0,col=3) # trace an horizontal line simulating the x-axis
> grid(col="red") # if you prefer to add a red 'grid'
> zz <- uniroot(btp,c(1.0e-2,1.5e-2))
> zz
$root
[1] 0.01288338

$f.root
[1] -0.0005999008
Computational tools command inputs
$iter
[1] 2

$init.it
[1] NA

$estim.prec
[1] 6.103516e-05
> # Observe that 'zz' is a list, and we have indeed:
> str(zz)
List of 5
$ root : num 0.0129
$ f.root : num -6e-04
$ iter : int 2
$ init.it : int NA
$ estim.prec: num 6.1e-05

> zz$root # Note that the root is just 1.28%, which is significantly smaller than 3% !!!!!
[1] 0.01288338
> str(zz)

> # We recall that to extract entries/columns/rows of a matrix we can use


> # different ways:
> A <- matrix(c(2,3,4,5,6,7,8,9,10),3,3,byrow=T)
>A
[,1] [,2] [,3]
[1,] 2 3 4
[2,] 5 6 7
[3,] 8 9 10
> B <- A[1:2,2:3] # assigns B with the submatrix A[1:2,2:3] extracting rows from 1 to 2 and columns from 2
to 3
>B
[,1] [,2]
[1,] 3 4
[2,] 6 7
> C <- A[-3,-1] # does exactly the same with matrix C
>C
[,1] [,2]
[1,] 3 4
[2,] 2 4
> D <- A[,2] # assigns D with the 2nd column of 'A'
>D
[1] 3 2 1
> E <- A[,-2] # assigns E with 'A' deprived of

its 2nd column


>E
Computational tools command inputs
[,1] [,2]
[1,] -2 4
[2,] -3 4
[3,] 0 1

> # We also recall the use and definition of a user-defined function, e.g.
> tent <- function(x) ifelse(x<0.5,2*x, 2-2*x)
> tent(0.1)
[1] 0.2
> tent(5)
[1] -8
> tent(18)
[1] -34
> # ...and we can draw the graph of function 'tent' (with a red grid) using
> curve(tent(x),-2,5)
> grid(col="red")
> points(-2:5,tent(-2:5)) # this command also gives points on the graph

> windows() # opens a second graphic window


> plot(x,y,t="p",xlab="Sampled points", main="Squared and Perturbed x")

> # Another graphic command is 'polygon' (similarly to the command 'points' it


> # requires to open the graphic environment (e.g. using 'plot')
> plot(x,y)
> polygon(c(2,3,4,2),c(10,20,10,10),col="skyblue") # draws a polygon with vertices
# in the points (2,10), (3,20)
# (4,10) and (2,10)
> polygon(c(20,30,40,20),col="skyblue") # draws a polygon with vertices
# in the points (1,20), (2,30)
# (3,40) and (4,20)
> polygon(1:4,c(20,30,40,20),col="skyblue") # exactly as the previous command

> # To reverse the effects of a command we can use 'rev', whose effect depends on the
> # function it is applied to. Here is an example:
>x
[1] 3.486252 7.020825 5.031558 4.218064 4.195686 6.465254 5.578663 5.536243
[9] 5.651386 1.673548
> sort(x)
[1] 1.673548 3.486252 4.195686 4.218064 5.031558 5.536243 5.578663 5.651386
[9] 6.465254 7.020825
> rev(sort(x))
[1] 7.020825 6.465254 5.651386 5.578663 5.536243 5.031558 4.218064 4.195686
[9] 3.486252 1.673548
> sort(x,decreasing=T)
[1] 7.020825 6.465254 5.651386 5.578663 5.536243 5.031558 4.218064 4.195686
[9] 3.486252 1.673548
> y <- log(x+2)
> plot(x,y)
Computational tools command inputs
> polygon(c(x,rev(y)),c(y,rev(x)),col="red") # the vertices of the polygon have coordinates
# in the vectors 'c(x,rev(y))' and 'c(y,rev(x))'

###########################
# Plots with two unknowns #
###########################
> # Other graphic commands can be define

d using 'outer', e.g.


> f <- function(x,y) x^2+x*y+y^2-y
> z <- outer((-5:5),(-10:10),f) # 'z' contains a matrix of entries with dimensions 11x21,
# being 11=5-(-5)+1 and 21=10-(-10)+1. The entry z[i,j]
# contains the value of 'f' at the point in position (i,j)
>z
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] 185 160 137 116 97 80 65 52 41 32 25 20 17
[2,] 166 142 120 100 82 66 52 40 30 22 16 12 10
[3,] 149 126 105 86 69 54 41 30 21 14 9 6 5
[4,] 134 112 92 74 58 44 32 22 14 8 4 2 2
[5,] 121 100 81 64 49 36 25 16 9 4 1 0 1
[6,] 110 90 72 56 42 30 20 12 6 2 0 0 2
[7,] 101 82 65 50 37 26 17 10 5 2 1 2 5
[8,] 94 76 60 46 34 24 16 10 6 4 4 6 10
[9,] 89 72 57 44 33 24 17 12 9 8 9 12 17
[10,] 86 70 56 44 34 26 20 16 14 14 16 20 26
[11,] 85 70 57 46 37 30 25 22 21 22 25 30 37
[,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
[1,] 16 17 20 25 32 41 52 65
[2,] 10 12 16 22 30 40 52 66
[3,] 6 9 14 21 30 41 54 69
[4,] 4 8 14 22 32 44 58 74
[5,] 4 9 16 25 36 49 64 81
[6,] 6 12 20 30 42 56 72 90
[7,] 10 17 26 37 50 65 82 101
[8,] 16 24 34 46 60 76 94 114
[9,] 24 33 44 57 72 89 108 129
[10,] 34 44 56 70 86 104 124 146
[11,] 46 57 70 85 102 121 142 165
> str(z)
num [1:11, 1:21] 185 166 149 134 121 110 101 94 89 86 ... # showing that 'z' is a numerical matrix
> z[2,3] # gives the entry in the 2nd line and 3rd column
[1] 120
> f(-5+1,-10+2) # gives exactly the same value just computed
[1] 120
> z[,2]
[1] 160 142 126 112 100 90 82 76 72 70 70
> x <- -5:5
> y <- -10:10
Computational tools command inputs

> # With the 'persp' command we can plot a 3D graph of the function 'z=f(x,y)'
> persp(x,y,z)
> windows()
> persp(x,y,z,theta=30,phi=30,ticktype="detailed") # gives the same 3D plot with a different perspective
# 'theta' gives the azimuthal direction, 'phi' the colatitude
> persp(x,y,z,theta=30, phi=30,ticktype="detailed",box=F) # will not plot any bounding box around f(x,y)
> ?persp # to see many more parameters/arguments of 'persp' function
> # We also have a different way to plot the 3D graph of a function, using its level curves, i.e.
> image(x,y,z)
> # and it is possible to add contours relative to the same levels of the function
> contour(x,y,z, add=T)
> contour(x,y,z, add=F)
> contour(x,y,z, add=T, labcex=1.2) # this adds the size of the level sets (using 'labcex')
> grid(col=2) # this adds, as usual, a red (i.e. col=2) grid
> # There is also the chance to zoom into the 3D plot around a specific point, say (1,2):
> x <- seq(0.5,1.5, length=31)
> y <- seq(1,3,length=40)
> z2 <- outer(x,y,f)
> image(x,y,z2)
> contour(x,y,z2,add=T,nlevels=20) # 'nlevels' specifies the number of desired level curves

> #=========================================================
> # EXERCISE n.13 (from the exam N.101 in January 7, 2015):
> #=========================================================
> f <- function(p) 4*exp(1.5*p) - 0.6*exp(4*p)
> curve(f,-5,5)
> curve(f,-1,1)
> grid(col=2)
> # In this case we want to find a maximum, thus we properly use 'optimize' as
> optimize(f, c(0,0.8), maximum=T)
$maximum
[1] 0.3665013

$objective
[1] 4.332155

> 0.31*1.182 # i.e. we increase the current price of 18.2%


[1] 0.36642
> f(0.31) # i.e. we compute f(p) at the current price p=0.31
[1] 4.294689

> # this implies that the correct answer to the exercise is (b)

> #===========================================
> # Exercise 1 (optimization with 2 unknowns)
> #===========================================
NULL
Computational tools command inputs
> fb(c(0.5789234,-1.7893906)) # Remember that 'fb' is a function of a 2-real vector !!!!!!!
[1] -8.842105

> # In case a 'maximization' is pursued, then the command becomes


> optim(c(0,0),fb,control=list(fnscale=-5)) # any negative value, other than -5, can be
# assigned to fnscale to perform maximization

> # Now let us draw the function 'fb'. We have to define a grid of points
> # nearby the maximum point found above
> x <- seq(-3,3,len=31)
>x
[1] -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4
[19] 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0
> y <- seq(-3,3,len=31)
>y
[1] -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4
[19] 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0
> zf <- outer(x,y,f)
> str(zf)
num [1:31, 1:31] 61 53.8 47 40.6 34.6 29 23.8 19 14.6 10.6 ...
> image(x,y,zf)
> contour(x,y,zf,add=T,labcex=1.3)

> #===========================================
> # Exercise 2 (optimization with 2 unknowns)
> #===========================================
> f <- function(x,y) (x^2+y^2+x*y)/6 + exp(-x^2+2*x) + exp(-y^2+2*y)
>x
[1] -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4
[19] 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0
>y
[1] -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4
[19] 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0
> fz <- outer(x,y,f)
> image(x,y,fz)
> contour(x,y,fz,add=T)
> contour(x,y,fz,labcex=2,add=T)

> # EXERCISE: taken from a previous exam


> #======================================
> g <- function(x,y) x^2 + y^2-x-20*dnorm((x+2)*(y-2))
> # We want to minimize this function; thus, we first plot it
> x <- seq(-3,3,len=100) # generate the grid w.r.t. 'x'
> y <- seq(-3,3,len=100) # generate the grid w.r.t. 'y'
> gz <- outer(x,y,g) # generate the mesh of the function 'g'
> str(gz)
num [1:31, 1:31] 21 19.6 18.3 16.1 11.2 ...
> windows()
> image(x,y,gz)
Computational tools command inputs
> contour(x,y,gz, add=T, labcex=1.2)
> grid(col=1)
> # Then, starting from 'g' we generate the function 'gb' of a 2-vector argument, to be optimized, i.e.
> gb <- function(x) g(x[1],x[2])
> optim(c(0.5,2),gb) # the starting point is (0.5,2) where the plot is much "red", i.e. close to a minimum
$par
[1] 0.4347055 1.9172768

$value
[1] -4.388433

$counts
function gradient
49 NA

$convergence
[1] 0

$message
NULL
> points(0.4347055,1.9172768,pch=19) # plots the minimum point
> g(0.4347055,1.9172768) # gives the value of the function 'g' (of two real unknowns)
[1] -4.388433
> gb(c(0.4347055,1.9172768)) # gives the value of the function 'gb' (of a 2-real vector)
[1] -4.388433

> #===========================
> # DERIVATIVES OF A FUNCTION
> #===========================
> # As regards the derivative of a function of one unknown, let us consider the next example and compare
the
> # plots of f(x) an f'(x) from a theoretical point of view: observe that the function increases when the
> # derivative is positive. Moreover, the function is convex whenever f''(x) is positive.
> f <- function(x) 3*x^3+2*x^2+x
> curve(f,-3,3)
> abline(h=0,col="green")
> g <- function(x) 9*x^2+4*x+1
> curve(g,-3,3,lty="dashed",add=T)
> h <- function(x) 18*x+4
> curve(h,-3,3,lty="dotted",add=T)

> # As regards partial derivatives f_x and f_y of the function f(x,y), it is possible to define them by using
> # finite differences, i.e.
> # f(x+h,y)-f(x,y) f(x,y+h)-f(x,y)
> # f_x = ----------------- f_y = -----------------
>#hh
> x <- seq(-3,3,len=31)
> y <- seq(-3,3,len=31)
> f <- function(x,y) (x^2+x*y+y^2)/6 + exp(-x^2+2*x) + exp(-y^2+2*y)
Computational tools command inputs
> fx <- function(x,y,h=0.0001) (f(x+h,y)-f(x,y))/h
> fy <- function(x,y,h=0.0001) (f(x,y+h)-f(x,y))/h
> # Now, let us plot the partial derivatives fx and fy. They are, similarly to f(x,y) functions of 2 unknowns,
thus,
> fz <- outer(x,y,f)
> fxz <- outer(x,y,fx)
> fyz <- outer(x,y,fy)
> str(fyz)
num [1:31, 1:31] -1.5 -1.47 -1.43 -1.4 -1.37 ...
> contour(x,y,fz, labcex=1.3) # plots the level contours of the function
> contour(x,y,fxz, add=T, col=2, lty=2, labcex=1, level=0, lwd=2) # plots the level contours of fx
> contour(x,y,fyz, add=T, col=3, lty=2, labcex=1, level=0, lwd=2) # plots the level contours of fy

> # BASICS ON LINEAR SYSTEMS:


> # We already know how to solve a linear system 'Ax=b' by 'solve(A, b)',
> # however, for the linear system 'x+y=1, x+y=2' with 'b=(1,2)' there is
> # evidently no solution (since it is SINGULAR), indeed...
> b <- c(1,2)
>b
[1] 1 2
> A <- matrix(c(1,1,1,1), 2,2, byrow=T)
>A
[,1] [,2]
[1,] 1 1
[2,] 1 1
> det(A)
[1] 0
> # thus 'A' has rank < 2. On the other hand, if let us take the complete matrix (A|b)
> Ab <- cbind(A,b)
> Ab
b
[1,] 1 1 1
[2,] 1 1 2
> qr(Ab)
$qr
b
[1,] -1.4142136 -2.1213203 -1.414214e+00
[2,] 0.7071068 0.7071068 2.220446e-16

$rank
[1] 2

$qraux
[1] 1.707107e+00 7.071068e-01 2.220446e-16

$pivot
[1] 1 3 2

attr(,"class")
[1] "qr"
Computational tools command inputs
> # so that the rank of the complete matrix is equal to 2, and Rouchè-Capelli Theorem
> # says that no solution is allowed for the linear system. Indeed,...
> solve(A,b)
Error in solve.default(A, b) :
Lapack routine dgesv: system is exactly singular: U[2,2] = 0

> # Let us now consider another example of linear system:


> # 'x+y=3 , 2x+2y= 6'. Since the two equations differ for the factor 2,
> # this linear system has INFINITE SOLUTIONS.

> # As a third case we consider for the matrix 'A' the expression
> A <- matrix(c(1,2,3,4,5,6,7,8,9),3,3,byrow=T)
>A
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> # and given the vector 'v' as
> v <- c(1,1/2,-1)
>v
[1] 1.0 0.5 -1.0
> # we obtain for the product 'A*v' (remember the use of the operator "%*%" in place of "*")
> A%*%v
[,1]
[1,] -1.0
[2,] 0.5
[3,] 2.0
> # I want to show that the previous product can be seen as the linear combination
> # of colums of 'A', indeed...
> 1*A[,1]+1/2*A[,2]-1*A[,3]
[1] -1.0 0.5 2.0
> # Now we compute the rank of 'A'
> rr <- qr(A)
> rr$rank
[1] 2
> # Let us also choose a vector 'b' of known terms for the linear system, as
> b <- c(7,5,3)
>b
[1] 7 5 3
>A
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> # Let us try to solve the linear system 'Ax=b' and verify that we obtain a specific error message
> solve(A,b)
Error in solve.default(A, b) :
il sistema è numericamente singolare: valore di condizione di reciprocità = 1.54198e-18
> # Let us verify if Rouchè-Capelli theorem is satisfied. Thus, we verify both the rank of
> # 'A' and the rank of the complete matrix 'Ab', i.e.
> Ab <- cbind(A,b)
> Ab
Computational tools command inputs
b
[1,] 1 2 3 7
[2,] 4 5 6 5
[3,] 7 8 9 3
> qr(Ab)$rank
[1] 2
> qr(A)$rank
[1] 2
> # which implies that by the Rouchè-Capelli Theorem we have infinite solutions to 'Ax=b'.
> # Let us choose now for 'b' the vector
> b <- c(-2,6,1)
>A
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> Ab <-cbind(A,b)
> Ab
b
[1,] 1 2 3 -2
[2,] 4 5 6 6
[3,] 7 8 9 1
> qr(Ab)$rank
[1] 3
> qr(A)$rank
[1] 2
> # and 'Ax=b' is an IMPOSSIBLE system, being 'qr(Ab)$rank' not equal to 'qr(A)$rank'

> # We choose now the matrix 'A <- matrix(c(1,1,2,3),2,2,byrow=T)' and the vector 'b <- c(1,2)', i.e.
> A <- matrix(c(1,1,2,3),2,2,byrow=T)
>A
[,1] [,2]
[1,] 1 1
[2,] 2 3
> b <- c(1,2)
> qr(A)$rank
[1] 2
> qr(cbind(A,b))$rank
[1] 2
> # thus, Rouchè-Capelli Theorem ensures that 'Ax=b' admits solutions
> sol <- solve(A,b)
> sol
[1] 1 0
> # Now, let us compute the inverse of matrix 'A', i.e. A^(-1), using 'solve(A)'
> invA <- solve(A)
> invA
[,1] [,2]
[1,] 3 -1
[2,] -2 1
> # and verify that invA*A is the identity matrix
> invA%*%A
[,1] [,2]
Computational tools command inputs
[1,] 1 0
[2,] 0 1
> # Of course when solving 'Ax=b' we also have 'x = A^(-1)b, indeed...
> invA%*%b
[,1]
[1,] 1
[2,] 0
> # which coincides with 'sol'. Let us now consider again one of the above examples
> A <- matrix(1:9,3,3, byrow=T)
>A
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> b <- c(7,5,3)
> solve(A,b)
Error in solve.default(A, b) :
il sistema è numericamente singolare: valore di condizione di reciprocità = 2.59052e-18
> det(A)
[1] 6.661338e-16
> # which implies that the matrix 'A' is near singular. However, since we also have
> qr(A)$rank
[1] 2
> qr(cbind(A,b))$rank
[1] 2
> # by Rouchè-Capelli we know that the linear system admits infinite solutions. One of them
> # can be found using the so called "Pseudo-inverse" or "Moore-Penrose pseudo-inverse" of 'A', as
> library(MASS)
> ginvA <- ginv(A)
> ginvA
[,1] [,2] [,3]
[1,] -0.63888889 -1.666667e-01 0.30555556
[2,] -0.05555556 3.469447e-17 0.05555556
[3,] 0.52777778 1.666667e-01 -0.19444444
> sol <- ginvA%*%b
> sol
[,1]
[1,] -4.3888889
[2,] -0.2222222
[3,] 3.9444444
> A%*%sol
[,1]
[1,] 7
[2,] 5
[3,] 3
>b
[1] 7 5 3

> # TO SUMMARIZE: the linear system 'Ax=b' admits solutions if and only if rk(A)=rk(A|b).
> # If the latter condition is satisfied, then to solve it you can use the inverse or the
> # generalized inverse of A, depending on the fact that A can be or be not singular. I.e.,
> # check if the system has a solution applying Rouchè-Capelli Theorem: if there is a solution
Computational tools command inputs
> # compute ginv(A)%*%b, because also in case 'A' is invertible, its "inverse" coincides with
> # its "pseudo-inverse".

> # In the next example (i.e. x+y=1, x+y=17) the linear system DOES NOT admit solution
> library(MASS)
> A <- matrix(c(1,1,1,1),2,2)
> b <- c(1,17)
> v <- ginv(A)%*%b
>v
[,1]
[1,] 4.5
[2,] 4.5
> # however we have that
> A%*%v
[,1]
[1,] 9
[2,] 9
>b
[1] 1 17
> # thus, we found the vector 'v' which IS NOT A SOLUTION of the linear system !!!!!!!!
> qr(A)$rank
[1] 1
> qr(cbind(A,b))$rank
[1] 2

> # State Preference Model: description of a financial market


> # there are two periods t=0, t=1; 'n' assets; uncertainty
> # because there are 'm' possible states of the world at t=1.
> # Nature picks just one of the states, but at t=0 we do not know it.
> # Assets will pay different payoffs depending on which state realizes
> # state-contingent assets.

> # Let us consider the next payoff-matrix with 3 scenarios and 3 assets
> Y <- matrix(c(104, 104, 104, 108, 102, 100, 112, 106, 93),3,3)
>Y
[,1] [,2] [,3]
[1,] 104 108 112
[2,] 104 102 106
[3,] 104 100 93
> # suppose we have the portfolio 'x' as
> x <- c(1/3,1/3,1/3)
> # and let us compute the payoffs
> y <- Y%*%x
>y
[,1]
[1,] 108
[2,] 104
[3,] 99
> # equivalently 'y' is a linear combination of the columns of 'Y', i.e.
> 1/3*Y[,1]+1/3*Y[,2]+1/3*Y[,3]
[1] 108 104 99
> # Let us consider the next vector of pay-per-unit of the assets
Computational tools command inputs
> pai <- c(100,100,100)
> # so that our portfolio costs, for any asset,
> x*pai
[1] 33.33333 33.33333 33.33333
> # and the overall cost is
> sum(x*pai)
[1] 100
> # Can the asset b <- c(112,106,104) be "replicated"?
> qr(Y)$rank
[1] 3
> qr(cbind(Y,c(112,106,104)))$rank
[1] 3
> # thus 'b' can surely be replicated (i.e. the linear system Yx=b admits solutions)
> b <- c(112,106,104)
> x <- ginv(Y)%*%b
>x
[,1]
[1,] 3.846154e-02
[2,] 1.000000e+00
[3,] 1.776357e-15
> # and to prove that 'Yx=b' admits solution we have...
> Y%*%x
[,1]
[1,] 112
[2,] 106
[3,] 104
>b
[1] 112 106 104
>Y
[,1] [,2] [,3]
[1,] 104 108 112
[2,] 104 102 106
[3,] 104 100 93
> solve(Y,b)
[1] 0.03846154 1.00000000 0.00000000
> # Which is now the price of the vector b we replicated?
> pai
[1] 100 100 100
> sum(pai*x)
[1] 103.8462

> #===========================================================

> # Let us consider now a different payoff-matrix 'Y'


> Y <- matrix(c(104,104,104,108,102,100,1 12,106,104),3,3)
>Y
[,1] [,2] [,3]
[1,] 104 108 112
[2,] 104 102 106
[3,] 104 100 104
> # and the next vector of assets prices
> pai <- c(100,100,104)
Computational tools command inputs
> # Let us check if the third asset can be obtained as a linear combination of the other two.
> # Equivalently we want to solve the linear system 'Y[,1:2]*x = Y[,3]'
> Y[,1:2]
[,1] [,2]
[1,] 104 108
[2,] 104 102
[3,] 104 100
> Y[,3]
[1] 112 106 104
> # compare the ranks of the complete and incomplete matrix
> qr(Y[,1:2])$rank
[1] 2
> qr(Y)$rank
[1] 2
> # I.e., the linear system admits solution, and we can compute it using the pseude-inverse
> library(MASS)
> ginv(Y[,1:2])%*%Y[,3]
[,1]
[1,] 0.03846154
[2,] 1.00000000
> #... and the evidence that it is a solution is given by
> 0.03846154*Y[,1]+1.0000*Y[,2]
[1] 112 106 104
> Y[,3]
[1] 112 106 104
> # Thus, the third column of 'Y' CAN BE REPLICATED, and since the vector of prices is
> pai
[1] 100 100 104
> # then the costs for replicating Y[,3] are
> 0.03846154*pai[1]+1.00000000*pai[2]
[1] 103.8462
> # so that 103.85 < 104 and as a consequence we have an ARBITRAGE opportunity.
> # Indeed, our portfolio is
> x <- c(-0.03846154,-1,1)
>x
[1] -0.03846154 -1.00000000 1.00000000
> # and using the formula Y*x we have
> Y%*%x
[,1]
[1,] -1.6e-07
[2,] -1.6e-07
[3,] -1.6e-07
> # which proves that we have arbitrage.
>x
[1] -0.03846154 -1.00000000 1.00000000
> # Moreover we have (see also the slides of the current lesson)
> pai
[1] 100 100 104
> sum(x*pai)
[1] 0.153846
> # which represents the amount "the trader cashes replicating and selling
> # the asset in the third column of 'Y'
Computational tools command inputs

> # Another example


> Y <- matrix(c(97,108,117,102,117,101,89.3,100.8,98.9),3,3)
>Y
[,1] [,2] [,3]
[1,] 97 102 89.3
[2,] 108 117 100.8
[3,] 117 101 98.9
> pai <- c(95.78,94.88,85.838)
> pai
[1] 95.780 94.880 85.838
> qr(Y[,1:2])$rank
[1] 2
> qr(Y)$rank
[1] 2
> # which implies that by Rouchè-Capelli Theorem the third colum can be obtained
> # replicating the other two columns. Let us compute the solution of 'Y[,1:2]x=Y[,3]'
> ginv(Y[,1:2])%*%Y[,3]
[,1]
[1,] 0.5
[2,] 0.4
> # and indeed, we have by a simple check
> 0.5*Y[,1]+0.4*Y[,2]
[1] 89.3 100.8 98.9
> Y[,3]
[1] 89.3 100.8 98.9
> # Let us compare the cost of buying a combination of assets 1 and 2, with respect to the cost of buying 3
> pai
[1] 95.780 94.880 85.838
> pai[3]
[1] 85.838
> # cost of replication
> 0.5*pai[1]+0.4*pai[2]
[1] 85.842
> # and since 85.842 > 85.838 there is NO ARBITRAGE NOW !

> # Another example


> Y <- matrix(c(104,104,104,120,95,116,80.8,68.3,78.8),3,3)
>Y
[,1] [,2] [,3]
[1,] 104 120 80.8
[2,] 104 95 68.3
[3,] 104 116 78.8
> pai <- c(93.6,101,40)
> pai
[1] 93.6 101.0 40.0
> # Let us verify, also in this case, if Y[,3] can be replicated
> qr(Y[,1:2])$rank
[1] 2
> qr(Y)$rank
Computational tools command inputs
[1] 2
> # thus, Y[,3] can be replicated and it is
> ginv(Y[,1:2])%*%Y[,3]
[,1]
[1,] 0.2
[2,] 0.5
> 0.2*Y[,1]+0.5*Y[,2]
[1] 80.8 68.3 78.8
> Y[,3]
[1] 80.8 68.3 78.8
> # compare prices
> pai[3]
[1] 40
> 0.2*pai[1]+0.5*pai[2]
[1] 69.22
> # since 40 < 69.22 there is NO ARBITRAGE condition!
> # Let us now change the third asset in the previous example, as
>Y
[,1] [,2] [,3]
[1,] 104 120 80.8
[2,] 104 95 68.3
[3,] 104 116 78.8
> Y[,3] <- c(14,105,200)
>Y
[,1] [,2] [,3]
[1,] 104 120 14
[2,] 104 95 105
[3,] 104 116 200
> # now let us verify if Y[,3] can be replicated
> qr(Y[,1:2])$rank
[1] 2
> qr(Y)$rank
[1] 3
> qr(Y[,1:2])
> # ranks are different therefore we cannot obtain the third asset by combining the first and second.
> # I.e. the third asset CANNOT BE REPLICATED ! ...and as a consequence we cannot have arbitrage !
Inizio modulo
Fine modulo

You might also like