Package: metadpy
metadpyis a Python implementation of standard Bayesian models of behavioural metacognition. It is aimed to provide simple yet powerful functions to compute standard indexes and metrics of signal detection theory (SDT) and metacognitive efficiency (meta-d’ and hierarchical meta-d’). The only input required is a data frame encoding task performances and confidence ratings at the trial level.metadpyis written in Python 3. It usesNumpy,ScipyandPandasfor most of its operation, comprizing meta-d’ estimation using maximum likelihood estimation (MLE). The (Hierarchical) Bayesian modelling is implemented inAesara(now renamedPyTensorfor versions ofpymc>=5.0).📖Documentation✏️TutorialsInstallationThe package can be installed using pip:pipinstallmetadpyFor most of the operations, the following packages are required:Numpy(>=1.15)Scipy(>=1.3.0)Pandas(>=0.24)Matplotlib(>=3.0.2)Seaborn(>=0.9.0)Bayesian models will require:PyTensorpymc(>=5.0)Why metadpy?metadpy stands for meta-d' (meta-d prime) in Python. meta-d' is a behavioural metric commonly used in consciousness and metacognition research. It is modelled to reflect metacognitive efficiency (i.e the relationship between subjective reports about performances and objective behaviour).metadpy first aims to be the Python equivalent of thehMeta-d toolbox(Matlab and R). It tries to make these models available to a broader open-source ecosystem and to ease their use via cloud computing interfaces. One notable difference is that While thehMeta-d toolboxrelies on JAGS for the Bayesian modelling of confidence data (see [4]) to analyse task performance and confidence ratings, metadpy is built on the top ofpymc, and uses Hamiltonina Monte Carlo methods (NUTS).For an extensive introduction to metadpy, you can navigate the following notebooks that are Python adaptations of the introduction to thehMeta-d toolboxwritten in Matlab by Olivia Faul for theZurich Computational Psychiatry course.TutorialsNotebookColabWhat metacognition looks like?Fitting the model (MLE)Comparing with the hmetad toolboxExamplesNotebookColabSubject and group level (MLE)Subject and group level (Bayesian)Or just follow the quick tour below.Importing dataClassical metacognition experiments contain two phases: task performance and confidence ratings. The task performance could for example be the ability to distinguish the presence of a dot on the screen. By relating trials where stimuli are present or absent and the response provided by the participant (Can you see the dot: yes/no), it is possible to obtain the accuracy. The confidence rating is proposed to the participant when the response is made and should reflect how certain the participant is about his/her judgement.An ideal observer would always associate very high confidence ratings with correct task-I responses, and very low confidence ratings with an incorrect task-1 response, while a participant with a low metacognitive efficiency will have a more mixed response pattern.A minimal metacognition dataset will therefore consist in a data frame populated with 5 columns:Stimuli: Which of the two stimuli was presented [0 or 1].Response: The response made by the participant [0 or 1].Accuracy: Was the participant correct? [0 or 1].Confidence: The confidence level [can be continuous or discrete rating].ntrial: The trial number.Due to the logical dependence between theStimuli,ResponsesandAccuracycolumns, in practice only two of those columns are necessary, the third being deduced from the others. Most of the functions inmetadpywill accept DataFrames containing only two of these columns, and will automatically infer the missing information. Similarly, as the metacognition models described here does not incorporate the temporal dimension, the trial number is optional.metadpyincludes a simulation function that will let you create one such data frame for one or many participants and condition, controlling for a variety of parameters. Here, we will simulate 200 trials from  participant havingd=1andc=0(task performances) and ameta-d=1.5(metacognitive sensibility). The confidence ratings were provided using a 1-to-4 rating scale.frommetadpy.utilsimportresponseSimulationsimulation=responseSimulation(d=1,metad=1.5,c=0,nRatings=4,nTrials=200)simulationStimuliResponsesAccuracyConfidencenTrialSubject01114001001410211122030104304001340frommetadpy.utilsimporttrials2countsnR_S1,nR_S2=trials2counts(data=simulation,stimuli="Stimuli",accuracy="Accuracy",confidence="Confidence",nRatings=4)Data visualizationYou can easily visualize metacognition results using one of the plotting functions. Here, we will use theplot_confidenceand theplot_rocfunctions to visualize the metacognitive performance of our participant.importmatplotlib.pyplotaspltfrommetadpy.plottingimportplot_confidence,plot_rocfig,axs=plt.subplots(1,2,figsize=(13,5))plot_confidence(nR_S1,nR_S2,ax=axs[0])plot_roc(nR_S1,nR_S2,ax=axs[1])Signal detection theory (SDT)frommetadpy.sdtimportcriterion,dprime,rates,roc_auc,scoresAll metadpy functions are registred as Pandas flavors (seepandas-flavor), which means that the functions can be called as a method from the result data frame.simulation.criterion()5.551115123125783e-17simulation.dprime()0.9917006946949065simulation.rates()(0.69, 0.31)simulation.roc_auc(nRatings=4)0.695689287238583simulation.scores()(69, 31, 31, 69)Estimating meta dprime using Maximum Likelyhood Estimates (MLE)frommetadpy.mleimportmetadmetad(data=simulation,nRatings=4,stimuli='Stimuli',accuracy='Accuracy',confidence='Confidence',verbose=0)dprimemeta_dm_ratiom_diff00.9706351.459251.50340.488613Estimating meta-dprime using hierarchical Bayesian modelingSubject levelimportpymcaspmfrommetadpy.bayesianimporthmetadmodel,trace=hmetad(data=simulation,nRatings=4,stimuli='Stimuli',accuracy='Accuracy',confidence='Confidence')Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [c1, d1, meta_d, cS1_hn, cS2_hn]
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 10 seconds.importarvizasazaz.plot_trace(trace,var_names=['meta_d','cS2','cS1']);az.summary(trace)meansdhdi_3%hdi_97%mcse_meanmcse_sdess_bulkess_tailr_hatmeta_d1.3840.2540.9091.860.0040.003327029801References[1] Maniscalco, B., & Lau, H. (2014). Signal Detection Theory Analysis of Type 1 and Type 2 Data: Meta-d′, Response-Specific Meta-d′, and the Unequal Variance SDT Model. In The Cognitive Neuroscience of Metacognition (pp. 25–66). Springer Berlin Heidelberg.https://doi.org/10.1007/978-3-642-45190-4_3[2] Maniscalco, B., & Lau, H. (2012). A signal detection theoretic approach for estimating metacognitive sensitivity from confidence ratings. Consciousness and Cognition, 21(1), 422–430. doi:10.1016/j.concog.2011.09.021[3] Fleming, S. M., & Lau, H. C. (2014). How to measure metacognition. Frontiers in Human Neuroscience, 8.https://doi.org/10.3389/fnhum.2014.00443[4] Fleming, S.M. (2017) HMeta-d: hierarchical Bayesian estimation of metacognitive efficiency from confidence ratings, Neuroscience of Consciousness, 3(1) nix007,https://doi.org/10.1093/nc/nix007