As you have certainly seen now, I like working on artificial neural networks. I have written a few posts about models with neural networks (Models to generate networks, Want to win to Guess Who and Study of spatial segregation).

Unfortunately, I missed so far a nice and pleasant aspect of networks : its graphical approach. Indeed, plots of neural networks are often really nice and really useful to understand the network.

Sometimes such a graph can point out some characteristics of the network. For example, we see in the graph below that there is one "central" node linked with many other nodes. It would not be that obvious if we were looking at a simple print() of a network matrix!

So if you like this representation, let me introduce you to the package network.

Let's first download the package and use a home-made function to generate networks randomly (if you want to see more details about this function, see here): 

install.packages('network')
library(network) 

generateBA = function(n = 100, n0 = 2){
  mat = matrix(0, nrow= n, ncol = n)
  for(i in 1:n0){
    for(j in 1:n0){
      if(i != j){
        mat[i,j] = 1
        mat[j,i] = 1
      }
    }
  }
  for(i in n0:n){
    list = c()
    for(k in 1:(i-1)){
      list = c(list, sum(mat[,k]))
    }
    link = sample(c(1:(i-1)), size = 1, prob = list)
    mat[link,i] = 1
    mat[i,link] = 1
  }
  return(mat)
}


artificialNet= generateBA(200)

To create an object network from a matrix, the package uses the function network():

a = network(artificialNet, directed = FALSE)
# The parameter directed is specified as FALSE because in our case, artificialNet is a symetrical matrix.
# It is really convenient to plot a nice network
plot(a)
#plot.network() is the function to use if we want to change some parameters





There are many other useful function in this package. Let's start with the function to study the properties of the network. We can compute the density of the network ("The density of a network is defined as the ratio of extant edges to potential edges."), the number of nodes and the number of edges.

network.density(a)
network.size(a)
network.edgecount(a)


Besides, this package offers the possibility to create and manage operations on the object network. The function network.initialize(n) creates a network with n nodes and no edges.

net = network.initialize(10)
plot(net)

Adding a edge (and therefore define our whole network) is then really simple : 

net[1,2] = 1
plot(net)

But the real power of this package is in the definition of operators. Here are the operators (source : the library) :

+ : An (i; j) edge is created in the return graph for every (i; j) edge in each of the input graphs.

- : An (i; j) edge is created in the return graph for every (i; j) edge in the first input that is not
matched by an (i; j) edge in the second input; if the second input has more (i; j) edges than
the first, no (i; j) edges are created in the return graph.

* : An (i; j) edge is created for every pairing of (i; j) edges in the respective input graphs.

%c% : An (i; j) edge is created in the return graph for every edge pair (i; k); (k; j) with the first edge
in the first input and the second edge in the second input.

! : An (i; j) edge is created in the return graph for every (i; j) in the input not having an edge.

| : An (i; j) edge is created in the return graph if either input contains an (i; j) edge.

& :  An (i; j) edge is created in the return graph if both inputs contain an (i; j) edge.



These operators enable to do some really convenient operations between two networks.

If you want to find more information about this wonderful package, you can read the pdf of the package.
1

View comments

The financial market is not only made of stock options. Other financial products enable market actors to target specific aims. For example, an oil buyer like a flight company may want to cover the risk of increase in the price of oil. In this case it is possible to buy on the financial market what is known as a "Call" or a "Call Option".

A Call Option is a contract between two counterparties (the flight company and a financial actor). The buyer of the Call has the opportunity but not the obligation to buy a certain  quantity of a certain product (called the underlying) at a certain date (the maturity) for a certain price (the strike).

If the flight company doesn't want to suffer from the risk of an increase of oil on the market within the next year, a Call Option of maturity 1 year, with a certain strike K may be bought. In this case, if the oil price on the market is over the strike, the flight company will use the Call option to buy oil for the price of the strike. If the price of the oil is lower than the strike, then the flight will not use the option and will buy oil on the market.

As you can see, this contract is not symmetric  The flight company has an option (to buy or not to buy) while the second counterparty just follows the decision of the flight company. Therefore, the flight company will have to pay something to the second counterparty. One of the great question is how much should it be charged.

We will therefore use a pricing algorithm to estimate the price of this Call Option. We first build an algorithm to simulate the value of an asset in the model of Black-Scholes. Then we simulate the historical value of this asset in order to simulate the final value of the Call Option.

Indeed, if we know the value of the asset at the maturity (A(T)), we get directly the value of the Call Option of strike K at maturity : max(A(T) - K , 0).

Our asset is randomly distributed as:
A(t+dt) = A(t) (1 + r dt + v(B(t+dt) -B(t))

Where r is the interest rate, v the volatility of the asset and B a Brownian process (for more detail you can look at the post on Brownian motion).

After repeating the process many times, we estimate the mean of the Call Option for different strike in order to estimate the price of the Call Option.


As we can see the price of the Call decreases with the strike. Actually it converges towards 0. Indeed, the higher the strike is, the lower the chance are such that the asset goes over the strike.

What is done here can be done for many, many, many other financial products in order to price them by Monte Carlo.



The code (R):


sample.size <- 365
mu <- 0.1
sigma <- 0.2

a0 <- 1
Asset <- function(sample.size = 365, mu = 0.1, sigma = 0.2, a0 = 1){
  dt <- 1/sample.size
  sdt =sigma*sqrt(dt)
  gauss <- rnorm(sample.size)
  asset <- NULL
  asset[1] <- a0 + mu + sigma * gauss[1]
  test.default <- FALSE
  for(i in 2:365){
    if (!test.default){
      asset[i] <- asset[i-1] * (1 + dt*mu + sdt * gauss[i])
    }
    else{
      asset[i] <- 0
    }
    if (asset[i] <= 0){
      asset[i] <- 0
      test.default <- TRUE
    }
  }
  return(asset)
}


PriceEstimation <- function(t, tf = 365, r = 0.1, strike = 1, n = 1000, mu = 0.1, sigma = 0.2, a0 = 1){
  mean <- 0
  for(i1 in 1:n){
    mean <- mean + exp(-r*(tf-t)/tf) * max(0, (Asset(tf, mu, sigma, a0)-strike))
  }
  mean <- mean/n
  return(mean)
}

res <- NULL
for(i in 1:length(seq(0, 3, 0.05))){
  res[i] <- PriceEstimation(t = 0, tf = 365, r = 0.1, strike = seq(0, 3, 0.05)[i], n = 1000, mu = 0.1, sigma = 0.2, a0 = 1)
}

plot(seq(0,3, 0.05), res,type = 'l', xlab = "Strike Value",  ylab = "Price of the Call")



0

Add a comment

Blog Archive
Translate
Translate
Loading