File: dtmcbd.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 (105 lines) | stat: -rw-r--r-- 3,691 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
101
102
103
104
105
## Copyright (C) 2012, 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{P} =} dtmcbd (@var{b}, @var{d})
##
## @cindex Markov chain, discrete time
## @cindex DTMC
## @cindex discrete time Markov chain
## @cindex birth-death process, DTMC
##
## Returns the transition probability matrix @math{P} for a discrete
## birth-death process over state space @math{@{1, @dots{}, N@}}.
## For each @math{i=1, @dots{}, (N-1)},
## @code{@var{b}(i)} is the transition probability from state
## @math{i} to @math{(i+1)}, and @code{@var{d}(i)} is the transition
## probability from state @math{(i+1)} to @math{i}.
##
## Matrix @math{\bf P} is defined as:
##
## @tex
## $$ \pmatrix{ (1-\lambda_1) & \lambda_1 & & & & \cr
##              \mu_1 & (1 - \mu_1 - \lambda_2) & \lambda_2 & & \cr
##              & \mu_2 & (1 - \mu_2 - \lambda_3) & \lambda_3 & & \cr
##              \cr
##              & & \ddots & \ddots & \ddots & & \cr
##              \cr
##              & & & \mu_{N-2} & (1 - \mu_{N-2}-\lambda_{N-1}) & \lambda_{N-1} \cr
##              & & & & \mu_{N-1} & (1-\mu_{N-1}) }
## $$
## @end tex
## @ifnottex
## @example
## @group
## /                                                             \
## | 1-b(1)     b(1)                                             |
## |  d(1)  (1-d(1)-b(2))     b(2)                               |
## |            d(2)      (1-d(2)-b(3))     b(3)                 |
## |                                                             |
## |                 ...           ...          ...              |
## |                                                             |
## |                         d(N-2)   (1-d(N-2)-b(N-1))  b(N-1)  |
## |                                        d(N-1)      1-d(N-1) |
## \                                                             /
## @end group
## @end example
## @end ifnottex
##
## @noindent where @math{\lambda_i} and @math{\mu_i} are the birth and
## death probabilities, respectively.
##
## @seealso{ctmcbd}
##
## @end deftypefn

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

function P = dtmcbd( b, d )

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

  ( isvector( b ) && isvector( d ) ) || ...
      error( "birht and death must be vectors" );
  b = b(:); # make b a column vector
  d = d(:); # make d a column vector
  size_equal( b, d ) || ...
      error( "birth and death vectors must have the same length" );
  all( b >= 0 ) || ...
      error( "birth probabilities must be >= 0" );
  all( d >= 0 ) || ...
      error( "death probabilities must be >= 0" );
  all( ([b; 0] + [0; d]) <= 1 ) || ...
      error( "d(i)+b(i+1) must be <= 1");

  P = diag( b, 1 ) + diag( d, -1 );
  P += diag( 1-sum(P,2) );
endfunction
%!test
%! birth = [.5 .5 .3];
%! death = [.6 .2 .3];
%! fail("dtmcbd(birth,death)","must be");

%!demo
%! birth = [ .2 .3 .4 ];
%! death = [ .1 .2 .3 ];
%! P = dtmcbd( birth, death );
%! disp(P)