2%> This is the base
class for generating objects with methods and
3%> storage components
for computing and storing and visualizing
4%> the covariance matrix of an input data.<br>
7%> This is merely a convenience
class for easy computation of covariance and its storage all in one place.<br>
8%> The primary advantage of
this class over the MATLAB intrinsic functions is in the ability of this class to
9%> compute the result
for input dataframe table and
return the results always in MATLAB ``table`` format.<br>
12%> See the documentation of the
class constructor [pm.stats.
Cov](@ref
Cov::
Cov) below.<br>
17%> \JoshuaOsborne, May 21 2024, 4:25 AM, University of Texas at Arlington<br>
18%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
19%> \AmirShahmoradi, July 5 2024, 1:07 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
20classdef
Cov < pm.matlab.Handle
22 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
24 properties(Access = public)
28 %> A scalar object of class [pm.container.
DataFrame](@ref DataFrame)
29 %> containing the user-specified data whose covariance must be computed.<br>
35 %> The scalar MATLAB string containing the
36 %> method of computing the covariance matrix.<br>
37 %> It can be either:<br>
39 %> <li> ``"pearson"`` : for computing the Pearson covariance matrix of the input data.<br>
40 %> <li> ``"spearman"`` : for computing the Spearman rank covariance matrix of the input data.<br>
47 %> The MATLAB ``table`` of rank ``2`` serving as a
48 %> convenient storage component for the covariance matrix.<br>
49 %> This component is automatically populated at the time of
50 %> constructing an object of class [pm.stats.
Cov](@ref Cov).<br>
51 %> It must be populated manually at all other times.<br>
57 %> The scalar MATLAB ``struct`` containing the set of
58 %> predefined visualizations for the output data.<br>
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 methods(Access = public)
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 %> Return an object of class [pm.stats.
Cov](@ref Cov).<br>
73 %> This is the constructor of the [pm.stats.
Cov](@ref Cov) class.<br>
75 %> \param[in] dfref : The input MATLAB matrix or table of rank ``2``
76 %> containing the data as ``ncol`` columns of ``nrow``
77 %> observations whose covariance matrix must be computed.<br>
78 %> Ideally, the user would want to pass a reference
79 %> to a dataframe (e.g., as a function handle ``@()df``)
80 %> so that the data remains dynamically up-to-date.<br>
81 %> (**optional**. If missing or empty, the covariance matrix will not be computed.)
82 %> \param[in] method : The input scalar MATLAB string that can be either:<br>
84 %> <li> ``"pearson"`` : for computing the Pearson covariance matrix of the input data.<br>
85 %> <li> ``"spearman"`` : for computing the Spearman rank covariance matrix of the input data.<br>
87 %> (**optional**, default = ``"pearson"``)
90 %> ``self`` : The output object of class [pm.stats.
Cov](@ref Cov).<br>
95 %> mat = pm.stats.
Cov([])
96 %> mat = pm.stats.Cov([], [])
97 %> mat = pm.stats.Cov([], method)
99 %> mat = pm.stats.Cov(dfref)
100 %> mat = pm.stats.Cov(dfref, [])
101 %> mat = pm.stats.Cov(dfref, method)
106 %> \include{lineno} example/stats/
Cov/main.m
108 %> \include{lineno} example/stats/
Cov/main.out.m
110 %> \image html example/stats/
Cov/
Cov.unifrnd.png width=700
115 %> \JoshuaOsborne, May 21 2024, 4:29 AM, University of Texas at Arlington<br>
116 %> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
117 %> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute
for Computational Engineering and Sciences (ICES), UT Austin<br>
118 function self =
Cov(dfref, method)
125 self.val = self.get(dfref, self.method);
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 %> Return the covariance matrix of the input data.<br>
138 %> This is a dynamic method of the [pm.stats.Cov](@ref
Cov)
class.<br>
139 %> This method automatically stores any input information in
140 %> the corresponding components of the parent
object.<br>
141 %> However, any components of the parent
object
142 %> corresponding to the output of
this method
143 %> must be set explicitly manually.<br>
145 %> \param[inout] self : The **implicitly-passed** input argument representing the parent
object of the method.<br>
146 %> \param[in] dfref : The input (reference of function handle returning a) MATLAB matrix or table of rank ``2``
147 %> containing the ``ncol`` columns of ``nrow`` data whose covariance matrix must be computed.<br>
148 %> Ideally, the user would want to pass a reference to a dataframe (e.g., as a function handle ``@()df``)
149 %> so that the data remains dynamically up-to-date.<br>
150 %> (**optional**. If missing, the contents of the corresponding internal component of the parent
object will be used.)
151 %> \param[in] method : The input scalar MATLAB
string that can be either:<br>
153 %> <li> ``
"pearson"`` :
for computing the Pearson covariance matrix of the input data.<br>
154 %> <li> ``
"spearman"`` :
for computing the Spearman rank covariance matrix of the input data.<br>
156 %> (**optional**,
default = ``
"pearson"``)
159 %> ``val`` : The output MATLAB ``table`` containing the covariance matrix.<br>
164 %> mat = pm.stats.Cov(dfref, method)
166 %> mat.val = mat.get()
167 %> mat.val = mat.get([])
168 %> mat.val = mat.get([], [])
169 %> mat.val = mat.get([], method)
171 %> mat.val = mat.get(dfref)
172 %> mat.val = mat.get(dfref, [])
173 %> mat.val = mat.get(dfref, method)
178 %> See the documentation of the
class constructor
179 %> [pm.stats.Cor](@ref
Cor::Cor)
for example usage.<br>
184 %> \JoshuaOsborne, May 21 2024, 4:31 AM, University of Texas at Arlington<br>
185 %> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
186 %> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute
for Computational Engineering and Sciences (ICES), UT Austin<br>
187 function val = get(self, dfref, method)
192 self.method = method;
199 self.dfref = pm.container.DataFrame(dfref);
201 dfcopy = self.dfref.copy();
206 help(
"pm.stats.Cov");
208 +
"A non-mepty ``dfref`` attribute or input argument is required for computing the covariance matrix." + newline ...
214 %%%% Convert data to rank
if necessary.
217 if strcmpi(self.method,
"spearman")
219 data = tiedrank(data);
221 val = NaN(size(data, 2), size(data, 2));
222 warning ( newline ...
223 +
string(me.identifier) + " : " +
string(me.message) + newline ...
224 + "skipping the covariance matrix computation..." + newline ...
229 elseif ~strcmpi(self.method, "pearson")
230 help("pm.stats.
Cov");
234 + "Invalid value specified for the covariance computation ``method``." + newline ...
240 val = array2table(cov(data));
242 val = NaN(size(data, 2), size(data, 2));
243 warning ( newline ...
244 +
string(me.identifier) + " : " +
string(me.message) + newline ...
245 + "skipping the covariance matrix computation..." + newline ...
252 %%%% Set the matrix column and row names.
255 if isa(dfcopy, "table")
256 val.Properties.VariableNames = dfcopy.Properties.VariableNames;
258 val.Properties.RowNames = val.Properties.VariableNames;
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 methods(Access = public, Hidden)
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 %> Set up the visualization tools of the covariance matrix.<br>
276 %> This is a dynamic ``Hidden`` method of the [pm.stats.
Cov](@ref
Cov) class.<br>
277 %> This method is inaccessible to the end users of the ParaMonte MATLAB library.<br>
279 %> \param[inout] self : The **implicitly-passed** input argument representing the parent
object of the method.<br>
280 %> \param[in] val : The input (reference of function handle returning a) MATLAB matrix or table of rank ``2``
281 %> containing the computed covariance matrix to be visualized.<br>
282 %> Ideally, the user would want to pass a reference to a dataframe (e.g., as a function handle ``@()df``)
283 %> so that the data remains dynamically up-to-date.<br>
284 %> (**optional**. If missing, the contents of the corresponding ``var`` attribute of the parent
object will be used.)
286 %> \interface{setvis}
289 %> mat = pm.stats.Cov(dfref, method)
290 %> mat.setvis(); % This method is automatically called within the
object constructor.
297 %> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
298 %> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute
for Computational Engineering and Sciences (ICES), UT Austin<br>
299 function setvis(self, val)
306 self.vis.heatmap = pm.vis.PlotHeatmap(@()self.val);
307 self.vis.heatmap.subplot.title.titletext =
"Covariance Matrix";
308 self.vis.heatmap.subplot.title.enabled =
true;
309 self.vis.heatmap.subplot.precision = 2;
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Cor(in dfref, in method)
Return an object of class pm.stats.Cor.
This is the base class for generating objects with methods and storage components for computing and s...
function setvis(in self, in val)
Set up the visualization tools of the covariance matrix.
function get(in self, in dfref, in method)
Return the covariance matrix of the input data.
function Cov(in dfref, in method)
Return an object of class pm.stats.Cov.
This is the abstract class for generating instances of objects that can contain basic attributes requ...
This is the base class for generating subclass of MATLAB handle superclass whose annoying methods are...