Revision 1037, 0.9 kB
(checked in by suzdalev, 14 years ago)
|
MultiNom? pdf in matlab
|
-
Property svn:eol-style set to
native
|
Line | |
---|
1 | classdef mexMultiNom < mexEpdf |
---|
2 | properties |
---|
3 | % from wikipedia |
---|
4 | p % vector of probabilities |
---|
5 | end |
---|
6 | methods |
---|
7 | function m=mean(obj) |
---|
8 | m = obj.p; |
---|
9 | end |
---|
10 | |
---|
11 | function obj=validate(obj) |
---|
12 | if length(obj.p)<1 |
---|
13 | error('empty p'); |
---|
14 | end |
---|
15 | sump = sum(obj.p); |
---|
16 | if sump~=1 |
---|
17 | error('non-normalized p') |
---|
18 | obj.p=obj.p/sump; |
---|
19 | end |
---|
20 | end |
---|
21 | |
---|
22 | function dim=dimension(obj) |
---|
23 | dim = 1; |
---|
24 | end |
---|
25 | |
---|
26 | function v=variance(obj) |
---|
27 | v=obj.p.*(1-obj.p); |
---|
28 | end |
---|
29 | |
---|
30 | function l=evallog(obj,x) |
---|
31 | l=log(obj.p(x)); |
---|
32 | end |
---|
33 | |
---|
34 | function s=sample(obj) |
---|
35 | csum=cumsum(obj.p);%sample_exponential(0,1); |
---|
36 | u = rand; |
---|
37 | s=1; |
---|
38 | while (csum(s)<u) |
---|
39 | s=s+1; |
---|
40 | end |
---|
41 | end |
---|
42 | end |
---|
43 | end |
---|