vendredi 31 mai 2019

Conditional correlations using an IF statement - Python

I am trying to conduct an IF statement in order to evaluate conditional threshold correlations. The equation is as follows:

Threshold Correlation

I tried doing an IF statement, but it doesn't work. I kind of managed to do it with pandas, but it would be really a bad code.

p = np.arange(0.1,1,0.1)

r1 = log_r['AEX']; r2 = log_r['MBI10']
np.quantile(r1, p[0])

corrcoef = pd.Series()
if r1<np.quantile(r1,p[0]) & r2<np.quantile(r2,p[0]) & p[0]<0.5:
    corrcoef[0] = np.corrcoef(r1,r2) 

Alternatively, I did the following:

df = pd.DataFrame(pd.concat([log_r['AEX'],log_r['MBI10']],axis=1))
df['p0.1 AEX'] = (df['AEX'] < np.quantile(df['AEX'], p[0]))*df['AEX']    
df['p0.1 MBI10'] = (df['MBI10'] < np.quantile(df['MBI10'], p[0]))*df['MBI10']
np.corrcoef(df['p0.1 AEX'],df['p0.1 MBI10'])

This works, however it would be really messy since I would need to do this for not only p[0] but for the entire np.arange and also AEX and MBI10 are only 2/36 pairs that I have, so I am really looking for a more elegant solution to this. Thanks!

I am trying to get a correlation coefficient populated in the first row of corrcoef. I am getting the following error when I try to do the for loop:

TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]

Aucun commentaire:

Enregistrer un commentaire