swh:1:snp:b1bc5cf54dab7987e9f85b6673e79b3b1d27ac15
Raw File
Tip revision: 1921801ba8c39c62c7e5db14ed499d9b35d63bb4 authored by Arun Maiya on 02 August 2022, 15:41:20 UTC
minor edits
Tip revision: 1921801
meta.propensity.html
---

title: Metalearner Propensity


keywords: fastai
sidebar: home_sidebar

summary: "Metalearner Propensity"
description: "Metalearner Propensity"
nb_path: "nbs/05h_meta.propensity.ipynb"
---
<!--

#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: nbs/05h_meta.propensity.ipynb
# command to build the docs after a change: nbdev_build_docs

-->

<div class="container" id="notebook-container">
        
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="PropensityModel" class="doc_header"><code>class</code> <code>PropensityModel</code><a href="https://github.com/amaiya/causalnlp/tree/main/causalnlp/meta/propensity.py#L37" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>PropensityModel</code>(<strong><code>clip_bounds</code></strong>=<em><code>(0.001, 0.999)</code></em>, <strong>**<code>model_kwargs</code></strong>)</p>
</blockquote>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="LogisticRegressionPropensityModel" class="doc_header"><code>class</code> <code>LogisticRegressionPropensityModel</code><a href="https://github.com/amaiya/causalnlp/tree/main/causalnlp/meta/propensity.py#L98" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>LogisticRegressionPropensityModel</code>(<strong><code>clip_bounds</code></strong>=<em><code>(0.001, 0.999)</code></em>, <strong>**<code>model_kwargs</code></strong>) :: <a href="/causalnlp/meta.propensity.html#PropensityModel"><code>PropensityModel</code></a></p>
</blockquote>
<p>Propensity regression model based on the LogisticRegression algorithm.</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="SimplePropensityModel" class="doc_header"><code>class</code> <code>SimplePropensityModel</code><a href="https://github.com/amaiya/causalnlp/tree/main/causalnlp/meta/propensity.py#L121" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>SimplePropensityModel</code>(<strong><code>clip_bounds</code></strong>=<em><code>(0.001, 0.999)</code></em>, <strong>**<code>model_kwargs</code></strong>) :: <a href="/causalnlp/meta.propensity.html#PropensityModel"><code>PropensityModel</code></a></p>
</blockquote>
<p>Propensity regression model based on the LogisticRegression algorithm.</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="ElasticNetPropensityModel" class="doc_header"><code>class</code> <code>ElasticNetPropensityModel</code><a href="https://github.com/amaiya/causalnlp/tree/main/causalnlp/meta/propensity.py#L133" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>ElasticNetPropensityModel</code>(<strong><code>clip_bounds</code></strong>=<em><code>(0.001, 0.999)</code></em>, <strong>**<code>model_kwargs</code></strong>) :: <a href="/causalnlp/meta.propensity.html#LogisticRegressionPropensityModel"><code>LogisticRegressionPropensityModel</code></a></p>
</blockquote>
<p>Propensity regression model based on the LogisticRegression algorithm.</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="GradientBoostedPropensityModel" class="doc_header"><code>class</code> <code>GradientBoostedPropensityModel</code><a href="https://github.com/amaiya/causalnlp/tree/main/causalnlp/meta/propensity.py#L137" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>GradientBoostedPropensityModel</code>(<strong><code>early_stop</code></strong>=<em><code>False</code></em>, <strong><code>clip_bounds</code></strong>=<em><code>(0.001, 0.999)</code></em>, <strong>**<code>model_kwargs</code></strong>) :: <a href="/causalnlp/meta.propensity.html#PropensityModel"><code>PropensityModel</code></a></p>
</blockquote>
<p>Gradient boosted propensity score model with optional early stopping.</p>
<h2 id="Notes">Notes<a class="anchor-link" href="#Notes"> </a></h2><p>Please see the xgboost documentation for more information on gradient boosting tuning parameters:
<a href="https://xgboost.readthedocs.io/en/latest/python/python_api.html">https://xgboost.readthedocs.io/en/latest/python/python_api.html</a></p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="calibrate" class="doc_header"><code>calibrate</code><a href="https://github.com/amaiya/causalnlp/tree/main/causalnlp/meta/propensity.py#L216" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>calibrate</code>(<strong><code>ps</code></strong>, <strong><code>treatment</code></strong>)</p>
</blockquote>
<p>Calibrate propensity scores with logistic GAM.</p>
<p>Ref: <a href="https://pygam.readthedocs.io/en/latest/api/logisticgam.html">https://pygam.readthedocs.io/en/latest/api/logisticgam.html</a></p>
<p>Args:
    ps (numpy.array): a propensity score vector
    treatment (numpy.array): a binary treatment vector (0: control, 1: treated)</p>
<p>Returns:
    (numpy.array): a calibrated propensity score vector</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="compute_propensity_score" class="doc_header"><code>compute_propensity_score</code><a href="https://github.com/amaiya/causalnlp/tree/main/causalnlp/meta/propensity.py#L234" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>compute_propensity_score</code>(<strong><code>X</code></strong>, <strong><code>treatment</code></strong>, <strong><code>p_model</code></strong>=<em><code>None</code></em>, <strong><code>X_pred</code></strong>=<em><code>None</code></em>, <strong><code>treatment_pred</code></strong>=<em><code>None</code></em>, <strong><code>calibrate_p</code></strong>=<em><code>True</code></em>)</p>
</blockquote>
<p>Generate propensity score if user didn't provide</p>
<p>Args:
    X (np.matrix): features for training
    treatment (np.array or pd.Series): a treatment vector for training
    p_model (propensity model object, optional):
        ElasticNetPropensityModel (default) / GradientBoostedPropensityModel
    X_pred (np.matrix, optional): features for prediction
    treatment_pred (np.array or pd.Series, optional): a treatment vector for prediciton
    calibrate_p (bool, optional): whether calibrate the propensity score</p>
<p>Returns:
    (tuple)</p>

<pre><code>    - p (numpy.ndarray): propensity score
    - p_model (PropensityModel): a trained PropensityModel object</code></pre>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

</div>


back to top