I always like the version that comes in the standard J stats library:

  corr
cov % *&stddev
  cov
spdev % <:@[EMAIL PROTECTED]
  spdev
+/@(*~ dev)
  dev
-"_1 _ mean
  mean
+/ % #
  stddev
%:@var
  var
ssdev % <:@#
  ssdev
+/@:*:@dev



On 6/26/07, Pablo Landherr <[EMAIL PROTECTED]> wrote:

Going for educational value rather than terseness, here is an alternative:

   corr=:     [EMAIL PROTECTED]        NB. correlation is the crossproduct of
standardised values
   cprod=:    (+/ .*)~ |:           NB. crossproduct
   standard=: [EMAIL PROTECTED]         NB. standardised numbers are
'adjusted'
and 'centered'
   adjust=:   ] %"1 [: sqroot sqsum NB. adjusted numbers have a variance
of
1
   center=:   -"1 avg               NB. centered numbers have a mean of 0
   avg=:      +/ % #                NB. artitmetic mean
   sqroot=:   %:                    NB. square root
   sqsum=:    +/@:*:                NB. sum of squared

   corr 1 3 2 4,"(0) 2 4 3 3.5
       1 0.831522
0.831522        1


On 6/26/07, June Kim <[EMAIL PROTECTED]> wrote:
>
> Thank you, Henry.
>
> While I try to translate a mathematical expression into a J
> expression, I often discover hidden patterns in the expression with
> great joy. It sometimes leads me to a path to new insights.
>
> It's the reason I am looking for the shortest form this time.
>
> 2007/6/26, Henry Rich <[EMAIL PROTECTED]>:
> > Not really, but gee, shouldn't we be using our wonderful
> > language to provide the most expressive form of c rather
> > than just the shortest?
> >
> >   a =. (# * +/)@:* - *&(+/)
> >   c =. a % [EMAIL PROTECTED] %:@* [EMAIL PROTECTED]
> >   1 3 2 4 c 2 4 3 3.5
> > 0.831522
> >
> >   a =. ((# * +/)@:* - *&(+/)) "1
> >   c =. %: @ (%~/ . *) @: (a/~) @: ,:
> >   1 3 2 4 c 2 4 3 3.5
> > 0.831522
> >
> > I don't think either of these forms is useful for real work,
> > since they involve subtracting big near-equal numbers, but
> > I learned something about the computation by writing them.
> >
> > Henry Rich
> >
> > > -----Original Message-----
> > > From: [EMAIL PROTECTED]
> > > [mailto:[EMAIL PROTECTED] On Behalf Of June Kim
> > > Sent: Tuesday, June 26, 2007 10:08 AM
> > > To: Programming forum
> > > Subject: [Jprogramming] Correlation Coefficient
> > >
> > > I've been playing with the expression at
> > > http://en.wikipedia.org/wiki/Correlation
> > >
> > >
> > >    c=.(*&[EMAIL PROTECTED]@:*)%*&(s(-~(#*s=.+/))&.:*:])
> > >    1 3 2 4 c 2 4 3 3.5
> > > 0.831522
> > >
> > > c is as short as I can get. Could you come up with a verb for
> > > correlation, shorter than c?
> > >
----------------------------------------------------------------------
> > > For information about J forums see
> > > http://www.jsoftware.com/forums.htm
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm




--
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to