如何构造表并更改为用户输入

时间:2017-06-04 12:12:15

标签: r input

下面是我的代码,但它应该允许用户指定T,n和M的输入我该怎么做?

### Simulation of a discrete process on [0, T]

# Write a program to simulate the process
process_simulation <- function(X0, t, n, mu, sigma, M) {

  #initialize simulation results
  X_simulated <- matrix(0, ncol=n+1, nrow=M)
  dt <- t/n
  #run simulations in a loop
  for (m in 1:M) {
    #initialize simulated process
    X <- rep(0, n+1)
    X[1] <- X0
    #calculate sum in a loop
    for (i in 0:(n-1)) {
      #next Wiener process value
      dz <- (dt)^0.5*rnorm(1)
      #calculate next X value
      dX <- mu(X[i+1], i*dt)*dt+sigma(X[i+1], i*dt)*dz
      X[i+2] <- X[i+1]+dX
    }
    #save simulated process
    X_simulated[m, ] <- X
  }

      #return all simulated processes
      return(X_simulated)
    }


#(a)
X0 <- 1
t <- 1
n <- 100
mu <- function(x, t) { return(0) }
sigma <- function(x, t) { return(1) }
M <- 1000

Xa_1 <- process_simulation(X0, t, n, mu, sigma, M)

#(i) compare graphically distribution of x(t) at t=0.5 and t=1
plot(density(Xa_1[, n/2+1]), col="blue", xlab="x", xlim=c(-5, 5), main=paste("n=", n, ", M=", M, sep=""), xaxs="i", yaxs="i")
lines(density(Xa_1[, n+1]), col="green")
#plot the true distributions (in grey color)
lines(seq(-5, 5, length.out=1000), dnorm(seq(-5, 5, length.out=1000), 1, 1), col="grey")
lines(seq(-5, 5, length.out=1000), dnorm(seq(-5, 5, length.out=1000), 1, 0.5), col="grey")
legend("topright", legend=c("simulated t=0.5", "simulated t=1", "theoretical"), lty=1, col=c("blue", "green", "grey"))

#(ii) experiment with n and M
n <- 50
Xa_2 <- process_simulation(X0, t, n, mu, sigma, M)
plot(density(Xa_2[, n/2+1]), col="blue", xlab="x", xlim=c(-5, 5), main=paste("n=", n, ", M=", M, sep=""), xaxs="i", yaxs="i")
lines(density(Xa_2[, n+1]), col="green")
lines(seq(-5, 5, length.out=1000), dnorm(seq(-5, 5, length.out=1000), 1, 1), col="grey")
lines(seq(-5, 5, length.out=1000), dnorm(seq(-5, 5, length.out=1000), 1, 0.5), col="grey")
legend("topright", legend=c("simulated t=0.5", "simulated t=1", "theoretical"), lty=1, col=c("blue", "green", "grey"))

n <- 200
Xa_3 <- process_simulation(X0, t, n, mu, sigma, M)
plot(density(Xa_3[, n/2+1]), col="blue", xlab="x", xlim=c(-5, 5), main=paste("n=", n, ", M=", M, sep=""), xaxs="i", yaxs="i")
lines(density(Xa_3[, n+1]), col="green")
lines(seq(-5, 5, length.out=1000), dnorm(seq(-5, 5, length.out=1000), 1, 1), col="grey")
lines(seq(-5, 5, length.out=1000), dnorm(seq(-5, 5, length.out=1000), 1, 0.5), col="grey")
legend("topright", legend=c("simulated t=0.5", "simulated t=1", "theoretical"), lty=1, col=c("blue", "green", "grey"))

#The higher the value of n, the better fitting to the true distribution.

n <- 100
M <- 500
Xa_4 <- process_simulation(X0, t, n, mu, sigma, M)
plot(density(Xa_4[, n/2+1]), col="blue", xlab="x", xlim=c(-5, 5), main=paste("n=", n, ", M=", M, sep=""), xaxs="i", yaxs="i")
lines(density(Xa_4[, n+1]), col="green")
lines(seq(-5, 5, length.out=1000), dnorm(seq(-5, 5, length.out=1000), 1, 1), col="grey")
lines(seq(-5, 5, length.out=1000), dnorm(seq(-5, 5, length.out=1000), 1, 0.5), col="grey")
legend("topright", legend=c("simulated t=0.5", "simulated t=1", "theoretical"), lty=1, col=c("blue", "green", "grey"))

M <- 2000
Xa_5 <- process_simulation(X0, t, n, mu, sigma, M)
plot(density(Xa_5[, n/2+1]), col="blue", xlab="x", xlim=c(-5, 5), main=paste("n=", n, ", M=", M, sep=""), xaxs="i", yaxs="i")
lines(density(Xa_5[, n+1]), col="green")
lines(seq(-5, 5, length.out=1000), dnorm(seq(-5, 5, length.out=1000), 1, 1), col="grey")
lines(seq(-5, 5, length.out=1000), dnorm(seq(-5, 5, length.out=1000), 1, 0.5), col="grey")
legend("topright", legend=c("simulated t=0.5", "simulated t=1", "theoretical"), lty=1, col=c("blue", "green", "grey"))

#The higher the value of M, the better fitting to the true distribution.

n <- 200
Xa_6 <- process_simulation(X0, t, n, mu, sigma, M)
plot(density(Xa_6[, n/2+1]), col="blue", xlab="x", xlim=c(-5, 5), main=paste("n=", n, ", M=", M, sep=""), xaxs="i", yaxs="i")
lines(density(Xa_6[, n+1]), col="green")
lines(seq(-5, 5, length.out=1000), dnorm(seq(-5, 5, length.out=1000), 1, 1), col="grey")
lines(seq(-5, 5, length.out=1000), dnorm(seq(-5, 5, length.out=1000), 1, 0.5), col="grey")
legend("topright", legend=c("simulated t=0.5", "simulated t=1", "theoretical"), lty=1, col=c("blue", "green", "grey"))

如何使用此代码,让用户决定T,n和M,然后计算代码中的内容?

0 个答案:

没有答案