PyDDM icon indicating copy to clipboard operation
PyDDM copied to clipboard

access trial-by-trial drift

Open alessandromazza93 opened this issue 2 years ago • 2 comments

I am trying to fit across-trials drifts, but i cannot access the estimated drifts.

I followed the steps suggested on the documentation page, but maybe i'm doing something wrong (?):

Create a sample object to be input in the model.

    df_sample = Sample.from_pandas_dataframe(df_to_fit, 
                                            rt_column_name="RT", 
                                            choice_column_name="Choice", 
                                            choice_names=("Yes", "No"))
    

Create function for trial-by-trial drift subclass coming next

    RESOLUTION = n_tot 
    def prepare_sample_for_variable_drift(orig_sample, resolution=RESOLUTION):
        new_samples = []
        for i in range(0, resolution):
            choice_upper = orig_sample.choice_upper.copy()
            choice_lower = orig_sample.choice_lower.copy()
            undecided = orig_sample.undecided
            conditions = copy.deepcopy(orig_sample.conditions)
            conditions['driftnum'] = (np.asarray([i]*len(choice_upper)),
                                    np.asarray([i]*len(choice_lower)),
                                    np.asarray([i]*undecided))
            new_samples.append(Sample(choice_upper, choice_lower, undecided, choice_names=orig_sample.choice_names, **conditions))
        new_sample = new_samples.pop()
        for s in new_samples:
            new_sample += s
        return new_sample
    

Create new sample

      new_sample_tbt = prepare_sample_for_variable_drift(df_sample, RESOLUTION)

Define model

    our_drift = DriftUniform(drift=Fittable(minval=-20, maxval=20), width=Fittable(minval=1, maxval=2))
    my_model = Model(name=condition_to_fit,
                    drift=our_drift,
                    noise=NoiseConstant(noise=noise_val),
                    bound=BoundConstant(B=boundaries_val),
                    IC=ICPointRatio(x0=Fittable(minval=-.9, maxval=.9)),
                    overlay=OverlayNonDecision(nondectime=ndt_val),
                    dx=.009,
                    dt=.009,
                    T_dur=rt_hi_abs_thresh,
                    choice_names=("Yes", "No"))

Fitting

    my_model_fit = fit_adjust_model(sample=new_sample_tbt, 
                                    model=my_model, 
                                    fitting_method="differential_evolution",
                                    lossfunction=LossRobustBIC,
                                    verbose=False)

If i try accessing parameters fitted_params = my_model_fit.get_model_parameters() I am returned a single drift estimate.

Which command should i use? Am i missing something?

Thank you in advance for your help!

alessandromazza93 avatar Aug 21 '23 22:08 alessandromazza93

What is "our_drift"? The drift function needs to also be able to use the setup provided by prepare_samples_for_variable_drift, which should be on the same documentation page. But more generally, you will still only get a single drift, which is the centre of the distribution you are using. (In this case, that is the uniform distribution.) You may get a width for your uniform distribution as well if you allow that to be fit.

PyDDM is really not designed for distributions of drift rates. It is always going to be rather hacky, so be warned!

mwshinn avatar Aug 22 '23 12:08 mwshinn

our_drift is an instance of the subclass DriftUniform(Drift) (as reported on the doc page) to use in model definition.

Yes I see your point, thank you for your reply!

alessandromazza93 avatar Aug 22 '23 19:08 alessandromazza93