forked from milleratotago/Cupid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtrVal2L.m
117 lines (98 loc) · 3.87 KB
/
ExtrVal2L.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
classdef ExtrVal2L < dContinuous
% Extreme Value Type II distribution given by Luce (1986, p 508) with parameters shape (his alpha) and scale>0 (his beta).
% As shape gets larger, distribution becomes more normal.
% Unlike Luce, here we divide by scale so that larger scale values give distributions with larger scores.
% This is the same as the Frechet2 distribution.
properties(SetAccess = protected)
shape, scale
end
methods (Static)
function Reals = ParmsToReals(Parms,~)
Reals = [NumTrans.GT2Real(eps,Parms(1)) NumTrans.GT2Real(eps,Parms(2))];
end
function Parms = RealsToParms(Reals,~)
Parms = [NumTrans.Real2GT(eps,Reals(1)) NumTrans.Real2GT(eps,Reals(2))];
end
end
methods
function obj=ExtrVal2L(varargin)
obj=obj@dContinuous('ExtrVal2L');
obj.ParmTypes = 'rr';
obj.DefaultParmCodes = 'rr';
obj.NDistParms = 2;
obj.SearchOptions.MaxFunEvals = 2000;
obj.SearchOptions.MaxIter = 2000;
obj.StartParmsMLEfn = @obj.StartParmsMLE;
switch nargin
case 0
case 2
ResetParms(obj,[varargin{:}]);
otherwise
ME = MException('ExtrVal2L:Constructor', ...
'ExtrVal2L constructor needs 0 or 2 arguments.');
throw(ME);
end
end
function []=ResetParms(obj,newparmvalues)
ClearBeforeResetParmsC(obj);
obj.shape = newparmvalues(1);
obj.scale = newparmvalues(2);
ReInit(obj);
end
function PerturbParms(obj,ParmCodes)
newshape = ifelse(ParmCodes(1)=='f', obj.shape, 1.05*obj.shape);
newscale = ifelse(ParmCodes(2)=='f', obj.scale, 0.95*obj.scale);
obj.ResetParms([newshape newscale]);
end
function []=ReInit(obj)
assert(obj.shape>0,'ExtrVal2L shape must be > 0.');
assert(obj.scale>0,'ExtrVal2L scale must be > 0.');
obj.Initialized = true;
obj.LowerBound = InverseCDF(obj,obj.CDFNearlyZero);
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
XX = X(InBounds) / obj.scale;
XXP = XX.^(-obj.shape);
thispdf(InBounds) = obj.shape*XXP.*exp(-XXP)./(XX*obj.scale);
end
function thiscdf=CDF(obj,X)
[thiscdf, InBounds, Done] = MaybeSplineCDF(obj,X);
if Done
return;
end
XX = X(InBounds) / obj.scale;
XX = XX.^(-obj.shape);
thiscdf(InBounds) = exp(-XX);
end
function thisval=InverseCDF(obj,P)
[thisval, ~, Done] = MaybeSplineInvCDF(obj,P);
if Done
return;
end
XX = -log(P);
XX = XX.^(-1/obj.shape);
thisval = XX * obj.scale;
end
function parms = StartParmsMLE(obj,X)
obsmedian = median(X);
obs90pct = prctile(X,90);
syms symshape tttt
F(tttt) = exp( -(tttt/obsmedian)^(-symshape) ); % Modified Luce
vpsoln = vpasolve(F(obs90pct)==0.90,symshape);
estshape = double(vpsoln);
estscale = obsmedian;
HoldParms = obj.ParmValues;
obj.EstPctile([obsmedian obs90pct],[0.5 0.9]);
parms = obj.ParmValues;
obj.ResetParms(HoldParms);
end
end % methods
end % class ExtrVal2L