-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDblMon.m
183 lines (163 loc) · 7.48 KB
/
DblMon.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
classdef DblMon < dContinuous
% Double Monomial distribution with parameters tzero>0, delta>0, epsilon>1
% Reference: Luce (1986), page 510.
properties(SetAccess = protected) % These properties can only be set by the methods of this class and its descendants.
tzero, delta, epsilon, % The distribution's parameters should be listed here.
Frac1, deltaPlusOne, EpsMinusOne, CDFAttzero, SecondTermAttzero
end
methods (Static)
function Reals = ParmsToReals(Parms,~)
Reals = [NumTrans.GT2Real(0,Parms(1)) NumTrans.GT2Real(0,Parms(2)) NumTrans.GT2Real(1,Parms(3))] ;
end
function Parms = RealsToParms(Reals,~)
Parms = [NumTrans.Real2GT(0,Reals(1)) NumTrans.Real2GT(0,Reals(2)) NumTrans.Real2GT(1,Reals(3))];
end
end
methods
function obj=DblMon(varargin) % Constructor
obj=obj@dContinuous('DblMon');
obj.ParmTypes = 'rrr';
obj.DefaultParmCodes = 'rrr';
obj.NDistParms = 3;
obj.XNearlyZero = eps;
obj.CDFNearlyZero = 1e-7; % Heavy trimming due to long tails. e-4 serious numerical errors
obj.CDFNearlyOne = 1 - obj.CDFNearlyZero;
obj.StartParmsMLEfn = @obj.StartParmsMLE;
switch nargin
case 0
case 3
ResetParms(obj,[varargin{:}]);
otherwise
ME = MException('DblMon:Constructor', ...
'DblMon constructor needs 0 or 3 arguments.');
throw(ME);
end
end
function []=ResetParms(obj,newparmvalues)
ClearBeforeResetParmsC(obj);
obj.tzero = newparmvalues(1);
obj.delta = newparmvalues(2);
obj.epsilon = newparmvalues(3);
ReInit(obj);
end
function PerturbParms(obj,ParmCodes)
% Perturb parameter values prior to estimation attempts.
newtzero = ifelse(ParmCodes(1)=='f', obj.tzero, 1.01*obj.tzero);
newdelta = ifelse(ParmCodes(2)=='f', obj.delta, 1.01*obj.delta);
newepsilon = ifelse(ParmCodes(3)=='f', obj.epsilon,1.01*obj.epsilon);
obj.ResetParms([newtzero newdelta newepsilon]);
end
function []=ReInit(obj)
assert(obj.tzero>0,'DblMon tzero must be > 0.');
assert(obj.delta>0,'DblMon delta must be > 0.');
assert(obj.epsilon>1,'DblMon epsilon must be > 1.');
obj.deltaPlusOne = obj.delta + 1;
obj.EpsMinusOne = obj.epsilon - 1;
obj.Frac1 = obj.deltaPlusOne*(obj.EpsMinusOne) / ( (obj.delta+obj.epsilon)*obj.tzero );
obj.LowerBound = obj.XNearlyZero;
obj.UpperBound = 2*obj.tzero;
obj.Initialized = true;
obj.CDFAttzero = CDF(obj,obj.tzero);
obj.SecondTermAttzero = SecondTerm(obj,obj.tzero);
StillLooking = true;
while StillLooking
obj.UpperBound = obj.UpperBound * 2;
obj.LowerBound = InverseCDF(obj,obj.CDFNearlyZero);
StillLooking = CDF(obj,obj.UpperBound-1) < obj.CDFNearlyOne;
end
obj.UpperBound = InverseCDF(obj,obj.CDFNearlyOne);
if (obj.NameBuilding)
BuildMyName(obj);
end
end
function thispdf=PDF(obj,X)
[thispdf, InBounds, Done] = MaybeSplinePDF(obj,X);
if Done
return;
end
xFrac = X / obj.tzero;
XX = zeros(size(X));
BigX = (X>obj.tzero) & (X>=obj.LowerBound) & (X<=obj.UpperBound);
SmallX = (X<=obj.tzero) & (X>=obj.LowerBound) & (X<=obj.UpperBound);
XX(SmallX) = xFrac(SmallX).^obj.delta;
XX(BigX) = xFrac(BigX).^(-obj.epsilon);
% xxsize = size(XX)
% pdfsize = size(thispdf(InBounds))
thispdf(InBounds) = obj.Frac1 * XX(InBounds);
% for i=1:numel(X)
% if (X(i) >= obj.LowerBound) && (X(i) <= obj.UpperBound)
% xFrac = X(i) / obj.tzero;
% if X(i) <= obj.tzero
% XX = xFrac^obj.delta;
% else
% XX = xFrac^(-obj.epsilon);
% end
% thispdf(i) = obj.Frac1 * XX;
% end
% end
end
function thisval=SecondTerm(obj,X)
xFrac = X / obj.tzero;
XX = xFrac.^(-obj.epsilon)/(-obj.epsilon+1);
thisval = obj.Frac1 .* XX .* X;
end
function thiscdf=CDF(obj,X)
[thiscdf, InBounds, Done] = MaybeSplineCDF(obj,X);
if Done
return;
end
for i=1:numel(X)
if X(InBounds)
if X(i) <= obj.tzero
xFrac = X(i) / obj.tzero;
XX = xFrac^obj.delta/(obj.delta+1);
thiscdf(i) = obj.Frac1 * XX * X(i);
else
thiscdf(i) = obj.CDFAttzero + SecondTerm(obj,X(i)) - obj.SecondTermAttzero;
end
end
end
end
function thisval=Mean(obj)
if ~obj.Initialized
error(UninitializedError(obj));
end
if obj.epsilon <= 2
% thisval = NaN; % Theoretically correct but not for truncated version implemented here.
thisval = Mean@dContinuous(obj);
else
thisval = obj.tzero * (obj.deltaPlusOne) * (obj.EpsMinusOne) / ( (obj.delta + 2) * (obj.epsilon - 2) );
end
end
function thisval=Variance(obj)
if ~obj.Initialized
error(UninitializedError(obj));
end
if obj.epsilon <= 3
% thisval = NaN; % Theoretically correct but not for truncated version implemented here.
thisval = Variance@dContinuous(obj);
else
thisval = obj.tzero^2 * (obj.deltaPlusOne) * (obj.EpsMinusOne) / ( (obj.delta + 3) * (obj.epsilon - 3) ) - Mean(obj)^2;
end
end
% function thisval=Hazard(obj,x) % Luce gives this but it does not agree with the PDF/CDF values so I do not think it is correct.
% assert(obj.Initialized,UninitializedError(obj));
% thisval=zeros(size(x));
% for i=1:numel(x)
% if x(i) > obj.tzero
% thisval(i) = (obj.EpsMinusOne) / x(i);
% else
% xOvertzero = x(i) / obj.tzero;
% Denom = 1/obj.EpsMinusOne + 1/obj.deltaPlusOne - xOvertzero^obj.deltaPlusOne/obj.deltaPlusOne;
% thisval(i) = xOvertzero^obj.epsilon * 1/obj.tzero / Denom;
% end
% end
% end
function parms = StartParmsMLE(obj,X)
starttzero = median(X);
startdelta = 10;
startepsilon = 5;
parms = double([starttzero startdelta startepsilon]);
end
end % methods
end % class DblMon