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