-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweightedcorrs.m
537 lines (534 loc) · 31 KB
/
weightedcorrs.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
% Downloaded from https://www.mathworks.com/matlabcentral/fileexchange/20846-weighted-correlation-matrix
%
% =============== start of license.txt =======================
%
% Copyright (c) 2008, Liber Eleutherios
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%
% =============== end of license.txt =======================
function R = weightedcorrs(Y, w)
%
% WEIGHTEDCORRS returns a symmetric matrix R of weighted correlation
% coefficients calculated from an input T-by-N matrix Y whose rows are
% observations and whose columns are variables and an input T-by-1 vector
% w of weights for the observations. This function may be a valid
% alternative to CORRCOEF if observations are not all equally relevant
% and need to be weighted according to some theoretical hypothesis or
% knowledge.
%
% R = WEIGHTEDCORRS(Y, w) returns a positive semidefinite matrix R,
% i.e. all its eigenvalues are non-negative (see Example 1).
%
% WEIGHTEDCORRS is such that
% WEIGHTEDCORRS(Y, w) == WEIGHTEDCORRS(a * Y + b, w)
% where a and b are two real numbers (see Example 1).
%
% Furthermore, the result provided by the function doesn't change if the
% unit system of each column of Y is changed through an arbitrary affine
% transformation y = a * x + b, where a and b are two real numbers, with
% a > 0 (see Example 2).
%
% If w = ones(size(Y, 1), 1), no difference exists between
% WEIGHTEDCORRS(Y, w) and CORRCOEF(Y) (see Example 4).
%
%
% REFERENCE: the mathematical formulas in matrix notation, together with
% the code, is also available in
% F. Pozzi, T. Di Matteo, T. Aste,
% "Exponential smoothing weighted correlations",
% The European Physical Journal B, Volume 85, Issue 6, 2012.
% DOI:10.1140/epjb/e2012-20697-x.
%
% % ======================================================================
% % EXAMPLE 0: some common shapes in 2D.
% % ======================================================================
%
% T = 1000; % number of observations
% % CHOOSE WEIGHTS
% alpha = 2 / T;
% w0 = 1 / sum(exp(((1:T) - T) * alpha));
% w = w0 * exp(((1:T) - T) * alpha); % weights: exponential decay
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % a) A line is always a line!
% Y(:, 1) = 1:T;
% Y(:, 2) = rand * Y(:, 1) + rand; % Linear relation
% r1 = corrcoef(Y);
% r1 = r1(2) % Traditional Correlation
% r2 = weightedcorrs(Y, w);
% r2 = r2(2) % Weighted Correlation
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % b) An horizontal line has a correlation equal to 0
% Y(1:T, 1) = rand;
% Y(:, 2) = 1:T; % Linear relation
% r1 = corrcoef(Y);
% r1 = r1(2) % Traditional Correlation
% r2 = weightedcorrs(Y, w);
% r2 = r2(2) % Weighted Correlation
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % c) A vertical line has a correlation equal to 0
% Y(:, 1) = 1:T;
% Y(:, 2) = rand; % Linear relation
% r1 = corrcoef(Y);
% r1 = r1(2) % Traditional Correlation
% r2 = weightedcorrs(Y, w);
% r2 = r2(2) % Weighted Correlation
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % d) A symmetric parabolic shape ... makes a huge difference!
% Y(:, 1) = (1:T) - T / 2 - 0.5;
% Y(:, 2) = rand * Y(:, 1) .^ 2 + rand; % Parabolic relation (symmetric)
% r1 = corrcoef(Y);
% r1 = r1(2) % Traditional Correlation
% r2 = weightedcorrs(Y, w);
% r2 = r2(2) % Weighted Correlation
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % e) A circle
% angle = (2 * rand(T, 1) - 1) * pi;
% rho = rand(T, 1);
% Y(:, 1) = rho .* cos(angle);
% Y(:, 2) = rho .* sin(angle); % Circle
% r1 = corrcoef(Y);
% r1 = r1(2) % Traditional Correlation
% r2 = weightedcorrs(Y, w);
% r2 = r2(2) % Weighted Correlation
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % f) An exponential curve
% Y(:, 1) = 1:T;
% Y(:, 2) = exp(3 * (1:T) / T); % Exponential relation
% r1 = corrcoef(Y);
% r1 = r1(2) % Traditional Correlation
% r2 = weightedcorrs(Y, w);
% r2 = r2(2) % Weighted Correlation
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % g) A logarithm
% Y(:, 1) = 1:T;
% Y(:, 2) = log(1:T); % Logarithmic relation
% r1 = corrcoef(Y);
% r1 = r1(2) % Traditional Correlation
% r2 = weightedcorrs(Y, w);
% r2 = r2(2) % Weighted Correlation
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% % ======================================================================
% % EXAMPLE 1: verify some of the properties for weighted correlations.
% % ======================================================================
%
% % GENERATE CORRELATED STOCHASTIC PROCESSES
% N = 500; % number of variables
% T = 1000; % number of observations
% Y = randn(T, N); % shocks from standardized normal distribution
% lambda = 2;
% Y = Y + repmat(poissrnd(lambda, T, 1) .* (2 * rand(T, 1) - 1), 1, N); % shocks plus common factor
% Y = cumsum(Y); % correlated stochastic processes
%
% % CHOOSE WEIGHTS
% alpha = 2 / T;
% w0 = 1 / sum(exp(((1:T) - T) * alpha));
% w = w0 * exp(((1:T) - T) * alpha); % weights: exponential decay
%
% % COMPUTE CORRELATION MATRIX
% r1 = weightedcorrs(Y, w); % Weighted Correlation matrix for Y
%
% % COMPUTE CORRELATION MATRIX FOR MODIFIED DATA
% a = 1000 * (2 * rand - 1); % Multiplicative coefficient
% b = 1000 * (2 * rand - 1); % Shift
% r2 = weightedcorrs(a * Y + b, w); % Weighted Correlation matrix for modified Y
%
% % FIND RELEVANT INDEXES AND PLOT A SCATTER DIAGRAM
% I = ones(N);
% indexes = find(tril(I, -1)); % Indexes of lower triangular square matrix
%
% figure('Position', [50 50 950 630]);
% plot([-1:1], [-1:1], 'r', 'LineWidth', 10); % 45� Line
% hold on;
% plot(r1(indexes), r2(indexes), '*', 'MarkerSize', 6); % Comparison between the coefficients of the two Matrices
% title('Identical correlations for Y and a * Y + b', ... % title label for the plot
% 'FontSize', 20, 'FontWeight', 'Bold'); % ... font properties
% xlabel('Correlations for Y', 'FontSize', 16, 'FontWeight', 'Bold');
% ylabel('Correlations for modified Y = a * Y + b', ...
% 'FontSize', 16, 'FontWeight', 'Bold');
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
% % PLOT HISTOGRAM OF DIFFERENCES BETWEEN COEFFICIENTS
% % OF THE TWO CORRELATION MATRICES
% figure('Position', [50 50 950 630]);
% hist(r1(indexes) - r2(indexes), 100); % Histogram of the differences (which are always negligible)
% title('Identical correlations for Y and a * Y + b', ... % title label for the plot ...
% 'FontSize', 20, 'FontWeight', 'Bold'); % ... font properties
% xlabel('Negligible differences', 'FontSize', 16, 'FontWeight', 'Bold');
% ylabel('Absolute Frequencies', 'FontSize', 16, 'FontWeight', 'Bold');
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
% % VERIFY SYMMETRY
% all(all(r1 == r1'))
% all(all(r2 == r2'))
%
% % VERIFY POSITIVENESS FOR ALL EIGENVALUES
% all(eig(r1) >= 0)
% all(eig(r2) >= 0)
%
%
% % ======================================================================
% % EXAMPLE 2: the unit system of each column of Y is changed through
% % arbitrary affine transformations.
% % ======================================================================
%
% % GENERATE CORRELATED STOCHASTIC PROCESSES
% N = 500; % number of variables
% T = 1000; % number of observations
% Y = randn(T, N); % shocks from standardized normal distribution
% lambda = 2;
% Y = Y + repmat(poissrnd(lambda, T, 1) .* (2 * rand(T, 1) - 1), 1, N); % shocks plus common factor
% Y = cumsum(Y); % correlated stochastic processes
%
% % CHOOSE WEIGHTS
% alpha = 2 / T;
% w0 = 1 / sum(exp(((1:T) - T) * alpha));
% w = w0 * exp(((1:T) - T) * alpha); % weights: exponential decay
%
% % COMPUTE CORRELATION MATRIX
% r1 = weightedcorrs(Y, w); % Weighted Correlation matrix for Y
%
% % COMPUTE CORRELATION MATRIX FOR MODIFIED DATA
% a = 1000 * rand(1, N); % Multiplicative coefficients (positive)
% b = 1000 * (2 * rand(1, N) - 1); % Shifts
% r2 = weightedcorrs(repmat(a, T, 1) .* Y + repmat(b, T, 1), w); % Weighted Correlation matrix for modified Y
%
% % FIND RELEVANT INDEXES AND PLOT A SCATTER DIAGRAM
% I = ones(N);
% indexes = find(tril(I, -1)); % Indexes of lower triangular square matrix
%
% figure('Position', [50 50 950 630]);
% plot([-1:1], [-1:1], 'r', 'LineWidth', 10); % 45� Line
% hold on;
% plot(r1(indexes), r2(indexes), '*', 'MarkerSize', 6); % Comparison between the coefficients of the two Matrices
% str = 'Identical correlations after arbitrary affine transformations';
% title(str, 'FontSize', 20, 'FontWeight', 'Bold'); % title label for the plot
% xlabel('Correlations for Y', 'FontSize', 16, 'FontWeight', 'Bold');
% ylabel('Correlations for modified Y', ...
% 'FontSize', 16, 'FontWeight', 'Bold');
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
% % PLOT HISTOGRAM OF DIFFERENCES BETWEEN COEFFICIENTS
% % OF THE TWO CORRELATION MATRICES
% figure('Position', [50 50 950 630]);
% hist(r1(indexes) - r2(indexes), 100); % Histogram of the differences (which are always negligible)
% title('Identical correlations for Y and modified Y', ... % title label for the plot
% 'FontSize', 20, 'FontWeight', 'Bold'); % ... font properties
% xlabel('Negligible differences', 'FontSize', 16, 'FontWeight', 'Bold');
% ylabel('Absolute Frequencies', 'FontSize', 16, 'FontWeight', 'Bold');
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
%
% % ======================================================================
% % Example 3: differences with respect to traditional correlations.
% % ======================================================================
%
% % GENERATE CORRELATED STOCHASTIC PROCESSES
% N = 500; % number of variables
% T = 1000; % number of observations
% Y = randn(T, N); % shocks from standardized normal distribution
% lambda = 2;
% Y = Y + repmat(poissrnd(lambda, T, 1) .* (2 * rand(T, 1) - 1), 1, N); % shocks plus common factor
% Y = cumsum(Y); % correlated stochastic processes
%
% % CHOOSE WEIGHTS AND PLOT THEM
% alpha = 2 / T;
% w0 = 1 / sum(exp(((1:T) - T) * alpha));
% w = w0 * exp(((1:T) - T) * alpha); % weights: exponential decay
%
% figure('Position', [50 50 950 630]);
% plot([floor(-T / 2):ceil(T + T / 2)], ... % x values ...
% w0 * exp(((floor(-T / 2):ceil(T + T / 2)) - T) * alpha), ... % ... y values ...
% '.r', 'MarkerSize', 7); % ... plot properties
% hold on;
% plot(w, '.', 'MarkerSize', 30);
% title('Exponential weights assigned to observations', ... % title label for the plot
% 'FontSize', 20, 'FontWeight', 'Bold'); % ... font properties
% xlabel('Observations', 'FontSize', 16, 'FontWeight', 'Bold');
% ylabel('Weights', 'FontSize', 16, 'FontWeight', 'Bold');
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
% % COMPUTE CORRELATION MATRICES
% r1 = weightedcorrs(Y, w); % Weighted Correlation matrix
% r2 = corrcoef(Y); % Traditional Correlation matrix
%
% % FIND RELEVANT INDEXES AND PLOT A SCATTER DIAGRAM
% I = ones(N);
% indexes = find(tril(I, -1)); % Indexes of lower triangular square matrix
%
% figure('Position', [50 50 950 630]);
% plot(r1(indexes), r2(indexes), '.'); % Comparison with the Traditional Correlation matrix
% hold on;
% plot([-1:1], [-1:1], 'r', 'LineWidth', 5); % 45� Line
% title('Scatter Diagram for Traditional and Weighted Correlations', ... % title label for the plot ...
% 'FontSize', 20, 'FontWeight', 'Bold'); % ... font properties
% xlabel('Weighted Correlation coefficients', ... % x label ...
% 'FontSize', 16, 'FontWeight', 'Bold'); % ... font properties
% ylabel('Traditional Correlation coefficients', ... % y label ...
% 'FontSize', 16, 'FontWeight', 'Bold'); % ... font properties
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
% % PLOT HISTOGRAM OF DIFFERENCES BETWEEN COEFFICIENTS
% % OF THE TWO CORRELATION MATRICES
% figure('Position', [50 50 950 630]);
% hist(r2(indexes) - r1(indexes), 100); % Differences between the two correlation matrices
% title('Differences between Traditional and Weighted Correlations', ... % title label for the plot ...
% 'FontSize', 20, 'FontWeight', 'Bold'); % ... font properties
% ylabel('Absolute Frequencies', 'FontSize', 16, 'FontWeight', 'Bold');
% xlabel('Differences between Traditional and Weighted Correlations', ... % x label ...
% 'FontSize', 16, 'FontWeight', 'Bold'); % ... font properties
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
%
% % ======================================================================
% % Example 4: If weights are uniform, then the traditional correlation
% % matrix is obtained.
% % ======================================================================
%
% % GENERATE CORRELATED STOCHASTIC PROCESSES
% N = 500; % number of variables
% T = 1000; % number of observations
% Y = randn(T, N); % shocks from standardized normal distribution
% lambda = 2;
% Y = Y + repmat(poissrnd(lambda, T, 1) .* (2 * rand(T, 1) - 1), 1, N); % shocks plus common factor
% Y = cumsum(Y); % correlated stochastic processes
%
% % CHOOSE WEIGHTS AND PLOT THEM
% w = ones(T, 1) / T; % weights: uniform
%
% figure('Position', [50 50 950 630]);
% plot(w, '*', 'MarkerSize', 7);
% title('Uniform Weights assigned to observations', ... % title label for the plot ...
% 'FontSize', 20, 'FontWeight', 'Bold'); % ... font properties
% xlabel('Observations', 'FontSize', 16, 'FontWeight', 'Bold');
% ylabel('Weights', 'FontSize', 16, 'FontWeight', 'Bold');
% axis([0 T 0 2 * w(1)]);
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
% % COMPUTE CORRELATION MATRICES
% r1 = weightedcorrs(Y, w); % Weighted Correlation matrix
% r2 = corrcoef(Y); % Traditional Correlation matrix
%
% % FIND RELEVANT INDEXES AND PLOT A SCATTER DIAGRAM
% I = ones(N);
% indexes = find(tril(I, -1)); % Indexes of lower triangular square matrix
%
% figure('Position', [50 50 950 630]);
% plot([-1:1], [-1:1], 'r', 'LineWidth', 10); % 45� Line
% hold on;
% plot(r1(indexes), r2(indexes), '*', 'MarkerSize', 6); % Comparison with the Traditional Correlation matrix
% title('Scatter Diagram for Traditional and Weighted Correlations', ... % title label for the plot ...
% 'FontSize', 20, 'FontWeight', 'Bold'); % ... font properties
% xlabel('Weighted Correlation coefficients (uniform weights!!!)', ... % x label ...
% 'FontSize', 16, 'FontWeight', 'Bold'); % ... font properties
% ylabel('Traditional Correlation coefficients', ... % y label ...
% 'FontSize', 16, 'FontWeight', 'Bold'); % ... font properties
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
% % PLOT HISTOGRAM OF DIFFERENCES BETWEEN COEFFICIENTS
% % OF THE TWO CORRELATION MATRICES
% figure('Position', [50 50 950 630]);
% hist(r2(indexes) - r1(indexes), 100); % Difference between the two correlation matrices
% temp = 'Differences between Traditional and Weighted ';
% temp(2, :) = 'Correlation coefficients (uniform weights!!!)';
% title(temp, 'FontSize', 20, 'FontWeight', 'Bold');
% ylabel('Absolute Frequencies', 'FontSize', 16, 'FontWeight', 'Bold');
% xlabel(temp, 'FontSize', 16, 'FontWeight', 'Bold');
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
%
% % ======================================================================
% % Example 5: more reliable dynamic correlations (it may take some mins):
% % sensitive with respect to recent shocks, not to remote ones!
% % ======================================================================
%
% % GENERATE CORRELATED STOCHASTIC PROCESSES
% N = 500; % number of variables
% T = 1000; % number of observations
% Y = randn(T, N); % shocks from standardized normal distribution
% lambda = 2;
% Y = Y + repmat(poissrnd(lambda, T, 1) .* (2 * rand(T, 1) - 1), 1, N); % shocks plus common factor
% Y = cumsum(Y); % correlated stochastic processes
%
% % CHOOSE WEIGHTS
% delta = 100; % Running window
% alpha = 2 / delta;
% w0 = 1 / sum(exp(((1:delta) - delta) * alpha));
% w = w0 * exp(((1:delta) - delta) * alpha); % weights: exponential decay
%
% % FIND RELEVANT INDEXES AND PLOT A SCATTER DIAGRAM
% I = ones(N);
% indexes = find(tril(I, -1)); % indexes of lower triangular square matrix
%
% % COMPUTE DYNAMIC CORRELATION MATRICES
% for i = 1:(T - delta + 1)
% temp = weightedcorrs(Y(i:(delta + i - 1), :), w); % Dynamic Weighted Correlation matrix
% r1(i) = mean(temp(indexes)); % consider only the lower matrix, in vectorial form, compute the mean
% temp = corrcoef(Y(i:(delta + i - 1), :)); % Dynamic Traditional Correlation matrix
% r2(i) = mean(temp(indexes)); % consider only the lower matrix, in vectorial form, compute the mean
% end
%
% % PLOT THE AVERAGE CORRELATIONS, BOTH WEIGHTED AND TRADITIONAL
% figure('Position', [50 50 950 630]);
% plot(r2, '.g', 'MarkerSize', 24); % the red curve is less sensitive with respect
% hold on; % to remote shocks at time (t - delta + 1) and more
% plot(r1, '.r', 'MarkerSize', 18); % sensitive with respect to present shocks at time t
% title('Moving Average Correlations', ... % title label for the plot ...
% 'FontSize', 20, 'FontWeight', 'Bold'); % ... font properties
% xlabel('Time', 'FontSize', 16, 'FontWeight', 'Bold');
% temp = ' Moving Average Correlations ';
% temp(2, :) = '(green --> traditional; red --> weighted)';
% ylabel(temp, 'FontSize', 16, 'FontWeight', 'Bold');
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
%
% % ======================================================================
% % Example 6: Bell-shaped weights centered on single observations
% % ======================================================================
%
% % GENERATE CORRELATED STOCHASTIC PROCESSES
% N = 2; % number of variables
% T = 1000; % number of observations
% Y = randn(T, N); % shocks from standardized normal distribution
% lambda = 2;
% Y = Y + repmat(poissrnd(lambda, T, 1) .* (2 * rand(T, 1) - 1), 1, N); % shocks plus common factor
% Y = cumsum(Y); % correlated stochastic processes
% alpha = 16 / T / T;
%
% % PLOT BELL-SHAPED WEIGHTS IN THREE CASES: AT THE BEGINNING,
% % IN THE MIDDLE AND AT THE END OF THE PERIOD
% figure('Position', [50 50 950 630]);
% i = 1;
% w = exp(-alpha * ((1:T) - i) .^ 2);
% w = w / sum(w);
% plot(w, 'g', 'LineWidth', 7);
% hold on;
% i = floor(T / 2);
% w = exp(-alpha * ((1:T) - i) .^ 2);
% w = w / sum(w);
% plot(w, 'r', 'LineWidth', 7);
% i = T;
% w = exp(-alpha * ((1:T) - i) .^ 2);
% w = w / sum(w);
% plot(w, 'b', 'LineWidth', 7);
% title('Weights assigned to observations', ... % title label for the plot ...
% 'FontSize', 20, 'FontWeight', 'Bold'); % ... font properties
% xlabel('Observations', 'FontSize', 16, 'FontWeight', 'Bold');
% ylabel('Weights', 'FontSize', 16, 'FontWeight', 'Bold');
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
% legend('Weights centered on t = 1', ... % Legend: first item ...
% sprintf('Weights centered on t = %d', floor(T / 2)), ... % ... second item ...
% sprintf('Weights centered on t = %d', T), ... % ... third item ...
% 'Location', 'North'); % Legend Location
%
% % COMPUTE CORRELATIONS FOR EACH POSSIBLE CENTRAL OBSERVATION
% for i = 1:T
% w = exp(-alpha * ((1:T) - i) .^ 2); % weights: exponential decay
% w = w / sum(w);
% temp = weightedcorrs(Y, w); % Weighted Correlation matrix
% r(i) = temp(2);
% end
% temp = corrcoef(Y);
% rr = temp(2);
%
% % PLOT CENTERED CORRELATION FOR EACH POSSIBLE CENTRAL OBSERVATION
% figure('Position', [50 50 950 630]);
% plot([1, T], [rr, rr], 'b', 'LineWidth', 10);
% hold on;
% plot(r, '.r', 'MarkerSize', 24);
% title('Correlations with bell-shaped weights', ... % title label for the plot ...
% 'FontSize', 20, 'FontWeight', 'Bold'); % ... font properties
% xlabel('Central Observation', 'FontSize', 16, 'FontWeight', 'Bold');
% ylabel([' Correlations with bell-shaped weights '; ... % y label, first line ...
% 'centered on different Central Observations'], ... % y label, second line ...
% 'FontSize', 16, 'FontWeight', 'Bold'); % ... font properties
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
% % PLOT HISTOGRAM OF CENTERED CORRELATIONS COMPUTED
% % FOR EACH POSSIBLE CENTRAL OBSERVATION
% figure('Position', [50 50 950 630]);
% hist(r, T / 20);
% title([' Histogram of the correlation computed with '; ... % title label, first line ...
% 'bell-shaped weights on different Central Observations'], ... % title label, second line ...
% 'FontSize', 20, 'FontWeight', 'Bold'); % ... font properties
% ylabel('Absolute Frequencies', 'FontSize', 16, 'FontWeight', 'Bold');
% xlabel('Correlation', 'FontSize', 16, 'FontWeight', 'Bold');
% set(gca, 'FontSize', 14, 'FontWeight', 'Bold');
% grid on;
%
%
% % ======================================================================
%
% See also CORRCOEF, COV, STD, MEAN.
%
% % ======================================================================
%
%-*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-*%
% %
% Author: Liber Eleutherios %
% E-Mail: [email protected] %
% Date: 23 July 2008 %
% Updated: 6 June 2012 %
% %
%-*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-* -*-*%
%
% % ======================================================================
%
% Check input
ctrl = isvector(w) & isreal(w) & ~any(isnan(w)) & ~any(isinf(w)) & all(w >= 0) & (sum(w) > 0);
if ctrl
w = w(:) / sum(w); % w is column vector
else
error('Check w: it needs be a vector of real non-negative numbers with no infinite or nan values!')
end
ctrl = isreal(Y) & ~any(isnan(Y)) & ~any(isinf(Y)) & (size(size(Y), 2) == 2);
if ~ctrl
error('Check Y: it needs be a 2D matrix of real numbers with no infinite or nan values!')
end
ctrl = length(w) == size(Y, 1);
if ~ctrl
error('size(Y, 1) has to be equal to length(w)!')
end
[T, N] = size(Y); % T: number of observations; N: number of variables
temp = Y - repmat(w' * Y, T, 1); % Remove mean (which is, also, weighted)
temp = temp' * (temp .* repmat(w, 1, N)); % Covariance Matrix (which is weighted)
temp = 0.5 * (temp + temp'); % Must be exactly symmetric
R = diag(temp); % Variances
R = temp ./ sqrt(R * R'); % Matrix of Weighted Correlation Coefficients