#Title:STAT 417: Project 3 - Markov Chain
#Author: Julie Lapine
#Date: 03 August 20221
#Description:
#A computer is shared by 2 users who send tasks
#to a computer remotely and work independently.
#At any minute, any connected user may disconnect
#with probability 0.5, and any disconnected user
#may connect with a new task with probability 0.2.
#(This makes up the following matrix)
#Let X(t) be the number of concurrent users at
#time t (in minutes). This is a Markov chain with
#3 states: 0, 1, and 2. The probability transition
#matrix can be calculated and it is
# 0.64 0.32 0.04
# 0.40 0.50 0.10
# 0.25 0.50 0.25
#Generate 10000 transitions of the Markov chain.
#What percent of times do you find your generated
#Markov chain in each of its 3 states?
#Code:

#Matrix data
M = matrix(data=c(0.64, 0.40, 0.25, 0.32, 0.50, 0.50, 0.04, 0.10, 0.25), nrow=3)

#States 0,1,2
S = c(0,1,2)

#Initial state
initial = 0 
t = c()
for (index in 1:10000) {
  if(initial == 0) {
    initial = sample(S, 1, prob = c(0.64,0.32,0.04))
  }
  if(initial == 1) {
    initial = sample(S, 1, prob = c(0.40,0.50,0.10)) 
  }
  if(initial == 2) {
    initial = sample(S, 1, prob = c(0.25,0.50,0.25)) 
  }
  t = c(t, initial)
}

##Simulated data 
print(t[1:100]) 
##   [1] 0 0 0 0 0 0 2 2 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0 0 2 1 0 0 1 1 0 0 0 0 0 1 1
##  [38] 1 0 0 0 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0
##  [75] 0 0 1 0 1 0 0 1 1 2 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0
#percent of times for state 0
print((sum(t == 0)/10000))
## [1] 0.6617
#percent of times for the state 1
print((sum(t == 1)/10000))
## [1] 0.314
#percent of times for the state 2
print((sum(t == 2)/10000))
## [1] 0.0243
#Summary Graphs/Tables:
hist(t, probability = TRUE) #graph of results

#Discussion:
#After 10,000 transitions are generated, the rates of the
#states 0,1,2 are: 65.31%, 32.33%, and 2.36%
#