1function s = std(o, geometric) % --*-- Unitary tests --*--
2
3% Returns the standard deviation of the variables in a @dseries object o.
4% See https://en.wikipedia.org/wiki/Geometric_standard_deviation
5%
6% INPUTS
7% - o             [dseries]   T observations and N variables.
8% - geometric     [logical]   if true returns the geometric standard deviation (default is false).
9%
10% OUTPUTS
11% - s             [double]    1*N vector.
12
13% Copyright (C) 2016-2017 Dynare Team
14%
15% This file is part of Dynare.
16%
17% Dynare is free software: you can redistribute it and/or modify
18% it under the terms of the GNU General Public License as published by
19% the Free Software Foundation, either version 3 of the License, or
20% (at your option) any later version.
21%
22% Dynare is distributed in the hope that it will be useful,
23% but WITHOUT ANY WARRANTY; without even the implied warranty of
24% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25% GNU General Public License for more details.
26%
27% You should have received a copy of the GNU General Public License
28% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
29
30if nargin<2
31    geometric = false;
32end
33
34if geometric
35    m = mean(o, true);
36    s = exp(sqrt(sum(log(bsxfun(@rdivide, o.data, m)).^2, 1)/nobs(o)));
37else
38    s = std(o.data);
39end
40
41%@test:1
42%$ % Define a dataset.
43%$ A = repmat([1.005, 1.05], 10, 1);
44%$
45%$ % Instantiate a time series object and compute the mean.
46%$ try
47%$    ts = dseries(A);
48%$    s1 = std(ts, true);
49%$    s2 = std(ts);
50%$    t(1) = 1;
51%$ catch
52%$    t = 0;
53%$ end
54%$
55%$ if t(1)
56%$    t(2) = dassert(isequal(size(s1),[1, 2]), true);
57%$    t(3) = dassert(isequal(size(s2),[1, 2]), true);
58%$    t(4) = dassert(s1, [1, 1]);
59%$    t(4) = all(abs(s2)<1e-12);
60%$ end
61%$ T = all(t);
62%@eof:1
63
64%@test:2
65%$ % Define a dataset.
66%$ A = repmat([1.005, 1.05], 10, 1);
67%$
68%$ % Instantiate a time series object and compute the mean.
69%$ try
70%$    ts = dseries(A);
71%$    s1 = ts.std(true);
72%$    s2 = ts.std();
73%$    t(1) = 1;
74%$ catch
75%$    t = 0;
76%$ end
77%$
78%$ if t(1)
79%$    t(2) = dassert(isequal(size(s1),[1, 2]), true);
80%$    t(3) = dassert(isequal(size(s2),[1, 2]), true);
81%$    t(4) = dassert(s1, [1, 1]);
82%$    t(4) = all(abs(s2)<1e-12);
83%$ end
84%$ T = all(t);
85%@eof:2
86
87%@test:3
88%$ % Define a dataset.
89%$ A = bsxfun(@plus, randn(100000000,2)*.1, [.5, 2]);
90%$
91%$ % Instantiate time series objects and compute the mean.
92%$ try
93%$    ts = dseries(A);
94%$    s = std(ts);
95%$    t(1) = 1;
96%$ catch
97%$    t = 0;
98%$ end
99%$
100%$ if t(1)
101%$    t(2) = dassert(isequal(size(s),[1, 2]), true);
102%$    t(3) = dassert(max(abs(s-[.1, .1]))<.0001, true);
103%$ end
104%$ T = all(t);
105%@eof:3
106
107%@test:4
108%$ % Define a dataset.
109%$ A = bsxfun(@plus, randn(100000000,2)*.1, [.5, 2]);
110%$
111%$ % Instantiate time series objects and compute the mean.
112%$ try
113%$    ts = dseries(A);
114%$    s = ts.std();
115%$    t(1) = 1;
116%$ catch
117%$    t = 0;
118%$ end
119%$
120%$ if t(1)
121%$    t(2) = dassert(isequal(size(s),[1, 2]), true);
122%$    t(3) = dassert(max(abs(s-[.1, .1]))<.0001, true);
123%$ end
124%$ T = all(t);
125%@eof:4