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

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

    To create the network, I used the  Barabási-Albert algorithm that you can find at the end of the post on the different algorithms for networks. Igraph is the library which has been used.

    In order to make a video from the .png I used a software called Ffmpeg. It took me a bit of time to use it but you can find some tutorials on Internet.

    Here is the kind of result you can expect :

    The code (R) : 


    n <- 300
    data <- matrix(0, ncol = 3, nrow = n-1)
    data[1,2] <- 1
    data[1:(n-1),1] <- 2:n
    data[, 3] <- 1:(n-1)
    weight <- NULL
    weight[1] <- 1
    weight[2] <- 1
    for(i1 in 2:(n-1)){
      link = sample(c(1:(i1)), size = 1, prob = weight)
      data[i1, 2] <- link
      weight[i1+1] <- 1
      weight[link] <- weight[link] + 1
    }

    install.packages("igraph")
    library(igraph)

    #generate the full graph
    g <- graph.edgelist(as.matrix(data[,c(1,2)]),directed=F)
    E(g)$time <- data[,3]

    #generate a cool palette for the graph
    YlOrBr <- c(hsv(0.925, 0.20, 0.7), hsv(0.925, 0.40, 0.7), hsv(0.925, 0.60, 0.7), hsv(0.925, 0.80, 0.7), hsv(0.925,1, 0.7))
    YlOrBr.Lab <- colorRampPalette(YlOrBr, space = "Lab")
    #colors for the nodes are chosen from the very beginning
    vcolor <- rev(YlOrBr.Lab(vcount(g)))

    #time in the edges goes from 1 to 300. We kick off at time 3
    ti <- 3
    #weights of edges formed up to time ti is 1. Future edges are weighted 0
    E(g)$weight <- ifelse(E(g)$time < ti,1,0)
    #generate first layout using weights.
    layout.old <- layout.fruchterman.reingold(g,params=list(weights=E(g)$weight))


    #total time of the dynamics
    total_time <- max(E(g)$time)
    #This is the time interval for the animation. In this case is taken to be 1/10
    #of the time (i.e. 10 snapshots) between adding two consecutive nodes
    dt <- 0.1
    #Output for each frame will be a png with HD size 1600x900 :)
    png(file="example%04d.png", width=1600,height=900)
    nsteps <- max(E(g)$time)
    #Time loop starts
    for(ti in seq(3,total_time,dt)){
      #define weight for edges present up to time ti.
      E(g)$weight <- ifelse(E(g)$time < ti,1,0)
      #Edges with non-zero weight are in gray. The rest are transparent
      E(g)$color <- ifelse(E(g)$time < ti,"black",rgb(0,0,0,0))
      #Nodes with at least a non-zero weighted edge are in color. The rest are transparent
      V(g)$color <- ifelse(graph.strength(g)==0,rgb(0,0,0,0),vcolor)
      #given the new weights, we update the layout a little bit
      layout.new <- layout.fruchterman.reingold(g,params=list(niter=10,start=layout.old,weights=E(g)$weight,maxdelta=1))
      #plot the new graph
      plot(g,layout=layout.new,vertex.label="",vertex.size=1+2*log(graph.strength(g)),vertex.color=V(g)$color,edge.width=1.5,asp=9/16,margin=-0.15)
      #use the new layout in the next round
      layout.old <- layout.new
    }
    dev.off()
    3

    View comments

Blog Archive
Translate
Translate
Loading