PBOR Ch 4_5 Markov jump processes Q5.1(iii)(a)

Discussion in 'CS2' started by rlsrachaellouisesmith, Jun 16, 2020.

  1. rlsrachaellouisesmith

    rlsrachaellouisesmith Ton up Member

    Good evening

    I have set up a function to calculate the probability of moving from state i to j over time s to t in a time inhomegenous Markov jump process. I am happy with the function to create the generator matrix but when I input this into another function as part of a loop it is not working for me.

    I attach my code, can you spot my error?

    Thank you
     

    Attached Files:

  2. Andrew Martin

    Andrew Martin ActEd Tutor Staff Member

    Hello

    The problem is with the loop in your P_s_t function, you're missing some brackets. I've highlighted where you need to put them in red below:

    P_s_t <- function(s,t,h){
    Q <- diag(3)
    for(j in 1:((t-s)/h)){
    Z = P_t_t_h(s+(j-1)*h,h)
    Q = Q %*% Z
    Pr = Q
    }
    Pr
    }

    To see the difference with and without the brackets see the below:

    Without the brackets:

    > 1: (35-25)/(1/12)
    [1] 12 24 36 48 60 72 84 96 108 120

    This first calculates 1: (35-25), which is 1,2,3,4,...,9,10 and then divides by (1/12) to get 12,24,....,120. This is not what we want.

    With the brackets:

    > 1: ((35-25)/(1/12))
    [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    [29] 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    [57] 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    [85] 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    [113] 113 114 115 116 117 118 119 120

    This is what we want, we want intervals of 1/12 over a length of 10, so we want to iterate 120 times.

    You can also trim down some of your code. Not essential, but could save you some time. I've put some adjusted functions below:

    P_t_t_h <- function(t,h){
    diag(3) + h*gen_t(t)
    }

    P_s_t <- function(s,t,h){
    Q <- diag(3)
    for(j in 1:((t-s)/h)){
    Z = P_t_t_h(s+(j-1)*h,h)
    Q = Q %*% Z
    }
    Q
    }

    Hope that helps!

    Andy
     
  3. rlsrachaellouisesmith

    rlsrachaellouisesmith Ton up Member

    Thank you Andy, that makes complete sense, and something I had not thought to check. It will be in my toolbox for error checking from now on. Thank you.
     
  4. rlsrachaellouisesmith

    rlsrachaellouisesmith Ton up Member

    A quick follow up on this Andy, in the very last part of the question (iv) the commentary mentions that the model assumes the number of voters remains constant. Is this a characteristic of the Markov 3 state model? Thank you
     
  5. Andrew Martin

    Andrew Martin ActEd Tutor Staff Member

    Hello

    For this particular model, this is because the only states we have are those that reflect a party affiliation (R, B and X). So, if we think about each member of the voting population being in this state space, the model doesn't allow for them to stop voting, as there is no state to represent, for example, 'no longer voting'. Similarly, the model does not allow for anyone to join the voting population because there is no state such as 'not yet voted' or even again 'no longer voting' (which could allow for transitions back to one of the party states if someone stops voting but then starts again).

    Hope this helps.

    Andy
     

Share This Page