figsize=(16.5, 14)) fig.patch.set_facecolor('white') # calculate trends zeros = np.zeros_like(time) n = 0 for j in range(ncols): for i in range(nrows): k = df.columns[n] print n, k # polynamial regression #------------------------------------------------------------- s = df[k] m, c = ap.linear_fit_robust(time, s.values, return_coef=True) x, y = ap.linear_fit_robust(time, s.values, return_coef=False) pol = np.polyfit(time, s.values, 2) yy = np.polyval(pol, time) #axs[i,j].fill_between(time, s.values+2*r, s.values-2*r, facecolor='0.5', # edgecolor='w', alpha=0.2) #axs[i,j].plot(time, zeros, ':', c='0.5', linewidth=0.5) axs[i, j].plot(time, y, c='0.2', linewidth=0.75, zorder=2) axs[i, j].plot(time, s.values, 's', c='0.5', markersize=4, zorder=1) if 0: axs[i, j].plot(time, yy, c='b', linewidth=1.5, zorder=2) if 1: axs[i, j].plot(time, ap.lasso_cv(time, s, max_deg=3), c='b', linewidth=1.75,
fig, axs = plt.subplots(nrows, ncols, sharex=False, sharey=False, figsize=(16.5,14)) fig.patch.set_facecolor('white') # calculate trends zeros = np.zeros_like(time) n = 0 for j in range(ncols): for i in range(nrows): k = df.columns[n] print n, k # polynamial regression #------------------------------------------------------------- s = df[k] m, c = ap.linear_fit_robust(time, s.values, return_coef=True) x, y = ap.linear_fit_robust(time, s.values, return_coef=False) pol = np.polyfit(time, s.values, 2) yy = np.polyval(pol, time) #axs[i,j].fill_between(time, s.values+2*r, s.values-2*r, facecolor='0.5', # edgecolor='w', alpha=0.2) #axs[i,j].plot(time, zeros, ':', c='0.5', linewidth=0.5) axs[i,j].plot(time, y, c='0.2', linewidth=0.75, zorder=2) axs[i,j].plot(time, s.values, 's', c='0.5', markersize=4, zorder=1) if 0: axs[i,j].plot(time, yy, c='b', linewidth=1.5, zorder=2) if 1: axs[i,j].plot(time, ap.lasso_cv(time, s, max_deg=3), c='b', linewidth=1.75, zorder=4) # set plots #-------------------------------------------------------------
def plot_ts(time2, lon, lat, dh_mean_cor, dh_mean, dg_mean, R, S, diff=True): if np.alltrue(np.isnan(dh_mean[1:])): return None #time2 = y2dt(time2) R = np.mean(R) S = np.mean(S) # use only non-null and non-zero entries for correlation ind, = np.where((~np.isnan(dh_mean)) & (~np.isnan(dg_mean)) & \ (dh_mean!=0) & (dg_mean!=0)) t = np.arange(len(dh_mean)) if not diff: x, y = ap.linear_fit(dg_mean[ind], dh_mean[ind]) x2, y2 = ap.linear_fit_robust(dg_mean[ind], dh_mean[ind]) fig = plt.figure() ax = fig.add_subplot((111)) plt.plot(dg_mean[ind], dh_mean[ind], 'o') plt.plot(x, y, linewidth=2, label='lstsq fit') plt.plot(x2, y2, linewidth=2, label='robust fit') plt.legend(loc=2).draw_frame(False) plt.xlabel('dAGC (dB)') plt.ylabel('dh (m)') plt.title('Mixed-term sensitivity') else: dh_mean2 = np.diff(dh_mean) dg_mean2 = np.diff(dg_mean) dh_mean2 = np.append(dh_mean2, np.nan) dg_mean2 = np.append(dg_mean2, np.nan) x, y = ap.linear_fit(dg_mean2[ind], dh_mean2[ind]) x2, y2 = ap.linear_fit_robust(dg_mean2[ind], dh_mean2[ind]) fig = plt.figure() ax = fig.add_subplot((111)) plt.plot(dg_mean2[ind], dh_mean2[ind], 'o') plt.plot(x, y, linewidth=2, label='lstsq fit') plt.plot(x2, y2, linewidth=2, label='robust fit') plt.legend(loc=2).draw_frame(False) plt.xlabel('$\Delta$dAGC (dB)') plt.ylabel('$\Delta$dh (m)') plt.title('Short-term sensitivity') ax1 = viz.add_inner_title(ax, 'corrcoef: R = %.2f' % R, 3) ax1 = viz.add_inner_title(ax, 'slope: S = %.2f' % S, 4) plt.savefig('corr.png') #----------------- if not diff: fig = plt.figure() ax2 = plt.subplot((211)) plt.plot(time2, dh_mean, 'b', linewidth=2, label='dh') plt.plot(time2, dh_mean_cor, 'r', linewidth=2, label='dh$_{COR}$') #plt.legend().draw_frame(False) viz.add_inner_title(ax2, 'dh', 2) viz.add_inner_title(ax2, 'dh$_{COR}$', 3) plt.title('lon = %.2f, lat = %.2f' % (lon, lat)) plt.ylabel('m') #plt.xlim(1992, 2012.1) #plt.gca().xaxis.set_major_formatter(plt.FormatStrFormatter('%d')) ax3 = plt.subplot((212)) plt.plot(time2, dg_mean, 'g', linewidth=2, label='dAGC') #plt.legend().draw_frame(False) viz.add_inner_title(ax3, 'dAGC', 3) plt.ylabel('dB') #plt.xlim(1992, 2012.1) #plt.gca().xaxis.set_major_formatter(plt.FormatStrFormatter('%d')) else: fig = plt.figure() ax2 = plt.subplot((311)) plt.plot(time2, dh_mean, 'b', linewidth=2, label='dh') plt.plot(time2, dh_mean_cor, 'r', linewidth=2, label='dh$_{COR}$') #plt.legend().draw_frame(False) viz.add_inner_title(ax2, 'dh', 2) viz.add_inner_title(ax2, 'dh$_{COR}$', 3) plt.title('lon = %.2f, lat = %.2f' % (lon, lat)) plt.ylabel('m') #plt.xlim(1992, 2012.1) #plt.gca().xaxis.set_major_formatter(plt.FormatStrFormatter('%d')) ax3 = plt.subplot((312)) plt.plot(time2, dh_mean2, 'm', linewidth=2, label='$\Delta$dh') #plt.legend().draw_frame(False) viz.add_inner_title(ax3, '$\Delta$dh', 3) plt.ylabel('m') #plt.xlim(1992, 2012.1) #plt.gca().xaxis.set_major_formatter(plt.FormatStrFormatter('%d')) ax4 = plt.subplot((313)) plt.plot(time2, dg_mean2, 'c', linewidth=2, label='$\Delta$dAGC') #plt.legend().draw_frame(False) viz.add_inner_title(ax4, '$\Delta$dAGC', 3) plt.ylabel('dB') #plt.xlim(1992, 2012.1) #plt.gca().xaxis.set_major_formatter(plt.FormatStrFormatter('%d')) fig.autofmt_xdate() plt.savefig('ts.png') return fig
y = sin_data['y'].values X = dmatrix('C(x, Poly)') N = 5 w = 1/noise out = ap.lstsq_cv(x, y, cv=10, max_deg=N, weight=w, randomise=True, return_coef=True) y_wls, coef, deg, mse, var = out y_ols = ap.lstsq_cv(x, y, cv=10, max_deg=N, weight=None, randomise=True) a2 = np.polyfit(x, y, 1, w=None)#w) y_line = np.polyval(a2, x) m, c = ap.linear_fit(x, y, return_coef=True) m2, c2 = ap.linear_fit_robust(x, y, return_coef=True) out = ap.lasso_cv(x, y, cv=10, max_deg=N, return_model=True) y_lasso, lasso = out a = np.append(lasso.intercept_, lasso.coef_) #y_lasso = np.dot(X[:,:N+1], a) #dy_lasso = a[1] * X[:,0] + 2 * a[2] * X[:,1] # + 3 * a[3] * X[:,2] dy_lasso = np.gradient(y_lasso, x[2] - x[1]) print a[1] print 'coef.:', a print 'slope:', y_lasso[-1] - y_lasso[0] plt.figure()