Guest User

Untitled

a guest
Jan 21st, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. %% Fragment 1 %%
  2.  
  3. p = zeros(1,nMax);
  4. for n = 1:nMax
  5. p(n)=exp(-mu*T+n*log(mu*T)-sum(log(1:n)))*xf(n,b)/P_ab;
  6. T = T + p(n) * xb(:,n-1) * R;
  7. for k in 2:n
  8. T = T + p(n) * ( xb(:,n-k) * xf(k-1,:) ) .* R ;
  9. end
  10. end
  11.  
  12. % However, the number of stepping stones is too large to do the above, so
  13. % we focus on the analysis of fold transitions
  14.  
  15. %% Fragment 2 %%
  16.  
  17. Qfolds = matrix of rates summed over all stepping stones in each fold
  18.  
  19. % e.g. if we have 9 stepping stones of three lengths
  20. % divided into three folds in the form
  21.  
  22. fold = [ 1,2,3, 1,2,3, 1,2,3 ];
  23. % length 1 length 2 length 3
  24.  
  25. folds = cell(nFolds,1);
  26. for n=1:nFolds
  27. folds(n) = fold==n;
  28. end
  29.  
  30. % Then Qfolds would be
  31.  
  32. % Qfolds = [ sum(Q([1,3,7],[1,3,7])) sum(Q([1,3,7],[2,4,8])) sum(Q([1,3,7],[3,6,9])) ;
  33. % sum(Q([2,4,8],[1,3,7])) sum(Q([2,4,8],[2,4,8])) sum(Q([2,4,8],[3,6,9])) ;
  34. % sum(Q([3,6,9],[1,3,7])) sum(Q([3,6,9],[2,4,8])) sum(Q([3,6,9],[3,6,9])) ]
  35.  
  36. % But that won't give a proper transition matrix -- need to do the marginalisation
  37. % on Q I think, and then form Rfolds from this. Does this give the right probs though?'
  38.  
  39. % Where Rfolds(i,j) indicates the rate for transitioning between fold i and fold j
  40. % The above would be slow in Matlab, but could be computed at the same time as the
  41. % original R matrix.
  42.  
  43. % Similarly we need a fold level version of xf and xb
  44.  
  45. for k=1:nFolds
  46. xffolds(1,k) = sum(xf(1,folds(k)));
  47. end
  48. for k=1:nFolds
  49. xbfolds(1,k) = sum(xb(1,folds(k)));
  50. end
  51.  
  52. for i = 2:nMax
  53. xffolds(i,:) = xffolds(i-1,:) * Rfolds;
  54. xbfolds(:,i) = Rfolds * xbfolds(:,i-1);
  55. % Then to compute the expected number of such transitions, we use Corollary 3 of H&J
  56.  
  57. p = zeros(1,nMax);
  58. Tfolds = zeros(nFolds);
  59. for n = 1:nMax
  60. p(n)=exp(-mu*T+n*log(mu*T)-sum(log(1:n)))*xffolds(n,b)/P_ab;
  61. Tfolds = Tfolds + p(n) * xbfolds(:,n-1) * R;
  62. for k in 2:n
  63. Tfolds = Tfolds + p(n) * ( xbfolds(:,n-k) * xffolds(k-1,:) ) .* Rfolds ;
  64. end
  65. end
Add Comment
Please, Sign In to add comment