ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
Cov.m
Go to the documentation of this file.
1%> \brief
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>
5%>
6%> \details
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>
10%>
11%> \note
12%> See the documentation of the class constructor [pm.stats.Cov](@ref Cov::Cov) below.<br>
13%>
14%> \final
15%>
16%> \author
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
21
22 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
23
24 properties(Access = public)
25 %>
26 %> ``dfref``
27 %>
28 %> A scalar object of class [pm.container.DataFrame](@ref DataFrame)
29 %> containing the user-specified data whose covariance must be computed.<br>
30 %>
31 dfref = [];
32 %>
33 %> ``method``
34 %>
35 %> The scalar MATLAB string containing the
36 %> method of computing the covariance matrix.<br>
37 %> It can be either:<br>
38 %> <ol>
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>
41 %> </ol>
42 %>
43 method = "pearson";
44 %>
45 %> ``val``
46 %>
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>
52 %>
53 val = [];
54 %>
55 %> ``vis``
56 %>
57 %> The scalar MATLAB ``struct`` containing the set of
58 %> predefined visualizations for the output data.<br>
59 %>
60 vis = [];
61 end
62
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64
65 methods(Access = public)
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68
69 %> \brief
70 %> Return an object of class [pm.stats.Cov](@ref Cov).<br>
71 %>
72 %> \details
73 %> This is the constructor of the [pm.stats.Cov](@ref Cov) class.<br>
74 %>
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>
83 %> <ol>
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>
86 %> </ol>
87 %> (**optional**, default = ``"pearson"``)
88 %>
89 %> \return
90 %> ``self`` : The output object of class [pm.stats.Cov](@ref Cov).<br>
91 %>
92 %> \interface{Cov}
93 %> \code{.m}
94 %>
95 %> mat = pm.stats.Cov([])
96 %> mat = pm.stats.Cov([], [])
97 %> mat = pm.stats.Cov([], method)
98 %>
99 %> mat = pm.stats.Cov(dfref)
100 %> mat = pm.stats.Cov(dfref, [])
101 %> mat = pm.stats.Cov(dfref, method)
102 %>
103 %> \endcode
104 %>
105 %> \example{Cov}
106 %> \include{lineno} example/stats/Cov/main.m
107 %> \output{Cov}
108 %> \include{lineno} example/stats/Cov/main.out.m
109 %> \vis{Cov}
110 %> \image html example/stats/Cov/Cov.unifrnd.png width=700
111 %>
112 %> \final{Cov}
113 %>
114 %> \author
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)
119
120 if 1 < nargin
121 self.method = method;
122 end
123
124 if 0 < nargin
125 self.val = self.get(dfref, self.method);
126 end
127
128 self.setvis();
129
130 end
131
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133
134 %> \brief
135 %> Return the covariance matrix of the input data.<br>
136 %>
137 %> \details
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>
144 %>
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>
152 %> <ol>
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>
155 %> </ol>
156 %> (**optional**, default = ``"pearson"``)
157 %>
158 %> \return
159 %> ``val`` : The output MATLAB ``table`` containing the covariance matrix.<br>
160 %>
161 %> \interface{get}
162 %> \code{.m}
163 %>
164 %> mat = pm.stats.Cov(dfref, method)
165 %>
166 %> mat.val = mat.get()
167 %> mat.val = mat.get([])
168 %> mat.val = mat.get([], [])
169 %> mat.val = mat.get([], method)
170 %>
171 %> mat.val = mat.get(dfref)
172 %> mat.val = mat.get(dfref, [])
173 %> mat.val = mat.get(dfref, method)
174 %>
175 %> \endcode
176 %>
177 %> \note
178 %> See the documentation of the class constructor
179 %> [pm.stats.Cor](@ref Cor::Cor) for example usage.<br>
180 %>
181 %> \final{get}
182 %>
183 %> \author
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)
188
189 if nargin < 3
190 method = "pearson";
191 end
192 self.method = method;
193
194 if nargin < 2
195 dfref = [];
196 end
197
198 if ~isempty(dfref)
199 self.dfref = pm.container.DataFrame(dfref);
200 end
201 dfcopy = self.dfref.copy();
202
203 if ~isempty(dfcopy)
204 data = dfcopy{:,:};
205 else
206 help("pm.stats.Cov");
207 error ( newline ...
208 + "A non-mepty ``dfref`` attribute or input argument is required for computing the covariance matrix." + newline ...
209 + newline ...
210 );
211 end
212
213 %%%%
214 %%%% Convert data to rank if necessary.
215 %%%%
216
217 if strcmpi(self.method, "spearman")
218 try
219 data = tiedrank(data);
220 catch me
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 ...
225 + newline ...
226 );
227 return;
228 end
229 elseif ~strcmpi(self.method, "pearson")
230 help("pm.stats.Cov");
231 disp("self.method");
232 disp( self.method );
233 error ( newline ...
234 + "Invalid value specified for the covariance computation ``method``." + newline ...
235 + newline ...
236 );
237 end
238
239 try
240 val = array2table(cov(data));
241 catch me
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 ...
246 + newline ...
247 );
248 return;
249 end
250
251 %%%%
252 %%%% Set the matrix column and row names.
253 %%%%
254
255 if isa(dfcopy, "table")
256 val.Properties.VariableNames = dfcopy.Properties.VariableNames;
257 end
258 val.Properties.RowNames = val.Properties.VariableNames;
259
260 end
261
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263
264 end
265
266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267
268 methods(Access = public, Hidden)
269
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271
272 %> \brief
273 %> Set up the visualization tools of the covariance matrix.<br>
274 %>
275 %> \details
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>
278 %>
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.)
285 %>
286 %> \interface{setvis}
287 %> \code{.m}
288 %>
289 %> mat = pm.stats.Cov(dfref, method)
290 %> mat.setvis(); % This method is automatically called within the object constructor.
291 %>
292 %> \endcode
293 %>
294 %> \final{setvis}
295 %>
296 %> \author
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)
300
301 if 1 < nargin
302 self.val = val;
303 end
304
305 self.vis = struct();
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;
310
311 end
312
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314
315 end
316
317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318
319end
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...
Definition: Cov.m:21
Property method
Definition: Cov.m:47
Property val
Definition: Cov.m:58
Property dfref
Definition: Cov.m:34
Property vis
Definition: Cov.m:66
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...
Definition: DataFrame.m:25
This is the base class for generating subclass of MATLAB handle superclass whose annoying methods are...
Definition: Handle.m:24