The expected life time of the mouse in this Markov chain model

I read Markov’s cat and mouse model on wikipedia and decided to write Julia’s code to empirically confirm the analytical results:

P = [ 
  0     0       0.5     0       0.5 ;
  0     0       1       0       0   ;
  0.25  0.25    0       0.25    0.25;
  0     0       0.5     0       0.5 ;
  0     0       0       0       1
]
prob_states = transpose([0.0, 1, 0, 0, 0])
prob_end = [0.0]
for i in 1:2000
  prob_states = prob_states * P
  prob_end_new = (1 - sum(prob_end)) * prob_states[end]
  push!(prob_end, prob_end_new)
  println("Ending probability: ", prob_end_new)
  println("Cumulative: ", sum(prob_end))
end
println("Expected lifetime: ", sum(prob_end .* Array(1:2001)))

Here Pis the transition matrix, prob_statesis the probability distribution of states at each iteration, prob_endis the array of completion probabilities at each step (for example, prob_end[3]is the probability of completion at stage 3).

According to the result of this script, the expected mouse lifetime is about 4.3, and the analytical result is 4.5. The script makes sense to me, so I really don't know where it could go wrong. Can anyone help?

PS An increase in the number of iterations by an order of magnitude changes almost nothing.

+4
2

. , , 64- ( ), .

, prob_end , . Float64 .

4.5; , , , , . .

, , , . , - .

( ).

+6

, :

const first_index = 1
const last_index = 5
const cat_start = 2
const mouse_start = 4

function move(i)
    if i == first_index
        return first_index + 1
    elseif i == last_index
        return last_index - 1
    else
        return i + rand([-1,1])
    end
end

function step(cat, mouse)
    return move(cat), move(mouse)
end

function game(cat, mouse)
    i = 1
    while cat != mouse
        cat, mouse = step(cat, mouse)
        i += 1
    end
    return i
end

function run()
    res = [game(cat_start, mouse_start) for i=1:10_000_000]
    return mean(res), std(res)/sqrt(length(res))
end

μ,σ = run()
println("Mean lifetime: $μ ± $σ")

:

Mean lifetime: 4.5004993 ± 0.0009083568998918751
+3

Source: https://habr.com/ru/post/1668198/


All Articles