def plot_slopes(subject_means, color, label):
     rlm_coeff = rlm_coeffs(subject_means)
     ellipser = regression_ellipsoid(subject_means[:,0], subject_means[:,1])
     t = np.linspace(0, np.pi*2, 100)
     ex, ey = ellipser(t)
     plt.plot(rlm_coeff.c[0], rlm_coeff.c[1], marker='o', color=color, label=label)
     plt.plot(ex, ey, color=color, linestyle='--', label='95% confidence region')
     plt.xlabel('Slope')
     plt.ylabel('Intercept')
def pursuit_yaw_new():
   
    ci = [1,3] 
    is_control = True
    txt = "free" if is_control else "tp"
    pdf_out = PdfPages('/tmp/pursuit_vs_yaw_pooled_per_subject_'+str(ci)+'_'+txt+'.pdf')
    table = get_pooled_data(ci, is_control)

    #invalid = (table['sid'] == 2011081107) & (table['speed'] < -5)
    #table = table[ ~invalid ]
    
    xrange = (0,20)
    
    sids = np.unique(table['sid'])
    subject_means = []
    lsr_coeffs = []
    for sid in sids:
        d = table[table['sid'] == sid]
        ymean = np.mean(d['yaw'])
        smean = np.mean(d['speed'])
        subject_means.append((ymean, smean))
        lsr_fit = np.polyfit(d['yaw'], d['speed'], 1)
        lsr = np.poly1d(lsr_fit)
        lsr_coeffs.append(lsr)
        #print sid, lsr.c, scipy.stats.pearsonr(d['yaw'], d['speed'])
        
        plt.figure()
        plt.title(sid)
        plt.xlim(xrange)
        plt.ylim(-20,20)
        plt.plot(d['yaw'], d['speed'], '.k')
        plt.plot(xrange, lsr(xrange),'-b')
        #plt.plot(xrange, model(xrange), '-g')
        plt.xlabel('mean yaw during fixation (deg)')
        plt.ylabel('gaze speed (deg/s)')
        pdf_out.savefig()
        plt.close()
        
    subject_means = np.array(subject_means)
    lsr_bs = np.array([x.c[0] for x in lsr_coeffs])
    lsr_as = np.array([x.c[1] for x in lsr_coeffs])
    
    fit_all = np.polyfit(table['yaw'], table['speed'], 1)
    lsr_all = np.poly1d(fit_all)
    print 'lsr all laps b=%f' % (lsr_all.c[0])
    
    #fit_subject = np.polyfit(subject_means[:,0], subject_means[:,1], 1)
    #lsr_subject = np.poly1d(fit_subject)
    params = np.hstack([subject_means[:,0].reshape(-1,1),np.ones(len(subject_means[:,0])).reshape(-1,1)])
    ols_model = sm.OLS(subject_means[:,1].reshape(-1,1), params)
    rlm_model = sm.RLM(subject_means[:,1].reshape(-1,1), params, M=sm.robust.norms.HuberT())
    yaw_model = np.poly1d([0.5, 0])
    ols_results = ols_model.fit()
    rlm_results = rlm_model.fit()
    ols_coeff = np.poly1d(ols_results.params)
    rlm_coeff = np.poly1d(rlm_results.params)
    print 'ols subjects b=%f' % (ols_coeff.c[0])
    print 'rlm subjects b=%f' % (rlm_coeff.c[0])
    print ols_results.summary()
    
    # binomial test
    n = len(lsr_bs)
    s = np.sum(lsr_bs > 0)
    p = 0.5
    p_binomial = np.math.factorial(n) / (np.math.factorial(s) * np.math.factorial(n-s)) * p**s * p**(n-s)
    print 'binomial p(%i) = %f' % (s, p_binomial)

    sy = np.std(subject_means[:,1])
    print 'standard deviation of gaze speed %f' % sy
    ss_residual = np.sum( (subject_means[:,1]-ols_coeff(subject_means[:,0]))**2 )
    sxy = np.sqrt( ss_residual / (len(subject_means[:,0]-2)) )
    print 'standard error of estimate %f' % sxy
    model_ss_residual = np.sum( (subject_means[:,1]-yaw_model(subject_means[:,0]))**2 )
    model_error = np.sqrt( model_ss_residual / (len(subject_means[:,0]-2)) )
    print 'error of estimate from model %f' % model_error
    
    # F stat
    n = len(subject_means[:,0])
    df1 = 2
    df2 = n-df1
    f = ((model_ss_residual-ss_residual) / df1) / (ss_residual / df2)
    p_f = 1-scipy.stats.f(df1,df2).cdf(f)
    print 'F=%f, p=%f' % (f, p_f)

    # confidence region stuff
    from tru.ols_confidence_region import regression_ellipsoid
    ellipser = regression_ellipsoid(subject_means[:,0], subject_means[:,1])
    t = np.linspace(0, np.pi*2, 100)
    ex, ey = ellipser(t)

    plt.figure()
    plt.plot(table['yaw'], table['speed'], '.k')
    plt.plot(xrange, lsr_all(xrange), '-b')
    pdf_out.savefig()
    plt.close()
    
    plt.figure()
    plt.plot(subject_means[:,0], subject_means[:,1], '.k')
    plt.plot(xrange, ols_coeff(xrange), '-g', label='Ordinary least squares fit')
    plt.plot(xrange, rlm_coeff(xrange), '-r', label='huber t')
    plt.plot(xrange, yaw_model(xrange), 'b--', label='Future path model')
    plt.xlabel('Mean yaw rate (deg/s)')
    plt.ylabel('Mean gaze speed (deg/s)')
    plt.legend(loc='lower right')
    pdf_out.savefig()
    plt.close()
    
    plt.figure()
    #plt.plot(lsr_bs, lsr_as, '.k')
    plt.plot(ols_coeff.c[0], ols_coeff.c[1], 'og', label='OLS fit')
    plt.plot(rlm_coeff.c[0], rlm_coeff.c[1], 'oy', label='huber t fit')
    plt.plot(0.5, 0, 'ob', label='Future path model')
    plt.plot(0, 0, 'or', label='Tangent point model')
    plt.plot(ex, ey, 'k--', label='95% confidence region')
    plt.xlabel('Slope')
    plt.ylabel('Intercept')
    plt.legend(loc='upper right', numpoints=1)
    pdf_out.savefig()
    plt.close()
    """
    model_errors = subject_means[:,1]-ols_coeff(subject_means[:,0]) 
    plt.figure()
    plt.hist(model_errors, bins=10)
    pdf_out.savefig()
    plt.close()
    """        
    pdf_out.close()