def __init__(self, Y, con, initial=1): self.Y = Y self.constraint = constraints((con.inequality, con.inequality_offset), None) self.sigma = initial self.constraint.covariance = self.sigma**2 * np.identity(con.dim)
def cone_with_slice(angles, which, fill_args={}, ax=None, label=None, Y=None): ax, poly, constraint, rays = cone_rays(angles, which, ax=ax, fill_args=fill_args) eta = rays[0] representation = constraints((constraint, np.zeros(2)), None) if Y is None: Y = simulate_from_constraints(representation) pyplot.scatter(Y[0], Y[1], s=100, label=label) ax.fill(poly[:,0], poly[:,1], label=r'$C(y)$', **fill_args) ax.arrow(0,0,eta[0]/200.,eta[1]/200., label=r'$\eta$', linewidth=5, head_width=0.05, fc='k') Vp, _, Vm = representation.pivots(eta, Y)[:3] Yperp = Y - (np.dot(eta, Y) / np.linalg.norm(eta)**2 * eta) if Vm == np.inf: Vm = 10000 width_points = np.array([(Yperp + Vp*eta / np.linalg.norm(eta)**2), (Yperp + Vm*eta / np.linalg.norm(eta)**2)]) ax.plot(width_points[:,0], width_points[:,1], 'r--', linewidth=2) ax.scatter(width_points[:,0], width_points[:,1], label=r'${\cal V}$', s=150, marker='x', linewidth=2, c='k') return ax, poly, constraint, rays
def main(): # import the CGH data mat = scipy.io.loadmat('y.mat') v = mat['v'] v = v.ravel() y = v - np.mean(v) # initialize the design matrix n = len(y) D = (np.identity(n) - np.diag(np.ones(n - 1), 1))[:-1] X = np.linalg.pinv(D) # get the constraint matrix and vector to compute conf_intervals A, b, X_e, beta, E, Proj = slope_constraints(X, y, 0.001) C = constraints((A.dot(X_e.T), b), None, covariance=0.007 * np.eye(n)) # beta_interval = [] mean_interval = [] jump = [] ''' for i in range(len(E)): beta_intv = C.interval(X_e[:,i], y) beta_interval.append(beta_intv) ''' for i in range(len(E)): if i == 0 or E[i] - E[i - 1] > 5: mean_intv = C.interval(Proj[:, E[i]], y) mean_interval.append(mean_intv) jump.append(E[i]) else: eta = np.mean(Proj[:, E[i]:(E[i + 1] + 1)], axis=1) mean_intv = C.interval(eta, y) mean_interval[-1] = mean_intv jump[-1] = E[i] mean_interval.append(C.interval(Proj[:, -1], y)) print mean_interval print jump print E fit_line = np.dot(X, beta) + np.mean(v) low = [l for l, h in mean_interval] high = [h for l, h in mean_interval] low_fit = np.zeros((n, 1)) high_fit = np.zeros((n, 1)) left = 0 for idx, e in enumerate(jump): right = e low_fit[left:right] = low[idx] high_fit[left:right] = high[idx] left = right low_fit[left:] = low[-1] high_fit[left:] = high[-1] low_fit += np.mean(v) high_fit += np.mean(v) print "before plotting" plt.figure() plt.scatter(range(n), v, s=40, facecolors='none', edgecolors='r') plt.plot(range(n), fit_line, linewidth=2) plt.plot(range(n), low_fit, linewidth=2, color='g') plt.plot(range(n), high_fit, linewidth=2, color='y') plt.xlim(0, n) plt.show()
def main(): # import the CGH data mat = scipy.io.loadmat('y.mat') v = mat['v'] v = v.ravel() y = v - np.mean(v) # initialize the design matrix n = len(y) D = (np.identity(n) - np.diag(np.ones(n-1),1))[:-1] X = np.linalg.pinv(D) # get the constraint matrix and vector to compute conf_intervals A, b, X_e, beta, E, Proj = slope_constraints(X, y, 0.001) C = constraints((A.dot(X_e.T),b), None, covariance=0.007 * np.eye(n)) # beta_interval = [] mean_interval = [] jump = [] ''' for i in range(len(E)): beta_intv = C.interval(X_e[:,i], y) beta_interval.append(beta_intv) ''' for i in range(len(E)): if i == 0 or E[i] - E[i-1] > 5: mean_intv = C.interval(Proj[:,E[i]], y) mean_interval.append(mean_intv) jump.append(E[i]) else: eta = np.mean(Proj[:,E[i]:(E[i+1]+1)], axis=1) mean_intv = C.interval(eta, y) mean_interval[-1] = mean_intv jump[-1] = E[i] mean_interval.append(C.interval(Proj[:,-1], y)) print mean_interval print jump print E fit_line = np.dot(X, beta) + np.mean(v) low = [l for l,h in mean_interval] high = [h for l,h in mean_interval] low_fit = np.zeros((n,1)) high_fit = np.zeros((n,1)) left = 0 for idx, e in enumerate(jump): right = e low_fit[left:right] = low[idx] high_fit[left:right] = high[idx] left = right low_fit[left:] = low[-1] high_fit[left:] = high[-1] low_fit += np.mean(v) high_fit += np.mean(v) print "before plotting" plt.figure() plt.scatter(range(n), v, s=40, facecolors='none', edgecolors='r') plt.plot(range(n), fit_line, linewidth=2) plt.plot(range(n), low_fit, linewidth=2, color='g') plt.plot(range(n), high_fit, linewidth=2, color='y') plt.xlim(0,n) plt.show()
def cone_with_point(angles, which, fill_args={}, ax=None, label=None, Y=None): ax, poly, constraint, rays = cone_rays(angles, which, ax=ax, fill_args=fill_args) eta = rays[0] representation = constraints((constraint, np.zeros(2)), None) if Y is None: Y = simulate_from_constraints(representation) pyplot.scatter(Y[0], Y[1], s=100, label=label) return ax, poly, constraint, rays
def cone_with_arrow(angles, which, fill_args={}, ax=None, label=None, Y=None): ax, poly, constraint, rays = cone_rays(angles, which, ax=ax, fill_args=fill_args) eta = rays[0] representation = constraints((constraint, np.zeros(2)), None) if Y is None: Y = simulate_from_constraints(representation) pyplot.scatter(Y[0], Y[1], s=100, label=label) ax.fill(poly[:,0], poly[:,1], label=r'$C(y)$', **fill_args) ax.arrow(0,0,eta[0]/200.,eta[1]/200., label=r'$\eta$', linewidth=5, head_width=0.05, fc='k') return ax, poly, constraint, rays
def hull_with_point(W, fill=True, fill_args={}, label=None, ax=None, Y=None): f, A, b, pairs, angles, perimeter = just_hull(W, fill=fill, label=label, ax=ax, fill_args=fill_args) representation = constraints((A, b), None) if Y is None: Y = simulate_from_constraints(representation) a = f.gca() a.scatter(Y[0], Y[1], label=r'$y$', s=100) return f, A, b, pairs, angles, perimeter
def hull_with_point(W, fill=True, fill_args={}, label=None, ax=None, Y=None): f, A, b, pairs, angles, perimeter = just_hull(W, fill=fill, label=label, ax=ax, fill_args=fill_args) representation = constraints((A,b), None) if Y is None: Y = simulate_from_constraints(representation) a = f.gca() a.scatter(Y[0], Y[1], label=r'$y$', s=100) return f, A, b, pairs, angles, perimeter
def hull_with_slice(W, fill=True, fill_args={}, label=None, ax=None, Y=None, eta=None): f, A, b, pairs, angles, perimeter = just_hull(W, fill=fill, label=label, ax=ax, fill_args=fill_args) representation = constraints((A, b), None) if Y is None: Y = simulate_from_constraints(representation) ax.scatter(Y[0], Y[1], label=r'$y$', s=100) if eta is None: eta = np.random.standard_normal(2) ax.arrow(0, 0, eta[0], eta[1], linewidth=5, head_width=0.05, fc='k') Vp, _, Vm = representation.pivots(eta, Y)[:3] Yperp = Y - (np.dot(eta, Y) / np.linalg.norm(eta)**2 * eta) if Vm == np.inf: Vm = 10000 width_points = np.array([(Yperp + Vp * eta / np.linalg.norm(eta)**2), (Yperp + Vm * eta / np.linalg.norm(eta)**2)]) ax.plot(width_points[:, 0], width_points[:, 1], 'r--', linewidth=2) ax.scatter(width_points[:, 0], width_points[:, 1], label=r'${\cal V}$', s=150, marker='x', linewidth=2, c='k') return f, A, b, pairs, angles, perimeter
def cone_with_slice(angles, which, fill_args={}, ax=None, label=None, Y=None): ax, poly, constraint, rays = cone_rays(angles, which, ax=ax, fill_args=fill_args) eta = rays[0] representation = constraints((constraint, np.zeros(2)), None) if Y is None: Y = simulate_from_constraints(representation) pyplot.scatter(Y[0], Y[1], s=100, label=label) ax.fill(poly[:, 0], poly[:, 1], label=r'$C(y)$', **fill_args) ax.arrow(0, 0, eta[0] / 200., eta[1] / 200., label=r'$\eta$', linewidth=5, head_width=0.05, fc='k') Vp, _, Vm = representation.pivots(eta, Y)[:3] Yperp = Y - (np.dot(eta, Y) / np.linalg.norm(eta)**2 * eta) if Vm == np.inf: Vm = 10000 width_points = np.array([(Yperp + Vp * eta / np.linalg.norm(eta)**2), (Yperp + Vm * eta / np.linalg.norm(eta)**2)]) ax.plot(width_points[:, 0], width_points[:, 1], 'r--', linewidth=2) ax.scatter(width_points[:, 0], width_points[:, 1], label=r'${\cal V}$', s=150, marker='x', linewidth=2, c='k') return ax, poly, constraint, rays
def cone_with_arrow(angles, which, fill_args={}, ax=None, label=None, Y=None): ax, poly, constraint, rays = cone_rays(angles, which, ax=ax, fill_args=fill_args) eta = rays[0] representation = constraints((constraint, np.zeros(2)), None) if Y is None: Y = simulate_from_constraints(representation) pyplot.scatter(Y[0], Y[1], s=100, label=label) ax.fill(poly[:, 0], poly[:, 1], label=r'$C(y)$', **fill_args) ax.arrow(0, 0, eta[0] / 200., eta[1] / 200., label=r'$\eta$', linewidth=5, head_width=0.05, fc='k') return ax, poly, constraint, rays
def hull_with_slice(W, fill=True, fill_args={}, label=None, ax=None, Y=None, eta=None): f, A, b, pairs, angles, perimeter = just_hull(W, fill=fill, label=label, ax=ax, fill_args=fill_args) representation = constraints((A,b), None) if Y is None: Y = simulate_from_constraints(representation) ax.scatter(Y[0], Y[1], label=r'$y$', s=100) if eta is None: eta = np.random.standard_normal(2) ax.arrow(0,0,eta[0],eta[1], linewidth=5, head_width=0.05, fc='k') Vp, _, Vm = representation.pivots(eta, Y)[:3] Yperp = Y - (np.dot(eta, Y) / np.linalg.norm(eta)**2 * eta) if Vm == np.inf: Vm = 10000 width_points = np.array([(Yperp + Vp*eta / np.linalg.norm(eta)**2), (Yperp + Vm*eta / np.linalg.norm(eta)**2)]) ax.plot(width_points[:,0], width_points[:,1], 'r--', linewidth=2) ax.scatter(width_points[:,0], width_points[:,1], label=r'${\cal V}$', s=150, marker='x', linewidth=2, c='k') return f, A, b, pairs, angles, perimeter
y = np.random.standard_normal(n) * sigma L = lasso(y, X, frac=0.5) L.fit(tol=1.e-14, min_its=200) C = L.inactive_constraints PR = np.identity(n) - L.PA try: U, D, V = np.linalg.svd(PR) except np.linalg.LinAlgError: D, U = np.linalg.eigh(PR) keep = D >= 0.5 U = U[:,keep] Z = np.dot(U.T, y) Z_inequality = np.dot(C.inequality, U) Z_constraint = constraints((Z_inequality, C.inequality_offset), None) class variance_estimator(object): def __init__(self, Y, con, initial=1): self.Y = Y self.constraint = constraints((con.inequality, con.inequality_offset), None) self.sigma = initial self.constraint.covariance = self.sigma**2 * np.identity(con.dim) def draw_sample(self, ndraw=2000, burnin=1000): """ Draw a sample from the truncated normal storing it as the attribute `sample`.
y = np.random.standard_normal(n) * sigma L = lasso(y, X, frac=0.5) L.fit(tol=1.e-14, min_its=200) C = L.inactive_constraints PR = np.identity(n) - L.PA try: U, D, V = np.linalg.svd(PR) except np.linalg.LinAlgError: D, U = np.linalg.eigh(PR) keep = D >= 0.5 U = U[:, keep] Z = np.dot(U.T, y) Z_inequality = np.dot(C.inequality, U) Z_constraint = constraints((Z_inequality, C.inequality_offset), None) class variance_estimator(object): def __init__(self, Y, con, initial=1): self.Y = Y self.constraint = constraints((con.inequality, con.inequality_offset), None) self.sigma = initial self.constraint.covariance = self.sigma**2 * np.identity(con.dim) def draw_sample(self, ndraw=2000, burnin=1000): """ Draw a sample from the truncated normal storing it as the attribute `sample`. """