SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sat Nov 25, 2017 12:41 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Autocorrelation
PostPosted: Fri Jan 27, 2017 9:25 am 
Offline

Joined: Wed Aug 08, 2007 6:32 pm
Posts: 207
Hi I am trying to reverse engineer your autocorrelation for my live system but having trouble with the code:

using something like this:

https://upload.wikimedia.org/math/6/d/a ... 18bb8a.png

leading to:

Code:
        public double GetAverage(List<double> data, int len)
        {
            if (len == 0)
                throw new Exception("No data");

            double sum = 0;

            for (int i = data.Count - len; i < data.Count; i++)
                sum += data[i];

            return sum / Convert.ToDouble(len);
        }

        public double GetAverage2(List<double> data, int len)
        {
            if (len == 0)
                throw new Exception("No data");

            double sum = 0;

            for (int i = data.Count - len-1; i < data.Count-1; i++)
                sum += data[i];

            return sum / Convert.ToDouble(len);
        }


        public double GetVariance(List<double> data, int len)
        {
            // Get average
            double avg = GetAverage(data, len);

            double sum = 0;

            for (int i = data.Count - len; i < data.Count; i++)
                sum += System.Math.Pow((data[i] - avg), 2);

            return sum / Convert.ToDouble(len);
        }

        public double GetVariance2(List<double> data, int len)
        {
            // Get average
            double avg = GetAverage2(data, len);

            double sum = 0;

            for (int i = data.Count - len-1; i < data.Count-1; i++)
                sum += System.Math.Pow((data[i] - avg), 2);

            return sum / Convert.ToDouble(len);
        }

        public double GetStdev(List<double> data, int len)
        {
            return Math.Sqrt(GetVariance(data, len));
        }

        public double GetStdev2(List<double> data, int len)
        {
            return Math.Sqrt(GetVariance2(data, len));
        }

        public double GetCorrelation(List<double> x, int len)
        {
            double avgX = GetAverage(x, len);
            double stdevX = GetStdev(x, len);
            double avgY = GetAverage2(x, len);
            double stdevY = GetStdev2(x, len);
            double covXY = 0;
            double pearson = 0;

            for (int i = x.Count - len; i < x.Count; i++)
                covXY += (x[i] - avgX) * (x[i-1] - avgY);
            covXY /= len;
            pearson = covXY / (stdevX * stdevY);
            return pearson;
        }


Can you show me where I am going wrong?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group