File: ctmcfpt.m

package info (click to toggle)
octave-queueing 1.2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,288 kB
  • sloc: makefile: 56
file content (117 lines) | stat: -rw-r--r-- 3,194 bytes parent folder | download
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
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
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
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
114
115
116
117
## Copyright (C) 2011, 2012, 2016, 2018 Moreno Marzolla
##
## This file is part of the queueing toolbox.
##
## The queueing toolbox is free software: you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation, either version 3 of the
## License, or (at your option) any later version.
##
## The queueing toolbox is distributed in the hope that it will be
## useful, but WITHOUT ANY WARRANTY; without even the implied warranty
## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with the queueing toolbox. If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
##
## @deftypefn {Function File} {@var{M} =} ctmcfpt (@var{Q})
## @deftypefnx {Function File} {@var{m} =} ctmcfpt (@var{Q}, @var{i}, @var{j})
##
## @cindex first passage times, CTMC
## @cindex CTMC
## @cindex continuous time Markov chain
## @cindex Markov chain, continuous time
##
## Compute mean first passage times for an irreducible continuous-time
## Markov chain.
##
## @strong{INPUTS}
##
## @table @code
##
## @item @var{Q}(i,j)
## Infinitesimal generator matrix. @var{Q} is a @math{N \times N}
## square matrix where @code{@var{Q}(i,j)} is the transition rate from
## state @math{i} to state @math{j}, for @math{1 @leq{} i, j @leq{} N},
## @math{i \neq j}. Transition rates must be nonnegative, and
## @math{\sum_{j=1}^N Q_{i,j} = 0}
##
## @item @var{i}
## Initial state.
##
## @item @var{j}
## Destination state.
##
## @end table
##
## @strong{OUTPUTS}
##
## @table @code
##
## @item @var{M}(i,j)
## average time before state
## @var{j} is visited for the first time, starting from state @var{i}.
## We let @code{@var{M}(i,i) = 0}.
##
## @item m
## @var{m} is the average time before state @var{j} is visited for the first
## time, starting from state @var{i}.
##
## @end table
##
## @seealso{ctmcmtta}
##
## @end deftypefn

## Author: Moreno Marzolla <moreno.marzolla(at)unibo.it>
## Web: http://www.moreno.marzolla.name/

function result = ctmcfpt( Q, i, j )

  persistent epsilon = 10*eps;

  if ( nargin != 1 && nargin != 3 )
    print_usage();
  endif

  [N err] = ctmcchkQ(Q);

  (N>0) || ...
      error(err);

  if ( nargin == 1 )
    result = zeros(N,N);
    for j=1:N
      QQ = Q;
      QQ(j,:) = 0; # make state j absorbing
      for i=1:N
	p0 = zeros(1,N); p0(i) = 1;
	result(i,j) = ctmcmtta(QQ,p0);
      endfor
    endfor
  else
    (isscalar(i) && i>=1 && j<=N) || error("i must be an integer in the range 1..%d", N);
    (isvector(j) && all(j>=1) && all(j<=N)) || error("j must be an integer or vector with elements in 1..%d", N);
    j = j(:)'; # make j a row vector
    Q(j,:) = 0; # make state(s) j absorbing
    p0 = zeros(1,N); p0(i) = 1;
    result = ctmcmtta(Q,p0);
  endif
endfunction
%!demo
%! Q = [ -1.0  0.9  0.1; ...
%!        0.1 -1.0  0.9; ...
%!        0.9  0.1 -1.0 ];
%! M = ctmcfpt(Q)
%! m = ctmcfpt(Q,1,3)

%!test
%! N = 10;
%! Q = reshape(1:N^2,N,N);
%! Q(1:N+1:end) = 0;
%! Q -= diag(sum(Q,2));
%! M = ctmcfpt(Q);
%! assert( all(diag(M) < 10*eps) );