Public API#

This page documents the public API that users should interact with. All classes and functions listed here are stable and intended for public use.

Note

For complete internal API usage, see the Internal API instead.

Models (stanbkt.models)#

Model definitions and related classes, including model variants and Bayesian prior specifications.

Variants#

The different types of models available in stanbkt.

Example Usage:

from stanbkt.models import StandardBKT

# Create an instance of the StandardBKT model
# By default, models use MCMC for estimation.
model = StandardBKT()

BKTModelBase([fit_method, ...])

Abstract base class for Stan Bayesian Knowledge Tracing (BKT) models.

StandardBKT([fit_method, ...])

Bayesian Knowledge Tracing (BKT) model implementation.

MultiBKT([fit_method, verbose, ...])

Grouped Bayesian Knowledge Tracing model.

Initial Knowledge Strategy#

Strategy enum for modeling initial knowledge behavior.

Example Usage:

from stanbkt.models import StandardBKT, InitKnowledgeStrategy

# instantiate a model that jointly estimates initial knowledge using student level pretest and correctess data
model = StandardBKT(
    individual_initial_knowledge=True,
    init_knowledge_strategy=InitKnowledgeStrategy.JOINT,
)

InitKnowledgeStrategy(*values)

Enumeration of initial knowledge estimation strategies.

Bayesian Priors#

Bayesian prior specification.

Example Usage:

from stanbkt.models import StandardBKT, StandardPriors
from stanbkt.fits import FitMethod

# create a model with the specified estimation method
model = StandardBKT(FitMethod.MCMC)

# change the prior for pi_know (i.e. probability of knowing the skill at the start) to have
# a mean of 0.2 and a sd of 0.1 on the logit scale.
priors = StandardPriors(pi_know_mu=0.2, pi_know_sigma=0.1)
# Fit the model to the data
# Note: Passing a single prior object will apply the same prior to all KCs in the data.
#       To apply different priors to different KCs, pass a dict mapping KC ID to a prior object instead.
model.fit(data, priors=priors)

StandardPriors([pi_b0_know_mu, ...])

Bayesian priors for the Standard BKT model parameters.

Fits (stanbkt.fits)#

Module containing fit methods, fit configuration options, and fit result classes.

Fit Method#

Types of inference methods available for fitting models to data. These include MCMC, Variational Inference, Maximum Likelihood Estimation, and Pathfinder. Usage: model = StandardBKT(fit_method = FitMethod.MCMC). Alternatively, users can specify the fitting method directly e.g. model = StandardBKT(fit_method='mcmc').

FitMethod(*values)

Enumeration of supported fitting methods.

Fit Configuration#

Configuration options for different inference methods. These wrap around the options provided by cmdstanpy.CmdStanModel to provide a consistent typed interface. Specifically, these are options that are passed to cmdstanpy.CmdStanModel.sample(), cmdstanpy.CmdStanModel.optimize(), cmdstanpy.CmdStanModel.pathfinder(), or cmdstanpy.CmdStanModel.variational() depending on the chosen fit method.

from stanbkt.fits import MCMCFitOptions, FitMethod
from stanbkt.models import StandardBKT

options = MCMCFitOptions(
    iter_sampling=1000,
    iter_warmup=500,
    chains=4,
    seed=42,
)

model = StandardBKT(FitMethod.MCMC)
# this will raise an error if the options provided are not compatible with the chosen fit method
model.fit(data, stan_fit_options=options)

BaseFitOptions([seed, extra_kwargs])

Base dataclass for typed Stan fit options.

MCMCFitOptions([seed, extra_kwargs, chains, ...])

Common options for cmdstanpy.CmdStanModel.sample().

VBFitOptions([seed, extra_kwargs, ...])

Common options for cmdstanpy.CmdStanModel.variational().

MLEFitOptions([seed, extra_kwargs, ...])

Common options for cmdstanpy.CmdStanModel.optimize().

PFFitOptions([seed, extra_kwargs, ...])

Common options for cmdstanpy.CmdStanModel.pathfinder().

Fit Results#

Fit result objects for different inference methods.

These classes encapsulate the results of fitting a model to data, including parameter estimates, diagnostics, and other relevant information. They are typically not used directly by users, however they can be used to access the underlying CmdStanPy Fit objects if needed.

FitBase([verbose, fits, fit_metadata, ...])

Base class for StanBKT fits.

MCMCFit([verbose, fits, fit_metadata, ...])

Fit class using Markov Chain Monte Carlo (MCMC) sampling.

MLEFit([verbose, fits, fit_metadata, ...])

Fit class using Maximum Likelihood Estimation (MLE) / Optimization.

VBFit([verbose, fits, fit_metadata, ...])

Fit class using Variational Bayes (VB) approximation.

PathfinderFit([verbose, fits, fit_metadata, ...])

Fit class using Pathfinder variational approximation.

Plotting (stanbkt.plot)#

Posterior visualization of model correctness, including:
  • Posterior predictive distributions of the probability of correctness

  • Predictions sampled from the posterior predictive distribution

plot_posterior_correctness(...[, grouped, ...])

Plot posterior predictions of correctness for a given KC.

Utilities (stanbkt.utils)#

CmdStanPy Setup#

StanBTK uses CmdStan for model compilation, fitting and inference. This utility function installs CmdStan and sets the appropriate environment variables. This is required for using StanBKT for the first time, but only needs to be done once per machine. On Windows, this always install RTools which installs the c++ compiler and make binary. See the CmdStanPy installation documentation for more details.

setup_cmdstanpy([n_cores])

Set up CmdStanPy by checking for CmdStan installation and setting the path if necessary.

Mapping Column Names#

Utility class to map expected column names to the actual user provided column names in the data.

from stanbkt.utils import ColumnNames

# Create a dict mapping expected column names to actual column names in the data
col_mapping = {
      ColumnNames.STUDENT_ID: "user_id",
      ColumnNames.PROBLEM_ID: "item_id",
      ColumnNames.CORRECTNESS: "is_correct",
      ColumnNames.ORDER: "timestamp",
   }
# by default, models will use MCMC for estimation.
model = StandardBKT()
# fit to data
model.fit(data, column_mapping=col_mapping)
# predict p(correct) and p(know)
model.predict(data, column_mapping=col_mapping)

ColumnNames(*values)

Enumeration of standard column names for BKT data.

Logging Control#

Control verbosity to specify the amount of logging information printed during model fitting and prediction. Three levels are available: DEBUG, INFO, and WARN, listed in decreasing order of verbosity.

from stanbkt.utils import VerbosityLevel
# Create Model with verbosity level set to WARNING
model = StandardBKT(verbosity=VerbosityLevel.WARN)
...
# this can be changed anytime
model.set_verbosity(VerbosityLevel.DEBUG)

VerbosityLevel(*values)

Enumeration of verbosity levels for logging output.

Compilation Cache Management#

StanBKT caches compiled Stan models to speed up subsequent fits and will automatically rebuild the models if the underlying Stan code changes.

clear_stan_cache([stan_file, cpp_options, ...])

Clear the compiled Stan model cache.

get_stan_model_cache_dir(stan_file[, ...])

Return the cache directory for a compiled Stan executable.

get_cache_root()

Return the root cache directory for all compiled Stan models.

list_cached_models([print_fn])

List all cached Stan model directories.