Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abhranildas committed Dec 30, 2020
1 parent 1cd76fd commit d4f088b
Show file tree
Hide file tree
Showing 16 changed files with 1,083 additions and 0 deletions.
Binary file added Generalized chi-square distribution.mltbx
Binary file not shown.
17 changes: 17 additions & 0 deletions demos.xml
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<demos><!--This is an autogenerated file, please do not modify-->
<name>Generalized chi-square distribution</name>
<type>toolbox</type>
<icon>HelpIcon.DEMOS</icon>
<website/>
<description>The generalized chi-square variable is a quadratic form of a normal variable, or equivalently, a linear sum of independent non-central chi-square variables and a normal variable. Try the Getting Started guide for a quick demo of all the functions.</description>
<demosection>
<label>doc</label>
<demoitem>
<label>GettingStarted</label>
<type>other</type>
<source>GettingStarted</source>
<file>doc/html/GettingStarted.html</file>
</demoitem>
</demosection>
</demos>
109 changes: 109 additions & 0 deletions doc/GettingStarted.html

Large diffs are not rendered by default.

Binary file added doc/GettingStarted.mlx
Binary file not shown.
124 changes: 124 additions & 0 deletions doc/html/GettingStarted.html

Large diffs are not rendered by default.

152 changes: 152 additions & 0 deletions gx2.prj
@@ -0,0 +1,152 @@
<deployment-project plugin="plugin.toolbox" plugin-version="1.0">
<configuration build-checksum="2263924193" file="C:\Users\abhra\Google Drive\Geisler Lab\IntClassNorm\gx2\gx2.prj" location="C:\Users\abhra\Google Drive\Geisler Lab\IntClassNorm\gx2" name="gx2" target="target.toolbox" target-name="Package Toolbox">
<param.appname>Generalized chi-square distribution</param.appname>
<param.authnamewatermark>Abhranil Das</param.authnamewatermark>
<param.email>abhranil.das@utexas.edu</param.email>
<param.company>The University of Texas at Austin</param.company>
<param.summary>Compute the statistics, pdf, cdf, inverse cdf and random numbers of the generalized chi-square distribution.</param.summary>
<param.description>The generalized chi-square variable is a quadratic form of a normal variable, or equivalently, a linear sum of independent non-central chi-square variables and a normal variable. Try the Getting Started guide for a quick demo of all the functions.</param.description>
<param.screenshot>${PROJECT_ROOT}\gx2_icon.png</param.screenshot>
<param.version>1.6.1</param.version>
<param.output>${PROJECT_ROOT}\Generalized chi-square distribution.mltbx</param.output>
<param.products.name>
<item>Statistics and Machine Learning Toolbox</item>
<item>Symbolic Math Toolbox</item>
</param.products.name>
<param.products.id>
<item>19</item>
<item>15</item>
</param.products.id>
<param.products.version>
<item>12.0</item>
<item>8.6</item>
</param.products.version>
<param.platforms />
<param.guid>6f9f2474-f422-495e-a245-dd928544dd14</param.guid>
<param.exclude.filters>% List files contained in your toolbox folder that you would like to exclude
% from packaging. Excludes should be listed relative to the toolbox folder.
% Some examples of how to specify excludes are provided below:
%
% A single file in the toolbox folder:
% .svn
%
% A single file in a subfolder of the toolbox folder:
% example/.svn
%
% All files in a subfolder of the toolbox folder:
% example/*
%
% All files of a certain name in all subfolders of the toolbox folder:
% **/.svn
%
% All files matching a pattern in all subfolders of the toolbox folder:
% **/*.bak
%
gx2_icon.png</param.exclude.filters>
<param.exclude.pcodedmfiles>true</param.exclude.pcodedmfiles>
<param.examples>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;examples&gt;
&lt;exampleCategory name="doc"&gt;
&lt;example name="GettingStarted" type="html"&gt;
&lt;file type="source"&gt;/doc/html/GettingStarted.html&lt;/file&gt;
&lt;file type="main"&gt;/doc/GettingStarted.mlx&lt;/file&gt;
&lt;file type="thumbnail"/&gt;
&lt;/example&gt;
&lt;/exampleCategory&gt;
&lt;/examples&gt;</param.examples>
<param.demosxml />
<param.apps />
<param.registered.apps />
<param.docs />
<param.getting.started.guide>${PROJECT_ROOT}\doc\GettingStarted.mlx</param.getting.started.guide>
<param.matlabpath.excludes />
<param.javaclasspath.excludes />
<param.exported.on.package>true</param.exported.on.package>
<param.required.addons />
<param.matlab.project.id />
<param.matlab.project.name />
<param.release.start />
<param.release.end />
<param.release.current.only>false</param.release.current.only>
<param.compatiblity.windows>true</param.compatiblity.windows>
<param.compatiblity.macos>true</param.compatiblity.macos>
<param.compatiblity.linux>true</param.compatiblity.linux>
<param.compatiblity.matlabonline>true</param.compatiblity.matlabonline>
<param.installation.map />
<param.additional.sw.names />
<param.additional.sw.licenses />
<param.additional.sw.win.url />
<param.additional.sw.mac.url />
<param.additional.sw.linux.url />
<unset>
<param.authnamewatermark />
<param.email />
<param.company />
<param.output />
<param.platforms />
<param.exclude.pcodedmfiles />
<param.demosxml />
<param.apps />
<param.registered.apps />
<param.docs />
<param.matlabpath.excludes />
<param.javaclasspath.excludes />
<param.required.addons />
<param.matlab.project.id />
<param.matlab.project.name />
<param.release.current.only />
<param.compatiblity.windows />
<param.compatiblity.macos />
<param.compatiblity.linux />
<param.compatiblity.matlabonline />
<param.installation.map />
<param.additional.sw.names />
<param.additional.sw.licenses />
<param.additional.sw.win.url />
<param.additional.sw.mac.url />
<param.additional.sw.linux.url />
</unset>
<fileset.rootdir>
<file>${PROJECT_ROOT}</file>
</fileset.rootdir>
<fileset.rootfiles>
<file>${PROJECT_ROOT}\demos.xml</file>
<file>${PROJECT_ROOT}\doc</file>
<file>${PROJECT_ROOT}\gx2_params_norm_quad.m</file>
<file>${PROJECT_ROOT}\gx2cdf.m</file>
<file>${PROJECT_ROOT}\gx2cdf_davies.m</file>
<file>${PROJECT_ROOT}\gx2cdf_imhof.m</file>
<file>${PROJECT_ROOT}\gx2cdf_ruben.m</file>
<file>${PROJECT_ROOT}\gx2inv.m</file>
<file>${PROJECT_ROOT}\gx2pdf.m</file>
<file>${PROJECT_ROOT}\gx2rnd.m</file>
<file>${PROJECT_ROOT}\gx2stat.m</file>
</fileset.rootfiles>
<fileset.depfun.included />
<fileset.depfun.excluded />
<fileset.package />
<build-deliverables>
<file location="${PROJECT_ROOT}" name="Generalized chi-square distribution.mltbx" optional="false">C:\Users\abhra\Google Drive\Geisler Lab\IntClassNorm\gx2\Generalized chi-square distribution.mltbx</file>
</build-deliverables>
<workflow />
<matlab>
<root>C:\Program Files\MATLAB\R2020b</root>
<toolboxes />
</matlab>
<platform>
<unix>false</unix>
<mac>false</mac>
<windows>true</windows>
<win2k>false</win2k>
<winxp>false</winxp>
<vista>false</vista>
<linux>false</linux>
<solaris>false</solaris>
<osver>10.0</osver>
<os32>false</os32>
<os64>true</os64>
<arch>win64</arch>
<matlab>true</matlab>
</platform>
</configuration>
</deployment-project>
Binary file added gx2_icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions gx2_params_norm_quad.m
@@ -0,0 +1,61 @@
function [lambda,m,delta,sigma,c]=gx2_params_norm_quad(mu,v,quad)

% GX2_PARAMS_NORM_QUAD A quadratic form of a normal variable is distributed
% as a generalized chi-squared. This function takes the normal parameters
% and the quadratic coeffs and returns the parameters of the generalized
% chi-squared.
%
% Abhranil Das <abhranil.das@utexas.edu>
% Center for Perceptual Systems, University of Texas at Austin
% If you use this code, please cite:
% <a href="matlab:web('https://arxiv.org/abs/2012.14331')"
% >A method to integrate and classify normal distributions</a>.
%
% Example:
% mu=[1;2]; % mean
% v=[2 1; 1 3]; % covariance matrix
% % Say q(x)=(x1+x2)^2-x1-1 = [x1;x2]'*[1 1; 1 1]*[x1;x2] + [-1;0]'*[x1;x2] - 1:
% quad.q2=[1 1; 1 1];
% quad.q1=[-1;0];
% quad.q0=-1;
%
% [lambda,m,delta,sigma,c]=gx2_params_norm_quad(mu,v,quad)
%
% Required inputs:
% mu column vector of normal mean
% v normal covariance matrix
% quad struct with quadratic form coefficients:
% q2 matrix of quadratic coefficients
% q1 column vector of linear coefficients
% q0 scalar constant
%
% Outputs:
% lambda row vector of coefficients of the non-central chi-squares
% m row vector of degrees of freedom of the non-central chi-squares
% delta row vector of non-centrality paramaters (sum of squares of
% means) of the non-central chi-squares
% sigma sd of normal term
% c constant term
%
% See also:
% <a href="matlab:open(strcat(fileparts(which('gx2cdf')),filesep,'doc',filesep,'GettingStarted.mlx'))">Interactive demos</a>
% gx2rnd
% gx2stat
% gx2cdf
% gx2pdf

% standardize the space
q2s=0.5*(quad.q2+quad.q2'); % symmetrize q2
q2=sqrtm(v)*q2s*sqrtm(v);
q1=sqrtm(v)*(2*q2s*mu+quad.q1);
q0=mu'*q2s*mu+quad.q1'*mu+quad.q0;

[R,D]=eig(q2);
d=diag(D)';
b=(R'*q1)';

[lambda,~,ic]=unique(nonzeros(d)'); % unique non-zero eigenvalues
m=accumarray(ic,1)'; % total dof of each eigenvalue
delta=arrayfun(@(x) sum((b(d==x)).^2),lambda)./(4*lambda.^2); % total non-centrality for each eigenvalue
sigma=norm(b(~d));
c=q0-sum(lambda.*delta);
78 changes: 78 additions & 0 deletions gx2cdf.m
@@ -0,0 +1,78 @@
function p=gx2cdf(x,lambda,m,delta,sigma,c,varargin)

% GX2CDF Returns the cdf of a generalized chi-squared (a weighted sum of
% non-central chi-squares and a normal), using Ruben's [1962] method,
% Davies' [1973] method, or the native ncx2cdf, depending on the input.
%
% Abhranil Das <abhranil.das@utexas.edu>
% Center for Perceptual Systems, University of Texas at Austin
% If you use this code, please cite:
% <a href="matlab:web('https://arxiv.org/abs/2012.14331')"
% >A method to integrate and classify normal distributions</a>.
%
% Usage:
% p=gx2cdf(x,lambda,m,delta,sigma,c)
% p=gx2cdf(x,lambda,m,delta,sigma,c,'upper')
% p=gx2cdf(x,lambda,m,delta,sigma,c,'AbsTol',0,'RelTol',1e-7)
%
% Example:
% f=gx2pdf(25,[1 -5 2],[1 2 3],[2 3 7],5,0)
%
% Required inputs:
% x points at which to evaluate the CDF
% lambda row vector of coefficients of the non-central chi-squares
% m row vector of degrees of freedom of the non-central chi-squares
% delta row vector of non-centrality paramaters (sum of squares of
% means) of the non-central chi-squares
% sigma sd of normal term
% c constant term
%
% Optional positional input:
% 'upper' more accurate estimate of the complementary CDF when it's small
%
% Optional name-value inputs:
% 'AbsTol' absolute error tolerance for the output
% 'RelTol' relative error tolerance for the output
% The absolute OR the relative tolerance is satisfied.
%
% Output:
% p computed cdf
%
% See also:
% <a href="matlab:open(strcat(fileparts(which('gx2cdf')),filesep,'doc',filesep,'GettingStarted.mlx'))">Interactive demos</a>
% gx2cdf_davies
% gx2cdf_imhof
% gx2cdf_ruben
% gx2pdf

parser = inputParser;
parser.KeepUnmatched = true;
addRequired(parser,'x',@(x) isreal(x));
addRequired(parser,'lambda',@(x) isreal(x) && isrow(x));
addRequired(parser,'m',@(x) isreal(x) && isrow(x));
addRequired(parser,'delta',@(x) isreal(x) && isrow(x));
addRequired(parser,'sigma',@(x) isreal(x) && isscalar(x));
addRequired(parser,'c',@(x) isreal(x) && isscalar(x));
addOptional(parser,'side','lower',@(x) strcmpi(x,'lower') || strcmpi(x,'upper') );
addParameter(parser,'AbsTol',1e-10,@(x) isreal(x) && isscalar(x) && (x>=0));
addParameter(parser,'RelTol',1e-6,@(x) isreal(x) && isscalar(x) && (x>=0));

parse(parser,x,lambda,m,delta,sigma,c,varargin{:});
side=parser.Results.side;

if ~sigma && length(unique(lambda))==1
% native ncx2 fallback
if (sign(unique(lambda))==1 && strcmpi(side,'lower')) || (sign(unique(lambda))==-1 && strcmpi(side,'upper'))
p=ncx2cdf((x-c)/unique(lambda),sum(m),sum(delta));
else
p=ncx2cdf((x-c)/unique(lambda),sum(m),sum(delta),'upper');
end
elseif ~sigma && (all(lambda>0)||all(lambda<0))
try
p=gx2cdf_ruben(x,lambda,m,delta,c,varargin{:});
catch
p=gx2cdf_davies(x,lambda,m,delta,sigma,c,varargin{:});
end
else
p=gx2cdf_davies(x,lambda,m,delta,sigma,c,varargin{:});
end

0 comments on commit d4f088b

Please sign in to comment.