Announcement

Collapse
No announcement yet.

Linear Regression Bands

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Linear Regression Bands

    What I would like to be able to do is to plot linear regression bands at various std deviations for the same period length. I have code that allows me to plot the linear regression as an average line (not a straight line), but I'm not sure how to plot upper and lower bands that are std deviations of 1, 2, and 3.

    The end result should look like a central "least squares average" line with three upper and three lower bands representing the various std deviations (plus and minus) from the main line. I know this shouldn't be hard to do, but I'm scratching my head over it at this point. Thanks...

  • #2
    Re: Reply to post 'Linear Regression Bands'

    If your returned variable is stdev then you should try return
    newArray(stdev,stdev*2,stdev*3,stdev*-1,stdev*-2)

    You will have to set up your line colors and thicknesses etc.
    ----- Original Message -----
    From: <[email protected]>
    To: <[email protected]>
    Sent: Tuesday, February 04, 2003 12:10 PM
    Subject: Reply to post 'Linear Regression Bands'


    > Hello rhbishop,
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >

    Comment


    • #3
      Re: Reply to post 'Linear Regression Bands'

      GW, how about posting your code, I would like to look at it, Thanks Bob
      ----- Original Message -----
      From: <[email protected]>
      To: <[email protected]>
      Sent: Tuesday, February 04, 2003 12:10 PM
      Subject: Reply to post 'Linear Regression Bands'


      > Hello rhbishop,
      >
      > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      >

      Comment


      • #4
        RH, thanks for your replies. Here's my code for plotting a linear regression line. I can't take credit for it. I downloaded it in the file share area. If i multiplied the output by 1, for instance, it would just give me the same value as opposed to the value of the line 1 std deviation higher. I'm getting kinda lost I think...

        function preMain() {
        setPriceStudy(true);
        setStudyTitle("Linear Regression Line Value");
        setCursorLabelName("Reg Value ", 0);
        setDefaultBarFgColor(Color.red, 0);
        setDefaultBarThickness(1, 0);
        }

        function main(nLength, nSource) {
        if (nLength == null) {
        nLength = 10;
        }
        if (nSource == null) {
        nSource = "Close";
        }

        var yValues = new Array(nLength-1);

        yValues = getValue(nSource, 0, -nLength);
        if (yValues == null) {
        return;
        }

        //slope
        var xySum = 0;
        var xSum = 0;
        var ySum = 0;
        var x2Sum = 0;
        for (i = 0; i < nLength; ++i) {
        xySum += (yValues[i] * i)
        xSum += i;
        ySum += yValues[i];
        x2Sum += (i * i);
        }
        var b = (xySum - ((xSum * ySum) / nLength)) / (x2Sum - ((xSum * xSum) / nLength));

        //intercept
        var a = (ySum - (b * xSum)) / nLength;

        /*** The line below is the related regression line for the most recent nLength. ***/
        //var y1 = (a + (-b * -(nLength-1)))
        //var y2 = (a + (-b * 0))
        //drawLineRelative(-(nLength-1), y1, 0, y2, PS_SOLID, 1, Color.blue, "myLine");

        return a;
        }

        Comment


        • #5
          Hi gw: Here's my code for sd bands. I'm using the 34 day average for the mean of the close and the sd is added or subtracted from that. You can change the series or the length in edit studies. You will notice that when price gets to extreme levels a reversion to the mean is immenent. I would appreciate comfirmation from mathemeticians out there that my code for sd is appropriate. TIA Bob


          function preMain() {
          setPriceStudy(true);
          setStudyTitle("STDEV");
          setCursorLabelName("STDEV1", 0);
          setDefaultBarFgColor(Color.lime, 0);
          setDefaultBarThickness(1, 0);
          setCursorLabelName("STDEV2", 1);
          setDefaultBarFgColor(Color.lime, 1);
          setDefaultBarThickness(1, 1);
          setCursorLabelName("STDEV3", 2);
          setDefaultBarFgColor(Color.lime, 2);
          setDefaultBarThickness(2, 2);
          setCursorLabelName("STDEV-1", 3);
          setDefaultBarFgColor(Color.red, 3);
          setDefaultBarThickness(1,3);
          setCursorLabelName("STDEV-2", 4);
          setDefaultBarFgColor(Color.red, 4);
          setDefaultBarThickness(1, 4);
          setCursorLabelName("STDEV-3", 5);
          setDefaultBarFgColor(Color.red, 5);
          setDefaultBarThickness(2,5);
          setCursorLabelName("MEAN", 6);
          setDefaultBarFgColor(Color.blue,6);
          setDefaultBarThickness(2,6);

          }

          function main(nLength, nSource) {
          if (nLength == null) {
          nLength = 34;
          }
          if (nSource == null) {
          nSource = "close";
          }

          var yValues = new Array(nLength-1);

          yValues = getValue(nSource, 0, -nLength);
          if (yValues == null) {
          return;
          }

          //slope


          var total = 0;
          var xSum = 0;
          var ySum = 0;
          var x2Sum = 0;
          var mean = 0;
          var sdDev = 0;
          total=0;

          for (i = 0; i < nLength; i++) {
          total += (yValues[i])}
          mean=total/nLength
          for(i=0;i < nLength;i++) {
          x2Sum +=((yValues[i]-mean)*(yValues[i]-mean))}
          sdDev=Math.sqrt(x2Sum/(nLength-1))


          //intercept



          return new Array(mean+sdDev,mean+(sdDev*2),mean+(sdDev*3),mea n-sdDev,mean-(2*sdDev),mean-(3*sdDev),mean)
          }

          Comment


          • #6
            Thanks RH. I think this code is what I needed. I'll do some testing with it and let you know how it turns out.

            Now I see what you mean about multiplying the StdDev values. The initial StdDev must be considered to be 1, then by multiplying one can arrive at higher order StdDev's.

            I guess I was making it more complicated than it actually was.

            Comment

            Working...
            X