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.

I found a golden website. The blog of Esteban Moro. He uses R to work on networks. In particular he has done a really nice code to make some great videos of networks. This post is purely a copy of his code. I just changed a few arguments to change colors and to do my own network.

3

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).

1

I already talked about networks a few times in this blog. In particular, I had this approach to explain spatial segregation in a city or to solve the Guess Who? problem. However, one of the question is how to generate a good network.

The function apply() is certainly one of the most useful function. I was scared of it during a while and refused to use it. But it makes the code so much faster to write and so efficient that we can't afford not using it.

1

Have you ever played the board game "Guess who?".

If you want to choose randomly your next holidays destination, you are likely to process in a way which is certainly biased. Especially if you choose randomly the latitude and the longitude.

4

My previous post is about a method to simulate a Brownian motion. A friend of mine emailed me yesterday to tell me that this is useless if we do not know how to simulate a normally distributed variable.

The Brownian motion is certainly the most famous stochastic process (a random variable evolving in the time). It has been the first way to model a stock option price (Louis Bachelier's thesis in 1900).

1

The merge of two insurance companies enables to curb the probability of ruin by sharing the risk and the capital of the two companies.

For example, we can consider two insurance companies, A and B.

How to estimate PI when we only have R and the formula for the surface of a circle (Surface = PI * r * r)?

The estimation of this number has been one of the greatest challenge in the history of mathematics. PI is the ratio between a circle's circumference and diameter.

I was in a party last night and a guy was totally drunk. Not just the guy who had a few drinks and speaks a bit too loud, but the one who is not very likely to remember what he has done during his night, but who is rather very likely to suffer from a huge headache today.

I am currently doing an internship in England. Therefore, I keep alternating between French and English in my different emails and other forms of communication on the Internet. I have been surprised to see that some websites are able to recognize when I use French or when I use English.

The VIX (volatility index) is a financial index which measures the expectation of the volatility of the stock market index S&P 500 (SPX). The higher is the value of the VIX the higher are the expectations of important variations in the S&P 500 during the next month.

The Olympic Games have finished a couple of days ago. Two entire weeks of complete devotion for sport. Unfortunately I hadn’t got any ticket but I didn’t fail to watch many games on TV and internet.

Hello (New World!), 

My name is Edwin, I’m a 22 year-old French student in applied mathematics. In particular, I study probability, statistics and risk theory.

Blog Archive
Translate
Translate
Loading