# random generation any easier code

BM = rep(0,T)
n = 100
count = 0
for (i in 1:n){
X = rnorm(T,mu,sigma)
BM[1] = X[1]
for (j in 2:T){
BM[j] = BM[j-1]+X[j]
}
if (BM[T]>3) {count = count+1}
}count/n

count = 0
for (i in 1:n){
X[1] = 5
for (j in 2:(T+1)){
inc = sample(posinc,1)
X[j] = X[j-1]+inc
X[j] = max(X[j],0)
}
if (X[T+1]<10){count = count + 1}
}
count/n

install.packages("expm")
library(expm)

vect = c(0.6,0.3,0.4,0.7)
P = matrix(vect,2,2)

P%^%5

P%^%10

P%^%20

# ~~~~~~~~~~~~~~~~~~~~~~~~~ b ~~~~~~~~~~~~~~~~~~~~~~~~~

P5 = P%^%5
P5[1,2]

t = 24
lambda = 20
n = 1000

Y = rep(0,n)

for (i in 1:n){
Nt = rpois(1,lambda*t)
X = rexp(Nt,1)
Y[i] = sum(X)
}

mean(Y)
var(Y)

entries = c(0.3,0.7,0,0.4,0.6,0,1,0,0)
P = matrix(entries,3,3)
P = t(P)

P%^%2

P%^%3

P%^%5

P%^%20

P%^%100

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ d ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

P = matrix(0,6,6)
P[1,] = c(0.3,0.7,0,0,0,0)
P[2,] = c(0.5,0.5,0,0,0,0)
P[3,] = c(0.3,0.2,0,0,0,0.5)
P[4,] = c(0,0,0.8,0,0.2,0)
P[5,] = c(0,0,0,0.6,0,0.4)
P[6,] = c(0,0,0,0,0,1)

P
rowSums(P)

P%^%2

P%^%3

P%^%5

P%^%20

P%^%100

P%^%1000

P%^%10000

n = 1e5
T = 90
X = matrix(0,T,n)

for (k in 1:n){
posinc = c(2,1,1,1,0,0,0,0,-1,-1)
X[1,k] = 20+sample(posinc,1)
}

for (k in 1:n){
for (j in 2:T){
posinc = c(2,1,1,1,0,0,0,0,-1,-1)
X[j,k] = X[j-1,k]+sample(posinc,1)
X[j,k] = max(X[j,k],0)
}
}

mean((X[T,]>5)*(X[T,]<15))

mean(X[60,])

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

t = 12
lambda = 10
n = 1e5

Y = rep(0,n)

for (i in 1:n){
Nt = rpois(1,lambda*t)
X = rgamma(Nt,1,1)
Y[i] = sum(X)
}

mean(Y>100)

Can you describe what you're trying to do here?

Also, it will help us help you if you could turn this into a self-contained reprex (short for reproducible example).

``````install.packages("reprex")
``````

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

There's also a nice FAQ on how to do a minimal reprex for beginners, below:

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

``````reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")
``````

For pointers specific to the community site, check out the reprex FAQ.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.