-
Notifications
You must be signed in to change notification settings - Fork 6
/
Gamma.m
213 lines (187 loc) · 7.49 KB
/
Gamma.m
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
classdef Gamma < dContinuous
% Gamma(integer N>=1, Rate>0) parameters are also known as Shape,Rate
% WARNING:
% This version only works for integer N. Use rnGamma for real N.
properties(SetAccess = private) % These properties can only be set by the methods of this class.
end
properties(SetAccess = protected) % These properties can only be set by the methods of this class and its descendants.
N, Rate, LnGammaOfN
end
methods (Static)
function Reals = ParmsToReals(Parms,~)
Reals = [NumTrans.GT2Real(1,Parms(1)) NumTrans.GT2Real(eps,Parms(2))];
end
function Parms = RealsToParms(Reals,~)
Parms = [NumTrans.Real2GT(1,Reals(1)) NumTrans.Real2GT(eps,Reals(2))];
end
end
methods
function obj=Gamma(varargin)
obj=obj@dContinuous('Gamma');
obj.ParmTypes = 'ir';
obj.DefaultParmCodes = 'ir';
obj.NDistParms = 2;
switch nargin
case 0
case 2
ResetParms(obj,[varargin{:}]);
otherwise
ME = MException('Gamma:Constructor', ...
'Gamma constructor must receive 0 or 2 arguments.');
throw(ME);
end
end
function []=ResetParms(obj,newparmvalues)
ClearBeforeResetParmsC(obj);
obj.N = VerifyIntegerGE(obj,1,newparmvalues(1));
obj.Rate = newparmvalues(2);
ReInit(obj);
end
function PerturbParms(obj,ParmCodes)
% Perturb parameter values prior to estimation attempts.
newN = ifelse(ParmCodes(1)=='f', obj.N, obj.N+1);
newRate = ifelse(ParmCodes(2)=='f', obj.Rate,obj.N/(obj.N+1)*obj.Rate);
obj.ResetParms([newN newRate]);
end
function []=ReInit(obj)
% assert(obj.N>=1,'Gamma N must be >= 1.');
assert(obj.Rate>0,'Gamma Rate must be > 0.');
obj.LnGammaOfN = gammaln(obj.N);
obj.LowerBound = 0;
obj.UpperBound = 20 / obj.Rate * obj.N; % 20 is upper bound for one standard exponential.
obj.Initialized = true;
if (obj.NameBuilding)
BuildMyName(obj);
end
end
function thispdf=PDF(obj,X)
[thispdf, InBounds, Done] = MaybeSplinePDF(obj,X);
if Done
return;
end
% thispdf = gampdf(x,obj.N,1/obj.Rate); % with stat toolbox
LnPDF = log(obj.Rate) * obj.N + log(X(InBounds)) * (obj.N - 1) - obj.Rate * X(InBounds) - obj.LnGammaOfN;
thispdf(InBounds) = exp(LnPDF);
% for iel=1:numel(X)
% if (X(iel) >= obj.LowerBound) && (X(iel) <= obj.UpperBound)
% LnPDF = log(obj.Rate) * obj.N + log(X(iel)) * (obj.N - 1) - obj.Rate * X(iel) - obj.LnGammaOfN;
% thispdf(iel) = exp(LnPDF);
% end
% end
end
function thiscdf=CDF(obj,X)
[thiscdf, InBounds, Done] = MaybeSplineCDF(obj,X);
if Done
return;
end
thiscdf(InBounds) = gamcdf(X(InBounds),obj.N,1/ obj.Rate); % with stat toolbox
% if x >= obj.UpperBound
% thiscdf = 1;
% elseif x <= 0
% thiscdf = 0;
% else
% c = 1;
% for r = 1:obj.N-1
% f = 1.0;
% for j = 1:r
% f = f*x*obj.Rate/j;
% c = c + f;
% end
% c = exp(-obj.Rate*x) * c;
% end
% thiscdf = 1 - c;
% end
end
function thisval=MGF(obj,Theta)
if ~obj.Initialized
error(UninitializedError(obj));
end
if Theta < obj.Rate
thisval = (1 - Theta/obj.Rate)^(-obj.N);
else
thisval = nan;
end
% Power = (1 - Theta * obj.Rate)^obj.N;
% thisval = 1 / Power;
end
function thisval=RawMoment(obj,I)
if ~obj.Initialized
error(UninitializedError(obj));
end
Sum1 = 0;
Sum2 = 0;
for J = 2:obj.N + I - 1
Add = log(J);
Sum1 = Sum1 + Add;
if J <= obj.N-1
Sum2 = Sum2 + Add;
end
end
thisval = exp(Sum1 - Sum2 - I * log(obj.Rate) );
end
% Maybe this ConditionalRawMoment can be added?
% function thisval=ConditionalRawMoment(obj,FromX, ToX,I)
% assert(Initialized,UninitializedError(obj));
% RateToNegI = log(obj.Rate);
% RateToNegI = exp(-RateToNegI*I);
% LowerFac = 1;
% for J = 2:obj.N - 1
% LowerFac = LowerFac * J;
% end
% UpperFac = 1;
% for J = 2:obj.N + I - 1
% UpperFac = UpperFac * J;
% end
% ProbRatioDenom = CDF(FromX);
% ProbRatioDenom = CDF(ToX) - ProbRatioDenom;
% GammaNplusI = thisval=Create;
% if UseExpRate
% NewRate = obj.Rate
% else
% NewRate = 1 / obj.Rate;
% end
% % NOTE: NEED TO USE A DERIVED GammaRV here:
% assert(Initialized,UninitializedError(obj));
% GammaNplusI.ResetParms([obj.N+I NewRate]);
% ProbRatioNum = GammaNplusI.CDF(FromX);
% ProbRatioNum = GammaNplusI.CDF(ToX) - ProbRatioNum;
% GammaNplusI.Destroy;
% ProbRatio = ProbRatioNum / ProbRatioDenom;
% thisval = UpperFac / LowerFac * RateToNegI * ProbRatio;
% end
function thisval=Random(obj,varargin)
if ~obj.Initialized
error(UninitializedError(obj));
end
Sum = random('exp',1,varargin{:});
for I = 2:obj.N
Sum = Sum + random('exp',1,varargin{:});
end
thisval = Sum / obj.Rate;
end
function thisval=Mean(obj)
if ~obj.Initialized
error(UninitializedError(obj));
end
thisval = obj.N / obj.Rate;
end
function thisval=Variance(obj)
if ~obj.Initialized
error(UninitializedError(obj));
end
thisval = obj.N / obj.Rate^2;
end
function thisval=RelSkewness(obj)
if ~obj.Initialized
error(UninitializedError(obj));
end
thisval = 2 / sqrt(obj.N);
end
function thisval=Kurtosis(obj)
if ~obj.Initialized
error(UninitializedError(obj));
end
thisval = 3 + 6.0 / obj.N;
end
end % methods
end % class Gamma