correlation using specific values of the table
Show older comments
Hello, im trying to calculate the correlations of 3 different stocks and an index. I got the code up to here
clc
clear all
formatSpec = '%s %f %f %f %f';
temp_dat = readtable('Prices4.csv','Format',formatSpec,'ReadVariableNames',true);
% Extract pirices from table
price = table2array(temp_dat(:,2:end));
%Returns
rets = log(price(2:end,:)./price(1:end-1,:));
I want to Find the correlation matrix between the stocks on days when index returns are positive, and the correlation matrix when index returns are below the 10th percentile.
I dont know how to tell matlab to only take in to account those specifc values .
3 Comments
dpb
on 9 Sep 2022
What is the first column in the .csv file -- a date string, probably? If so, I'd suggest a timetable could be the thing here, and it's unlikely would need the input format at all.
Then, use the table itself once you've got it instead of duplicating the same data into another array -- that'll have the secondary benefit of keeping the stock IDs with them that you carefully read in to begin with instead of throwing them away.
Then use logical addressing to select the data of interest (and, if use a timetable, retime) or rowfun to apply the calculation. You can create the grouping variable from the index returns and use groupsummary as well.
Per usual, it would make illustrating with real code simpler if you would attach a representative sample of the input file so folks have something to work with without having to also make up data...
Marlon
on 10 Sep 2022
Marlon
on 10 Sep 2022
Answers (1)
Here is my idea.
I reccomend you should use useful functions such as price2ret, corrplot (corr), prctile and the functionality of timetable as follows:
Sample data generation
t = datetime(2022,8,1:31,12,0,0)';
price = timetable(t,rand(31,1),rand(31,1))
Return calculation
tmp = price2ret(price);
price_to_return = tmp(:,["Var1","Var2"])
[1] correlation between positive returns
find the data indices that satisfy the condition (positive return) to work out the correlation.
pos_idx = (price_to_return.Var1 > 0) & (price_to_return.Var2 > 0);
[R,pValue] = corrplot(price_to_return(pos_idx,:))
[2] ccorrelation between returns under 10th percentile
first, calculation the 10 percentile values for Var1 and Var2 respectively:
prct10 = prctile(price_to_return.Variables,10)
find the data indices that meet the condition ( < 10 percentile)
Var1_idx = price_to_return.Var1 < prct10(1);
Var2_idx = price_to_return.Var2 < prct10(2);
calculate the correlation:
[R2,pValue2] = corrplot([price_to_return.Var1(Var1_idx),price_to_return.Var2(Var2_idx)])
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

