LinearRegression#
- class causalpy.pymc_models.LinearRegression[source]#
Custom PyMC model for linear regression.
Defines the PyMC model
\[\begin{split}\beta &\sim \mathrm{Normal}(0, 50) \\ \sigma &\sim \mathrm{HalfNormal}(1) \\ \mu &= X \cdot \beta \\ y &\sim \mathrm{Normal}(\mu, \sigma) \\\end{split}\]Example
>>> import causalpy as cp >>> import numpy as np >>> import xarray as xr >>> from causalpy.pymc_models import LinearRegression >>> rd = cp.load_data("rd") >>> rd["treated"] = rd["treated"].astype(int) >>> coeffs = ["x", "treated"] >>> X = xr.DataArray( ... rd[coeffs].values, ... dims=["obs_ind", "coeffs"], ... coords={"obs_ind": rd.index, "coeffs": coeffs}, ... ) >>> y = xr.DataArray( ... rd["y"].values[:, None], ... dims=["obs_ind", "treated_units"], ... coords={"obs_ind": rd.index, "treated_units": ["unit_0"]}, ... ) >>> lr = LinearRegression(sample_kwargs={"progressbar": False}) >>> coords={"coeffs": coeffs, "obs_ind": np.arange(rd.shape[0]), "treated_units": ["unit_0"]} >>> lr.fit(X, y, coords=coords) Inference data...
Methods
LinearRegression.__init__([sample_kwargs, ...])LinearRegression.add_coord(name[, values, ...])Register a dimension coordinate with the model.
LinearRegression.add_coords(coords, *[, lengths])Vectorized version of
Model.add_coord.LinearRegression.add_named_variable(var[, dims])Add a random graph variable to the named variables of the model.
LinearRegression.build_model(X, y, coords)Defines the PyMC model
LinearRegression.calculate_impact(y_true, y_pred)LinearRegression.check_start_vals(start, ...)Check that the logp is defined and finite at the starting point.
LinearRegression.compile_d2logp([vars, ...])Compiled log probability density hessian function.
LinearRegression.compile_dlogp([vars, jacobian])Compiled log probability density gradient function.
LinearRegression.compile_fn(outs, *[, ...])Compiles a PyTensor function.
LinearRegression.compile_logp([vars, ...])Compiled log probability density function.
Clone the model.
LinearRegression.create_value_var(rv_var, *, ...)Create a
TensorVariablethat will be used as the random variable's "value" in log-likelihood graphs.LinearRegression.d2logp([vars, jacobian, ...])Hessian of the models log-probability w.r.t.
LinearRegression.debug([point, fn, verbose])Debug model function at point.
LinearRegression.dlogp([vars, jacobian])Gradient of the models log-probability w.r.t.
Evaluate shapes of untransformed AND transformed free variables.
LinearRegression.fit(X, y[, coords])Draw samples from posterior, prior predictive, and posterior predictive distributions, placing them in the model's idata attribute.
LinearRegression.get_context([...])LinearRegression.initial_point([random_seed])Compute the initial point of the model.
LinearRegression.logp([vars, jacobian, sum])Elemwise log-probability of the model.
Compile a PyTensor function that computes logp and gradient.
LinearRegression.make_obs_var(rv_var, data, ...)Create a TensorVariable for an observed random variable.
Check if name has prefix and adds if needed.
LinearRegression.name_of(name)Check if name has prefix and deletes if needed.
LinearRegression.point_logps([point, round_vals])Compute the log probability of point for all random variables in the model.
Predict data given input data X
Generate priors dynamically based on the input data.
LinearRegression.profile(outs, *[, n, ...])Compile and profile a PyTensor function which returns
outsand takes values of model vars as a dict as an argument.LinearRegression.register_data_var(data[, dims])Register a data variable with the model.
LinearRegression.register_rv(rv_var, name, *)Register an (un)observed random variable with the model.
Clone and replace random variables in graphs with their value variables.
LinearRegression.score(X, y)Score the Bayesian \(R^2\) given inputs
Xand outputsy.LinearRegression.set_data(name, values[, coords])Change the values of a data variable in the model.
LinearRegression.set_dim(name, new_length[, ...])Update a mutable dimension.
LinearRegression.set_initval(rv_var, initval)Set an initial value (strategy) for a random variable.
LinearRegression.to_graphviz(*[, var_names, ...])Produce a graphviz Digraph from a PyMC model.
Attributes
basic_RVsList of random variables the model is defined in terms of.
continuous_value_varsAll the continuous value variables in the model.
coordsCoordinate values for model dimensions.
datalogpPyTensor scalar of log-probability of the observed variables and potential terms.
default_priorsdim_lengthsThe symbolic lengths of dimensions in the model.
discrete_value_varsAll the discrete value variables in the model.
isrootobservedlogpPyTensor scalar of log-probability of the observed variables.
parentpotentiallogpPyTensor scalar of log-probability of the Potential terms.
prefixrootunobserved_RVsList of all random variables, including deterministic ones.
unobserved_value_varsList of all random variables (including untransformed projections), as well as deterministics used as inputs and outputs of the model's log-likelihood graph.
value_varsList of unobserved random variables used as inputs to the model's log-likelihood (which excludes deterministics).
varlogpPyTensor scalar of log-probability of the unobserved random variables (excluding deterministic).
varlogp_nojacPyTensor scalar of log-probability of the unobserved random variables (excluding deterministic) without jacobian term.
- __init__(sample_kwargs=None, priors=None)#
- classmethod __new__(*args, **kwargs)#