Announcement

Collapse
No announcement yet.

Linear Regression - Standard Deviation and Standard Error

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Linear Regression - Standard Deviation and Standard Error

    could someone please verify ...

    the study defines standard deviation as:

    PHP Code:
    function StDev() {    
        var 
    nLength vLRline_length;
        var 
    sumX 0;
        var 
    sumX2 0;
        var 
    aSource getValue("Close"0, -nLength);
        if (
    aSource == null) return;
        
        for (
    0nLength; ++i) {
            
    sumX += aSource[i];
            
    sumX2 += (aSource[i] * aSource[i])
        }
        var 
    meanX = (sumX/nLength);
        var 
    stdev Math.sqrt((sumX2/nLength) - (meanX*meanX));

        return 
    stdev;

    but I think standard deviation of a linear regression would need to take account of the linear regression line itself i.e.

    sum the squares of the differences between the raw prices and the linear regression line. divide the sum by number of regression bars. take square root of result.

    something like ...

    PHP Code:
    for (var x=0x<length; ++x) {
       
    yDiff Math.abs(close(-barindex) - ((slope*x) + intercept));
       
    y2Sum += (yDiff yDiff); 
       --
    barindex;
    }
    stdev Math.sqrt((y2Sum/LRlength)); 
    Paul Williams
    Strategy & Applications
    www.futurenets.co.uk

  • #2
    Hello Paul,

    I don't think there is an error here. Dr. Mel Raiman's study uses the standard deviation of the close series to establish the distance between the upper and lower channel lines. It was not intended to take into account the moving linear regression series as you are suggesting. However, you are more than welcome to modify the standard deviation function to use the moving linear regression series to suit your needs.
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      thanks Jason

      I think the problem lies only with the regression lines themselves drawn at the end of the chart.

      The attached image shows the difference between the eSig LR tool Std Dev set to 1.

      However, when I use

      PHP Code:
      for (var x=0x<length; ++x) {
         
      yDiff Math.abs(close(-barindex) - ((slope*x) + intercept));
         
      y2Sum += (yDiff yDiff); 
         --
      barindex;
      }
      stdev Math.sqrt((y2Sum/LRlength)); 
      the Std Dev lines are the same as the eSig tool (again the tool defaults to 1.618 SD so it needs setting to 1 to compare).
      Attached Files
      Paul Williams
      Strategy & Applications
      www.futurenets.co.uk

      Comment


      • #4
        Hello Paul,

        I think there may be an error in the code you are using for comparison to the built-in linear regression tool. Try the attached formula, which is the original MR_MovLinReg.efs with an extra parameter to set the length of the standard deviation and the linear regression channel lines. The original study used a hard coded value of 23 for the standard deviation and the linear regression channel lines. Set both the attached study and built-in tool lengths to the same value and compare. You will see that they are virtually identical.



        Check to make sure you have entered 1 for the number of standard deviations for the channel lines in both studies. Also make sure that you have not checked the box for Standard Error in the built-in linear regression tool.

        If you're still having trouble figuring out what your difference may be, post your formula and I'll try to find the difference.
        Attached Files
        Jason K.
        Project Manager
        eSignal - an Interactive Data company

        EFS KnowledgeBase
        JavaScript for EFS Video Series
        EFS Beginner Tutorial Series
        EFS Glossary
        Custom EFS Development Policy

        New User Orientation

        Comment


        • #5
          yes Jason I'm using the update you sent me so we should be using the same study.

          in my defense ... 1 std dev should capture 68% of values either side of the linear regression, as shown here.

          2 things puzzle me ...

          1) your image appears to capture more than 68%

          2) I'm using the same study (your update). the study is set to 1SD and the eSig tool is set to 1SD. this image roughly covers the same period but its different.
          Attached Files
          Paul Williams
          Strategy & Applications
          www.futurenets.co.uk

          Comment


          • #6
            Jason

            It may be better to compare daily GB 1!-DT, see attached.

            My stdev function corrects this problem but it may not be suitable for the rest of the movingreg continuous study.

            I'll post something that solves the LR lines only.
            Attached Files
            Paul Williams
            Strategy & Applications
            www.futurenets.co.uk

            Comment


            • #7
              Hello Paul,

              In comparing the built-in with the EFS study and the chart images you posted, it appears to me that you have the option checked for using the Standard Error calculation rather than the Standard Deviation.

              When I set the built-in to use Standard Error in Edit Studies,



              I get a chart that looks very similar to yours.



              When I switch off the Standard Error option so that the study uses the Standard Deviation calculation,



              It matches the EFS, which is using Standard Deviation for the channels.



              Please verify that you're not using the Standard Error option and we'll go from there.
              Jason K.
              Project Manager
              eSignal - an Interactive Data company

              EFS KnowledgeBase
              JavaScript for EFS Video Series
              EFS Beginner Tutorial Series
              EFS Glossary
              Custom EFS Development Policy

              New User Orientation

              Comment


              • #8
                thanks Jason

                I'm actually comparing the movlinreg efs to the eSig LR tool (see attached) and setting them both to 1SD.

                I think you are comparing the movlinreg efs to the builtin LR efs.

                I can't log on at the moment, but will try and get to the bottom of this when I can.
                Attached Files
                Paul Williams
                Strategy & Applications
                www.futurenets.co.uk

                Comment


                • #9
                  Jason

                  I forgot to mention that the movlinreg doesn't use the linear regression line in its calculation of the Std Dev. So (unless I'm missing something) its not possible to determine what 1SD of the Linear Regression is.

                  To calculate the Std Dev either side of a linear regression line we must sum the squares of the differences between the raw prices and the linear regression line itself.

                  Once I'm back online I'll create something.
                  Paul Williams
                  Strategy & Applications
                  www.futurenets.co.uk

                  Comment


                  • #10
                    here is the efs which calculates the std dev of the linear regression line
                    Attached Files
                    Paul Williams
                    Strategy & Applications
                    www.futurenets.co.uk

                    Comment


                    • #11
                      and here is the comparison on a 3 minute ES chart over 100 bars:

                      1) eSig LR tool (not the efs built-in study) set to 1SD

                      2) LinReg_v1 set to 1SD (same as above)

                      3) movlinreg (and updates) set to 1SD, which I believe is incorrect.

                      at this point, I'd like to conclude that the Std Dev either side of a linear regression line must be based on the sum of the squares of the differences between the raw prices and the linear regression line itself - see LinReg_v1.efs
                      Attached Files
                      Paul Williams
                      Strategy & Applications
                      www.futurenets.co.uk

                      Comment


                      • #12
                        I'd like to correct my previous statement "movlinreg is incorrect", that's not true.

                        but the point I'm making is that its important for users to realise that its std dev calculation is not based on the linear regression line. there may well have been a valid reason not to do this.

                        thanks for your patience.
                        Paul Williams
                        Strategy & Applications
                        www.futurenets.co.uk

                        Comment


                        • #13
                          Paul
                          After comparing the results of your efs to two other programs as far as I can see it is calculating the Standard Error Channel and not the Standard Deviation Channel.
                          In the following three images you can see first your efs, then the Standard Error Channel in Metastock and then the Standard Deviation Channel (also from Metastock). The second program I used provides the same exact lines as shown here. For comparison purposes I also included in the last image Jason's Standard Deviation Channel
                          Alex









                          Originally posted by futurenets
                          at this point, I'd like to conclude that the Std Dev either side of a linear regression line must be based on the sum of the squares of the differences between the raw prices and the linear regression line itself - see LinReg_v1.efs

                          Comment


                          • #14
                            thanks Alexis.

                            I think I'll leave it here - LinReg_v1.efs (below) creates the same linear regression & std dev lines as the eSignal Linear Regression toolbar version.

                            the toolbar has std dev definitions for upper & lower.
                            Paul Williams
                            Strategy & Applications
                            www.futurenets.co.uk

                            Comment


                            • #15
                              Hello Paul,

                              I think I understand now why we're having such confusion on this topic. First, just let me say that neither is incorrect. We are simply comparing Standard Error to Standard Deviation. The calculation that you are using in your EFS is actually the Standard Error calculation. If you use the built-in Linear Regression study from the Basic Studies menu and set it to use Standard Error, I bet it matches your EFS calculation. I think the source of this confusion comes from the label in the LR drawing tool that you were using for comparison. Its upper and lower channels currently has the label of "Std. Devs." When those check boxes are checked the drawing tool is actually calculating the Standard Error, which is why it matches your EFS calculation. Hopefully this clears things up.

                              I don't think it’s accurate to expect 68% of the prices to fall within the LR channels when using the Standard Deviation of the close series because the regression line does not describe the bell curve distribution of the close series. Using Standard Error as you are would be a more accurate description of the 68% because the regression line is the basis for that calculation rather then the close series.
                              Jason K.
                              Project Manager
                              eSignal - an Interactive Data company

                              EFS KnowledgeBase
                              JavaScript for EFS Video Series
                              EFS Beginner Tutorial Series
                              EFS Glossary
                              Custom EFS Development Policy

                              New User Orientation

                              Comment

                              Working...
                              X