Exemple #1
0
def search_for_start(r,
                     X,
                     w0,
                     applythreshold,
                     hf0,
                     pm=(.85, .93, 1., 1.07, 1.15),
                     storeopt=False):
    """Search for starting values

    :Parameters:
        *d*
            data set
        *w0*
            coarse starting values
        *pm*
            increments or decrements
    """

    logging.info("Initial preoptimization")
    Mwh = model.history_model(r,
                              X[:, :hf0],
                              applythreshold=applythreshold,
                              w0=w0,
                              lm=0.1,
                              hf0=hf0,
                              emiter=100)
    Mnh = model.history_model(r,
                              X[:, :hf0],
                              applythreshold=applythreshold,
                              w0=w0,
                              lm=0.1,
                              hf0=hf0,
                              emiter=100)
    M0 = model.history_model(r,
                             X[:, :hf0],
                             applythreshold=applythreshold,
                             w0=w0,
                             lm=0.1,
                             hf0=hf0,
                             emiter=100)

    Mwh.w = np.concatenate((Mwh.w, np.zeros(6, 'd')))
    Mwh.X = X

    nhind = 0
    whind = 0
    i = 1
    for al in pm:
        for lm in pm:
            logging.info("::::: Optimizing from starting value %d :::::" %
                         (i, ))
            w0 = M0.w.copy()
            w0[1:hf0] *= al
            p0 = M0.pi.copy()
            p0[0] *= lm
            p0[-1] = 1 - p0[0] - p0[1]

            M_ = model.history_model(r,
                                     X,
                                     applythreshold=applythreshold,
                                     w0=w0,
                                     p0=p0,
                                     nu0=M0.nu,
                                     lm=0.1,
                                     hf0=hf0,
                                     verbose=True,
                                     emiter=300,
                                     storeopt=storeopt)
            if Mwh.loglikelihood < M_.loglikelihood:
                logging.info("  *model chosen for history*")
                Mwh = M_
                whind = i

            M_ = model.history_model(r,
                                     X[:, :hf0],
                                     applythreshold=applythreshold,
                                     w0=w0,
                                     p0=p0,
                                     nu0=M0.nu,
                                     lm=0.1,
                                     hf0=hf0,
                                     verbose=True,
                                     emiter=300,
                                     storeopt=storeopt)
            if Mnh.loglikelihood < M_.loglikelihood:
                logging.info("  *model chosen for independent*")
                Mnh = M_
                nhind = i
            i += 1

    logging.info("Mwh.w = %s\nMnh.w = %s" % (str(Mwh.w), str(Mnh.w)))
    logging.info("Mwh.ll = %g\nMnh.ll = %s" %
                 (Mwh.loglikelihood, Mnh.loglikelihood))
    logging.info(
        "Starting values:\n  with history: %d\n  without history: %d\n" %
        (whind, nhind))

    print "X", np.shape(X)
    print "Mwh", Mwh.w, np.shape(Mwh.X)
    return Mnh, Mwh
Exemple #2
0
def mcsimulation(generator,
                 collector,
                 nsamples,
                 w0,
                 p0,
                 nu0=0.,
                 verbose=False,
                 hf0=2,
                 applythreshold=[]):
    """run a monte carlo simulation

    :Parameters:
        *generator*
            a callable such that generator() will generate a new pair of
            responses and design matrix
        *collector*
            a callable such that collector(fitted_model) will be everything
            that is stored from the fitted dataset
        *nsamples*
            how many samples are to be generated
        *w0*
            starting values for the optimizations
        *verbose*
            if true, print status bar

    :Returns:
        a list of the collector outputs

    :Example:
    >>> np.random.seed(0)
    >>> generator = lambda: ((np.random.rand(10)>0.5).astype('d'),np.c_[np.ones(10),np.random.uniform(-1,1,size=10)])
    >>> collector = lambda M: (M.w,M.pi)
    >>> sim_results = mcsimulation ( generator, collector, 3, [10,10] )
    >>> len(sim_results)
    3
    >>> sim_results[0]
    (array([ 0.89742561,  1.36421569]), array([ 0.04270847,  0.03216249,  0.92512904]))
    """
    if verbose:
        status = np.arange(0, nsamples, nsamples / 10)
    else:
        status = [-1]
    collected_output = []
    for i in xrange(nsamples):
        r, X = generator()
        # print(np.shape(r))
        # print(np.shape(X))

        M = model.history_model(r,
                                X,
                                w0=w0,
                                p0=p0,
                                nu0=nu0,
                                hf0=hf0,
                                applythreshold=applythreshold,
                                emiter=80)
        collected_output.append(collector(M))
        w0 = M.w
        p0 = M.pi
        nu0 = M.nu
        if i in status:
            if verbose:
                sys.stderr.write("\r%d%%" % (100 * (float(i) / nsamples), ))
                sys.stderr.flush()
    if verbose:
        sys.stderr.write("\rDone\n")
        sys.stderr.flush()
    return collected_output
Exemple #3
0
def analysis(d,
             w0,
             nsamples=200,
             perm_collector=statistics.EvaluationCollector):
    """Analyze a dataset

    :Parameters:
        *d*
            a history.DataSet instance (typically a subclass of history.DataSet)
        *w0*
            starting values for the first parameters in the model. The remaining parameters will
            start at 0. It seems to be a good idea to give starting values for the stimulus dependent
            parameters in the model and leave the parameters for history features at 0.
        *nsamples*
            number of samples for monte carlo procedures
        *perm_collector*
            the collector object for the permutation tests. The default one should do for all
            experiments with a design matrix in which the first column is 1 and the second column
            refers to the slope.

    :Example:
    >>> d,w,plotinfo = load_plaid ()
    >>> results = analysis ( d, w, 10 )
    >>> results.keys()
    ['model_nohist', 'model_w_hist', 'bootstrap', 'permutation_nh', 'permutation_wh']
    """

    if d.__dict__.has_key('detection') and d.detection:
        dnh = d.__class__(d.fname, threshold=len(d.th_features) > 0)
    else:
        dnh = d.__class__(d.fname)  # reload the data without history
    dwh = d
    if getattr(d, 'audio', False):
        easy, difficult = d.performance_filter()
    else:
        easy, difficult = None, None
    logging.info("Fitting models")

    if getattr(d, 'p0', np.array(False)).all():
        # This code will only run for monkeys
        M0 = model.history_model(dnh.r,
                                 dnh.X,
                                 applythreshold=dnh.th_features,
                                 w0=w0,
                                 p0=d.p0,
                                 lm=0.1,
                                 hf0=dnh.hf0,
                                 emiter=40)
        Mnh = model.history_model(dnh.r,
                                  dnh.X,
                                  applythreshold=dnh.th_features,
                                  w0=M0.w,
                                  p0=M0.pi,
                                  lm=.1,
                                  hf0=dnh.hf0,
                                  verbose=True,
                                  emiter=300)
        logging.info("likelihood for independent responses: %g" %
                     (Mnh.loglikelihood, ))
        Mwh = model.history_model(dwh.r,
                                  dwh.X,
                                  applythreshold=dwh.th_features,
                                  w0=M0.w,
                                  p0=M0.pi,
                                  lm=.1,
                                  hf0=dwh.hf0,
                                  verbose=True,
                                  emiter=300)
        logging.info("likelihood for history model: %g" %
                     (Mwh.loglikelihood, ))
    else:
        Mnh, Mwh = search_for_start(d.r,
                                    d.X,
                                    w0,
                                    d.th_features,
                                    d.hf0,
                                    storeopt=True)
        logging.info("likelihood for independent responses: %g" %
                     (Mnh.loglikelihood, ))
        logging.info("likelihood for history model: %g" %
                     (Mwh.loglikelihood, ))

    print "nh", Mnh.w, Mnh.pi
    print "wh", Mwh.w, Mwh.pi
    print Mwh.applythreshold

    # Monte Carlo testing
    if nsamples > 0:
        r_, X_ = dwh.permutation()
        Mnh_perm, Mwh_perm = search_for_start(r_, X_, w0, d.th_features, d.hf0)

        logging.info("Permutation with history")
        dnh.rng.set_state(
            dwh.rng.get_state()
        )  # Set the states of the two random number generators to the same values to get the exact same sequence of random numbers
        perm_collector = statistics.EvaluationCollector(Mwh, easy, difficult)
        permutation_wh = pl.array(
            statistics.mcsimulation(dwh.permutation,
                                    perm_collector,
                                    nsamples,
                                    Mwh_perm.w,
                                    Mwh_perm.pi,
                                    Mwh_perm.nu,
                                    verbose=logging.root.level < 20,
                                    hf0=dwh.hf0,
                                    applythreshold=Mwh.applythreshold))

        logging.info("Permutation without history")
        perm_collector = statistics.EvaluationCollector(Mnh, easy, difficult)
        permutation_nh = pl.array(
            statistics.mcsimulation(dnh.permutation,
                                    perm_collector,
                                    nsamples,
                                    Mnh_perm.w,
                                    Mnh_perm.pi,
                                    Mnh_perm.nu,
                                    verbose=logging.root.level < 20,
                                    hf0=dnh.hf0,
                                    applythreshold=Mwh.applythreshold))

        logging.info("Bootstrap")
        kcollector = statistics.Kernel_and_Slope_Collector(dwh.h,
                                                           dwh.hf0,
                                                           slopeindex=range(
                                                               1, dwh.hf0))
        bootstrap = pl.array(
            statistics.mcsimulation(dwh.bootstrap,
                                    kcollector,
                                    nsamples,
                                    Mwh.w,
                                    Mwh.pi,
                                    Mwh.nu,
                                    verbose=logging.root.level < 20,
                                    hf0=dwh.hf0,
                                    applythreshold=Mwh.applythreshold))
    else:
        permutation_wh = None
        permutation_nh = None
        bootstrap = None

    results = {
        'model_nohist': Mnh,
        'model_w_hist': Mwh,
        'permutation_wh': permutation_wh,
        'permutation_nh': permutation_nh,
        'bootstrap': bootstrap
    }

    return results
Exemple #4
0
def search_for_start(r,
                     X,
                     w0,
                     applythreshold,
                     hf0,
                     pm=(.85, .93, 1., 1.07, 1.15),
                     storeopt=False,
                     modulation=False,
                     doublemodulation=False):
    """Search for starting values

    :Parameters:
        *d*
            data set
        *w0*
            coarse starting values
        *pm*
            increments or decrements
    """

    logging.info("Initial preoptimization")
    Mwh = model.history_model(r,
                              X[:, :hf0],
                              applythreshold=applythreshold,
                              w0=w0,
                              lm=0.1,
                              hf0=hf0,
                              emiter=100)
    Mnh = model.history_model(r,
                              X[:, :hf0],
                              applythreshold=applythreshold,
                              w0=w0,
                              lm=0.1,
                              hf0=hf0,
                              emiter=100)
    M0 = model.history_model(r,
                             X[:, :hf0],
                             applythreshold=applythreshold,
                             w0=w0,
                             lm=0.1,
                             hf0=hf0,
                             emiter=100)

    if modulation:
        Mhmod = model.history_model(r,
                                    X[:, :hf0],
                                    applythreshold=applythreshold,
                                    w0=w0,
                                    lm=0.1,
                                    hf0=hf0,
                                    emiter=100)

        # add the history weights the model with history
        Mwh.w = np.concatenate((Mwh.w, np.zeros(6, 'd')))
        Mwh.X = copy.copy(X)
        Mwh.X = Mwh.X[:, :-2]  # only the part that has no modulation

        # add those same weights + modulatory terms to the hmod model
        Mhmod.w = np.concatenate((Mhmod.w, np.zeros(15, 'd')))
        Mhmod.X = X

        # to check that the sizes work
        print "X", np.shape(X)
        print "Mwh", Mwh.w, np.shape(Mwh.X)
        print "Mhmod", Mhmod.w, np.shape(Mhmod.X)

    elif doublemodulation:
        Mhmod = model.history_model(r,
                                    X[:, :hf0],
                                    applythreshold=applythreshold,
                                    w0=w0,
                                    lm=0.1,
                                    hf0=hf0,
                                    emiter=100)

        # add the history weights to the model with just history
        Mwh.w = np.concatenate((Mwh.w, np.zeros(6, 'd')))
        Mwh.X = copy.copy(X)
        Mwh.X = Mwh.X[:, :-3]  # only the part that has no modulation

        # add those same weights + modulatory terms to the hmod model
        Mhmod.w = np.concatenate((Mhmod.w, np.zeros(24, 'd')))
        Mhmod.X = X

        # to check that the sizes work
        print "X", np.shape(X)
        print "Mwh", Mwh.w, np.shape(Mwh.X)
        print "Mhmod", Mhmod.w, np.shape(Mhmod.X)

    else:
        Mwh.w = np.concatenate((Mwh.w, np.zeros(6, 'd')))
        Mwh.X = X
        Mhmod = []  # return empty

    nhind = 0
    whind = 0
    i = 1
    for al in pm:
        for lm in pm:
            logging.info("::::: Optimizing from starting value %d :::::" %
                         (i, ))
            w0 = M0.w.copy()
            w0[1:hf0] *= al
            p0 = M0.pi.copy()
            p0[0] *= lm
            p0[-1] = 1 - p0[0] - p0[1]

            if modulation or doublemodulation:
                M_ = model.history_model(r,
                                         X,
                                         applythreshold=applythreshold,
                                         w0=w0,
                                         p0=p0,
                                         nu0=M0.nu,
                                         lm=0.1,
                                         hf0=hf0,
                                         verbose=True,
                                         emiter=300,
                                         storeopt=storeopt)
                if Mhmod.loglikelihood < M_.loglikelihood:
                    logging.info("  *model chosen for history + modulation*")
                    Mhmod = M_
                    whind = i

                if modulation:
                    M_ = model.history_model(r,
                                             X[:, :-2],
                                             applythreshold=applythreshold,
                                             w0=w0,
                                             p0=p0,
                                             nu0=M0.nu,
                                             lm=0.1,
                                             hf0=hf0,
                                             verbose=True,
                                             emiter=300,
                                             storeopt=storeopt)
                elif doublemodulation:
                    M_ = model.history_model(r,
                                             X[:, :-3],
                                             applythreshold=applythreshold,
                                             w0=w0,
                                             p0=p0,
                                             nu0=M0.nu,
                                             lm=0.1,
                                             hf0=hf0,
                                             verbose=True,
                                             emiter=300,
                                             storeopt=storeopt)
                if Mwh.loglikelihood < M_.loglikelihood:
                    logging.info("  *model chosen for history*")
                    Mwh = M_
                    whind = i
            else:
                M_ = model.history_model(r,
                                         X,
                                         applythreshold=applythreshold,
                                         w0=w0,
                                         p0=p0,
                                         nu0=M0.nu,
                                         lm=0.1,
                                         hf0=hf0,
                                         verbose=True,
                                         emiter=300,
                                         storeopt=storeopt)
                if Mwh.loglikelihood < M_.loglikelihood:
                    logging.info("  *model chosen for history*")
                    Mwh = M_
                    whind = i

            M_ = model.history_model(r,
                                     X[:, :hf0],
                                     applythreshold=applythreshold,
                                     w0=w0,
                                     p0=p0,
                                     nu0=M0.nu,
                                     lm=0.1,
                                     hf0=hf0,
                                     verbose=True,
                                     emiter=300,
                                     storeopt=storeopt)
            if Mnh.loglikelihood < M_.loglikelihood:
                logging.info("  *model chosen for independent*")
                Mnh = M_
                nhind = i
            i += 1

    logging.info("Mwh.w = %s\nMnh.w = %s" % (str(Mwh.w), str(Mnh.w)))
    logging.info("Mwh.ll = %g\nMnh.ll = %s" %
                 (Mwh.loglikelihood, Mnh.loglikelihood))
    logging.info(
        "Starting values:\n  with history: %d\n  without history: %d\n" %
        (whind, nhind))

    # NOW, THE HISTORY ONLY MODEL HAS SIZE 22!
    print "X", np.shape(X)
    print "Mwh", Mwh.w, np.shape(Mwh.X)
    if modulation:
        print "Mhmod", Mhmod.w, np.shape(Mhmod.X)

    return Mnh, Mwh, Mhmod