How to create a function to calculate the correlation / correlation matrix using J?

I already wrote the following code on my own, which step by step calculates the correlation / correlation matrix:

a=: 1 2 3
b=: 2 3 4
getmean=: +/%#
getmdevn=: -getmean
getvariance1=: (getmean@:*:)@getmdevn

getvariance1 a
getvariance1 b
corr_a_b=: getmean (a*b) - getmean a * getmean b

What is the best way to calculate the correlation / correlation matrix in J using a single function? Or is there a way to combine all my code into one function?

PS I just found several libraries, such as the "numerical" ones in J. However, there seems to be no documentation of these libraries on the Internet. Does anyone know where to find the details of these libraries?

+4
source share
2 answers

@EelVex , , J, " ", JSoftware.

, , , 2013 , J:

  • fork f g h
  • hook f g
  • atop f@:g
  • f&g
  • f&.:g

&, . , . !

, J, , , : - (+/@:*) - (*&(+/)).

- ( %!).

   c=:+/@:* % *&(+/)

   5!:4<'c'
         +- / --- +
  +- @: -+- *      
  +- %             
--+      +- *      
  +- & --+- / --- +

. , , , xs ys x̄s ȳs, , corr , , , , - , , ..

J-, , jem:

Cr=: +/@:*&(% +/&.:*:)&(- +/ % #)

2013 , - - , : , - , /.

sum             =:  +/         
of              =:  @          
the_product     =:  *          
after           =:  &          
scaling         =:  % +/&.:*:
after           =:  &          
standardizing   =:  - +/%#


corr =: sum of the_product after scaling after standardizing

, !

, , @EelVex. , :

, , , - , strong > .

:

, :

   CR=: (+/@:* % *&(+/)&.:*:)&(- +/ % #)

  0j20":CR/900000000(+,:-)1+i.1000000
_1.00000000000000000000

  COR f.
(+/ % #)@:*&(- (+/ % #)) % *&(%:@(+/ % #)@:*:@:- (+/ % #))

  0j20":COR/900000000(+,:-)1+i.1000000
_1.00000000000000000000

  0j20":c/900000000(+,:-)1+i.1000000
1.00000229430253350000

  load'stats'
  0j20":corr/900000000(+,:-)1+i.1000000
_0.99999999999619615000

. , , 2007 :

J-, .

.

+8

, 'stats'.

load'stats'
varp a               NB. variance of population a
  0.666667
a covp b             NB. covariance of a/b
  0.666667

Look at the statistics library

I would write yours corr_a_bas a binary function:

 corr_a_b =: ((getmean @: *) - (* &: getmean))
+7
source

Source: https://habr.com/ru/post/1680329/


All Articles