11import numpy as np
22from patsy import dmatrices , build_design_matrices
33
4+
45class LM (object ):
56 """An example ordinary least squares linear model class, analogous to R's
67 lm() function. Don't use this in real life, it isn't properly tested."""
8+
79 def __init__ (self , formula_like , data = {}):
810 y , x = dmatrices (formula_like , data , 1 )
911 self .nobs = x .shape [0 ]
@@ -12,27 +14,27 @@ def __init__(self, formula_like, data={}):
1214 self ._x_design_info = x .design_info
1315
1416 def __repr__ (self ):
15- summary = ("Ordinary least-squares regression\n "
16- " Model: %s ~ %s\n "
17- " Regression (beta) coefficients:\n "
18- % (self ._y_design_info .describe (),
19- self ._x_design_info .describe ()))
17+ summary = (
18+ "Ordinary least-squares regression\n "
19+ " Model: %s ~ %s\n "
20+ " Regression (beta) coefficients:\n "
21+ % (self ._y_design_info .describe (), self ._x_design_info .describe ())
22+ )
2023 for name , value in zip (self ._x_design_info .column_names , self .betas ):
2124 summary += " %s: %0.3g\n " % (name , value [0 ])
2225 return summary
2326
2427 def predict (self , new_data ):
25- (new_x ,) = build_design_matrices ([self ._x_design_info ],
26- new_data )
28+ (new_x ,) = build_design_matrices ([self ._x_design_info ], new_data )
2729 return np .dot (new_x , self .betas )
2830
2931 def loglik (self , new_data ):
30- (new_y , new_x ) = build_design_matrices ([ self . _y_design_info ,
31- self ._x_design_info ],
32- new_data )
32+ (new_y , new_x ) = build_design_matrices (
33+ [ self . _y_design_info , self ._x_design_info ], new_data
34+ )
3335 new_pred = np .dot (new_x , self .betas )
3436 sigma2 = self .rss / self .nobs
3537 # It'd be more elegant to use scipy.stats.norm.logpdf here, but adding
3638 # a dependency on scipy makes the docs build more complicated:
3739 Z = - 0.5 * np .log (2 * np .pi * sigma2 )
38- return Z + - 0.5 * (new_y - new_x ) ** 2 / sigma2
40+ return Z + - 0.5 * (new_y - new_x ) ** 2 / sigma2
0 commit comments