Path of system Excel files
////////////////////////////

library(readxl)
path <- readxl_example("datasets.xls")
path
[1] "C:/Users/Paul Yan/AppData/Local/R/win-library/4.3/readxl/extdata/datasets.xls"

////////////////////////////

normal distributions:
/////////////////////////////////
The normal distribution is a probability distribution that associates 
       the normal random variable X with a cumulative probability . 
        The normal distribution is defined by the following equation:
                           1                                     
           f(x) = ---------------- * exp(-0.5 *[(x-mean)/(sigma))]^2)               
                 sqrt(2*pi*sigma^2)                                

                          
/////////////////////////////////

Method II: show all the system Excel files
////////////////////////////

library(readxl)
> readxl_example()
 [1] "clippy.xls"    "clippy.xlsx"   "datasets.xls" 
 [4] "datasets.xlsx" "deaths.xls"    "deaths.xlsx"  
 [7] "geometry.xls"  "geometry.xlsx" "type-me.xls"  
[10] "type-me.xlsx" 
> 
////////////////////////////

why the normal distributions is so important for finance?
/////////////////////////////////
There are most important reasons:

 1) if our model is good, then the error terms should follow a normal   
            distribution with mean error is zero. 

 2) In finance, we assume that stock price follow a log-normal distribution 
             while stock returns follow a normal distribution. 
 
/////////////////////////////////

write.table(x,'clipboard')
////////////////////////////

Step 1: generate our data x

Step 2: write.table(x,'clipboard')

Step 3: launch Excel and paste there

////////////////////////////

Simulate stock prices (path)
/////////////////////////////////
Given parameters.
      st   : stock price at time t
       T   : maturity (in years)
       n   : number of steps
       r   : mean return or (risk-free rate)
     sigma : volatility of the stock 

             deltaT= T/n   a small interval 

1) simulate s(t+1), s(t+2) etc. 
---------------------------
     s(t+1)=st* exp((r-0.5*sigma^2 )*deltaT + sigma*random*sqrt(deltaT))
        where: s(t+1) is the stock price at time t+1
               random is a random number drawn from a standard normal distribution
      A better formula: s(t+1) = s(t) * exp( a + b)
         a = (r-0.5*sigma^2 )*deltaT
         b = sigma*random*sqrt(deltaT)

2) R command lines to simulate terminal stock price (sT) at time T
-------------------------------------------------------------
     sT=st* exp((r-0.5*sigma^2 )*T + sigma*rnorm(1)*sqrt(T))
           or
     a = (r-0.5*sigma^2 )*T
     b = sigma*rnorm(1)*sqrt(T)
     sT = st * exp( a + b)

   # R program 
    sT_f<-function(S,r,T,sigma){
         S*exp((r-0.5*sigma*sigma)*T+sigma*rnorm(1)*sqrt(T))
    }

3) R program to simultae stock prices 
-------------------------------------
sT_path_f<-function(S,r,T,n,sigma){
    delta_T<-T/n
    ST<-seq(1:n)*0
    ST[1]<-S
    for (i in 2:n)
    ST[i]<-ST[i-1]*exp((r-0.5*sigma*sigma)*delta_T+sigma*rnorm(1)*sqrt(delta_T))
    return(ST)
}

/////////////////////////////////