correlation using specific values of the table
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Hiro Yoshino
le 10 Sep 2022
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)])
0 commentaires
Voir également
Catégories
En savoir plus sur Data Preprocessing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!