prediction

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2026 License: MIT Imports: 4 Imported by: 0

README

prediction

Package prediction provides causal prediction methods including Double Machine Learning, naive adjustment regression, and instrumental variable regression.

Import path: github.com/asymmetric-effort/datascience/lib/pgm/prediction

Types

Type Description
DoubleMLRegressor Double Machine Learning for ATE estimation with cross-fitting
NaiveAdjustmentRegressor Back-door adjustment via OLS: outcome ~ treatment + adjustment set
NaiveIVRegressor Two-stage least squares (2SLS) instrumental variable regression

DoubleML

Estimates the Average Treatment Effect (ATE) using cross-fitting to avoid overfitting bias.

import (
    "github.com/asymmetric-effort/datascience/lib/pgm/prediction"
    "github.com/asymmetric-effort/datascience/lib/tabgo"
)

dml := prediction.NewDoubleMLRegressor(
    "Treatment",
    "Outcome",
    []string{"Confounder1", "Confounder2"},
)
dml.SetNSplits(5) // 5-fold cross-fitting

err := dml.Fit(data)
ate := dml.ATE()           // average treatment effect
se := dml.StandardError()  // standard error of ATE
ci := dml.ConfidenceInterval(0.95)

NaiveAdjustmentRegressor

Estimates causal effects by regressing the outcome on treatment plus adjustment variables (back-door adjustment).

adj := prediction.NewNaiveAdjustmentRegressor(
    "Treatment",
    "Outcome",
    []string{"Confounder1", "Confounder2"},
)

err := adj.Fit(data)
ate := adj.ATE()            // treatment coefficient
se := adj.StandardError()   // SE of treatment coefficient
predicted := adj.Predict(newData)
residuals := adj.Residuals()

NaiveIVRegressor

Estimates causal effects using two-stage least squares when confounders are unobserved but valid instruments exist.

iv := prediction.NewNaiveIVRegressor(
    "Treatment",
    "Outcome",
    []string{"Instrument1", "Instrument2"},
)

err := iv.Fit(data)
ate := iv.ATE()             // estimated causal effect
se := iv.StandardError()    // standard error
fstat := iv.FirstStageFStat() // instrument strength diagnostic

Documentation

Overview

Package prediction provides causal prediction methods including DoubleML, naive adjustment regression, and instrumental variable regression.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DoubleMLRegressor

type DoubleMLRegressor struct {
	// contains filtered or unexported fields
}

DoubleMLRegressor implements Double Machine Learning for causal effect estimation. It uses cross-fitting with configurable number of folds to estimate the Average Treatment Effect (ATE).

func NewDoubleMLRegressor

func NewDoubleMLRegressor(treatment, outcome string, confounders []string) *DoubleMLRegressor

NewDoubleMLRegressor creates a new DoubleMLRegressor. It defaults to 2-fold cross-fitting. Use SetNSplits to change.

func (*DoubleMLRegressor) ATE

func (d *DoubleMLRegressor) ATE() float64

ATE returns the estimated Average Treatment Effect.

func (*DoubleMLRegressor) ConfidenceInterval

func (d *DoubleMLRegressor) ConfidenceInterval(alpha float64) (float64, float64)

ConfidenceInterval returns the (lower, upper) bounds of a confidence interval for the ATE at the given significance level alpha (e.g., 0.05 for 95% CI). Uses a normal approximation.

func (*DoubleMLRegressor) EstimateCate

func (d *DoubleMLRegressor) EstimateCate() ([]float64, error)

EstimateCate estimates conditional average treatment effects (CATE) for each observation. It computes observation-level treatment effects as: cate_i = yResid_i / tResid_i, smoothed by local weighting. For a linear DML model, the CATE is constant and equals the ATE; this method returns per-observation influence-weighted effects.

func (*DoubleMLRegressor) Fit

func (d *DoubleMLRegressor) Fit(data *tabgo.DataFrame) error

Fit performs DML estimation with cross-fitting on the given data.

Algorithm (K-fold cross-fitting):

  1. Split data into K folds.
  2. For each fold k: train outcome and treatment models on all other folds, compute residuals on fold k.
  3. Pool all residuals and estimate ATE as the OLS coefficient of treatment residuals on outcome residuals (no intercept).
  4. Compute standard error from residuals.

func (*DoubleMLRegressor) PValue

func (d *DoubleMLRegressor) PValue() float64

PValue returns the two-sided p-value for testing H0: ATE = 0.

func (*DoubleMLRegressor) Predict

func (d *DoubleMLRegressor) Predict(data *tabgo.DataFrame) ([]float64, error)

Predict returns counterfactual outcome predictions for each row: predicted_outcome = ATE * treatment_value. This is a simplified prediction that applies the estimated treatment effect.

func (*DoubleMLRegressor) SE

func (d *DoubleMLRegressor) SE() float64

SE returns the standard error of the ATE estimate.

func (*DoubleMLRegressor) SetNSplits

func (d *DoubleMLRegressor) SetNSplits(n int)

SetNSplits sets the number of cross-fitting folds. Must be >= 2.

func (*DoubleMLRegressor) Summary

func (d *DoubleMLRegressor) Summary() string

Summary returns a formatted summary string with ATE, SE, CI, and p-value.

type NaiveAdjustmentRegressor

type NaiveAdjustmentRegressor struct {
	// contains filtered or unexported fields
}

NaiveAdjustmentRegressor implements naive back-door adjustment for causal effect estimation via OLS regression: outcome ~ treatment + adjustmentSet.

func NewNaiveAdjustmentRegressor

func NewNaiveAdjustmentRegressor(treatment, outcome string, adjustmentSet []string) *NaiveAdjustmentRegressor

NewNaiveAdjustmentRegressor creates a new NaiveAdjustmentRegressor.

func (*NaiveAdjustmentRegressor) ATE

ATE returns the estimated Average Treatment Effect, which is the OLS coefficient on the treatment variable.

func (*NaiveAdjustmentRegressor) ConfidenceInterval

func (r *NaiveAdjustmentRegressor) ConfidenceInterval(alpha float64) (float64, float64)

ConfidenceInterval returns (lower, upper) bounds for the ATE at significance level alpha.

func (*NaiveAdjustmentRegressor) Fit

Fit performs OLS regression: outcome ~ intercept + treatment + adjustmentSet.

func (*NaiveAdjustmentRegressor) PValue

func (r *NaiveAdjustmentRegressor) PValue() float64

PValue returns the two-sided p-value for testing H0: ATE = 0.

func (*NaiveAdjustmentRegressor) Predict

func (r *NaiveAdjustmentRegressor) Predict(data *tabgo.DataFrame) ([]float64, error)

Predict returns predicted outcome values for each row.

func (*NaiveAdjustmentRegressor) SE

SE returns the standard error of the ATE estimate.

func (*NaiveAdjustmentRegressor) Summary

func (r *NaiveAdjustmentRegressor) Summary() string

Summary returns a formatted summary string.

type NaiveIVRegressor

type NaiveIVRegressor struct {
	// contains filtered or unexported fields
}

NaiveIVRegressor implements instrumental variable regression using two-stage least squares (2SLS) for causal effect estimation.

func NewNaiveIVRegressor

func NewNaiveIVRegressor(treatment, outcome string, instruments []string) *NaiveIVRegressor

NewNaiveIVRegressor creates a new NaiveIVRegressor.

func (*NaiveIVRegressor) ATE

func (r *NaiveIVRegressor) ATE() float64

ATE returns the estimated Average Treatment Effect from 2SLS.

func (*NaiveIVRegressor) ConfidenceInterval

func (r *NaiveIVRegressor) ConfidenceInterval(alpha float64) (float64, float64)

ConfidenceInterval returns (lower, upper) bounds for the ATE at significance level alpha.

func (*NaiveIVRegressor) FirstStageFStat

func (r *NaiveIVRegressor) FirstStageFStat() float64

FirstStageFStat returns the F-statistic from the first stage regression, testing whether the instruments are jointly significant predictors of treatment. A value > 10 is typically considered evidence of strong instruments.

func (*NaiveIVRegressor) Fit

func (r *NaiveIVRegressor) Fit(data *tabgo.DataFrame) error

Fit performs two-stage least squares:

Stage 1: treatment ~ intercept + instruments (OLS) -> predicted treatment
Stage 2: outcome ~ intercept + predicted_treatment (OLS) -> ATE

func (*NaiveIVRegressor) PValue

func (r *NaiveIVRegressor) PValue() float64

PValue returns the two-sided p-value for testing H0: ATE = 0.

func (*NaiveIVRegressor) Predict

func (r *NaiveIVRegressor) Predict(data *tabgo.DataFrame) ([]float64, error)

Predict returns predicted outcome values: intercept + ATE * treatment.

func (*NaiveIVRegressor) SE

func (r *NaiveIVRegressor) SE() float64

SE returns the standard error of the ATE estimate.

func (*NaiveIVRegressor) Summary

func (r *NaiveIVRegressor) Summary() string

Summary returns a formatted summary string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL