def train(self, x, y): """ x - a list of x values y - a list of y values """ x_mean = mean(x) y_mean = mean(y) x_dev = sum([abs(i - x_mean) for i in x]) y_dev = sum([abs(i - y_mean) for i in y]) self.slope = (x_dev * y_dev) / (x_dev * x_dev) self.intercept = y_mean - (self.slope * x_mean)
def mean(A, ax=None): if isinstance(A, (int, long, float, np.ndarray)): return np.mean(A, ax) elif isinstance(A, f.frac): return f.mean(A) elif isinstance(A, p.Poly): return p.mean(A) raise NotImplementedError
class LinregNonMatrix(Algorithm): def train(self, x, y): x_mean = mean(x)
endfunction function y = vandermonde3(a) n = length(a) y = kron(a, ones(1, n)).^kron([0:n-1]', ones(1, n))' endfunction from base import Algorithm, mean class LinregNonMatrix(Algorithm): def train(self, x, y): x_mean = mean(x) y_mean = mean(y) x_dev = sum([i-x_mean for i in x]) y_dev = sum([i-y_mean for i in y]) self.slope = (x_dev*y_dev)/(x_dev*x_dev) self.intercept = y_mean - (self.slope*x_mean) def predict(self, x): return [i*self.slope + self.intercept for i in x] class LinregListMatrix(Algorithm):
Xrwm = base.rwm(post, N * N_temp, sigma_is[0], x0[:, 0], Disp=0) #random walk metropolis yy, ww = base.weight_samples(Xw, W_IS) xx = base.resample_IS(yy, ww, N) mean_r[i, :] = np.mean(Xrwm[N_temp * Burn_in:], 0) mean_pf[i, :] = np.mean(Xptf[Burn_in:, :, 0], 0) mean_sd[i, :] = np.mean(Xsd[Burn_in:, :, 0], 0) mean_uw[i, :] = np.mean(Xuw[Burn_in:, :, 0], 0) mean_w[i, :] = np.average(yy[Burn_in * math.factorial(N_temp):, :, 0], 0, weights=ww[Burn_in * math.factorial(N_temp):]) mean_y[i, :] = np.mean(xx[Burn_in:, :, 0], 0) mean_z[i, :] = base.mean(Xw[Burn_in:, :, :], W_IS[Burn_in:, :]) np.save('mean_r_circle_b.npy', mean_r) np.save('mean_p_circle_b.npy', mean_p) np.save('mean_uw_circle_b.npy', mean_uw) np.save('mean_w_circle_b.npy', mean_w) np.save('mean_sd_circle_b.npy', mean_sd) print('----------------------------------') print('----------------------------------') print('means') print(np.mean(mean_r, 0)) print(np.mean(mean_pf, 0)) print(np.mean(mean_sd, 0))