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
3%> and storage components for computing and storing the
4%> covariance matrix of an input data.<br>
5%>
6%> \details
7%> This is convenience class for easy computation
8%> of covariance and its storage all in one place.<br>
9%> The primary advantage of this class over the MATLAB
10%> intrinsic functions is in the ability of this class
11%> to compute the result for input dataframe table and
12%> return the results always in MATLAB ``table`` format.<br>
13%>
14%> \note
15%> See the documentation of the class constructor [pm.stats.Cov](@ref Cov::Cov) below.<br>
16%>
17%> \final
18%>
19%> \author
20%> \JoshuaOsborne, May 21 2024, 4:25 AM, University of Texas at Arlington<br>
21%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
22%> \AmirShahmoradi, July 5 2024, 1:07 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
23classdef Cov < pm.matlab.Handle
24
25 properties(Access = public)
26 %>
27 %> ``method``
28 %>
29 %> The scalar MATLAB string containing the
30 %> method of computing the covariance matrix.<br>
31 %> It can be either:<br>
32 %> <ol>
33 %> <li> ``"pearson"`` : for computing the Pearson covariance matrix of the input data.
34 %> <li> ``"kendall"`` : for computing the Kendall rank covariance matrix of the input data.
35 %> <li> ``"spearman"`` : for computing the Spearman rank covariance matrix of the input data.
36 %> </ol>
37 %>
38 method = "pearson";
39 %>
40 %> ``val``
41 %>
42 %> The MATLAB table of rank ``2`` serving as a
43 %> convenient storage component for the covariance matrix.<br>
44 %> This component is automatically populated at the time of
45 %> constructing an object of class [pm.stats.Cov](@ref Cov).<br>
46 %> It must be populated manually at all other times.<br>
47 %>
48 val = [];
49 end
50
51 methods(Access = public)
53 %> \brief
54 %> Return an object of class [pm.stats.Cov](@ref Cov).<br>
55 %>
56 %> \details
57 %> This is the constructor of the [pm.stats.Cov](@ref Cov) class.<br>
58 %>
59 %> \param[in] df : The input MATLAB matrix or table of rank ``2``
60 %> containing the data as ``ncol`` columns of ``nrow``
61 %> observations whose covariance matrix must be computed.<br>
62 %> (**optional**. If missing, the covariance matrix will not be computed.)
63 %>
64 %> \param[in] method : The input scalar MATLAB string that can be either:<br>
65 %> "pearson" : for computing the Pearson covariance matrix of the input data.<br>
66 %> "spearman" : for computing the Spearman rank covariance matrix of the input data.<br>
67 %> (**optional**, default = ``"pearson"``)
68 %>
69 %> \return
70 %> ``self`` : The output object of class [pm.stats.Cov](@ref Cov).<br>
71 %>
72 %> \interface{Cov}
73 %> \code{.m}
74 %>
75 %> mat = pm.stats.Cov()
76 %> mat = pm.stats.Cov(df)
77 %> mat = pm.stats.Cov(df, method)
78 %>
79 %> \endcode
80 %>
81 %> \example{Cov}
82 %> \include{lineno} example/stats/Cov/main.m
83 %> \output{Cov}
84 %> \include{lineno} example/stats/Cov/main.out.m
85 %> \vis{Cov}
86 %> \image html example/stats/Cov/Cov.unifrnd.png width=700
87 %>
88 %> \final{Cov}
89 %>
90 %> \author
91 %> \JoshuaOsborne, May 21 2024, 4:29 AM, University of Texas at Arlington<br>
92 %> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
93 %> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
94 function self = Cov(df, method)
95 if 1 < nargin
96 self.method = method;
97 end
98 if 0 < nargin
99 self.val = self.get(df, self.method);
100 end
101 end
102
103 %> \brief
104 %> Return the covariance matrix of the input data.<br>
105 %>
106 %> \details
107 %> This is a dynamic method of the [pm.stats.Cov](@ref Cov) class.<br>
108 %> This method automatically stores any input information
109 %> in the corresponding components of the parent object.<br>
110 %> However, any components of the parent object
111 %> corresponding to the output of this method
112 %> must be set explicitly manually.<br>
113 %>
114 %> \param[in] df : The input MATLAB matrix or table of rank ``2``
115 %> containing the data as ``ncol`` columns of ``nrow``
116 %> observations whose covariance matrix must be computed.<br>
117 %> \param[in] method : The input scalar MATLAB string that can be either:<br>
118 %> "pearson" : for computing the Pearson covariance matrix of the input data.<br>
119 %> "spearman" : for computing the Spearman rank covariance matrix of the input data.<br>
120 %> (**optional**, default = ``"pearson"``)
121 %>
122 %> \return
123 %> ``val`` : The output MATLAB ``table`` containing the covariance matrix.<br>
124 %>
125 %> \interface{get}
126 %> \code{.m}
127 %>
128 %> mat = pm.stats.Cov()
129 %> mat.val = mat.get(df)
130 %> mat.val = mat.get(df, method)
131 %>
132 %> \endcode
133 %>
134 %> \final{get}
135 %>
136 %> \author
137 %> \JoshuaOsborne, May 21 2024, 4:31 AM, University of Texas at Arlington<br>
138 %> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
139 %> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
140 function val = get(self, df, method)
141 if nargin < 2
142 help("pm.stats.Cov");
143 error ( newline ...
144 + "The input ``df`` argument is required for computing the covariance matrix." + newline ...
145 + newline ...
146 );
147 end
148 if nargin < 3
149 method = "pearson";
150 end
151 self.method = method;
152 if isa(df, "table")
153 data = table2array(df);
154 else
155 data = df;
156 end
157 if strcmpi(self.method, "spearman")
158 try
159 data = tiedrank(data);
160 catch me
161 val = NaN(size(data, 2), size(data, 2));
162 warning ( newline ...
163 + string(me.identifier) + " : " + string(me.message) + newline ...
164 + "skipping the covariance matrix computation..." + newline ...
165 + newline ...
166 );
167 return;
168 end
169 end
170 try
171 val = array2table(cov(data));
172 catch me
173 val = NaN(size(data, 2), size(data, 2));
174 warning ( newline ...
175 + string(me.identifier) + " : " + string(me.message) + newline ...
176 + "skipping the covariance matrix computation..." + newline ...
177 + newline ...
178 );
179 return;
180 end
181 if isa(df, "table")
182 val.Properties.VariableNames = df.Properties.VariableNames;
183 end
184 val.Properties.RowNames = val.Properties.VariableNames;
185 end
186
187 end
188
189end
This is the base class for generating objects with methods and storage components for computing and s...
Definition: Cov.m:24
Property method
Definition: Cov.m:41
Property val
Definition: Cov.m:52
function get(in self, in df, in method)
Return the covariance matrix of the input data.
function Cov(in df, in method)
Return an object of class pm.stats.Cov.
This is the base class for generating subclass of MATLAB handle superclass whose annoying methods are...
Definition: Handle.m:24