correlation.frink

View or download correlation.frink in plain text format


/** This library calculates the correlation between items in lists. */

/** This calculates the autocorrelation of a single numeric list, that is, it
    identifies the periods at which sequences appear to repeat.  The result
    is a sorted list containing pairs of [offset, correlation] with the terms
    of strongest correlation being first in the list.

    If you just want a single most likely autcorrelation period, it is found at
       result@0@0

    see correlationTest.frink for an example of using it.
*/

autocorrelation[list] :=
{
   results = new array
   size = length[list]
   average = sum[list] / size

   // Scale the list around its average
   normalizedList = new array[size]
   for i = 0 to size-1
      normalizedList@i = list@i - average
   
   for offset=1 to size-1
   {
      sum = 0
      for j = 0 to size-offset-1
         sum = sum + normalizedList@j * normalizedList@(j+offset)

      results.push[[offset, sum]]
   }

   return sort[results, {|a,b| b@1 <=> a@1 }]
}


View or download correlation.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 18593 days, 12 hours, 48 minutes ago.