Markov chain simulation

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I'm wondering whether there is an algorithm to simulate a discrete Markov chain with a specific number of occurrences of state knowing the transition matrix way.



For example, how to simulate in R a Markov chain of length $n$ with $p$ occurrences ($p < n$)



TransitionMatrix<- matrix(c(0.7, 0.3, 0.4, 0.6),byrow=TRUE, nrow=2)
colnames(TransitionMatrix) <- c('0','1') row.names(TransitionMatrix) <- c('0','1')






share|cite|improve this question






















  • I'm not sure what you mean by an "occurrence". (I apologize for my ignorance in case this is standard terminology in Markov chain theory I just wasn't aware of.) I would guess you are saying you want to select a starting state, let the state evolve from one step to the next stochastically using the transition matrix to determine the probabilities of where you'll go next given where you are, do that $n$ times, and finally count the number of times $p$ you visited a particular state of interest. Is that what you mean?
    – Dan Kneezel
    Jan 28 '14 at 7:56










  • @DanKneezel Assuming that n=20, rnd_occ1<-c(0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1) and rnd_occ2<-c(0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0) are two possible candidate having a such transition matrix. Moreover the state "1" has 9 and 6 occurences in the 1st and 2nd sequences respectively. What I'm looking for it is an algorithm or a library to simulate efficiently such a markov chain sequence with for instance 12 occurences of the state "1".
    – Armel
    Jan 28 '14 at 18:39











  • Got something from the answers?
    – Did
    May 28 '15 at 8:17














up vote
3
down vote

favorite












I'm wondering whether there is an algorithm to simulate a discrete Markov chain with a specific number of occurrences of state knowing the transition matrix way.



For example, how to simulate in R a Markov chain of length $n$ with $p$ occurrences ($p < n$)



TransitionMatrix<- matrix(c(0.7, 0.3, 0.4, 0.6),byrow=TRUE, nrow=2)
colnames(TransitionMatrix) <- c('0','1') row.names(TransitionMatrix) <- c('0','1')






share|cite|improve this question






















  • I'm not sure what you mean by an "occurrence". (I apologize for my ignorance in case this is standard terminology in Markov chain theory I just wasn't aware of.) I would guess you are saying you want to select a starting state, let the state evolve from one step to the next stochastically using the transition matrix to determine the probabilities of where you'll go next given where you are, do that $n$ times, and finally count the number of times $p$ you visited a particular state of interest. Is that what you mean?
    – Dan Kneezel
    Jan 28 '14 at 7:56










  • @DanKneezel Assuming that n=20, rnd_occ1<-c(0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1) and rnd_occ2<-c(0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0) are two possible candidate having a such transition matrix. Moreover the state "1" has 9 and 6 occurences in the 1st and 2nd sequences respectively. What I'm looking for it is an algorithm or a library to simulate efficiently such a markov chain sequence with for instance 12 occurences of the state "1".
    – Armel
    Jan 28 '14 at 18:39











  • Got something from the answers?
    – Did
    May 28 '15 at 8:17












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I'm wondering whether there is an algorithm to simulate a discrete Markov chain with a specific number of occurrences of state knowing the transition matrix way.



For example, how to simulate in R a Markov chain of length $n$ with $p$ occurrences ($p < n$)



TransitionMatrix<- matrix(c(0.7, 0.3, 0.4, 0.6),byrow=TRUE, nrow=2)
colnames(TransitionMatrix) <- c('0','1') row.names(TransitionMatrix) <- c('0','1')






share|cite|improve this question














I'm wondering whether there is an algorithm to simulate a discrete Markov chain with a specific number of occurrences of state knowing the transition matrix way.



For example, how to simulate in R a Markov chain of length $n$ with $p$ occurrences ($p < n$)



TransitionMatrix<- matrix(c(0.7, 0.3, 0.4, 0.6),byrow=TRUE, nrow=2)
colnames(TransitionMatrix) <- c('0','1') row.names(TransitionMatrix) <- c('0','1')








share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Oct 6 '15 at 12:41









rbm

25919




25919










asked Jan 28 '14 at 7:37









Armel

162




162











  • I'm not sure what you mean by an "occurrence". (I apologize for my ignorance in case this is standard terminology in Markov chain theory I just wasn't aware of.) I would guess you are saying you want to select a starting state, let the state evolve from one step to the next stochastically using the transition matrix to determine the probabilities of where you'll go next given where you are, do that $n$ times, and finally count the number of times $p$ you visited a particular state of interest. Is that what you mean?
    – Dan Kneezel
    Jan 28 '14 at 7:56










  • @DanKneezel Assuming that n=20, rnd_occ1<-c(0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1) and rnd_occ2<-c(0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0) are two possible candidate having a such transition matrix. Moreover the state "1" has 9 and 6 occurences in the 1st and 2nd sequences respectively. What I'm looking for it is an algorithm or a library to simulate efficiently such a markov chain sequence with for instance 12 occurences of the state "1".
    – Armel
    Jan 28 '14 at 18:39











  • Got something from the answers?
    – Did
    May 28 '15 at 8:17
















  • I'm not sure what you mean by an "occurrence". (I apologize for my ignorance in case this is standard terminology in Markov chain theory I just wasn't aware of.) I would guess you are saying you want to select a starting state, let the state evolve from one step to the next stochastically using the transition matrix to determine the probabilities of where you'll go next given where you are, do that $n$ times, and finally count the number of times $p$ you visited a particular state of interest. Is that what you mean?
    – Dan Kneezel
    Jan 28 '14 at 7:56










  • @DanKneezel Assuming that n=20, rnd_occ1<-c(0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1) and rnd_occ2<-c(0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0) are two possible candidate having a such transition matrix. Moreover the state "1" has 9 and 6 occurences in the 1st and 2nd sequences respectively. What I'm looking for it is an algorithm or a library to simulate efficiently such a markov chain sequence with for instance 12 occurences of the state "1".
    – Armel
    Jan 28 '14 at 18:39











  • Got something from the answers?
    – Did
    May 28 '15 at 8:17















I'm not sure what you mean by an "occurrence". (I apologize for my ignorance in case this is standard terminology in Markov chain theory I just wasn't aware of.) I would guess you are saying you want to select a starting state, let the state evolve from one step to the next stochastically using the transition matrix to determine the probabilities of where you'll go next given where you are, do that $n$ times, and finally count the number of times $p$ you visited a particular state of interest. Is that what you mean?
– Dan Kneezel
Jan 28 '14 at 7:56




I'm not sure what you mean by an "occurrence". (I apologize for my ignorance in case this is standard terminology in Markov chain theory I just wasn't aware of.) I would guess you are saying you want to select a starting state, let the state evolve from one step to the next stochastically using the transition matrix to determine the probabilities of where you'll go next given where you are, do that $n$ times, and finally count the number of times $p$ you visited a particular state of interest. Is that what you mean?
– Dan Kneezel
Jan 28 '14 at 7:56












@DanKneezel Assuming that n=20, rnd_occ1<-c(0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1) and rnd_occ2<-c(0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0) are two possible candidate having a such transition matrix. Moreover the state "1" has 9 and 6 occurences in the 1st and 2nd sequences respectively. What I'm looking for it is an algorithm or a library to simulate efficiently such a markov chain sequence with for instance 12 occurences of the state "1".
– Armel
Jan 28 '14 at 18:39





@DanKneezel Assuming that n=20, rnd_occ1<-c(0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1) and rnd_occ2<-c(0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0) are two possible candidate having a such transition matrix. Moreover the state "1" has 9 and 6 occurences in the 1st and 2nd sequences respectively. What I'm looking for it is an algorithm or a library to simulate efficiently such a markov chain sequence with for instance 12 occurences of the state "1".
– Armel
Jan 28 '14 at 18:39













Got something from the answers?
– Did
May 28 '15 at 8:17




Got something from the answers?
– Did
May 28 '15 at 8:17










2 Answers
2






active

oldest

votes

















up vote
3
down vote













First a reformulation of the question, then a pseudo-algorithm to solve it.



Reformulation: Let $ngeqslant1$ and assume that $X=(X_t)_1leqslant tleqslant n$ is a Markov chain of length $n$ on the state space $0,1$ with transition probability matrix $beginpmatrix1-a & a\ b& 1-bendpmatrix$ for some $a$ and $b$ in $(0,1)$.



Fix $kleqslant n$ and consider the set $Gamma_k^n$ of paths $gamma=(x_t)_1leqslant tleqslant n$ of length $n$ visiting $k$ times the state $1$. For every $x$ and $y$ in $0,1$ and every path $gamma$ in $Gamma_k^n$, let $N_gamma(xy)$ denote the number of times $tleqslant n$ such that $(x_t,x_t+1)=(x,y)$. Finally, let $M(gamma)=N_gamma(01)$.



Then, forgetting a possible discrepancy of $pm1$ here and there, one sees that $N_gamma(10)=M(gamma)$, $N_gamma(00)=n-k-M(gamma)$ and $N_gamma(11)=k-M(gamma)$, hence, for every $gamma$ in $Gamma_k^n$,
$$
P(X=gamma)=
(1-a)^N_gamma(00)a^N_gamma(01)b^N_gamma(10)(1-b)^N_gamma(11),
$$
is also, since $n$ and $k$ are fixed,
$$
P(X=gamma)= (1-a)^n-k-M(gamma)a^M(gamma)b^M(gamma)(1-b)^k-M(gamma)propto c^M(gamma),
$$
where
$$
c=fracab(1-a)(1-b).
$$
Thus, there exists some $A_k^n$ independent of $gamma$ such that, for every $gamma$ in $Gamma_k^n$,
$$
P(X=gammamid X textvisits 1 textexactly k texttimes)=A_k^ncdot c^M(gamma),
$$
and the question is to simulate a random path in $Gamma_k^n$ following this distribution.



Pseudo-algorithm: This assumes one is able to generate uniformly subsets $T$ of $1,2,ldots,n$ of size $k$.



  • Choose a path $gamma$ uniformly in $Gamma_k^n$, that is, choose uniformly a subset $Tsubseteq1,2,ldots,n$ of size $k$ and define $gamma=(x_t)$ by $x_t=1$ if $t$ is in $T$ and $x_t=0$ otherwise.

  • Compute $M(gamma)$.

  • Accept $gamma$ with probability proportional to $c^M(gamma)$.

  • Repeat until a path $gamma$ is accepted. Return $gamma$.

To generate $T$, the following procedure might prove useful:



  • Let $T=varnothing$.

  • Choose $x$ uniformly in $1,2,ldots,n$. If $x$ is not already in $T$, add $x$ to $T$.

  • Repeat until $T$ has size $k$. Return $T$.





share|cite|improve this answer




















  • Thanks for your reply. I'm not sure I properly understand the pseudo-algorithm. Please, could you develop into an algorithm?
    – Armel
    Jan 28 '14 at 19:46










  • What for? Please be specific.
    – Did
    Jan 28 '14 at 21:50










  • if we assume a=6/7 and b=1/2, a possible path γ=(0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0). Nonetheless,the algorithm that you propose will not converge in this case. Indeed, the computation of c gives c=6, and hence c^M(γ) will be greater than 1.
    – Armel
    Jan 29 '14 at 1:22











  • Please read on my lips: proportional to.
    – Did
    Jan 29 '14 at 6:01










  • What I'm saying is in the case c is greater than 1, then c^M(γ) will also be greater than 1 and therefore, it wouldn't be possible to find an integer const such that P(X=γ∣X visits 1 exactly k times)= const*c^M(γ)
    – Armel
    Jan 29 '14 at 15:58

















up vote
-1
down vote













Suppose you have transition probabilities $p_1 leq p_2 dots leq p_k$, $sum p_k = 1$. Define the distribution function in pseudo code:



function cdf(state) { a = math.random() % randomly generated number in U(0,1)
if 0 <= a < p_1, return 1
if p_1 <= a < p_1 + p_2, return 2
...
if p_1 + ... + p_k-1 <= a < 1, return k


In the above, I have assumed that the transition probabilities are all the same, i.e. independent of the state. Otherwise, the $p_i$ will have to depend on state.



The above code is just saying that if I have numbers $p_1$, ..., $p_n$ such that $sum p_i = 1$, then I can partition the unit interval $[0,1]$ with these numbers. The area of the rectangle [$p_1 + ... + p_k-1, p_1 + ... + p_k$] with unit height is $p_k$, and that's the probability we want for the $k^th$ transition. This is the standard way of generating probability distributions, since if $X$ has cdf $F$, then $F(X) sim U(0,1)$.



Now pick an initial state (say $s = 1$) and a state you want to measure, say $r = 4$.



count = 0
for i = 1 : n
s = cdf(s) // transition with the appropriate probabilities
if s == 4, count++
end
end


I should mention this algorithm is the worst.






share|cite|improve this answer




















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "69"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f654250%2fmarkov-chain-simulation%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    First a reformulation of the question, then a pseudo-algorithm to solve it.



    Reformulation: Let $ngeqslant1$ and assume that $X=(X_t)_1leqslant tleqslant n$ is a Markov chain of length $n$ on the state space $0,1$ with transition probability matrix $beginpmatrix1-a & a\ b& 1-bendpmatrix$ for some $a$ and $b$ in $(0,1)$.



    Fix $kleqslant n$ and consider the set $Gamma_k^n$ of paths $gamma=(x_t)_1leqslant tleqslant n$ of length $n$ visiting $k$ times the state $1$. For every $x$ and $y$ in $0,1$ and every path $gamma$ in $Gamma_k^n$, let $N_gamma(xy)$ denote the number of times $tleqslant n$ such that $(x_t,x_t+1)=(x,y)$. Finally, let $M(gamma)=N_gamma(01)$.



    Then, forgetting a possible discrepancy of $pm1$ here and there, one sees that $N_gamma(10)=M(gamma)$, $N_gamma(00)=n-k-M(gamma)$ and $N_gamma(11)=k-M(gamma)$, hence, for every $gamma$ in $Gamma_k^n$,
    $$
    P(X=gamma)=
    (1-a)^N_gamma(00)a^N_gamma(01)b^N_gamma(10)(1-b)^N_gamma(11),
    $$
    is also, since $n$ and $k$ are fixed,
    $$
    P(X=gamma)= (1-a)^n-k-M(gamma)a^M(gamma)b^M(gamma)(1-b)^k-M(gamma)propto c^M(gamma),
    $$
    where
    $$
    c=fracab(1-a)(1-b).
    $$
    Thus, there exists some $A_k^n$ independent of $gamma$ such that, for every $gamma$ in $Gamma_k^n$,
    $$
    P(X=gammamid X textvisits 1 textexactly k texttimes)=A_k^ncdot c^M(gamma),
    $$
    and the question is to simulate a random path in $Gamma_k^n$ following this distribution.



    Pseudo-algorithm: This assumes one is able to generate uniformly subsets $T$ of $1,2,ldots,n$ of size $k$.



    • Choose a path $gamma$ uniformly in $Gamma_k^n$, that is, choose uniformly a subset $Tsubseteq1,2,ldots,n$ of size $k$ and define $gamma=(x_t)$ by $x_t=1$ if $t$ is in $T$ and $x_t=0$ otherwise.

    • Compute $M(gamma)$.

    • Accept $gamma$ with probability proportional to $c^M(gamma)$.

    • Repeat until a path $gamma$ is accepted. Return $gamma$.

    To generate $T$, the following procedure might prove useful:



    • Let $T=varnothing$.

    • Choose $x$ uniformly in $1,2,ldots,n$. If $x$ is not already in $T$, add $x$ to $T$.

    • Repeat until $T$ has size $k$. Return $T$.





    share|cite|improve this answer




















    • Thanks for your reply. I'm not sure I properly understand the pseudo-algorithm. Please, could you develop into an algorithm?
      – Armel
      Jan 28 '14 at 19:46










    • What for? Please be specific.
      – Did
      Jan 28 '14 at 21:50










    • if we assume a=6/7 and b=1/2, a possible path γ=(0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0). Nonetheless,the algorithm that you propose will not converge in this case. Indeed, the computation of c gives c=6, and hence c^M(γ) will be greater than 1.
      – Armel
      Jan 29 '14 at 1:22











    • Please read on my lips: proportional to.
      – Did
      Jan 29 '14 at 6:01










    • What I'm saying is in the case c is greater than 1, then c^M(γ) will also be greater than 1 and therefore, it wouldn't be possible to find an integer const such that P(X=γ∣X visits 1 exactly k times)= const*c^M(γ)
      – Armel
      Jan 29 '14 at 15:58














    up vote
    3
    down vote













    First a reformulation of the question, then a pseudo-algorithm to solve it.



    Reformulation: Let $ngeqslant1$ and assume that $X=(X_t)_1leqslant tleqslant n$ is a Markov chain of length $n$ on the state space $0,1$ with transition probability matrix $beginpmatrix1-a & a\ b& 1-bendpmatrix$ for some $a$ and $b$ in $(0,1)$.



    Fix $kleqslant n$ and consider the set $Gamma_k^n$ of paths $gamma=(x_t)_1leqslant tleqslant n$ of length $n$ visiting $k$ times the state $1$. For every $x$ and $y$ in $0,1$ and every path $gamma$ in $Gamma_k^n$, let $N_gamma(xy)$ denote the number of times $tleqslant n$ such that $(x_t,x_t+1)=(x,y)$. Finally, let $M(gamma)=N_gamma(01)$.



    Then, forgetting a possible discrepancy of $pm1$ here and there, one sees that $N_gamma(10)=M(gamma)$, $N_gamma(00)=n-k-M(gamma)$ and $N_gamma(11)=k-M(gamma)$, hence, for every $gamma$ in $Gamma_k^n$,
    $$
    P(X=gamma)=
    (1-a)^N_gamma(00)a^N_gamma(01)b^N_gamma(10)(1-b)^N_gamma(11),
    $$
    is also, since $n$ and $k$ are fixed,
    $$
    P(X=gamma)= (1-a)^n-k-M(gamma)a^M(gamma)b^M(gamma)(1-b)^k-M(gamma)propto c^M(gamma),
    $$
    where
    $$
    c=fracab(1-a)(1-b).
    $$
    Thus, there exists some $A_k^n$ independent of $gamma$ such that, for every $gamma$ in $Gamma_k^n$,
    $$
    P(X=gammamid X textvisits 1 textexactly k texttimes)=A_k^ncdot c^M(gamma),
    $$
    and the question is to simulate a random path in $Gamma_k^n$ following this distribution.



    Pseudo-algorithm: This assumes one is able to generate uniformly subsets $T$ of $1,2,ldots,n$ of size $k$.



    • Choose a path $gamma$ uniformly in $Gamma_k^n$, that is, choose uniformly a subset $Tsubseteq1,2,ldots,n$ of size $k$ and define $gamma=(x_t)$ by $x_t=1$ if $t$ is in $T$ and $x_t=0$ otherwise.

    • Compute $M(gamma)$.

    • Accept $gamma$ with probability proportional to $c^M(gamma)$.

    • Repeat until a path $gamma$ is accepted. Return $gamma$.

    To generate $T$, the following procedure might prove useful:



    • Let $T=varnothing$.

    • Choose $x$ uniformly in $1,2,ldots,n$. If $x$ is not already in $T$, add $x$ to $T$.

    • Repeat until $T$ has size $k$. Return $T$.





    share|cite|improve this answer




















    • Thanks for your reply. I'm not sure I properly understand the pseudo-algorithm. Please, could you develop into an algorithm?
      – Armel
      Jan 28 '14 at 19:46










    • What for? Please be specific.
      – Did
      Jan 28 '14 at 21:50










    • if we assume a=6/7 and b=1/2, a possible path γ=(0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0). Nonetheless,the algorithm that you propose will not converge in this case. Indeed, the computation of c gives c=6, and hence c^M(γ) will be greater than 1.
      – Armel
      Jan 29 '14 at 1:22











    • Please read on my lips: proportional to.
      – Did
      Jan 29 '14 at 6:01










    • What I'm saying is in the case c is greater than 1, then c^M(γ) will also be greater than 1 and therefore, it wouldn't be possible to find an integer const such that P(X=γ∣X visits 1 exactly k times)= const*c^M(γ)
      – Armel
      Jan 29 '14 at 15:58












    up vote
    3
    down vote










    up vote
    3
    down vote









    First a reformulation of the question, then a pseudo-algorithm to solve it.



    Reformulation: Let $ngeqslant1$ and assume that $X=(X_t)_1leqslant tleqslant n$ is a Markov chain of length $n$ on the state space $0,1$ with transition probability matrix $beginpmatrix1-a & a\ b& 1-bendpmatrix$ for some $a$ and $b$ in $(0,1)$.



    Fix $kleqslant n$ and consider the set $Gamma_k^n$ of paths $gamma=(x_t)_1leqslant tleqslant n$ of length $n$ visiting $k$ times the state $1$. For every $x$ and $y$ in $0,1$ and every path $gamma$ in $Gamma_k^n$, let $N_gamma(xy)$ denote the number of times $tleqslant n$ such that $(x_t,x_t+1)=(x,y)$. Finally, let $M(gamma)=N_gamma(01)$.



    Then, forgetting a possible discrepancy of $pm1$ here and there, one sees that $N_gamma(10)=M(gamma)$, $N_gamma(00)=n-k-M(gamma)$ and $N_gamma(11)=k-M(gamma)$, hence, for every $gamma$ in $Gamma_k^n$,
    $$
    P(X=gamma)=
    (1-a)^N_gamma(00)a^N_gamma(01)b^N_gamma(10)(1-b)^N_gamma(11),
    $$
    is also, since $n$ and $k$ are fixed,
    $$
    P(X=gamma)= (1-a)^n-k-M(gamma)a^M(gamma)b^M(gamma)(1-b)^k-M(gamma)propto c^M(gamma),
    $$
    where
    $$
    c=fracab(1-a)(1-b).
    $$
    Thus, there exists some $A_k^n$ independent of $gamma$ such that, for every $gamma$ in $Gamma_k^n$,
    $$
    P(X=gammamid X textvisits 1 textexactly k texttimes)=A_k^ncdot c^M(gamma),
    $$
    and the question is to simulate a random path in $Gamma_k^n$ following this distribution.



    Pseudo-algorithm: This assumes one is able to generate uniformly subsets $T$ of $1,2,ldots,n$ of size $k$.



    • Choose a path $gamma$ uniformly in $Gamma_k^n$, that is, choose uniformly a subset $Tsubseteq1,2,ldots,n$ of size $k$ and define $gamma=(x_t)$ by $x_t=1$ if $t$ is in $T$ and $x_t=0$ otherwise.

    • Compute $M(gamma)$.

    • Accept $gamma$ with probability proportional to $c^M(gamma)$.

    • Repeat until a path $gamma$ is accepted. Return $gamma$.

    To generate $T$, the following procedure might prove useful:



    • Let $T=varnothing$.

    • Choose $x$ uniformly in $1,2,ldots,n$. If $x$ is not already in $T$, add $x$ to $T$.

    • Repeat until $T$ has size $k$. Return $T$.





    share|cite|improve this answer












    First a reformulation of the question, then a pseudo-algorithm to solve it.



    Reformulation: Let $ngeqslant1$ and assume that $X=(X_t)_1leqslant tleqslant n$ is a Markov chain of length $n$ on the state space $0,1$ with transition probability matrix $beginpmatrix1-a & a\ b& 1-bendpmatrix$ for some $a$ and $b$ in $(0,1)$.



    Fix $kleqslant n$ and consider the set $Gamma_k^n$ of paths $gamma=(x_t)_1leqslant tleqslant n$ of length $n$ visiting $k$ times the state $1$. For every $x$ and $y$ in $0,1$ and every path $gamma$ in $Gamma_k^n$, let $N_gamma(xy)$ denote the number of times $tleqslant n$ such that $(x_t,x_t+1)=(x,y)$. Finally, let $M(gamma)=N_gamma(01)$.



    Then, forgetting a possible discrepancy of $pm1$ here and there, one sees that $N_gamma(10)=M(gamma)$, $N_gamma(00)=n-k-M(gamma)$ and $N_gamma(11)=k-M(gamma)$, hence, for every $gamma$ in $Gamma_k^n$,
    $$
    P(X=gamma)=
    (1-a)^N_gamma(00)a^N_gamma(01)b^N_gamma(10)(1-b)^N_gamma(11),
    $$
    is also, since $n$ and $k$ are fixed,
    $$
    P(X=gamma)= (1-a)^n-k-M(gamma)a^M(gamma)b^M(gamma)(1-b)^k-M(gamma)propto c^M(gamma),
    $$
    where
    $$
    c=fracab(1-a)(1-b).
    $$
    Thus, there exists some $A_k^n$ independent of $gamma$ such that, for every $gamma$ in $Gamma_k^n$,
    $$
    P(X=gammamid X textvisits 1 textexactly k texttimes)=A_k^ncdot c^M(gamma),
    $$
    and the question is to simulate a random path in $Gamma_k^n$ following this distribution.



    Pseudo-algorithm: This assumes one is able to generate uniformly subsets $T$ of $1,2,ldots,n$ of size $k$.



    • Choose a path $gamma$ uniformly in $Gamma_k^n$, that is, choose uniformly a subset $Tsubseteq1,2,ldots,n$ of size $k$ and define $gamma=(x_t)$ by $x_t=1$ if $t$ is in $T$ and $x_t=0$ otherwise.

    • Compute $M(gamma)$.

    • Accept $gamma$ with probability proportional to $c^M(gamma)$.

    • Repeat until a path $gamma$ is accepted. Return $gamma$.

    To generate $T$, the following procedure might prove useful:



    • Let $T=varnothing$.

    • Choose $x$ uniformly in $1,2,ldots,n$. If $x$ is not already in $T$, add $x$ to $T$.

    • Repeat until $T$ has size $k$. Return $T$.






    share|cite|improve this answer












    share|cite|improve this answer



    share|cite|improve this answer










    answered Jan 28 '14 at 16:29









    Did

    243k23208443




    243k23208443











    • Thanks for your reply. I'm not sure I properly understand the pseudo-algorithm. Please, could you develop into an algorithm?
      – Armel
      Jan 28 '14 at 19:46










    • What for? Please be specific.
      – Did
      Jan 28 '14 at 21:50










    • if we assume a=6/7 and b=1/2, a possible path γ=(0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0). Nonetheless,the algorithm that you propose will not converge in this case. Indeed, the computation of c gives c=6, and hence c^M(γ) will be greater than 1.
      – Armel
      Jan 29 '14 at 1:22











    • Please read on my lips: proportional to.
      – Did
      Jan 29 '14 at 6:01










    • What I'm saying is in the case c is greater than 1, then c^M(γ) will also be greater than 1 and therefore, it wouldn't be possible to find an integer const such that P(X=γ∣X visits 1 exactly k times)= const*c^M(γ)
      – Armel
      Jan 29 '14 at 15:58
















    • Thanks for your reply. I'm not sure I properly understand the pseudo-algorithm. Please, could you develop into an algorithm?
      – Armel
      Jan 28 '14 at 19:46










    • What for? Please be specific.
      – Did
      Jan 28 '14 at 21:50










    • if we assume a=6/7 and b=1/2, a possible path γ=(0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0). Nonetheless,the algorithm that you propose will not converge in this case. Indeed, the computation of c gives c=6, and hence c^M(γ) will be greater than 1.
      – Armel
      Jan 29 '14 at 1:22











    • Please read on my lips: proportional to.
      – Did
      Jan 29 '14 at 6:01










    • What I'm saying is in the case c is greater than 1, then c^M(γ) will also be greater than 1 and therefore, it wouldn't be possible to find an integer const such that P(X=γ∣X visits 1 exactly k times)= const*c^M(γ)
      – Armel
      Jan 29 '14 at 15:58















    Thanks for your reply. I'm not sure I properly understand the pseudo-algorithm. Please, could you develop into an algorithm?
    – Armel
    Jan 28 '14 at 19:46




    Thanks for your reply. I'm not sure I properly understand the pseudo-algorithm. Please, could you develop into an algorithm?
    – Armel
    Jan 28 '14 at 19:46












    What for? Please be specific.
    – Did
    Jan 28 '14 at 21:50




    What for? Please be specific.
    – Did
    Jan 28 '14 at 21:50












    if we assume a=6/7 and b=1/2, a possible path γ=(0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0). Nonetheless,the algorithm that you propose will not converge in this case. Indeed, the computation of c gives c=6, and hence c^M(γ) will be greater than 1.
    – Armel
    Jan 29 '14 at 1:22





    if we assume a=6/7 and b=1/2, a possible path γ=(0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0). Nonetheless,the algorithm that you propose will not converge in this case. Indeed, the computation of c gives c=6, and hence c^M(γ) will be greater than 1.
    – Armel
    Jan 29 '14 at 1:22













    Please read on my lips: proportional to.
    – Did
    Jan 29 '14 at 6:01




    Please read on my lips: proportional to.
    – Did
    Jan 29 '14 at 6:01












    What I'm saying is in the case c is greater than 1, then c^M(γ) will also be greater than 1 and therefore, it wouldn't be possible to find an integer const such that P(X=γ∣X visits 1 exactly k times)= const*c^M(γ)
    – Armel
    Jan 29 '14 at 15:58




    What I'm saying is in the case c is greater than 1, then c^M(γ) will also be greater than 1 and therefore, it wouldn't be possible to find an integer const such that P(X=γ∣X visits 1 exactly k times)= const*c^M(γ)
    – Armel
    Jan 29 '14 at 15:58










    up vote
    -1
    down vote













    Suppose you have transition probabilities $p_1 leq p_2 dots leq p_k$, $sum p_k = 1$. Define the distribution function in pseudo code:



    function cdf(state) { a = math.random() % randomly generated number in U(0,1)
    if 0 <= a < p_1, return 1
    if p_1 <= a < p_1 + p_2, return 2
    ...
    if p_1 + ... + p_k-1 <= a < 1, return k


    In the above, I have assumed that the transition probabilities are all the same, i.e. independent of the state. Otherwise, the $p_i$ will have to depend on state.



    The above code is just saying that if I have numbers $p_1$, ..., $p_n$ such that $sum p_i = 1$, then I can partition the unit interval $[0,1]$ with these numbers. The area of the rectangle [$p_1 + ... + p_k-1, p_1 + ... + p_k$] with unit height is $p_k$, and that's the probability we want for the $k^th$ transition. This is the standard way of generating probability distributions, since if $X$ has cdf $F$, then $F(X) sim U(0,1)$.



    Now pick an initial state (say $s = 1$) and a state you want to measure, say $r = 4$.



    count = 0
    for i = 1 : n
    s = cdf(s) // transition with the appropriate probabilities
    if s == 4, count++
    end
    end


    I should mention this algorithm is the worst.






    share|cite|improve this answer
























      up vote
      -1
      down vote













      Suppose you have transition probabilities $p_1 leq p_2 dots leq p_k$, $sum p_k = 1$. Define the distribution function in pseudo code:



      function cdf(state) { a = math.random() % randomly generated number in U(0,1)
      if 0 <= a < p_1, return 1
      if p_1 <= a < p_1 + p_2, return 2
      ...
      if p_1 + ... + p_k-1 <= a < 1, return k


      In the above, I have assumed that the transition probabilities are all the same, i.e. independent of the state. Otherwise, the $p_i$ will have to depend on state.



      The above code is just saying that if I have numbers $p_1$, ..., $p_n$ such that $sum p_i = 1$, then I can partition the unit interval $[0,1]$ with these numbers. The area of the rectangle [$p_1 + ... + p_k-1, p_1 + ... + p_k$] with unit height is $p_k$, and that's the probability we want for the $k^th$ transition. This is the standard way of generating probability distributions, since if $X$ has cdf $F$, then $F(X) sim U(0,1)$.



      Now pick an initial state (say $s = 1$) and a state you want to measure, say $r = 4$.



      count = 0
      for i = 1 : n
      s = cdf(s) // transition with the appropriate probabilities
      if s == 4, count++
      end
      end


      I should mention this algorithm is the worst.






      share|cite|improve this answer






















        up vote
        -1
        down vote










        up vote
        -1
        down vote









        Suppose you have transition probabilities $p_1 leq p_2 dots leq p_k$, $sum p_k = 1$. Define the distribution function in pseudo code:



        function cdf(state) { a = math.random() % randomly generated number in U(0,1)
        if 0 <= a < p_1, return 1
        if p_1 <= a < p_1 + p_2, return 2
        ...
        if p_1 + ... + p_k-1 <= a < 1, return k


        In the above, I have assumed that the transition probabilities are all the same, i.e. independent of the state. Otherwise, the $p_i$ will have to depend on state.



        The above code is just saying that if I have numbers $p_1$, ..., $p_n$ such that $sum p_i = 1$, then I can partition the unit interval $[0,1]$ with these numbers. The area of the rectangle [$p_1 + ... + p_k-1, p_1 + ... + p_k$] with unit height is $p_k$, and that's the probability we want for the $k^th$ transition. This is the standard way of generating probability distributions, since if $X$ has cdf $F$, then $F(X) sim U(0,1)$.



        Now pick an initial state (say $s = 1$) and a state you want to measure, say $r = 4$.



        count = 0
        for i = 1 : n
        s = cdf(s) // transition with the appropriate probabilities
        if s == 4, count++
        end
        end


        I should mention this algorithm is the worst.






        share|cite|improve this answer












        Suppose you have transition probabilities $p_1 leq p_2 dots leq p_k$, $sum p_k = 1$. Define the distribution function in pseudo code:



        function cdf(state) { a = math.random() % randomly generated number in U(0,1)
        if 0 <= a < p_1, return 1
        if p_1 <= a < p_1 + p_2, return 2
        ...
        if p_1 + ... + p_k-1 <= a < 1, return k


        In the above, I have assumed that the transition probabilities are all the same, i.e. independent of the state. Otherwise, the $p_i$ will have to depend on state.



        The above code is just saying that if I have numbers $p_1$, ..., $p_n$ such that $sum p_i = 1$, then I can partition the unit interval $[0,1]$ with these numbers. The area of the rectangle [$p_1 + ... + p_k-1, p_1 + ... + p_k$] with unit height is $p_k$, and that's the probability we want for the $k^th$ transition. This is the standard way of generating probability distributions, since if $X$ has cdf $F$, then $F(X) sim U(0,1)$.



        Now pick an initial state (say $s = 1$) and a state you want to measure, say $r = 4$.



        count = 0
        for i = 1 : n
        s = cdf(s) // transition with the appropriate probabilities
        if s == 4, count++
        end
        end


        I should mention this algorithm is the worst.







        share|cite|improve this answer












        share|cite|improve this answer



        share|cite|improve this answer










        answered Jan 28 '14 at 15:26









        snarski

        4,1691119




        4,1691119



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f654250%2fmarkov-chain-simulation%23new-answer', 'question_page');

            );

            Post as a guest













































































            這個網誌中的熱門文章

            Is there any way to eliminate the singular point to solve this integral by hand or by approximations?

            Why am i infinitely getting the same tweet with the Twitter Search API?

            Carbon dioxide