Google Answers Logo
View Question
 
Q: Finding a time-weighted correlation between two sets of measurements ( No Answer,   0 Comments )
Question  
Subject: Finding a time-weighted correlation between two sets of measurements
Category: Science > Math
Asked by: gw-ga
List Price: $5.00
Posted: 09 Oct 2004 07:01 PDT
Expires: 11 Oct 2004 18:13 PDT
Question ID: 412454
Suppose I measure two quantities at regular intervals, and I have N
pairs of measurements (X[1], Y[1])...(X[N], Y[N]).  I would like to
find the correlation between the two but assign a linear "weight" to
each pair of measurements, such that (X[1], Y[1]) has a weight of 1/N,
(X[2], Y[2]) has a weight of 2/N, etc, and (X[N], Y[N]) has a weight
of N/N or 1.  The weights are intended to give more relevance to
recent measurements and less to old ones.

What equations would I use to calculate the effective M, B, and R
values for a time-weighted correlation between X and Y as described above?

Clarification of Question by gw-ga on 09 Oct 2004 07:03 PDT
I expect the equations to be based on a linear least-squares regression.

Clarification of Question by gw-ga on 11 Oct 2004 17:49 PDT
The code I wish to adapt (I wrote it some time ago) is as follows:

{
  This procedure performs linear least-squares regression.

  M = slope
  B = Y-intercept

  R = correlation coefficient
  Sqr(R) = coefficient of determination

  if Sqr(R) = 1, then the line fits perfectly
  if Sqr(R) = 0, then the line fits very poorly

  Sqr(R) = the fraction of the variation in y that can be explained by
variations in x
}
procedure LinearLeastSquaresRegression(const X: TDoubleDynArray;
                                       const Y: TDoubleDynArray;
                                       out M: Double;
                                       out B: Double;
                                       out R: Double);
var
  I: Integer;     // loop index
  SX: Double;     // sum of X values
  SY: Double;     // sum of Y values
  SSX: Double;    // sum of squares of X values
  SXY: Double;    // sum of products of X and Y values
  N: Integer;     // number of data points
  D: Double;      //
  YAv: Double;    // average Y value
  SELine: Double; // sum of errors in predicted Y values
  SEAv: Double;   //
begin
  N := Length(X);
  Assert(N >= 2);
  Assert(Length(Y) = N);

  SX := 0;
  SY := 0;
  SSX := 0;
  SXY := 0;

  for I := 0 to (N - 1) do
  begin
    SX := SX + X[I];
    SY := SY + Y[I];
    SSX := SSX + Sqr(X[I]);
    SXY := SXY + X[I] * Y[I];
  end;

  D := SSX * N - Sqr(SX);
  M := (SXY * N - SX * SY) / D;
  B := (SSX * SY - SX * SXY) / D;
  YAv := SY / N;

  SELine := 0;
  SEAv := 0;

  for I := 0 to (N - 1) do
  begin
    SELine := SELine + Sqr(Y[I] - (M * X[I] + B));
    SEAv := SEAv + Sqr(Y[I] - YAv);
  end;

  R := Sqrt(1 - SELine / SEAv);
end;

Clarification of Question by gw-ga on 11 Oct 2004 18:05 PDT
Perhaps instead of using a weight from 0..1, it would be better if the
sum of all the weights was N.

This block of code might be the only thing I need to modify:

  for I := 0 to (N - 1) do
  begin
    SX := SX + X[I];
    SY := SY + Y[I];
    SXX := SXX + Sqr(X[I]);
    SXY := SXY + X[I] * Y[I];
  end;

Possible new code:

  for I := 0 to (N - 1) do
  begin
    W := 2 * (I + 0.5) / N; // weight for this time index

    SX := SX + W * X[I];
    SY := SY + W * Y[I];
    SXX := SXX + W * Sqr(X[I]);
    SXY := SXY + W * X[I] * Y[I];
  end;

Clarification of Question by gw-ga on 11 Oct 2004 18:10 PDT
I guess that should have been

W := (I + 0.5) * 2 / N;

Clarification of Question by gw-ga on 11 Oct 2004 18:13 PDT
Silly me.  Those are the same.
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy