ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
AutoCorr.m
Go to the documentation of this file.
1%> \brief
2%> This is the base class for generating objects containing
3%> information about autocorrelation of the input data.<br>
4%>
5%> \note
6%> This is convenience class for easy computation
7%> of autocorrelation and its storage all in one place.<br>
8%> The primary advantage of this class over the MATLAB
9%> intrinsic functions is in the ability of this class
10%> to compute the result for input dataframe table and
11%> return the results always in MATLAB ``table`` format.<br>
12%> See the documentation of the class constructor below.<br>
13%>
14%> \note
15%> See the documentation of the class constructor below.<br>
16%>
17%> \final{AutoCorr}
18%>
19%> \author
20%> \JoshuaOsborne, May 21 2024, 4:06 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 AutoCorr < pm.matlab.Handle
24
25 properties(Access = public)
26 %>
27 %> ``numlag``
28 %>
29 %> The positive scalar MATLAB whole number representing the
30 %> number of lags for which the autocorrelation must be computed.<br>
31 %> This argument is directly passed to the corresponding argument
32 %> of the MATLAB intrinsic ``autocorr()`` in the Econometrics Toolbox.<br>
33 %>
34 numlag = [];
35 %>
36 %> ``numstd``
37 %>
38 %> The positive scalar MATLAB double representing the
39 %> number of standard deviations to be used in computing the
40 %> lower and upper significance levels of the autocorrelation.<br>
41 %> This argument is directly passed to the corresponding argument
42 %> of the MATLAB intrinsic ``autocorr()`` in the Econometrics Toolbox.<br>
43 %>
44 numstd = 1;
45 %>
46 %> ``bnd``
47 %>
48 %> The MATLAB table of rank ``2`` serving as a convenient
49 %> storage component each column of which corresponds to the
50 %> absolute ``numstd``-significance bound for the corresponding
51 %> computed autocorrelations of the input data (that is optionally
52 %> stored in the ``val`` component of the parent object).<br>
53 %> The values represent the ``numstd``-sigma standard
54 %> errors on the computed autocorrelations.<br>
55 %> Any autocorrelation value bound within these limits can be
56 %> considered random fluctuations at ``numstd``-sigma confidence level.<br>
57 %> This component is automatically populated when constructing
58 %> an object of class [pm.stats.AutoCorr](@ref AutoCorr).<br>
59 %> It must be populated manually at all other times.<br>
60 %>
61 bnd = [];
62 %>
63 %> ``lag``
64 %>
65 %> The MATLAB vector integers representing the
66 %> lags for which the autocorrelation is computed.<br>
67 %> This component is automatically populated when constructing
68 %> an object of class [pm.stats.AutoCorr](@ref AutoCorr).<br>
69 %> It must be populated manually at all other times.<br>
70 %>
71 lag = [];
72 %>
73 %> ``val``
74 %>
75 %> The MATLAB table of rank ``2`` serving as a
76 %> convenient storage component for the autocorrelation.<br>
77 %> This component is automatically populated when constructing
78 %> an object of class [pm.stats.AutoCorr](@ref AutoCorr).<br>
79 %> It must be populated manually at all other times.<br>
80 %>
81 val = [];
82 end
83
84 methods(Access = public)
85
86 %> \brief
87 %> Return an object of class [pm.stats.AutoCorr](@ref AutoCorr).<br>
88 %> This is the constructor of the [pm.stats.AutoCorr](@ref AutoCorr) class.<br>
89 %>
90 %> \param[in] df : The input MATLAB matrix or table of rank ``2``
91 %> containing the data as ``ncol`` columns of ``nrow``
92 %> observations whose autocorrelation must be computed.<br>
93 %> (**optional**. If missing, the autocorrelation will not be computed.)
94 %> \param[in] numlag : The input scalar MATLAB positive whole number representing the number
95 %> of lags for which the autocorrelation must be computed.<br>
96 %> This argument is directly passed to the corresponding argument
97 %> of the MATLAB intrinsic ``autocorr()`` in the Econometrics Toolbox.<br>
98 %> \param[in] numstd : The input positive scalar MATLAB double representing the
99 %> number of standard deviations to be used in computing the
100 %> lower and upper significance levels of the autocorrelation.<br>
101 %> This argument is directly passed to the corresponding argument
102 %> of the MATLAB intrinsic ``autocorr()`` in the Econometrics Toolbox.<br>
103 %> (**optional**, default = ``1``)
104 %>
105 %> \return
106 %> ``self`` : The output object of class [pm.stats.AutoCorr](@ref AutoCorr).<br>
107 %>
108 %> \interface{AutoCorr}
109 %> \code{.m}
110 %>
111 %> acf = pm.stats.AutoCorr()
112 %> acf = pm.stats.AutoCorr(df)
113 %> acf = pm.stats.AutoCorr(df, numlag)
114 %> acf = pm.stats.AutoCorr(df, [], numstd)
115 %> acf = pm.stats.AutoCorr(df, numlag, numstd)
116 %>
117 %> \endcode
118 %>
119 %> \example{AutoCorr}
120 %> \include{lineno} example/stats/AutoCorr/main.m
121 %> \vis{AutoCorr}
122 %> \image html example/stats/AutoCorr/AutoCorr.unifrnd.png width=700
123 %>
124 %> \final{AutoCorr}
125 %>
126 %> \author
127 %> \JoshuaOsborne, May 21 2024, 4:10 AM, University of Texas at Arlington<br>
128 %> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
129 %> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
130 function self = AutoCorr(df, numlag, numstd)
131 if nargin < 3
132 numstd = 1;
133 end
134 if nargin < 2
135 numlag = [];
136 end
137 if 0 < nargin
138 if isempty(numlag)
139 numlag = size(df, 1) - 1;
140 end
141 [self.val, self.lag, self.bnd] = self.get(df, numlag, numstd);
142 else
143 self.numlag = numlag;
144 self.numstd = numstd;
145 end
146 end
147
148 %> \brief
149 %> Return the autocorrelation of the input
150 %> data from a lag of ``0`` to ``numlag``.<br>
151 %>
152 %> \details
153 %> This is a dynamic method of the [pm.stats.AutoCorr](@ref AutoCorr) class.<br>
154 %> This method automatically stores any input information
155 %> in the corresponding components of the parent object.<br>
156 %> However, any components of the parent object
157 %> corresponding to the output of this method
158 %> must be set explicitly manually.<br>
159 %>
160 %> \param[inout] self : The input/output parent object of class [pm.stats.AutoCorr](@ref AutoCorr)
161 %> which is **implicitly** passed to this dynamic method (not by the user).<br>
162 %> \param[in] df : The input MATLAB matrix or table of rank ``2``
163 %> containing the data as ``ncol`` columns of ``nrow``
164 %> observations whose autocorrelation must be computed.<br>
165 %> \param[in] numlag : The input positive scalar MATLAB integer representing the
166 %> number of lags to be used in computing the autocorrelation.<br>
167 %> The default value will be used if the input ``numlag``
168 %> is unspecified or empty ``[]``.<br>
169 %> (**optional**, default = ``size(df, 1) - 1``)
170 %> \param[in] numstd : The input positive scalar MATLAB double representing the
171 %> number of standard deviations to be used in computing the
172 %> lower and upper significance levels of the autocorrelation.<br>
173 %> This argument is directly passed to the corresponding argument
174 %> of the MATLAB intrinsic ``autocorr()`` in the Econometrics Toolbox.<br>
175 %> The default value will be used if the input ``numstd`` is empty ``[]``.<br>
176 %> (**optional**, default = ``1``)
177 %>
178 %> \return
179 %> ``val`` : The output MATLAB ``table`` of size ``numlag + 1``
180 %> containing the autocorrelation from lag ``0`` to ``numlag``.<br>
181 %> ``lag`` : The output MATLAB ``table`` of size ``numlag + 1``
182 %> containing the autocorrelation lags from ``0`` to ``numlag``.<br>
183 %> ``bnd`` : The output MATLAB ``table`` of ``size(df, 2)`` rows by one column
184 %> containing the absolute ``numstd``-significance level of the
185 %> computed autocorrelations. Any autocorrelation value whose
186 %> magnitude is smaller than the corresponding ``bnd`` element
187 %> can be considered insignificant and mere fluctuation.<br>
188 %>
189 %> \interface{get}
190 %> \code{.m}
191 %>
192 %> acf = pm.stats.AutoCorr()
193 %> [acf.val, acf.lag, acf.bnd] = acf.get(df)
194 %> [acf.val, acf.lag, acf.bnd] = acf.get(df, [])
195 %> [acf.val, acf.lag, acf.bnd] = acf.get(df, numlag)
196 %> [acf.val, acf.lag, acf.bnd] = acf.get(df, [], numstd)
197 %> [acf.val, acf.lag, acf.bnd] = acf.get(df, numlag, numstd)
198 %>
199 %> \endcode
200 %>
201 %> \note
202 %> See the documentation of the class constructor
203 %> [pm.stats.AutoCorr](@ref AutoCorr::AutoCorr) for example usage.<br>
204 %>
205 %> \final{get}
206 %>
207 %> \author
208 %> \JoshuaOsborne, May 21 2024, 4:14 AM, University of Texas at Arlington<br>
209 %> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
210 %> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
211 function [val, lag, bnd] = get(self, df, numlag, numstd)
212 if nargin < 2
213 help("pm.stats.AutoCorr");
214 error ( newline ...
215 + "The input ``df`` argument is required for computing the autocorrelation." + newline ...
216 + newline ...
217 );
218 end
219 if nargin < 4
220 numstd = 1;
221 end
222 if nargin < 3
223 numlag = [];
224 end
225 self.numlag = numlag;
226 self.numstd = numstd;
227 if isa(df, "table")
228 data = table2array(df);
229 else
230 data = df;
231 end
232 if ~isempty(self.numlag)
233 numlag = self.numlag;
234 else
235 numlag = size(data, 1) - 1;
236 end
237 val = zeros(numlag + 1, size(data, 2));
238 lag = zeros(numlag + 1, 1);
239 bnd = zeros(size(data, 2), 1);
240 try
241 for icol = 1 : size(data, 2)
242 [val(:, icol), lag, bounds] = autocorr(data(:, icol), numlag);
243 bnd(icol) = abs(bounds(1)) * numstd;
244 end
245 val = array2table(val);
246 catch me
247 %val = NaN(numlag, size(data, 2));
248 warning ( newline ...
249 + string(me.identifier) + " : " + string(me.message) + newline ...
250 + "skipping the autocorrelation computation..." + newline ...
251 + newline ...
252 );
253 return;
254 end
255 if isa(df, "table")
256 val.Properties.VariableNames = df.Properties.VariableNames;
257 end
258 end
259
260 end
261
262end
function abs(in path, in style)
Return the Get absolute canonical path of a file or folder.
This is the base class for generating objects containing information about autocorrelation of the inp...
Definition: AutoCorr.m:24
Property lag
Definition: AutoCorr.m:77
Property bnd
Definition: AutoCorr.m:66
Property numstd
Definition: AutoCorr.m:48
function AutoCorr(in df, in numlag, in numstd)
Return an object of class pm.stats.AutoCorr. This is the constructor of the pm.stats....
Property numlag
Definition: AutoCorr.m:37
function get(in self, in df, in numlag, in numstd)
Return the autocorrelation of the input data from a lag of 0 to numlag.
Property val
Definition: AutoCorr.m:88
This is the base class for generating subclass of MATLAB handle superclass whose annoying methods are...
Definition: Handle.m:24
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...