Custom Tutorial 2: Using your model#
In this tutorial, we show you how to use the model you have created. If you have not done so already, go through Custom Tutorial 1 and 1a.
Once you have done this, import PyBEAM’s custom submodule.
[1]:
# import PyBEAM's custom submodule
import pybeam.custom as pbc
# also import pyplot to modify a figure
import matplotlib.pyplot as plt
# import os to check if you have input your model directory correctly
import os
Next, we need to tell the program where your model is located. To do this, create a string containing the FULL directory name. This will be input into future functions.
For this example, we use the custom model built in Tutorial 1a, the UGM_flip. The files needed for this model are located on the PyBEAM Github in folder UGM_flip.
To check that we input the directory correctly, we call the function isdir. If it outputs True, then we know we have specificed a directory on our computer.
[2]:
# the directory containing your model goes here
model_dir = ''
# for windows computers, r before the directory is necessary
# model_dir = r''
# check if directory is input properly
os.path.isdir(model_dir)
[2]:
True
Next, pybeam.custom provides a tool to test the functions you created in your model.pyx file. To use this, we must first specify a dictionary of parameter inputs. This has the same form as the precoded sub-module, but instead of referring to the parameters by their names, we refer to them by their array index.
Let’s test the model we created in Custom Tutorial 1. First, specify the full directory name where the model is located as above. Next, define the phi_test dictionary below. This dictionary contains the paramters used by your model. The keys of this dictionary specify which phi index the model.pyx file uses for that parameter, going from ‘phi[0]’, ‘phi[1]’, … , ‘phi[N_phi-1]’. For example, in the UGM_flip model file, parameter phi[0] was associated with the non-decision time. Thus, key ‘phi[0]’ is the parameter for phi[0], giving the non-decision time. Similarly, ‘phi[1]’ references parameter phi[1] which is in the relative start point function.
Once you have this dictionary, call the function functions_test. This accepts inputs of your model_dir location, parameter dictionary phi, accumulator state x, and time t. It outputs a list containing two columns: the model function names and the values output by those model functions. This provides a quick way to check that your functions are doing what you expect them to.
For example, I have set phi[0] to 0.25. Since phi[0] is under the non_decision_time function, I would expect that function to output 0.25. Running functions_test verifies that this is the case and that no programming error has been made.
It is STRONGLY recommended you run this function with any custom model you make.
[3]:
# dictionary containing model parameters
phi = {'phi[0]' : 0.25, # tnd: non-decision time
'phi[1]' : 0.5, # w: relative start point
'phi[2]' : 1.0, # mu: drift rate
'phi[3]' : 3.0, # l: leakage rate
'phi[4]' : 2.0, # k: urgency ratio
'phi[5]' : 0.5, # t_flip: time for first drift rate flip
'phi[6]' : 1.0, # sigma: model scale
'phi[7]' : 1.0} # b: threshold location
pbc.functions_test(model_dir = model_dir, # string containing directory where your model files are
phi = phi, # dictionary of model parameters
x = 0.0, # accumulator state
t = 1.0) # time
[3]:
[['non_decision_time', 0.25],
['relative_start', 0.5],
['drift', -3.0],
['diffusion', 3.0],
['upper_decision_threshold', 1.0],
['lower_decision_threshold', -1.0],
['contamination_strength', 0.0],
['contamination_probability', 0.0],
['modify_dt', 1.0]]
The remaining functions we introudce in this file are the same as for the pybeam.precoded module. Two changes are present in them. First, instead of defining a model class as you do in the precoded module, you instead define the model directory. Second, as of now, contamination models can be fit with the parmeter inference tool, but they can’t be simulated using the simulate function. This functionality will be implemented in a future PyBEAM update.
[4]:
# simulate data from the model
# NOTE: In the present state of PyBEAM, we are not able
# to simulate the contamination model. It still works
# for fitting and plotting purposes.
rt = pbc.simulate(N_sims = 100, # number of data points to simulate
model_dir = model_dir, # string containing directory where your model files are
phi = phi) # model parameters, used to simulate data
rt
[4]:
{'rt_upper': array([0.5523, 0.6935, 0.6452, 0.7105, 0.7537, 0.8728, 0.4295, 0.453 ,
0.4843, 0.4314, 0.687 , 0.9114, 0.594 , 0.5939, 0.575 , 0.5578,
0.5355, 1.2613, 0.4261, 0.812 , 0.6462, 0.5585, 0.5582, 0.5639,
0.7097, 1.1483, 0.5429, 0.6518, 0.6339, 0.6194, 0.5383, 0.7037,
0.6582, 0.7076, 0.7137, 0.5956, 0.5975, 0.815 , 0.4161, 0.4014,
0.8796, 0.3745, 0.5561, 0.6263, 0.6889, 0.4994, 0.5777, 0.5709,
0.7218, 0.7643, 0.595 , 1.0685, 0.9713, 0.8163, 0.4569, 0.431 ,
0.4455, 0.4208, 0.8969, 0.4422, 0.4119, 0.7364, 0.7035, 0.475 ,
0.9278]),
'rt_lower': array([0.8297, 0.5946, 0.8823, 0.7726, 0.3724, 0.4491, 0.9788, 1.3331,
1.0076, 0.6229, 0.9686, 0.9672, 0.7522, 0.8751, 0.5742, 0.8075,
0.8841, 1.0738, 0.4777, 0.5293, 0.8662, 1.0687, 0.8049, 0.7619,
1.474 , 1.1098, 0.9 , 0.3908, 0.8069, 1.0501, 0.5302, 0.7165,
0.9839, 0.8121, 0.9214])}
[5]:
# calculate model likelihood function
lh = pbc.likelihood(model_dir = model_dir, # string containing directory where your model files are
phi = phi, # dictionary of model parameters, used to simulate data
rt_max = 2.0) # maximum time to solve likelihood function to
lh
[5]:
{'time': array([0.25010375, 0.2502075 , 0.25031125, 0.250415 , 0.25051875,
0.2506225 , 0.25072625, 0.25083 , 0.25093375, 0.2510375 ,
0.25114125, 0.251245 , 0.25134875, 0.2514525 , 0.25155625,
0.25166 , 0.25176375, 0.2518675 , 0.25197125, 0.252075 ,
0.25217875, 0.2522825 , 0.25238625, 0.25249 , 0.25259375,
0.25280021, 0.25310939, 0.25352127, 0.25403588, 0.25465319,
0.25537321, 0.25619595, 0.2571214 , 0.25814956, 0.25928044,
0.26051402, 0.26185032, 0.26328934, 0.26483106, 0.2664755 ,
0.26822265, 0.27007251, 0.27202509, 0.27408037, 0.27623837,
0.27849909, 0.28086251, 0.28332865, 0.2858975 , 0.28856906,
0.29134334, 0.29422033, 0.29720003, 0.30028244, 0.30346756,
0.3067554 , 0.31014595, 0.31363921, 0.31723519, 0.32093388,
0.32473528, 0.32863939, 0.33264621, 0.33675575, 0.340968 ,
0.34528296, 0.34970064, 0.35422103, 0.35884413, 0.36356994,
0.36839846, 0.3733297 , 0.37836365, 0.38350031, 0.38873969,
0.39408178, 0.39952658, 0.40507409, 0.41072431, 0.41647725,
0.4223329 , 0.42829126, 0.43435234, 0.44051613, 0.44678263,
0.45315184, 0.45962376, 0.4661984 , 0.47287575, 0.47965581,
0.48653859, 0.49352408, 0.50061228, 0.50780319, 0.51509681,
0.52249315, 0.5299922 , 0.53759396, 0.54529844, 0.55310563,
0.56101553, 0.56902814, 0.57714346, 0.5853615 , 0.59368225,
0.60210571, 0.61063189, 0.61926078, 0.62799238, 0.63682669,
0.64576371, 0.65480345, 0.6639459 , 0.67319106, 0.68253894,
0.69198953, 0.70154283, 0.71119884, 0.72095756, 0.730819 ,
0.74078315, 0.75085001, 0.76101959, 0.77129188, 0.78166688,
0.79204188, 0.80241688, 0.81279188, 0.82316688, 0.83354188,
0.84391688, 0.85429188, 0.86466688, 0.87504188, 0.88541688,
0.89579188, 0.90616688, 0.91654188, 0.92691688, 0.93729188,
0.94766688, 0.95804188, 0.96841688, 0.97879188, 0.98916688,
0.99954188, 1.00991688, 1.02029188, 1.03066688, 1.04104188,
1.05141688, 1.06179188, 1.07216688, 1.08254188, 1.09291688,
1.10329188, 1.11366688, 1.12404188, 1.13441688, 1.14479188,
1.15516688, 1.16554188, 1.17591688, 1.18629188, 1.19666688,
1.20704188, 1.21741688, 1.22779188, 1.23816688, 1.24854188,
1.25891688, 1.26929188, 1.27966688, 1.29004188, 1.30041688,
1.31079188, 1.32116688, 1.33154188, 1.34191688, 1.35229188,
1.36266688, 1.37304188, 1.38341688, 1.39379188, 1.40416688,
1.41454188, 1.42491688, 1.43529188, 1.44566688, 1.45604188,
1.46641688, 1.47679188, 1.48716688, 1.49754188, 1.50791688,
1.51829188, 1.52866688, 1.53904188, 1.54941688, 1.55979188,
1.57016688, 1.58054188, 1.59091688, 1.60129188, 1.61166688,
1.62204188, 1.63241688, 1.64279188, 1.65316688, 1.66354188,
1.67391688, 1.68429188, 1.69466688, 1.70504188, 1.71541688,
1.72579188, 1.73616688, 1.74654188, 1.75691688, 1.76729188,
1.77766688, 1.78804188, 1.79841688, 1.80879188, 1.81916688,
1.82954188, 1.83991688, 1.85029188, 1.86066688, 1.87104188,
1.88141688, 1.89179188, 1.90216688, 1.91254188, 1.92291688,
1.93329188, 1.94366688, 1.95404188, 1.96441688, 1.97479188,
1.98516688, 1.99554188, 2.00591688]),
'lh_upper': array([1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.51349580e-05, 4.38659389e-05, 1.15241233e-04,
2.76756711e-04, 6.12429157e-04, 1.25811861e-03, 2.41610233e-03,
4.36548582e-03, 7.46522778e-03, 1.21476789e-02, 1.89022485e-02,
2.82506078e-02, 4.07162422e-02, 5.67919048e-02, 7.69085520e-02,
1.01408789e-01, 1.30526943e-01, 1.64376867e-01, 2.02947589e-01,
2.46106194e-01, 2.93606756e-01, 3.45103883e-01, 4.00169359e-01,
4.58310425e-01, 5.18988465e-01, 5.81637066e-01, 6.45678709e-01,
7.10539562e-01, 7.75662099e-01, 8.40515411e-01, 9.04603253e-01,
9.67469941e-01, 1.02870430e+00, 1.08794190e+00, 1.14486583e+00,
1.19920626e+00, 1.25073906e+00, 1.29928363e+00, 1.34470023e+00,
1.38688688e+00, 1.42577610e+00, 1.46133143e+00, 1.49354400e+00,
1.52242913e+00, 1.54802305e+00, 1.57037969e+00, 1.58956778e+00,
1.60566803e+00, 1.61877056e+00, 1.62897262e+00, 1.63637643e+00,
1.64108735e+00, 1.64321224e+00, 1.64285804e+00, 1.64013064e+00,
1.63513392e+00, 1.62796903e+00, 1.61873384e+00, 1.60752266e+00,
1.59442603e+00, 1.57953072e+00, 1.56291985e+00, 1.54467311e+00,
1.52486708e+00, 1.50357560e+00, 1.48087017e+00, 1.45682041e+00,
1.43149449e+00, 1.40495958e+00, 1.37728226e+00, 1.34852890e+00,
1.31876599e+00, 1.28806049e+00, 1.25648007e+00, 1.22409329e+00,
1.19096984e+00, 9.54311353e-01, 7.47964257e-01, 6.68680514e-01,
5.88202228e-01, 5.40615082e-01, 4.91583147e-01, 4.58887209e-01,
4.24547698e-01, 4.00283219e-01, 3.74363940e-01, 3.55359381e-01,
3.34726654e-01, 3.19151615e-01, 3.01992338e-01, 2.88701551e-01,
2.73887928e-01, 2.62143370e-01, 2.48955394e-01, 2.38284639e-01,
2.26265523e-01, 2.16377006e-01, 2.05246005e-01, 1.95971729e-01,
1.85565814e-01, 1.76819977e-01, 1.67052687e-01, 1.58801293e-01,
1.49633375e-01, 1.41872271e-01, 1.33291047e-01, 1.26030578e-01,
1.18035904e-01, 1.11290693e-01, 1.03885840e-01, 9.76684102e-02,
9.08545223e-02, 8.51717018e-02, 7.89447750e-02, 7.37960260e-02,
6.81456014e-02, 6.35226384e-02, 5.84316187e-02, 5.43188412e-02,
4.97640033e-02, 4.61394096e-02, 4.20923195e-02, 3.89286715e-02,
3.53567966e-02, 3.26228789e-02, 2.94907628e-02, 2.71526319e-02,
2.44230399e-02, 2.24451959e-02, 2.00801735e-02, 1.84266112e-02,
1.63884195e-02, 1.50235350e-02, 1.32754422e-02, 1.21647833e-02,
1.06717054e-02, 9.78256445e-03, 8.51155507e-03, 7.81340934e-03,
6.73400751e-03, 6.19881862e-03, 5.28326514e-03, 4.88565253e-03,
4.10899180e-03, 3.82629699e-03, 3.16638113e-03, 2.97864151e-03,
2.41605515e-03, 2.30590565e-03, 1.82382911e-03, 1.77634964e-03,
1.36037765e-03, 1.36290258e-03, 1.00083369e-03, 1.04273808e-03,
7.24348108e-04, 7.96823027e-04, 5.13633796e-04, 6.09460913e-04,
3.54513157e-04, 4.67846578e-04, 2.35483819e-04, 3.61645002e-04,
1.47313182e-04, 2.82602899e-04, 8.26687990e-05, 2.24198449e-04,
3.57884496e-05, 1.81331690e-04, 1.00000000e-05, 1.50055824e-04,
2.15710674e-05, 1.27347956e-04, 3.81242501e-05, 1.10916594e-04,
4.94470881e-05, 9.90424629e-05, 5.70151546e-05, 9.04487737e-05,
6.19172551e-05, 8.41969732e-05, 6.49483291e-05, 7.96041181e-05,
6.66825870e-05, 7.61782498e-05, 6.75303306e-05, 7.35685074e-05,
6.77815278e-05, 7.15271083e-05, 6.76388055e-05, 6.98807384e-05,
6.72421155e-05, 6.85092889e-05, 6.66869486e-05, 6.73302484e-05,
6.60376165e-05, 6.62873849e-05, 6.53368204e-05]),
'lh_lower': array([1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.00000000e-05,
1.00000000e-05, 1.00000000e-05, 1.00000000e-05, 1.60517592e-05,
3.86794226e-05, 8.58872370e-05, 1.77057321e-04, 3.41240226e-04,
6.18824362e-04, 1.06220709e-03, 1.73514577e-03, 2.71070036e-03,
4.06793030e-03, 5.88771241e-03, 8.24816131e-03, 1.12201518e-02,
1.48633771e-02, 1.92232601e-02, 2.43288956e-02, 3.01920701e-02,
3.68072953e-02, 4.41527141e-02, 5.21916942e-02, 6.08749066e-02,
7.01426923e-02, 7.99275452e-02, 9.01565639e-02, 1.00753762e-01,
1.11642162e-01, 1.22745612e-01, 1.33990322e-01, 1.45306093e-01,
1.56627265e-01, 1.67893395e-01, 1.79049710e-01, 1.90047337e-01,
2.00843372e-01, 2.11400801e-01, 2.21688307e-01, 2.31679985e-01,
2.41354997e-01, 2.50697174e-01, 2.59694589e-01, 2.68339104e-01,
2.76625914e-01, 2.84553081e-01, 2.92121068e-01, 2.99332282e-01,
3.06190620e-01, 3.12701036e-01, 3.18869105e-01, 3.24700626e-01,
3.30201228e-01, 3.35376020e-01, 3.40229258e-01, 3.44764062e-01,
3.48982170e-01, 3.52883743e-01, 3.56467222e-01, 3.59729243e-01,
3.62664607e-01, 3.65266312e-01, 3.67525643e-01, 3.69432317e-01,
3.70974679e-01, 3.72139945e-01, 3.72914485e-01, 3.73284134e-01,
3.73234538e-01, 3.72751500e-01, 3.71821347e-01, 3.70431288e-01,
3.68569767e-01, 3.66226800e-01, 3.63394292e-01, 3.60066316e-01,
3.56239379e-01, 4.36453280e-01, 5.31915163e-01, 5.87842076e-01,
6.40216194e-01, 6.77467535e-01, 7.09134317e-01, 7.29551330e-01,
7.44062138e-01, 7.49431528e-01, 7.49672833e-01, 7.42871305e-01,
7.32297535e-01, 7.16665055e-01, 6.98640822e-01, 6.77210826e-01,
6.54539546e-01, 6.29730675e-01, 6.04546657e-01, 5.78149487e-01,
5.51989226e-01, 5.25266825e-01, 4.99193040e-01, 4.73003435e-01,
4.47725978e-01, 4.22630999e-01, 3.98606137e-01, 3.74957127e-01,
3.52462704e-01, 3.30463881e-01, 3.09653603e-01, 2.89407120e-01,
2.70348625e-01, 2.51885868e-01, 2.34586966e-01, 2.17890041e-01,
2.02316709e-01, 1.87333213e-01, 1.73422043e-01, 1.60075371e-01,
1.47742383e-01, 1.35939145e-01, 1.25086247e-01, 1.14721830e-01,
1.05241763e-01, 9.62046597e-02, 8.79849232e-02, 8.01602348e-02,
7.30862901e-02, 6.63586070e-02, 6.03165247e-02, 5.45723037e-02,
4.94509384e-02, 4.45804436e-02, 4.02731873e-02, 3.61720303e-02,
3.25781769e-02, 2.91484848e-02, 2.61742335e-02, 2.33254726e-02,
2.08846005e-02, 1.85340869e-02, 1.65483229e-02, 1.46214552e-02,
1.30205948e-02, 1.14508472e-02, 1.01726460e-02, 8.90136226e-03,
7.89125090e-03, 6.86728117e-03, 6.07794108e-03, 5.25715935e-03,
4.64799865e-03, 3.99273880e-03, 3.52930268e-03, 3.00774566e-03,
2.66109256e-03, 2.24663327e-03, 1.99270274e-03, 1.66332027e-03,
1.48231405e-03, 1.21996399e-03, 1.09575637e-03, 8.85799281e-04,
8.05388887e-04, 6.36064546e-04, 5.89075059e-04, 4.51028364e-04,
4.29262441e-04, 3.15123595e-04, 3.12171213e-04, 2.16190148e-04,
2.27090300e-04, 1.44823336e-04, 1.65776283e-04, 9.38215439e-05,
1.21947633e-04, 5.77248298e-05, 9.08651840e-05, 3.24348273e-05,
6.89888738e-05, 1.49058415e-05, 5.37006180e-05, 1.00000000e-05,
4.30834691e-05, 1.00000000e-05, 3.57478582e-05, 1.06326080e-05,
3.06965841e-05, 1.41700648e-05, 2.72211980e-05, 1.64291076e-05,
2.48234468e-05, 1.78244972e-05, 2.31564304e-05, 1.86431978e-05,
2.19810516e-05, 1.90816666e-05, 2.11341639e-05, 1.92729626e-05,
2.05055513e-05, 1.93062240e-05, 2.00214850e-05])}
[6]:
# calculate loglikelihood of rt data using model's likelihood function
ll = pbc.loglikelihood(model_dir = model_dir, # string containing directory where your model files are
phi = phi, # model parameters, used to simulate data
rt = rt) # dictionary of rt data
ll
[6]:
-38.37405883327233
[7]:
# plot data and model likelihood function
pbc.plot_rt(model_dir = model_dir, # string containing directory where your model files are
phi = phi, # model parameters, used to simulate data
rt_max = 1.75, # maximum time to solve likelihood function to
rt = rt); # dictionary of rt data