fpex0.linreg API documentation

# fpex0.linreg

View Source

      1import numpy as np
      2
      3
      4class LinearRegression():
      5    """
      6    Assumes a linear model:  Yi = a + b*Xi + ei    with ei ~ N(0,s^2)
      7    and calculates the simple linear regression according to [1].
      8
      9    Executes regression and holds regression parameters.
     10    <br> If a first regression has been made, any following execution of self.linReg(X, Y) will update the
     11    regression with the additional point (X, Y).
     12
     13    ## Parameters
     14    Regression represented by following fields:
     15    **a** Estimate for a
     16
     17    **b**: Estimate for b
     18
     19    **s2**: Estimate for s^2
     20
     21    **Xmean**: Mean value of Xi
     22
     23    **Ymean**: mean value of Yi
     24
     25    **Sxx**: S_X^2
     26
     27    **Sxy**: S_XY
     28
     29    **Syy**: S_Y^2
     30
     31    **See**: S_e^2
     32
     33
     34    ### Sources
     35    [1] Jerome H. Klotz: "Updating Simple Linear Regression", 1995. <br>
     36    [2] Statistica Sinica 5 (1995), 399-403.
     37
     38
     39    """
     40    def __init__(self):
     41        self.n     = 0
     42        self.Xmean = None
     43        self.Ymean = None
     44        self.See   = None
     45        self.Sxx   = None
     46        self.Sxy   = None
     47        self.a     = None
     48        self.b     = None
     49        self.s2    = None
     50
     51
     52    def linReg(self, X, Y):
     53        """
     54        Sets or updates the regression attributes.
     55
     56        ## Takes
     57        **X**: numpy.array or number <br>
     58        **Y**: numpy.array or number
     59
     60        X, Y must be either both arrays or both numbers.
     61
     62        ### Note
     63        A vector update has not yet been implemented, but is planned.
     64        """
     65
     66
     67        if self.n == 0:
     68            n = len(X)
     69            Xmean = np.mean(X)
     70            Ymean = np.mean(Y)
     71            XminusXmean = X - Xmean
     72            YminusYmean = Y - Ymean
     73            Sxx = np.dot(XminusXmean,XminusXmean)
     74            Sxy = np.dot(XminusXmean,YminusYmean)
     75            b   = Sxy / Sxx
     76            a   = Ymean - b*Xmean
     77            See = YminusYmean - b*XminusXmean
     78            See = np.dot(See, See)
     79            s2  = See / (n-2)
     80
     81            # assign values
     82            self.n     = n
     83            self.Xmean = Xmean
     84            self.Ymean = Ymean
     85            self.See   = See
     86            self.Sxx   = Sxx
     87            self.Sxy   = Sxy
     88            self.a     = a
     89            self.b     = b
     90            self.s2    = s2
     91
     92
     93        # is X primitive?
     94        # --> this also accepts strings for example,
     95        #     but gives the user the freedom to use customized floats
     96        elif not hasattr(X, '__len__'):
     97            # accessors
     98            n      = self.n
     99            bn     = self.b
    100            Xmeann = self.Xmean
    101            Ymeann = self.Ymean
    102            Sxxn   = self.Sxx
    103            Sxyn   = self.Sxy
    104            Seen   = self.See
    105
    106
    107            ## scalar update
    108            N = n + 1
    109
    110            XmeanN = Xmeann + (X - Xmeann)/N
    111            YmeanN = Ymeann + (Y - Ymeann)/N
    112
    113            XminusXmean = X - Xmeann
    114            YminusYmean = Y - Ymeann
    115
    116            SxxN = Sxxn + n/N * XminusXmean * XminusXmean
    117            SxyN = Sxyn + n/N * XminusXmean * YminusYmean
    118            SeeN = Seen + n/N * (YminusYmean - bn*XminusXmean)**2 * Sxxn / SxxN
    119
    120            bN = SxyN / SxxN
    121            aN = YmeanN - bN*XmeanN  # Formula (1)
    122            s2N = SeeN / (N-2)
    123
    124            ## store values
    125            self.n     = N
    126            self.Xmean = XmeanN
    127            self.Ymean = YmeanN
    128            self.Sxx   = SxxN
    129            self.Sxy   = SxyN
    130            self.See   = SeeN
    131            self.a     = aN
    132            self.b     = bN
    133            self.s2    = s2N
    134
    135
    136        elif len(X) >  1:
    137            # accessors
    138            n      = self.n
    139            bn     = self.b
    140            Xmeann = self.Xmean
    141            Ymeann = self.Ymean
    142            Sxxn   = self.Sxx
    143            Sxyn   = self.Sxy
    144            Seen   = self.See
    145
    146            nnew = len(X)
    147            N = n + nnew
    148            sqrtn = np.sqrt(n)
    149            sqrtN = np.sqrt(N)
    150
    151            XmeanN = (n / N) * (Xmeann + nnew/n * np.mean(X))  # own derivation
    152            YmeanN = (n / N) * (Ymeann + nnew/n * np.mean(Y))  #
    153
    154            XmeanSTAR = (Xmeann*sqrtn + XmeanN*sqrtN) / (sqrtn + sqrtN)  # below Formula (6)
    155            YmeanSTAR = (Ymeann*sqrtn + YmeanN*sqrtN) / (sqrtn + sqrtN)  # below Formula (7)
    156
    157            XminusXmeanSTAR = (X - XmeanSTAR)
    158            SxxSTAR = np.dot(XminusXmeanSTAR, XminusXmeanSTAR)
    159            SxxN    = Sxxn + SxxSTAR                            # Formula (6)
    160
    161            YminusYmeanSTAR = (Y - YmeanSTAR)                   #
    162            SxySTAR = np.dot(XminusXmeanSTAR, YminusYmeanSTAR)         #
    163            SxyN    = Sxyn + SxySTAR                            # Formula (7)
    164
    165            bN = SxyN / SxxN                                    # Formula (8)
    166
    167            Sxn = np.sqrt(Sxxn)
    168            SxN = np.sqrt(SxxN)
    169            bSTAR = (bn * Sxn + bN*SxN) / (Sxn + SxN)           # Formula (10)
    170
    171            SeeSTAR = YminusYmeanSTAR - bSTAR*XminusXmeanSTAR   #
    172            SeeSTAR = np.dot(SeeSTAR, SeeSTAR)                         # Formula (9)
    173            SeeN    = Seen + SeeSTAR                            # Formula (8)
    174
    175            aN  = YmeanN - bN*XmeanN   # Formula (1)
    176            s2N = SeeN / (N-2)
    177
    178
    179            ## store values
    180            self.n     = N
    181            self.Xmean = XmeanN
    182            self.Ymean = YmeanN
    183            self.Sxx   = SxxN
    184            self.Sxy   = SxyN
    185            self.See   = SeeN
    186            self.a     = aN
    187            self.b     = bN
    188            self.s2    = s2N

class LinearRegression: View Source

      5class LinearRegression():
      6    """
      7    Assumes a linear model:  Yi = a + b*Xi + ei    with ei ~ N(0,s^2)
      8    and calculates the simple linear regression according to [1].
      9
     10    Executes regression and holds regression parameters.
     11    <br> If a first regression has been made, any following execution of self.linReg(X, Y) will update the
     12    regression with the additional point (X, Y).
     13
     14    ## Parameters
     15    Regression represented by following fields:
     16    **a** Estimate for a
     17
     18    **b**: Estimate for b
     19
     20    **s2**: Estimate for s^2
     21
     22    **Xmean**: Mean value of Xi
     23
     24    **Ymean**: mean value of Yi
     25
     26    **Sxx**: S_X^2
     27
     28    **Sxy**: S_XY
     29
     30    **Syy**: S_Y^2
     31
     32    **See**: S_e^2
     33
     34
     35    ### Sources
     36    [1] Jerome H. Klotz: "Updating Simple Linear Regression", 1995. <br>
     37    [2] Statistica Sinica 5 (1995), 399-403.
     38
     39
     40    """
     41    def __init__(self):
     42        self.n     = 0
     43        self.Xmean = None
     44        self.Ymean = None
     45        self.See   = None
     46        self.Sxx   = None
     47        self.Sxy   = None
     48        self.a     = None
     49        self.b     = None
     50        self.s2    = None
     51
     52
     53    def linReg(self, X, Y):
     54        """
     55        Sets or updates the regression attributes.
     56
     57        ## Takes
     58        **X**: numpy.array or number <br>
     59        **Y**: numpy.array or number
     60
     61        X, Y must be either both arrays or both numbers.
     62
     63        ### Note
     64        A vector update has not yet been implemented, but is planned.
     65        """
     66
     67
     68        if self.n == 0:
     69            n = len(X)
     70            Xmean = np.mean(X)
     71            Ymean = np.mean(Y)
     72            XminusXmean = X - Xmean
     73            YminusYmean = Y - Ymean
     74            Sxx = np.dot(XminusXmean,XminusXmean)
     75            Sxy = np.dot(XminusXmean,YminusYmean)
     76            b   = Sxy / Sxx
     77            a   = Ymean - b*Xmean
     78            See = YminusYmean - b*XminusXmean
     79            See = np.dot(See, See)
     80            s2  = See / (n-2)
     81
     82            # assign values
     83            self.n     = n
     84            self.Xmean = Xmean
     85            self.Ymean = Ymean
     86            self.See   = See
     87            self.Sxx   = Sxx
     88            self.Sxy   = Sxy
     89            self.a     = a
     90            self.b     = b
     91            self.s2    = s2
     92
     93
     94        # is X primitive?
     95        # --> this also accepts strings for example,
     96        #     but gives the user the freedom to use customized floats
     97        elif not hasattr(X, '__len__'):
     98            # accessors
     99            n      = self.n
    100            bn     = self.b
    101            Xmeann = self.Xmean
    102            Ymeann = self.Ymean
    103            Sxxn   = self.Sxx
    104            Sxyn   = self.Sxy
    105            Seen   = self.See
    106
    107
    108            ## scalar update
    109            N = n + 1
    110
    111            XmeanN = Xmeann + (X - Xmeann)/N
    112            YmeanN = Ymeann + (Y - Ymeann)/N
    113
    114            XminusXmean = X - Xmeann
    115            YminusYmean = Y - Ymeann
    116
    117            SxxN = Sxxn + n/N * XminusXmean * XminusXmean
    118            SxyN = Sxyn + n/N * XminusXmean * YminusYmean
    119            SeeN = Seen + n/N * (YminusYmean - bn*XminusXmean)**2 * Sxxn / SxxN
    120
    121            bN = SxyN / SxxN
    122            aN = YmeanN - bN*XmeanN  # Formula (1)
    123            s2N = SeeN / (N-2)
    124
    125            ## store values
    126            self.n     = N
    127            self.Xmean = XmeanN
    128            self.Ymean = YmeanN
    129            self.Sxx   = SxxN
    130            self.Sxy   = SxyN
    131            self.See   = SeeN
    132            self.a     = aN
    133            self.b     = bN
    134            self.s2    = s2N
    135
    136
    137        elif len(X) >  1:
    138            # accessors
    139            n      = self.n
    140            bn     = self.b
    141            Xmeann = self.Xmean
    142            Ymeann = self.Ymean
    143            Sxxn   = self.Sxx
    144            Sxyn   = self.Sxy
    145            Seen   = self.See
    146
    147            nnew = len(X)
    148            N = n + nnew
    149            sqrtn = np.sqrt(n)
    150            sqrtN = np.sqrt(N)
    151
    152            XmeanN = (n / N) * (Xmeann + nnew/n * np.mean(X))  # own derivation
    153            YmeanN = (n / N) * (Ymeann + nnew/n * np.mean(Y))  #
    154
    155            XmeanSTAR = (Xmeann*sqrtn + XmeanN*sqrtN) / (sqrtn + sqrtN)  # below Formula (6)
    156            YmeanSTAR = (Ymeann*sqrtn + YmeanN*sqrtN) / (sqrtn + sqrtN)  # below Formula (7)
    157
    158            XminusXmeanSTAR = (X - XmeanSTAR)
    159            SxxSTAR = np.dot(XminusXmeanSTAR, XminusXmeanSTAR)
    160            SxxN    = Sxxn + SxxSTAR                            # Formula (6)
    161
    162            YminusYmeanSTAR = (Y - YmeanSTAR)                   #
    163            SxySTAR = np.dot(XminusXmeanSTAR, YminusYmeanSTAR)         #
    164            SxyN    = Sxyn + SxySTAR                            # Formula (7)
    165
    166            bN = SxyN / SxxN                                    # Formula (8)
    167
    168            Sxn = np.sqrt(Sxxn)
    169            SxN = np.sqrt(SxxN)
    170            bSTAR = (bn * Sxn + bN*SxN) / (Sxn + SxN)           # Formula (10)
    171
    172            SeeSTAR = YminusYmeanSTAR - bSTAR*XminusXmeanSTAR   #
    173            SeeSTAR = np.dot(SeeSTAR, SeeSTAR)                         # Formula (9)
    174            SeeN    = Seen + SeeSTAR                            # Formula (8)
    175
    176            aN  = YmeanN - bN*XmeanN   # Formula (1)
    177            s2N = SeeN / (N-2)
    178
    179
    180            ## store values
    181            self.n     = N
    182            self.Xmean = XmeanN
    183            self.Ymean = YmeanN
    184            self.Sxx   = SxxN
    185            self.Sxy   = SxyN
    186            self.See   = SeeN
    187            self.a     = aN
    188            self.b     = bN
    189            self.s2    = s2N

Assumes a linear model: Yi = a + b*Xi + ei with ei ~ N(0,s^2) and calculates the simple linear regression according to [1].

Executes regression and holds regression parameters.
If a first regression has been made, any following execution of self.linReg(X, Y) will update the regression with the additional point (X, Y).

## Parameters

Regression represented by following fields: a Estimate for a

b: Estimate for b

s2: Estimate for s^2

Xmean: Mean value of Xi

Ymean: mean value of Yi

Sxx: S_X^2

Sxy: S_XY

Syy: S_Y^2

See: S_e^2

### Sources

[1] Jerome H. Klotz: "Updating Simple Linear Regression", 1995.
[2] Statistica Sinica 5 (1995), 399-403.

LinearRegression() View Source

    41    def __init__(self):
    42        self.n     = 0
    43        self.Xmean = None
    44        self.Ymean = None
    45        self.See   = None
    46        self.Sxx   = None
    47        self.Sxy   = None
    48        self.a     = None
    49        self.b     = None
    50        self.s2    = None

def linReg(self, X, Y): View Source

     53    def linReg(self, X, Y):
     54        """
     55        Sets or updates the regression attributes.
     56
     57        ## Takes
     58        **X**: numpy.array or number <br>
     59        **Y**: numpy.array or number
     60
     61        X, Y must be either both arrays or both numbers.
     62
     63        ### Note
     64        A vector update has not yet been implemented, but is planned.
     65        """
     66
     67
     68        if self.n == 0:
     69            n = len(X)
     70            Xmean = np.mean(X)
     71            Ymean = np.mean(Y)
     72            XminusXmean = X - Xmean
     73            YminusYmean = Y - Ymean
     74            Sxx = np.dot(XminusXmean,XminusXmean)
     75            Sxy = np.dot(XminusXmean,YminusYmean)
     76            b   = Sxy / Sxx
     77            a   = Ymean - b*Xmean
     78            See = YminusYmean - b*XminusXmean
     79            See = np.dot(See, See)
     80            s2  = See / (n-2)
     81
     82            # assign values
     83            self.n     = n
     84            self.Xmean = Xmean
     85            self.Ymean = Ymean
     86            self.See   = See
     87            self.Sxx   = Sxx
     88            self.Sxy   = Sxy
     89            self.a     = a
     90            self.b     = b
     91            self.s2    = s2
     92
     93
     94        # is X primitive?
     95        # --> this also accepts strings for example,
     96        #     but gives the user the freedom to use customized floats
     97        elif not hasattr(X, '__len__'):
     98            # accessors
     99            n      = self.n
    100            bn     = self.b
    101            Xmeann = self.Xmean
    102            Ymeann = self.Ymean
    103            Sxxn   = self.Sxx
    104            Sxyn   = self.Sxy
    105            Seen   = self.See
    106
    107
    108            ## scalar update
    109            N = n + 1
    110
    111            XmeanN = Xmeann + (X - Xmeann)/N
    112            YmeanN = Ymeann + (Y - Ymeann)/N
    113
    114            XminusXmean = X - Xmeann
    115            YminusYmean = Y - Ymeann
    116
    117            SxxN = Sxxn + n/N * XminusXmean * XminusXmean
    118            SxyN = Sxyn + n/N * XminusXmean * YminusYmean
    119            SeeN = Seen + n/N * (YminusYmean - bn*XminusXmean)**2 * Sxxn / SxxN
    120
    121            bN = SxyN / SxxN
    122            aN = YmeanN - bN*XmeanN  # Formula (1)
    123            s2N = SeeN / (N-2)
    124
    125            ## store values
    126            self.n     = N
    127            self.Xmean = XmeanN
    128            self.Ymean = YmeanN
    129            self.Sxx   = SxxN
    130            self.Sxy   = SxyN
    131            self.See   = SeeN
    132            self.a     = aN
    133            self.b     = bN
    134            self.s2    = s2N
    135
    136
    137        elif len(X) >  1:
    138            # accessors
    139            n      = self.n
    140            bn     = self.b
    141            Xmeann = self.Xmean
    142            Ymeann = self.Ymean
    143            Sxxn   = self.Sxx
    144            Sxyn   = self.Sxy
    145            Seen   = self.See
    146
    147            nnew = len(X)
    148            N = n + nnew
    149            sqrtn = np.sqrt(n)
    150            sqrtN = np.sqrt(N)
    151
    152            XmeanN = (n / N) * (Xmeann + nnew/n * np.mean(X))  # own derivation
    153            YmeanN = (n / N) * (Ymeann + nnew/n * np.mean(Y))  #
    154
    155            XmeanSTAR = (Xmeann*sqrtn + XmeanN*sqrtN) / (sqrtn + sqrtN)  # below Formula (6)
    156            YmeanSTAR = (Ymeann*sqrtn + YmeanN*sqrtN) / (sqrtn + sqrtN)  # below Formula (7)
    157
    158            XminusXmeanSTAR = (X - XmeanSTAR)
    159            SxxSTAR = np.dot(XminusXmeanSTAR, XminusXmeanSTAR)
    160            SxxN    = Sxxn + SxxSTAR                            # Formula (6)
    161
    162            YminusYmeanSTAR = (Y - YmeanSTAR)                   #
    163            SxySTAR = np.dot(XminusXmeanSTAR, YminusYmeanSTAR)         #
    164            SxyN    = Sxyn + SxySTAR                            # Formula (7)
    165
    166            bN = SxyN / SxxN                                    # Formula (8)
    167
    168            Sxn = np.sqrt(Sxxn)
    169            SxN = np.sqrt(SxxN)
    170            bSTAR = (bn * Sxn + bN*SxN) / (Sxn + SxN)           # Formula (10)
    171
    172            SeeSTAR = YminusYmeanSTAR - bSTAR*XminusXmeanSTAR   #
    173            SeeSTAR = np.dot(SeeSTAR, SeeSTAR)                         # Formula (9)
    174            SeeN    = Seen + SeeSTAR                            # Formula (8)
    175
    176            aN  = YmeanN - bN*XmeanN   # Formula (1)
    177            s2N = SeeN / (N-2)
    178
    179
    180            ## store values
    181            self.n     = N
    182            self.Xmean = XmeanN
    183            self.Ymean = YmeanN
    184            self.Sxx   = SxxN
    185            self.Sxy   = SxyN
    186            self.See   = SeeN
    187            self.a     = aN
    188            self.b     = bN
    189            self.s2    = s2N

Sets or updates the regression attributes.

## Takes

X: numpy.array or number
Y: numpy.array or number

X, Y must be either both arrays or both numbers.

### Note

A vector update has not yet been implemented, but is planned.