-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgx2inv.m
84 lines (79 loc) · 3.34 KB
/
gx2inv.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
function x=gx2inv(p,w,k,lambda,s,m,varargin)
% GX2INV Returns the inverse cdf of a generalized chi-squared distribution.
%
% Abhranil Das
% Center for Perceptual Systems, University of Texas at Austin
% Comments, questions, bugs to abhranil.das@utexas.edu
% If you use this code, please cite:
% 1. <a href="matlab:web('https://arxiv.org/abs/2012.14331')"
% >A method to integrate and classify normal distributions</a>
% 2. <a href="matlab:web('https://arxiv.org/abs/2404.05062')"
% >New methods for computing the generalized chi-square distribution</a>
%
% Usage:
% x=gx2inv(p,w,k,lambda,s,m)
% x=gx2inv(p,w,k,lambda,s,m,'upper','method','imhof','AbsTol',0,'RelTol',1e-7)
% etc.
%
% Example:
% x=gx2inv(0.9,[1 -5 2],[1 2 3],[2 3 7],0,5)
% x=gx2inv(-100,[1 -5 2],[1 2 3],[2 3 7],0,5,'upper','method','ray','n_rays',1e4)
%
% Required inputs:
% p probabilities at which to evaluate the inverse cdf.
% Negative values indicate log probability.
% w row vector of weights of the non-central chi-squares
% k row vector of degrees of freedom of the non-central chi-squares
% lambda row vector of non-centrality paramaters (sum of squares of
% means) of the non-central chi-squares
% s scale of normal term
% m offset
%
% Optional positional input:
% 'upper' for more accurate quantiles when entering an upper tail
% probability (complementary cdf)
%
% Optional name-value inputs:
% This function numerically finds roots of the gx2cdf function, so most
% options for the gx2cdf function can be used here, eg 'method', which
% will be passed on to gx2cdf
%
% Output:
% x computed quantile
%
% See also:
% <a href="matlab:open(strcat(fileparts(which('gx2cdf')),filesep,'doc',filesep,'GettingStarted.mlx'))">Getting Started guide</a>
parser=inputParser;
parser.KeepUnmatched=true;
addRequired(parser,'p',@(x) isreal(x) && all(x<=1));
addRequired(parser,'w',@(x) isreal(x));
addRequired(parser,'k',@(x) isreal(x));
addRequired(parser,'lambda',@(x) isreal(x));
addRequired(parser,'s',@(x) isreal(x) && isscalar(x));
addRequired(parser,'m',@(x) isreal(x) && isscalar(x));
addOptional(parser,'side','lower',@(x) strcmpi(x,'lower') || strcmpi(x,'upper') );
parse(parser,p,w,k,lambda,s,m,varargin{:});
side=parser.Results.side;
if ~s && length(unique(w))==1
% native ncx2 fallback
if strcmpi(side,'upper')
p=1-p;
end
if sign(unique(w))==1
x=ncx2inv(p,sum(k),sum(lambda))*unique(w)+m;
elseif sign(unique(w))==-1
x=ncx2inv(1-p,sum(k),sum(lambda))*unique(w)+m;
elseif unique(w)==0
x=0;
end
else
mu=gx2stat(w,k,lambda,s,m);
if p>0
x=arrayfun(@(p) fzero(@(x) gx2cdf(x,w,k,lambda,s,m,varargin{:})-p,mu),p);
else % log probability, inverted using sym
% options = optimset('PlotFcns',{@optimplotx,@optimplotfval});
% x=arrayfun(@(p) fzero(@(x) double(log10(gx2cdf(x,w,k,lambda,s,m,varargin{:})))-p,mu),p);
% options = optimset('FunValCheck','off')
x=arrayfun(@(p) fzero(@(x) log_gx2cdf(x,w,k,lambda,s,m,varargin{:})-p,mu),p);
end
end