def estimate_residual_var()

in svinfer/linear_model/linear_regression.py [0:0]


    def estimate_residual_var(self):
        if self.yty is None:
            self.sigma_sq = None
            return
        df = self.n
        if self.df_corrected:
            # I think it should be df -= self.k
            # but use df -= self.k - 1 for now to be aligned with the R package
            df -= self.k - 1
        term1 = (
            self.yty
            - 2 * self.beta.T.dot(self.xty)
            + self.beta.T.dot(self.xtx).dot(self.beta)
        ) * self.n / df
        term2 = (self.beta ** 2 * self.x_s2).sum()
        sigma_sq = term1 - term2
        # check whether variance is positive or not
        if sigma_sq < 0.0:
            logging.warning("negative residual variance!")
        self.sigma_sq = sigma_sq