shc443 / Converting-Python-to-J

Converting mathematics and statistics equations from python code to J language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Converting-Python-to-J

J is so simple and consistent, albeit hard to learn.

From "Partial correlation analysis: applications for financial markets" by Professor Kenett (2014)
The Partial Correlation eqaution can be wrttien in Python as:

cor = cor.values # n x m matrix: row = Date, col = Stocks 
pcor = np.zeros((cor.shape[0],cor.shape[0],cor.shape[0]))
for x in range(len(cor)):
    for y in range(len(cor)):
        for z in range(len(cor)):
            pcor[z,y,x] = (cor[x,y]-cor[x,z]*cor[y,z])/np.sqrt((1-cor[x,z]**2)*(1-cor[y,z]**2))

This can be written in J as:

pcor =: 3 : '((-"2 (*"0 1 ~ )) ~ y) %"2 (%: (*"0 1 ~ ) (1-*: y))' 

About

Converting mathematics and statistics equations from python code to J language