سؤال

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main () 
{
    double g_1, e_1, e_2, e_3, e_4, e_5; 
    int k;
   // double e[k];

        e_1 = 3.0;
    e_2 = 9.0;
    e_3 = 27.0;
    e_4 = 81.0;
    e_5 = 243.0;


    g_1 = ((e_1*e_2 + e_2*e_3 + e_3*e_4 + e_4*e_5)/5) - (((e_1)* + (e_2) + (e_3) +(e_4) + (e_5)/5)*((e_1)* + (e_2) + (e_3) +(e_4) + (e_5)/5));
printf("\n\n this is g(1): %f",g_1);


return (0);
}

I am trying to write a program that calculates the correlation between values. The mathematical function I have is the autocorrelation function or mathematical correlation function which is

g(T) = sum(from t = 1 to m-T) [ (e_t)*(e_(T+t)] - (sum(from t = 1 to m) [e_t/m] )^2

where m is the number of values I have.

Above I have tried to do the simplest version by taking 5 numbers and just plugging them into the formula. But eventually I need to be able to read a file containing any number of values from 100 to 5000 and find the correlation between them. I will worry about reading the file to the entries of an array later but first I would like to know is there a logical way of doing this using arrays?

For example I tried to do the following:

e[1] = 3.0;
    e[2] = 9.0;
    e[3] = 27.0;
    e[4] = 81.0;
    e[5] = 243.0;

for(k=1;k<=5;k++)
    {   
        g[k]= ((e[k]*e[k+1] + e[k+1]*e[k+2] + e[k+2]*e[k+3] + e[k+3]*e[k+4])/5) - (((e[k] + e[k+1] + e[k+2] + e[k+3] + e[k+4])/5)*((e[k] + e[k+1] + e[k+2] + e[k+3] + e[k+4])/5))
             }

But this would only make sense for k=1, because by the time it gets to k = 5, then k+1 will be 6, k+2 will be 7.. and I don't have these values.. But I'm not sure how exactly to program this.. Can anybody help?

Thank you

This is the formula using MathJax

g(\tau) = \sum_{\tau_{0}=1}^{m-\tau} ((\epsilon_{\tau{_0}} * \epsilon_{\tau+\tau_{0}})/m) - (\sum_{\tau_{0}=1}^m \epsilon_{\tau_{0}}/m)^2

an alternative form of the formula is:

g(\tau) = <\epsilon_{\tau_{0}}\epsilon_{\tau_{0}+\tau}>-<\epsilon_{\tau_{0}}>^2

where means expectation of a.

هل كانت مفيدة؟

المحلول 2

The C code you are looking for would be in this form:

  int     tau = 7      
  int     m   = 80;
  double  *e; /* An array filled with m values */
  double  lhs,rhs,answer;
  int     tau0;

  /* Left Summation */
  for(sum=0,tau0=1; tau0 < m-tau; ++tau0)
    sum += e[tau0] * e[tau+tau0]; 
  lhs = sum / m;

  /* Right Summation */
  for(sum=0,tau0=1; tau0 < m; ++tau0)
    sum += e[tau0] / m;
  rhs = sum * sum;

  answer = lhs - rhs;

Hopefully this get you closer to your solution.

نصائح أخرى

This is more of a how to read a math expression problem. It seems like in your example, m = 5, so the sums never reference beyond that.

I'm not entirely clear on your equation. Have tried Mathjax, but the formula is still not clear to me, but...

When translating a math expression to code, think of the summation sign (sigma) as equivalent to a for loop!

when writing the code, write (initially) in the most explicit way possible. For this problem code TWO for loops, one after the other, NOT nested. Each for loop does part of the calculation for g(k).

Get this to work for g(1). Then code a third for loop that wraps or surrounds the two loops you just got working. This loop will calculate g(1), g(2), etc. Note. if m is 5 and you only have 5 data points then you can only compute g(1), if m is 6 you can compute g(1) and g(2), etc.

Hope this helps, please post if you more information or questions.

To answer your comment, the following for loop implements a sum or sigma. Note. This is NOT exactly what your code should do, but it does demo using two for loops.

   int g1; x = 0; y = 0;
   int i;

   // compute x = sum(g(i)) + sum(f(i))

   // sum i = 0 to 2 [g(i)]
   for (i=0; i < 3; i++) {x += g[i];}

   // sum i = 0 to 1 [f(i)];
   for (i=0; i < 2; i++) {y += f[i];}

   g1 = x - y^2;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top