File: ctmcbd.m

package info (click to toggle)
octave-queueing 1.2.8-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,288 kB
  • sloc: makefile: 56
file content (100 lines) | stat: -rw-r--r-- 3,629 bytes parent folder | download | duplicates (2)
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
## 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{Q} =} ctmcbd (@var{b}, @var{d})
##
## @cindex Markov chain, continuous time
## @cindex continuous time Markov chain
## @cindex CTMC
## @cindex birth-death process, CTMC
##
## Returns the infinitesimal generator matrix @math{Q} for a
## continuous birth-death process over the finite state space
## @math{@{1, @dots{}, N@}}. For each @math{i=1, @dots{}, (N-1)},
## @code{@var{b}(i)} is the transition rate from state @math{i} to
## state @math{(i+1)}, and @code{@var{d}(i)} is the transition rate from state
## @math{(i+1)} to state @math{i}.
##
## Matrix @math{\bf Q} is therefore defined as:
##
## @tex
## $$ \pmatrix{ -\lambda_1 & \lambda_1 & & & & \cr
##              \mu_1 & -(\mu_1 + \lambda_2) & \lambda_2 & & \cr
##              & \mu_2 & -(\mu_2 + \lambda_3) & \lambda_3 & & \cr
##              \cr
##              & & \ddots & \ddots & \ddots & & \cr
##              \cr
##              & & & \mu_{N-2} & -(\mu_{N-2}+\lambda_{N-1}) & \lambda_{N-1} \cr
##              & & & & \mu_{N-1} & -\mu_{N-1} }
## $$
## @end tex
## @ifnottex
##
## @example
## @group
## /                                                          \
## | -b(1)     b(1)                                           |
## |  d(1) -(d(1)+b(2))     b(2)                              |
## |           d(2)     -(d(2)+b(3))        b(3)              |
## |                                                          |
## |                ...           ...          ...            |
## |                                                          |
## |                       d(N-2)    -(d(N-2)+b(N-1))  b(N-1) |
## |                                       d(N-1)     -d(N-1) |
## \                                                          /
## @end group
## @end example
## @end ifnottex
##
## @noindent where @math{\lambda_i} and @math{\mu_i} are the birth and
## death rates, respectively.
##
## @seealso{dtmcbd}
##
## @end deftypefn

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

function Q = ctmcbd( birth, death )

  if ( nargin != 2 )
    print_usage();
  endif

  ( isvector( birth ) && isvector( death ) ) || ...
      error( "birth and death must be vectors" );
  birth = birth(:); # make birth a column vector
  death = death(:); # make death a column vector
  size_equal( birth, death ) || ...
      error( "birth and death rates must have the same length" );
  all( birth >= 0 ) || ...
      error( "birth rates must be >= 0" );
  all( death >= 0 ) || ...
      error( "death rates must be >= 0" );

  ## builds the infinitesimal generator matrix
  Q = diag( birth, 1 ) + diag( death, -1 );
  Q -= diag( sum(Q,2) );
endfunction
%!test
%! birth = [ 1 1 1 ];
%! death = [ 2 2 2 ];
%! Q = ctmcbd( birth, death );
%! assert( ctmc(Q), [ 8/15 4/15 2/15 1/15 ], 1e-5 );