Created
March 10, 2017 17:52
-
-
Save fxborg/72e699f1b6bf546fd61c8dacd55400b5 to your computer and use it in GitHub Desktop.
onlineregression.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import math | |
class OnlineRegression(): | |
def __init__(self): | |
self.n=0 | |
self.x=0.0 | |
self.y=0.0 | |
self.xx=0.0 | |
self.yy=0.0 | |
self.xy=0.0 | |
def __add__(self, other): | |
if not isinstance(other, OnlineRegression): return | |
combined = OnlineRegression() | |
combined.n = self.n + other.n | |
combined.x = self.x + other.x | |
combined.y = self.y + other.y | |
combined.xx = self.xx + other.xx | |
combined.yy = self.yy + other.yy | |
combined.xy = self.xy + other.xy | |
return combined | |
""" 追加 """ | |
def push(self, x, y): | |
self.n += 1 | |
self.x += x | |
self.y += y | |
self.xx += x * x | |
self.yy += y * y | |
self.xy += x * y | |
"""標準誤差""" | |
def stderr(self): | |
return math.sqrt(self.residuals()) / (self.n-2.0) | |
"""切片""" | |
def intercept(self): | |
return (self.y - self.slope() * self.x) / self.n if self.n > 0 else self.mean_y() | |
"""傾き""" | |
def slope(self): | |
devsqx=self.dev_sq_x() | |
return self.dev_prod_xy() / devsqx if devsqx > 0 else 0.0 | |
"""平均X""" | |
def mean_x(self): | |
return self.x / self.n if self.n > 0 else 0.0 | |
"""平均Y""" | |
def mean_y(self): | |
return self.y / self.n if self.n > 0 else 0.0 | |
"""偏差平方和 X""" | |
def dev_sq_x(self): | |
return (self.xx * self.n - self.x * self.x) / self.n if self.n > 0 else 0.0 | |
"""偏差平方和 Y""" | |
def dev_sq_y(self): | |
return (self.yy * self.n - self.y * self.y) / self.n if self.n > 0 else 0.0 | |
"""偏差積和 XY""" | |
def dev_prod_xy(self): | |
return (self.xy * self.n - self.x * self.y) / self.n if self.n > 0 else 0.0 | |
"""残差平方和""" | |
def residuals(self): | |
devsqx=self.dev_sq_x() | |
return self.dev_sq_y() - (pow(self.dev_prod_xy(),2) / devsqx) if devsqx > 0 else 0.0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment