How to plot cross quantile correltaion heatmap

How do I plot similar cross quantile correlation plot

1 Comment

Umar
Umar on 23 Jul 2024
Edited: Umar on 23 Jul 2024

Hi Hamed,

Since you did not provide the data, the plot cannot be generated. However, to perform cross-quantile correlation analysis, you can follow my provided example code snippet. It generates random data for two variables, calculates quantiles, computes the correlation between quantiles, and visualizes the correlation matrix as a heatmap. By executing this code, you can analyze the relationship between the quantiles of two variables visually through the heatmap, providing insights into their cross-quantile correlation patterns. Please modify the code based on your preferences.

% Generate random data

data1 = randn(100, 1); % Random data for variable 1

data2 = randn(100, 1); % Random data for variable 2

% Calculate quantiles

quantiles1 = quantile(data1, [0.25, 0.5, 0.75]); % Quantiles for variable 1

quantiles2 = quantile(data2, [0.25, 0.5, 0.75]); % Quantiles for variable 2

% Calculate cross-quantile correlation

corr_matrix = zeros(3, 3); % Initialize correlation matrix

for i = 1:2 % Adjusted loop range to prevent index exceeding for j = 1:2 % Adjusted loop range to prevent index exceeding corr_matrix(i, j) = corr(data1(data1 >= quantiles1(i) & data1 <= quantiles1(i+1)), ...

            data2(data2 >= quantiles2(j) & data2 <= quantiles2(j+1)));
    end
end

% Plot the heatmap

heatmap(corr_matrix, 'XData', {'Q1', 'Q2', 'Q3'}, 'YData', {'Q1', 'Q2', 'Q3'});

title('Cross-Quantile Correlation Heatmap');

xlabel('Variable 1 Quantiles');

ylabel('Variable 2 Quantiles');

For more information on function heatmap, please refer to

https://www.mathworks.com/help/matlab/ref/heatmap.html

Please see attached plot.

Sign in to comment.

Answers (1)

Some data:
C=(0.71*rand(21,21)).^2;
Plot the data
x=0:.05:1; y=x;
[X,Y]=meshgrid(x,y);
surf(X,Y,C)
colormap hot; colorbar; axis equal; view(0,90)
xlabel('Tax Revenue Quantile'); ylabel('Energy Generation Quantile')
OK

19 Comments

If you need to compute the cross-quantilogram, use this package
The documentation says that it produces plots.
Thanks so much, I have the systematic risk toolbox but I haven't used it because I don't seems to understand its usage. The readme file isn't really explanatory and I found some of the functions confusing.
Hi Hamid, Please let us know if we answered your question, “ How to plot cross quantile correltaion heatmap”, or do you have any further questions
The above plot answered my question. However, I still have difficulty in generating the quantile correlation matrix...
Hi Hamid,
Could you please share your code with us, so we can help you figure out.
I have a data called data.
data= xlsread('data.xlsx,1,'B2:F300'); a=0.05:0.05:0.95; k=10; Extracting the needed 2 variables first X1 = data(:,2); X2 = data(:,3); Combined = [X1,X2];
%Using the systemic risk toolbox [cq,ci] = cross_quantilograms_sb(combined, a,k); But I got an error that input should be in struct. When I tried to create a structure, giving it an s, I got another error saying the data should be in double and not in struct. % my structure data ='data'; values = combined; a = 'a'; values2 = a; k= 'k'; values3= k;
s = struct('data',values, 'a', a,'k',k);
%where s is the structure
[cq,ci] = cross_quantilograms_sb(s);
Umar
Umar on 24 Jul 2024
Moved: Voss on 24 Jul 2024
Hi Hamid,
When working with the cross_quantilograms_sb function in Matlab and encountering errors related to input data format, it is crucial to ensure that the data is appropriately structured and passed to the function. Let's delve into the detailed troubleshooting steps to address the issues you are facing.
Structuring the Input Data Correctly
In your code snippet, you attempted to create a struct s to hold the data, quantile levels a, and parameter k. However, the way you assigned values to the struct fields might be causing the type mismatch issues. To correctly structure the input data, you should assign the actual values to the struct fields instead of assigning strings representing the variable names. Here is how you can structure the input data correctly:
% Create a struct with the required fields and actual data
s.data = combined;
s.a = a;
s.k = k;
Passing the Structured Data to the Function
Once you have correctly structured the input data in the s struct, you can pass this struct to the cross_quantilograms_sb function for analysis. Ensure that you pass the struct itself as the input parameter to the function.Here is how you can call the function with the structured data:
[cq, ci] = cross_quantilograms_sb(s);
By following these steps, you should be able to resolve the errors related to input data type mismatch and successfully utilize the cross_quantilograms_sb function with the structured input data. This is what your final modified code should look like
% Load data from Excel file
data = xlsread('data.xlsx', 1, 'B2:F300');
X1 = data(:, 2);
X2 = data(:, 3);
combined = [X1, X2];
% Define parameters
a = 0.05:0.05:0.95;
k = 10;
% Create a structure for input data
s.data = combined;
s.a = a;
s.k = k;
% Perform cross quantilograms analysis
[cq, ci] = cross_quantilograms_sb(s);
Remember to verify that the dimensions and types of the data in the struct align with the function's requirements to avoid any further issues.If you encounter any additional errors or need further assistance, feel free to provide more details for a more tailored solution. Happy coding!
After doing this, I still got the same error . The value data is invalid. Expected input to be one of these types: double instead it's type was struct.
Umar
Umar on 24 Jul 2024
Moved: Voss on 24 Jul 2024
Hi Hamid,
It’s time to debugging, follow these instructions and let me know what results are you getting.
Verify the data loaded from the Excel file is stored as a double array, use the class function to check the data type:
disp(class(data));
Ensure that X1, X2, and combined are all double arrays.
Check how the structure s is created and ensure that the fields data, a, and k are assigned correctly:
disp(s);
Before calling cross_quantilograms_sb, validate the structure s and its fields:
disp(s.data);
disp(s.a);
disp(s.k);
In addition to the excellent advice from @Umar, you may also find help by posting your questions on the Discussion forum for the Systemic Risk package, at the File Exchange. Go to https://www.mathworks.com/matlabcentral/fileexchange/62482-systemic-risk and click the tab for Discussions. See if your questions are already answered, or post a new question.
[edit: correct spelling mistake]
I downloaded and installed the Systemic Risk package, v.3.6.0, from the File Exchange.
I made a three-column file with dates and closing stock prices for Starbucks and Apple for 1 year.
data=xlsread('sbux_aapl_stockprice.xlsx','b2:c253');
Since the price data is in reverse chronological order, flip it:
data=flipud(data);
Compute cross quantilogram value for the two time series, at quantile a=0.05 and lag k=10:
a=0.05; k=10;
>> [cq,ci]=cross_quantilograms_sb(data,a,k)
cq =
0.4311
ci =
-0.3291 0.1509
Function ..._sb(), above, estimates the confidence interval with stationary bootstrap. Function ..._sn(), below, estimates the confidence interval with self-normalization. The cq value is the same with both functions. The CI is larger with sn.
>> [cq,ci]=cross_quantilograms_sn(data,a,k)
cq =
0.4311
ci =
-1.2268 1.2268
Both functions work. But you want a whole matrix of cross-quantilogram values, for quantiles from 0.05 to 0.95. These functions in this package only allow a to be a scalar, not a vector, and a must be in the range [0.01,0.10], as you will see, if you try to use values for a that are outside this range.
[Edited paragraphs 1 and 2 for clarity.]
I skimmed the original cross-quantilogram paper by Han et al., 2016, here. It does not include any figures like the figure in your original post. In other words, it does not include a figure that is a 2-D array of values for quantile combinations from 0.05 to 0.95. Where did you find the figure in your original post?
After reading Han et al. 2016, I wondered if the 2-D array of values is the partial cross quantilogram, described in Section 4 of their paper. The example partial cross quantilogram (Figure 5 in their paper) does not look like the 2D array which you posted. The comments for cross_quantilograms_sb() and for cross_quantilograms_sn() say that the partial cross quantilogram is computed, if there are more than 2 time series in the data array. Therefore I made a new spreadsheet with three stock prices (Starbucks, Apple, Microsoft, attached) and passed it as the data array. In this case, a partial cross quantilogram is returned for the first two columns. The partial cross quantilogram removes the effects of the third column on columns 1 and 2. Even so, cross_quantilograms_sb() returned only a single value, not an array of values. The partial cross quantilogram value for Starbucks and Apple, after accountring for Microsoft, is slightly smaller than the cross quantilogram between Starbucks and Apple. This is consistent with correlation versus partial correlation, and consistsent with coherence versus partial coherence.
data=xlsread('sbux_aapl_msft_stockprice.xlsx','b2:d253');
data=flipud(data);
a=0.05; k=10;
>> [cq,ci]=cross_quantilograms_sb(data,a,k)
cq =
0.4293
ci =
-0.3410 0.1630
I also skimmed the paper "Partial cross-quantilogram networks: Measuring quantile connectedness of financial institutions" by Qian et al, 2022. It too does not include any figures like the 2D array in your original post.
Thanks a lot for the help. There is a cross Quantilogram package in gretl that can plot similar heatmap. You can check "The qcorr package: a cross-quantilogram analisys tool in gretl" by Luca Pedini.
@Umar has kindly provided code, above, to estimate correlations for the heatmap.
Thanks everyone.. I really appreciate the help
No problem, Hamid. Glad to help out. Please let us know if you have any further questions.
@Hamid Muili, you're welcome.
@Umar, thank you for your code idea, above, to estimate the cross quantilogram.
I posted a function on the File Exchange to compute the cross quantilogram matrix, here. It uses a modified version of Umar's idea. I also included a function to plot the cross quantilogram matrix, and an example script demonstrating the use of the functions, and a PDF file with a longer description and more examples.
Wow.. This is awesome. Thanks so much for the quick response. I so much loved this. Wonder if you can help make similar toolbox for dynamic panel fixed effect quantile regression with instrumental variable by Galvao(2009,2011) at your leisure time. Although, I actually have the R code but it only compute the estimate without, t statistics, p values and other test statistic to access the significance of the coefficients. I believe these can be bootstrapped to generate these test statistic.
@Hamid Muili, I looked at Galvao 2011., which has the title to which you referred. I also looked at this 2009 paper by Galvao. I'm afraid I will not be able to help you compute t statistics and p values for the estimates in the 2011 paper. Good luck with your research.

Sign in to comment.

Categories

Asked:

on 23 Jul 2024

Commented:

on 25 Jul 2024

Community Treasure Hunt

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

Start Hunting!