MATLAB Answers

MOVING CORRELATIONs AND PLOT in a TIME SERIES FORMAT

15 views (last 30 days)
Marco
Marco on 27 May 2011
Answered: Samantha Cruz on 19 Jun 2020
Hi there, I need to construct a function that is able to plot moving correlations among each pairs in a subplot format (e.g. in case of 3 assets I want 3*3 matrix and each plot contain the time series of moving correlation with a specified window)
To be cleared I provide you my code at the moment, I am struggling with the plot part, if you believe there's an efficient way to code the first part of the function let me know:
% Moving correlation
%
% INPUT:
% - dataMatrix(r*c)
% - windowSize N
% - display matrix graph (1=graph, 0= no graph)
%
% OUTPUT:
% - correlationTV(r-N*c)
%
% e.g. 3 series: 1 with 2 and 2 with 3 and then 1 with 3
% e.g. 4 series: 1 with 2 and 2 with 3 and 3 with 4 and then
%
dataMatrix=rand(100,4);
windowSize=30;
graph=0
% function correlationTV = movingCorrelation(dataMatrix, windowSize, graph)
%% Dimension
[r,c] = size(dataMatrix);
% %% Some arg checking % % if nargin==1 % default: no garch effect & no multi start solver & no graph % windowSize=size(dataMatrix,1); % graph=0; % else % nargin==2 % graph=0; % end % % if windowSize>r && windowSize<2 % error('windowSize must smaller than number of raws and at least equal to 2'); % end % % if graph~=0 && graph~=1 % error('Insert 0 or 1 for graph'); % end
%% Prealocation
correlationTV = cell(r,1);
for i=1:r correlationTV{i,1}=nan(c,c); end
%% Code
count=windowSize;
for t = windowSize+1:r correlationTV{count,1} = corrcoef(dataMatrix(t-windowSize:t, :)); count=count+1; end
if graph==1 for i=1:c for j=1:c for l=windowSize:r end subplot(c,c,
subplot(i,j);
for i=windowSize:r
subplot
Thanks Marco

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by