示例#1
0
    def history_evaluation(self, X, r, p):
        """Determine variance explained by history

        :Parameters:
            *X*
                design matrix
            *r*
                responses
            *p*
                probability correct that is considered the border between
                easy and difficult
        """
        if self.easy is None or self.difficult is None:
            difficult, easy = model.performance_filter(r,
                                                       X,
                                                       p[0],
                                                       p[1],
                                                       hf0=self.hf0)
        else:
            difficult, easy = self.difficult, self.easy
        # easy = np.logical_not ( difficult )

        current_stimulus = np.dot(X[:, 1:self.hf0], self.w[1:self.hf0])
        history_features = np.dot(X[:, self.hf0:], self.w[self.hf0:])
        decision_signal = current_stimulus + history_features
        difficult_var = np.var ( current_stimulus[difficult] ) + \
                np.var ( history_features[difficult] )
        easy_var = np.var ( current_stimulus[easy] ) + \
                np.var ( history_features[easy] )

        self.vdifficult = np.var ( history_features[difficult] ) / \
                difficult_var
        self.veasy = np.var(history_features[easy]) / easy_var
        self.vstimulus = np.var(current_stimulus[easy]) / easy_var

        S = []
        V = []
        C = []

        for condition in xrange(1, self.hf0):
            stimuli = np.unique(abs(X[:, condition]))
            S_ = []
            V_ = []
            for s in stimuli:
                if abs(s) < 1e-10:
                    continue
                i = abs(X[:, condition]) == s
                S_.append(s)
                V_.append(
                    np.var(history_features[i]) /
                    np.var(history_features[i] + current_stimulus[i]))
            S.append(S_)
            V.append(V_)
            C.append([condition] * len(S_))
        self.stimuli = np.concatenate(S)
        self.conditions = np.concatenate(C)
        self.variance_explained = np.concatenate(V)

        pred_hist = self.pi[1] + self.pi[2] * model.logistic(
            history_features[difficult] + self.w[0])
        pred_stim = self.pi[1] + self.pi[2] * model.logistic(
            current_stimulus[difficult] + self.w[0])
        pred_SH = self.pi[1] + self.pi[2] * model.logistic(
            decision_signal[difficult] + self.w[0])
        pred_easy = self.pi[1] + self.pi[2] * model.logistic(
            history_features[easy] + self.w[0])
        self.phist = np.mean((pred_hist > 0.5) == r[difficult])
        self.pstim = np.mean((pred_stim > 0.5) == r[difficult])
        self.pSH = np.mean((pred_SH > 0.5) == r[difficult])
        self.peasy = np.mean((pred_easy > 0.5) == r[easy])
示例#2
0
def figure3 ( ):
    w,h = 25,8.5
    fig = pl.figure ( figsize=(fullwidth,h*fullwidth/w) )

    # a,b,c,d = place_axes ( fig, 1.5,2, [9,9,5,5],[6]*4,
    #         [True]*2+[False]*2, [1.8,1.8,.5,.5], (w,h) )
    a,b,c = place_axes ( fig, 1.5,2, [9,9,5],[6]*3,
            [True]*2+[False], [1.8,1.8,.5], (w,h) )
    d = fig.add_axes ( [10,10,1,1] )
    a.text ( .05, laby, r"\textbf{a}", transform=a.transAxes )
    b.text ( .05, laby, r"\textbf{b}", transform=b.transAxes )
    c.text ( .05, laby, r"\textbf{c}", transform=c.transAxes )
    d.text ( .05, laby, r"\textbf{d}", transform=d.transAxes )
    M = results['model_w_hist']

    # Figures 3 A,B
    for condition in plotinfo['conditions']:
        condition = int ( condition )
        print "c",condition
        d_ = data.getsummary ( condition )
        # x = pl.mgrid[0:plotinfo['xmax']:100j]
        x = pl.mgrid[0:30:100j]
        # if len(data.th_features)>0:
        #     x = threshold.u_v ( x, results['model_w_hist'].nu )

        wfit  = results['model_w_hist'].w[plotinfo['indices'][condition]]
        w0fit = results['model_nohist'].w[plotinfo['indices'][condition]]
        pfit  = results['model_w_hist'].pi
        p0fit  = results['model_nohist'].pi
        x_ = threshold.u_v ( x, results['model_w_hist'].nu )
        x0 = threshold.u_v ( x, results['model_nohist'].nu )

        col = plotinfo['colors'][condition]
        pmf = 0.5*(pfit[1]+pfit[2]*model.logistic ( wfit[0]+wfit[1]*x_ )) + \
                0.5*(1-(pfit[1]+pfit[2]*model.logistic ( wfit[0]-wfit[1]*x_ )))
        p0f = 0.5*(p0fit[1]+p0fit[2]*model.logistic ( w0fit[0]+w0fit[1]*x0 )) + \
                0.5*(1-(p0fit[1]+p0fit[2]*model.logistic ( w0fit[0]-w0fit[1]*x0 )))
        print p0fit
        perror = (1-p0f-(1-pmf))/(1-p0f)

        a.plot ( x, pmf, color = col )
        a.plot ( x, p0f, color = col, linestyle='--' )
        b.plot ( x, pl.clip(perror,0,1e5), color = col )

    a.yaxis.set_major_formatter ( prcformatter )
    a.xaxis.set_major_formatter ( myformatter )
    a.set_xticks ( (0,10,20,30) )

    pl.setp ( (a,b), xlabel='Stimulus intensity' )
    a.set_ylabel ( 'Probability correct [\%]' )
    b.set_ylabel ( 'Error rate exp. [\%]' )
    b.set_xticks ( (0,10,20,30) )
    b.yaxis.set_major_locator ( tckr ( density=2, figure=fig, which=1 ) )
    b.yaxis.set_major_formatter ( prcformatter )
    b.xaxis.set_major_formatter ( myformatter )
    if observer in ['KP','sim_KP','sim_KP_nh']:
        b.set_ylim ( 0, .35 )
    if observer in ['pk']:
        pl.setp ( (a,b), xlim=(-.1,30.1) )

    # figure 3 C
    textfile.write ( "Figure 3C:\n" )
    z0 = 0
    C = statistics.EvaluationCollector ( M )
    ewh = C(results['model_w_hist'])
    enh = C(results['model_nohist'])
    hf0 = M.hf0
    # perm = results['permutation_wh']
    # # TODO: These indices have to be adapted to the revised collector
    # thresholds_wh = pl.array([C.get_thres ( perm[i,13+hf0:13+2*hf0], perm[i,12+hf0], perm[i,9:12], p=0.75 ) \
    #         for i in xrange ( 2000 )])
    # perm = results['permutation_nh']
    # thresholds_nh = pl.array([C.get_thres ( perm[i,13+hf0:13+2*hf0], perm[i,12+hf0], perm[i,9:12], p=0.75 ) \
    #         for i in xrange ( 2000 )])
    if thlev == .75:
        thind = 11
    elif thlev == .85:
        thind = 10+hf0
    else:
        raise ValueError

    for condition in xrange ( 1, M.hf0 ):
        s_wh = results['permutation_wh'][:,thind+condition]
        s_nh = results['permutation_nh'][:,thind+condition]
        # s_wh = thresholds_wh[:,condition]
        # s_nh = thresholds_nh[:,condition]
        s_ratio = s_wh/s_nh
        s_ratio_obs = ewh[thind+condition]/enh[thind+condition]
        # s_ratio_obs = results['model_w_hist'].w[condition]/results['model_nohist'].w[condition]
        z = (s_ratio_obs-pl.mean(s_ratio))/pl.std(s_ratio)
        cpe = pl.mean ( s_ratio < s_ratio_obs )
        ci = pl.prctile ( s_ratio, (2.5,97.5) )
        if z < z0 and ci[1]-ci[0] > 0:
            c0 = condition
            s_ratio_ = s_ratio
            s_ratio_obs_ = s_ratio_obs
            ci_ = ci
        textfile.write (
                "Condition %d\n  th75_ratio = %g\n  cpe = %g\n  percentiles of Null-Distribution: %g, %g\n" % \
                        (condition,s_ratio_obs,cpe,ci[0],ci[1]) )
    try:
        print "Using condition %d for figure 3C" % (c0,)
    except:
        c0 = 1
        s_ratio_ = s_ratio
        s_ratio_obs_ = s_ratio_obs
        ci_ = ci

    hist,bins = pl.histogram ( s_ratio_ )
    c.bar ( bins[:-1], hist, pl.diff ( bins ),
        edgecolor=graphics.histogram_color, facecolor=graphics.histogram_color )
    yrange = c.get_ylim ()
    # c.plot ( [1]*2, yrange, 'k:' )
    if s_ratio_obs<ci_[0]:
        c.plot ( [s_ratio_obs_]*2, (yrange[0],yrange[0]+0.85*(yrange[1]-yrange[0])), linewidth=2,
                color=graphics.observed_color )
        c.plot ( [s_ratio_obs_], [yrange[0]+0.95*(yrange[1]-yrange[0])], '*', color=graphics.observed_color )
    else:
        c.plot ( [s_ratio_obs_]*2, yrange, linewidth=2, color=graphics.observed_color )
    c.plot ( [ci_[0]]*2, yrange, color=graphics.C95_color )
    c.plot ( [ci_[1]]*2, yrange, color=graphics.C95_color )
    c.set_ylim ( *yrange )
    c.set_xlabel ( r'Threshold ratio' )
    c.xaxis.set_major_formatter ( myformatter )
    c.xaxis.set_major_formatter ( myformatter )
    # c.text ( .7, 0.7, r"$\frac{\theta_\mathrm{h}}{\theta_0}$",
    #         transform=c.transAxes )
    # c.set_xlim ( trimmed_hlim ( s_ratio_, s_ratio_obs_ ) )
    # c.xaxis.set_major_locator ( tckr ( density=0.4, figure=fig, which=0 ) )
    c.set_xlim ( .99, 1.01 )
    # c.xaxis.set_ticks ( (.95,1) )
    # c.set_xlim ( .85, 1.05 )
    c.xaxis.set_ticks ( (.99,1.,1.01) )


    # figure 3 D
    l_wh  = 0.5*results['permutation_wh'][:,[9,10]].sum(1)
    l_nh  = 0.5*results['permutation_nh'][:,[9,10]].sum(1)
    l_ratio = l_wh-l_nh
    l_ratio_obs = results['model_w_hist'].pi[[0,1]].sum()-results['model_nohist'].pi[[0,1]].sum()
    cpe = pl.mean ( l_ratio < l_ratio_obs )
    ci = pl.prctile ( l_ratio, (2.5,97.5) )
    textfile.write (
        "Figure 3D:\n  lapse_ratio = %g\n  cpe = %g\n  percentiles of Null-distribution: %g, %g\n  lapse_rate (w hist) = %g\n  lapse_rate (no hist) = %g\n" % \
                (l_ratio_obs,cpe,ci[0],ci[1],results['model_w_hist'].pi[[0,1]].sum(),results['model_nohist'].pi[[0,1]].sum()) )

    d = graphics.prepare_axes ( d, haveon=('bottom',) )
    # hist,bins = pl.histogram ( l_ratio )
    hist,bins = pl.histogram ( l_ratio, bins=good_lapse_bins ( l_ratio ) )
    # hist,bins = pl.histogram ( l_ratio, bins=pl.mgrid[-.0001:.0001:20j] )
    d.bar ( bins[:-1], hist, pl.diff(bins),
        edgecolor=graphics.histogram_color, facecolor=graphics.histogram_color, zorder=0 )
    yrange = d.get_ylim ()
    # d.plot ( [1]*2, yrange, 'k:' )
    if l_ratio_obs < ci[0] or l_ratio_obs > ci[1]:
        d.plot ( [l_ratio_obs]*2, [yrange[0], yrange[0]+0.85*(yrange[1]-yrange[0])],
                linewidth=2, color=graphics.observed_color)
        d.plot ( [l_ratio_obs], [yrange[0]+0.95*(yrange[1]-yrange[0])], '*', color=graphics.observed_color)
    else:
        print "lrobs",l_ratio_obs
        d.plot ( [l_ratio_obs]*2, yrange, color=graphics.observed_color, zorder=2)
    d.plot ([ci[0]]*2, yrange, color=graphics.C95_color, zorder=1 )
    d.plot ([ci[1]]*2, yrange, color=graphics.C95_color, zorder=1 )
    d.set_ylim ( yrange )

    d.set_xlabel ( r'Asymptote difference' )
    # d.text ( .7, 0.7, r"$\frac{\lambda_\mathrm{h}}{\lambda_0}$",
    #         transform=d.transAxes )
    # d.set_xlim ( trimmed_hlim ( l_ratio, l_ratio_obs, (0,5) ) )
    d.set_xlim ( -.003, .001 )
    d.xaxis.set_major_locator ( tckr ( density=0.4, figure=fig, which=0 ) )
    d.xaxis.set_ticks ( (-.002,0) )
    # d.set_xlim ( (.75, 1.25) )
    d.xaxis.set_major_formatter ( myformatter )

    a.set_ylim ( .49, 1.01 )

    pl.savefig ( "figures/%s3.pdf" % ( figname, ) )
    pl.savefig ( "figures/%s3.eps" % ( figname, ) )
示例#3
0
 def e(x):
     p1 = pi[1] + pi[2] * model.logistic(w[0] + w[i] *
                                         threshold.u_v(x, nu))
     p2 = 1 - (pi[1] + pi[2] *
               model.logistic(w[0] + w[i] * threshold.u_v(-x, nu)))
     return abs(.5 * p1 + .5 * p2 - p)
示例#4
0
def calculate_probabilities(home_strength, away_strength, home_theta,
                            away_theta):
    home_prob = model.logistic(home_theta + (home_strength - away_strength))
    away_prob = model.logistic(away_theta - (home_strength - away_strength))
    draw_prob = 1 - (home_prob + away_prob)
    return home_prob, draw_prob, away_prob