Initial commit
This commit is contained in:
524
skills/domain-knowledge/policyengine-us-skill/SKILL.md
Normal file
524
skills/domain-knowledge/policyengine-us-skill/SKILL.md
Normal file
@@ -0,0 +1,524 @@
|
||||
---
|
||||
name: policyengine-us
|
||||
description: PolicyEngine-US tax and benefit microsimulation patterns, situation creation, and common workflows
|
||||
---
|
||||
|
||||
# PolicyEngine-US
|
||||
|
||||
PolicyEngine-US models the US federal and state tax and benefit system.
|
||||
|
||||
## For Users 👥
|
||||
|
||||
### What is PolicyEngine-US?
|
||||
|
||||
PolicyEngine-US is the "calculator" for US taxes and benefits. When you use policyengine.org/us, PolicyEngine-US runs behind the scenes.
|
||||
|
||||
**What it models:**
|
||||
|
||||
**Federal taxes:**
|
||||
- Income tax (with standard/itemized deductions)
|
||||
- Payroll tax (Social Security, Medicare)
|
||||
- Capital gains tax
|
||||
|
||||
**Federal benefits:**
|
||||
- Earned Income Tax Credit (EITC)
|
||||
- Child Tax Credit (CTC)
|
||||
- SNAP (food stamps)
|
||||
- WIC, ACA premium tax credits
|
||||
- Social Security, SSI, TANF
|
||||
|
||||
**State programs (varies by state):**
|
||||
- State income tax (all 50 states + DC)
|
||||
- State EITC, CTC
|
||||
- State-specific benefits
|
||||
|
||||
**See full list:** https://policyengine.org/us/parameters
|
||||
|
||||
### Understanding Variables
|
||||
|
||||
When you see results in PolicyEngine, these are variables:
|
||||
|
||||
**Income variables:**
|
||||
- `employment_income` - W-2 wages
|
||||
- `self_employment_income` - 1099 income
|
||||
- `qualified_dividend_income` - Dividends
|
||||
- `capital_gains` - Capital gains
|
||||
|
||||
**Tax variables:**
|
||||
- `income_tax` - Federal income tax
|
||||
- `state_income_tax` - State income tax
|
||||
- `payroll_tax` - FICA taxes
|
||||
|
||||
**Benefit variables:**
|
||||
- `eitc` - Earned Income Tax Credit
|
||||
- `ctc` - Child Tax Credit
|
||||
- `snap` - SNAP benefits
|
||||
|
||||
**Summary variables:**
|
||||
- `household_net_income` - Income after taxes and benefits
|
||||
- `household_tax` - Total taxes
|
||||
- `household_benefits` - Total benefits
|
||||
|
||||
## For Analysts 📊
|
||||
|
||||
### Installation and Setup
|
||||
|
||||
```bash
|
||||
# Install PolicyEngine-US
|
||||
pip install policyengine-us
|
||||
|
||||
# Or with uv (recommended)
|
||||
uv pip install policyengine-us
|
||||
```
|
||||
|
||||
### Quick Start
|
||||
|
||||
```python
|
||||
from policyengine_us import Simulation
|
||||
|
||||
# Create a household
|
||||
situation = {
|
||||
"people": {
|
||||
"you": {
|
||||
"age": {2024: 30},
|
||||
"employment_income": {2024: 50000}
|
||||
}
|
||||
},
|
||||
"families": {"family": {"members": ["you"]}},
|
||||
"marital_units": {"marital_unit": {"members": ["you"]}},
|
||||
"tax_units": {"tax_unit": {"members": ["you"]}},
|
||||
"spm_units": {"spm_unit": {"members": ["you"]}},
|
||||
"households": {
|
||||
"household": {
|
||||
"members": ["you"],
|
||||
"state_name": {2024: "CA"}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Calculate taxes and benefits
|
||||
sim = Simulation(situation=situation)
|
||||
income_tax = sim.calculate("income_tax", 2024)[0]
|
||||
eitc = sim.calculate("eitc", 2024)[0]
|
||||
|
||||
print(f"Income tax: ${income_tax:,.0f}")
|
||||
print(f"EITC: ${eitc:,.0f}")
|
||||
```
|
||||
|
||||
### Web App to Python
|
||||
|
||||
**Web app URL:**
|
||||
```
|
||||
policyengine.org/us/household?household=12345
|
||||
```
|
||||
|
||||
**Equivalent Python (conceptually):**
|
||||
The household ID represents a situation dictionary. To replicate in Python, you'd create a similar situation.
|
||||
|
||||
### When to Use This Skill
|
||||
|
||||
- Creating household situations for tax/benefit calculations
|
||||
- Running microsimulations with PolicyEngine-US
|
||||
- Analyzing policy reforms and their impacts
|
||||
- Building tools that use PolicyEngine-US (calculators, analysis notebooks)
|
||||
- Debugging PolicyEngine-US calculations
|
||||
|
||||
## For Contributors 💻
|
||||
|
||||
### Repository
|
||||
|
||||
**Location:** PolicyEngine/policyengine-us
|
||||
|
||||
**To see current implementation:**
|
||||
```bash
|
||||
git clone https://github.com/PolicyEngine/policyengine-us
|
||||
cd policyengine-us
|
||||
|
||||
# Explore structure
|
||||
tree policyengine_us/
|
||||
```
|
||||
|
||||
**Key directories:**
|
||||
```bash
|
||||
ls policyengine_us/
|
||||
# - variables/ - Tax and benefit calculations
|
||||
# - parameters/ - Policy rules (YAML)
|
||||
# - reforms/ - Pre-defined reforms
|
||||
# - tests/ - Test cases
|
||||
```
|
||||
|
||||
## Core Concepts
|
||||
|
||||
### 1. Situation Dictionary Structure
|
||||
|
||||
PolicyEngine requires a nested dictionary defining household composition and characteristics:
|
||||
|
||||
```python
|
||||
situation = {
|
||||
"people": {
|
||||
"person_id": {
|
||||
"age": {2024: 35},
|
||||
"employment_income": {2024: 50000},
|
||||
# ... other person attributes
|
||||
}
|
||||
},
|
||||
"families": {
|
||||
"family_id": {"members": ["person_id", ...]}
|
||||
},
|
||||
"marital_units": {
|
||||
"marital_unit_id": {"members": ["person_id", ...]}
|
||||
},
|
||||
"tax_units": {
|
||||
"tax_unit_id": {"members": ["person_id", ...]}
|
||||
},
|
||||
"spm_units": {
|
||||
"spm_unit_id": {"members": ["person_id", ...]}
|
||||
},
|
||||
"households": {
|
||||
"household_id": {
|
||||
"members": ["person_id", ...],
|
||||
"state_name": {2024: "CA"}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Key Rules:**
|
||||
- All entities must have consistent member lists
|
||||
- Use year keys for all values: `{2024: value}`
|
||||
- State must be two-letter code (e.g., "CA", "NY", "TX")
|
||||
- All monetary values in dollars (not cents)
|
||||
|
||||
### 2. Creating Simulations
|
||||
|
||||
```python
|
||||
from policyengine_us import Simulation
|
||||
|
||||
# Create simulation from situation
|
||||
simulation = Simulation(situation=situation)
|
||||
|
||||
# Calculate variables
|
||||
income_tax = simulation.calculate("income_tax", 2024)
|
||||
eitc = simulation.calculate("eitc", 2024)
|
||||
household_net_income = simulation.calculate("household_net_income", 2024)
|
||||
```
|
||||
|
||||
**Common Variables:**
|
||||
|
||||
**Income:**
|
||||
- `employment_income` - W-2 wages
|
||||
- `self_employment_income` - 1099/business income
|
||||
- `qualified_dividend_income` - Qualified dividends
|
||||
- `capital_gains` - Capital gains
|
||||
- `interest_income` - Interest income
|
||||
- `social_security` - Social Security benefits
|
||||
- `pension_income` - Pension/retirement income
|
||||
|
||||
**Deductions:**
|
||||
- `charitable_cash_donations` - Cash charitable giving
|
||||
- `real_estate_taxes` - State and local property taxes
|
||||
- `mortgage_interest` - Mortgage interest deduction
|
||||
- `medical_expense` - Medical and dental expenses
|
||||
- `casualty_loss` - Casualty and theft losses
|
||||
|
||||
**Tax Outputs:**
|
||||
- `income_tax` - Total federal income tax
|
||||
- `payroll_tax` - FICA taxes
|
||||
- `state_income_tax` - State income tax
|
||||
- `household_tax` - Total taxes (federal + state + local)
|
||||
|
||||
**Benefits:**
|
||||
- `eitc` - Earned Income Tax Credit
|
||||
- `ctc` - Child Tax Credit
|
||||
- `snap` - SNAP benefits
|
||||
- `household_benefits` - Total benefits
|
||||
|
||||
**Summary:**
|
||||
- `household_net_income` - Income minus taxes plus benefits
|
||||
|
||||
### 3. Using Axes for Parameter Sweeps
|
||||
|
||||
To vary a parameter across multiple values:
|
||||
|
||||
```python
|
||||
situation = {
|
||||
# ... normal situation setup ...
|
||||
"axes": [[{
|
||||
"name": "employment_income",
|
||||
"count": 1001,
|
||||
"min": 0,
|
||||
"max": 200000,
|
||||
"period": 2024
|
||||
}]]
|
||||
}
|
||||
|
||||
simulation = Simulation(situation=situation)
|
||||
# Now calculate() returns arrays of 1001 values
|
||||
incomes = simulation.calculate("employment_income", 2024) # Array of 1001 values
|
||||
taxes = simulation.calculate("income_tax", 2024) # Array of 1001 values
|
||||
```
|
||||
|
||||
**Important:** Remove axes before creating single-point simulations:
|
||||
```python
|
||||
situation_single = situation.copy()
|
||||
situation_single.pop("axes", None)
|
||||
simulation = Simulation(situation=situation_single)
|
||||
```
|
||||
|
||||
### 4. Policy Reforms
|
||||
|
||||
```python
|
||||
from policyengine_us import Simulation
|
||||
|
||||
# Define a reform (modifies parameters)
|
||||
reform = {
|
||||
"gov.irs.credits.ctc.amount.base_amount": {
|
||||
"2024-01-01.2100-12-31": 5000 # Increase CTC to $5000
|
||||
}
|
||||
}
|
||||
|
||||
# Create simulation with reform
|
||||
simulation = Simulation(situation=situation, reform=reform)
|
||||
```
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Pattern 1: Single Household Calculation
|
||||
|
||||
```python
|
||||
from policyengine_us import Simulation
|
||||
|
||||
situation = {
|
||||
"people": {
|
||||
"parent": {
|
||||
"age": {2024: 35},
|
||||
"employment_income": {2024: 60000}
|
||||
},
|
||||
"child": {
|
||||
"age": {2024: 5}
|
||||
}
|
||||
},
|
||||
"families": {"family": {"members": ["parent", "child"]}},
|
||||
"marital_units": {"marital_unit": {"members": ["parent"]}},
|
||||
"tax_units": {"tax_unit": {"members": ["parent", "child"]}},
|
||||
"spm_units": {"spm_unit": {"members": ["parent", "child"]}},
|
||||
"households": {
|
||||
"household": {
|
||||
"members": ["parent", "child"],
|
||||
"state_name": {2024: "NY"}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sim = Simulation(situation=situation)
|
||||
income_tax = sim.calculate("income_tax", 2024)[0]
|
||||
ctc = sim.calculate("ctc", 2024)[0]
|
||||
```
|
||||
|
||||
### Pattern 2: Marginal Tax Rate Analysis
|
||||
|
||||
```python
|
||||
# Create baseline with axes varying income
|
||||
situation_with_axes = {
|
||||
# ... situation setup ...
|
||||
"axes": [[{
|
||||
"name": "employment_income",
|
||||
"count": 1001,
|
||||
"min": 0,
|
||||
"max": 200000,
|
||||
"period": 2024
|
||||
}]]
|
||||
}
|
||||
|
||||
sim = Simulation(situation=situation_with_axes)
|
||||
incomes = sim.calculate("employment_income", 2024)
|
||||
taxes = sim.calculate("income_tax", 2024)
|
||||
|
||||
# Calculate marginal tax rate
|
||||
import numpy as np
|
||||
mtr = np.gradient(taxes) / np.gradient(incomes)
|
||||
```
|
||||
|
||||
### Pattern 3: Charitable Donation Impact
|
||||
|
||||
```python
|
||||
# Baseline (no donation)
|
||||
situation_baseline = create_situation(income=100000, donation=0)
|
||||
sim_baseline = Simulation(situation=situation_baseline)
|
||||
tax_baseline = sim_baseline.calculate("income_tax", 2024)[0]
|
||||
|
||||
# With donation
|
||||
situation_donation = create_situation(income=100000, donation=5000)
|
||||
sim_donation = Simulation(situation=situation_donation)
|
||||
tax_donation = sim_donation.calculate("income_tax", 2024)[0]
|
||||
|
||||
# Tax savings from donation
|
||||
tax_savings = tax_baseline - tax_donation
|
||||
effective_discount = tax_savings / 5000 # e.g., 0.24 = 24% discount
|
||||
```
|
||||
|
||||
### Pattern 4: State Comparison
|
||||
|
||||
```python
|
||||
states = ["CA", "NY", "TX", "FL"]
|
||||
results = {}
|
||||
|
||||
for state in states:
|
||||
situation = create_situation(state=state, income=75000)
|
||||
sim = Simulation(situation=situation)
|
||||
results[state] = {
|
||||
"state_income_tax": sim.calculate("state_income_tax", 2024)[0],
|
||||
"total_tax": sim.calculate("household_tax", 2024)[0]
|
||||
}
|
||||
```
|
||||
|
||||
## Helper Scripts
|
||||
|
||||
This skill includes helper scripts in the `scripts/` directory:
|
||||
|
||||
```python
|
||||
from policyengine_skills.situation_helpers import (
|
||||
create_single_filer,
|
||||
create_married_couple,
|
||||
create_family_with_children,
|
||||
add_itemized_deductions
|
||||
)
|
||||
|
||||
# Quick situation creation
|
||||
situation = create_single_filer(
|
||||
income=50000,
|
||||
state="CA",
|
||||
age=30
|
||||
)
|
||||
|
||||
# Add deductions
|
||||
situation = add_itemized_deductions(
|
||||
situation,
|
||||
charitable_donations=5000,
|
||||
mortgage_interest=10000,
|
||||
real_estate_taxes=8000
|
||||
)
|
||||
```
|
||||
|
||||
## Common Pitfalls and Solutions
|
||||
|
||||
### Pitfall 1: Member Lists Out of Sync
|
||||
**Problem:** Different entities have different members
|
||||
```python
|
||||
# WRONG
|
||||
"tax_units": {"tax_unit": {"members": ["parent"]}},
|
||||
"households": {"household": {"members": ["parent", "child"]}}
|
||||
```
|
||||
|
||||
**Solution:** Keep all entity member lists consistent:
|
||||
```python
|
||||
# CORRECT
|
||||
all_members = ["parent", "child"]
|
||||
"families": {"family": {"members": all_members}},
|
||||
"tax_units": {"tax_unit": {"members": all_members}},
|
||||
"households": {"household": {"members": all_members}}
|
||||
```
|
||||
|
||||
### Pitfall 2: Forgetting Year Keys
|
||||
**Problem:** `"age": 35` instead of `"age": {2024: 35}`
|
||||
|
||||
**Solution:** Always use year dictionary:
|
||||
```python
|
||||
"age": {2024: 35},
|
||||
"employment_income": {2024: 50000}
|
||||
```
|
||||
|
||||
### Pitfall 3: Net Taxes vs Gross Taxes
|
||||
**Problem:** Forgetting to subtract benefits from taxes
|
||||
|
||||
**Solution:** Use proper calculation:
|
||||
```python
|
||||
# Net taxes (what household actually pays)
|
||||
net_tax = sim.calculate("household_tax", 2024) - \
|
||||
sim.calculate("household_benefits", 2024)
|
||||
```
|
||||
|
||||
### Pitfall 4: Axes Persistence
|
||||
**Problem:** Axes remain in situation when creating single-point simulation
|
||||
|
||||
**Solution:** Remove axes before single-point simulation:
|
||||
```python
|
||||
situation_single = situation.copy()
|
||||
situation_single.pop("axes", None)
|
||||
```
|
||||
|
||||
### Pitfall 5: State-Specific Variables
|
||||
**Problem:** Using NYC-specific variables without `in_nyc: True`
|
||||
|
||||
**Solution:** Set NYC flag for NY residents in NYC:
|
||||
```python
|
||||
"households": {
|
||||
"household": {
|
||||
"state_name": {2024: "NY"},
|
||||
"in_nyc": {2024: True} # Required for NYC taxes
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## NYC Handling
|
||||
|
||||
For New York City residents:
|
||||
```python
|
||||
situation = {
|
||||
# ... people setup ...
|
||||
"households": {
|
||||
"household": {
|
||||
"members": ["person"],
|
||||
"state_name": {2024: "NY"},
|
||||
"in_nyc": {2024: True} # Enable NYC tax calculations
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Version Compatibility
|
||||
|
||||
- Always use `policyengine-us>=1.155.0` for 2024 calculations
|
||||
- Check version: `import policyengine_us; print(policyengine_us.__version__)`
|
||||
- Different years may require different package versions
|
||||
|
||||
## Debugging Tips
|
||||
|
||||
1. **Enable tracing:**
|
||||
```python
|
||||
simulation.trace = True
|
||||
result = simulation.calculate("variable_name", 2024)
|
||||
```
|
||||
|
||||
2. **Check intermediate calculations:**
|
||||
```python
|
||||
agi = simulation.calculate("adjusted_gross_income", 2024)
|
||||
taxable_income = simulation.calculate("taxable_income", 2024)
|
||||
```
|
||||
|
||||
3. **Verify situation structure:**
|
||||
```python
|
||||
import json
|
||||
print(json.dumps(situation, indent=2))
|
||||
```
|
||||
|
||||
4. **Test with PolicyEngine web app:**
|
||||
- Go to policyengine.org/us/household
|
||||
- Enter same inputs
|
||||
- Compare results
|
||||
|
||||
## Additional Resources
|
||||
|
||||
- **Documentation:** https://policyengine.org/us/docs
|
||||
- **API Reference:** https://github.com/PolicyEngine/policyengine-us
|
||||
- **Example Notebooks:** https://github.com/PolicyEngine/analysis-notebooks
|
||||
- **Variable Explorer:** https://policyengine.org/us/variables
|
||||
|
||||
## Examples Directory
|
||||
|
||||
See `examples/` for complete working examples:
|
||||
- `single_filer.yaml` - Single person household
|
||||
- `married_couple.yaml` - Married filing jointly
|
||||
- `family_with_children.yaml` - Family with dependents
|
||||
- `itemized_deductions.yaml` - Using itemized deductions
|
||||
- `donation_sweep.yaml` - Analyzing donation impacts with axes
|
||||
@@ -0,0 +1,71 @@
|
||||
# Example: Analyzing charitable donation impacts using axes
|
||||
# Married couple with 2 children in New York
|
||||
# Sweeps charitable donations from $0 to $50,000
|
||||
|
||||
people:
|
||||
parent_1:
|
||||
age:
|
||||
2024: 35
|
||||
employment_income:
|
||||
2024: 100000
|
||||
parent_2:
|
||||
age:
|
||||
2024: 35
|
||||
employment_income:
|
||||
2024: 50000
|
||||
child_1:
|
||||
age:
|
||||
2024: 8
|
||||
child_2:
|
||||
age:
|
||||
2024: 5
|
||||
|
||||
families:
|
||||
family:
|
||||
members:
|
||||
- parent_1
|
||||
- parent_2
|
||||
- child_1
|
||||
- child_2
|
||||
|
||||
marital_units:
|
||||
marital_unit:
|
||||
members:
|
||||
- parent_1
|
||||
- parent_2
|
||||
- child_1
|
||||
- child_2
|
||||
|
||||
tax_units:
|
||||
tax_unit:
|
||||
members:
|
||||
- parent_1
|
||||
- parent_2
|
||||
- child_1
|
||||
- child_2
|
||||
|
||||
spm_units:
|
||||
spm_unit:
|
||||
members:
|
||||
- parent_1
|
||||
- parent_2
|
||||
- child_1
|
||||
- child_2
|
||||
|
||||
households:
|
||||
household:
|
||||
members:
|
||||
- parent_1
|
||||
- parent_2
|
||||
- child_1
|
||||
- child_2
|
||||
state_name:
|
||||
2024: NY
|
||||
|
||||
# Axes: Vary charitable donations from $0 to $50,000
|
||||
axes:
|
||||
- - name: charitable_cash_donations
|
||||
count: 1001
|
||||
min: 0
|
||||
max: 50000
|
||||
period: 2024
|
||||
@@ -0,0 +1,38 @@
|
||||
# Example: Single tax filer in California
|
||||
# Income: $60,000, Age: 30, with charitable donations
|
||||
|
||||
people:
|
||||
person:
|
||||
age:
|
||||
2024: 30
|
||||
employment_income:
|
||||
2024: 60000
|
||||
charitable_cash_donations:
|
||||
2024: 5000
|
||||
|
||||
families:
|
||||
family:
|
||||
members:
|
||||
- person
|
||||
|
||||
marital_units:
|
||||
marital_unit:
|
||||
members:
|
||||
- person
|
||||
|
||||
tax_units:
|
||||
tax_unit:
|
||||
members:
|
||||
- person
|
||||
|
||||
spm_units:
|
||||
spm_unit:
|
||||
members:
|
||||
- person
|
||||
|
||||
households:
|
||||
household:
|
||||
members:
|
||||
- person
|
||||
state_name:
|
||||
2024: CA
|
||||
@@ -0,0 +1,257 @@
|
||||
"""
|
||||
Helper functions for creating PolicyEngine-US situations.
|
||||
|
||||
These utilities simplify the creation of situation dictionaries
|
||||
for common household configurations.
|
||||
"""
|
||||
|
||||
CURRENT_YEAR = 2024
|
||||
|
||||
|
||||
def create_single_filer(income, state="CA", age=35, **kwargs):
|
||||
"""
|
||||
Create a situation for a single tax filer.
|
||||
|
||||
Args:
|
||||
income (float): Employment income
|
||||
state (str): Two-letter state code (e.g., "CA", "NY")
|
||||
age (int): Person's age
|
||||
**kwargs: Additional person attributes (e.g., self_employment_income)
|
||||
|
||||
Returns:
|
||||
dict: PolicyEngine situation dictionary
|
||||
"""
|
||||
person_attrs = {
|
||||
"age": {CURRENT_YEAR: age},
|
||||
"employment_income": {CURRENT_YEAR: income},
|
||||
}
|
||||
person_attrs.update({k: {CURRENT_YEAR: v} for k, v in kwargs.items()})
|
||||
|
||||
return {
|
||||
"people": {"person": person_attrs},
|
||||
"families": {"family": {"members": ["person"]}},
|
||||
"marital_units": {"marital_unit": {"members": ["person"]}},
|
||||
"tax_units": {"tax_unit": {"members": ["person"]}},
|
||||
"spm_units": {"spm_unit": {"members": ["person"]}},
|
||||
"households": {
|
||||
"household": {
|
||||
"members": ["person"],
|
||||
"state_name": {CURRENT_YEAR: state}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def create_married_couple(
|
||||
income_1, income_2=0, state="CA", age_1=35, age_2=35, **kwargs
|
||||
):
|
||||
"""
|
||||
Create a situation for a married couple filing jointly.
|
||||
|
||||
Args:
|
||||
income_1 (float): First spouse's employment income
|
||||
income_2 (float): Second spouse's employment income
|
||||
state (str): Two-letter state code
|
||||
age_1 (int): First spouse's age
|
||||
age_2 (int): Second spouse's age
|
||||
**kwargs: Additional household attributes
|
||||
|
||||
Returns:
|
||||
dict: PolicyEngine situation dictionary
|
||||
"""
|
||||
members = ["spouse_1", "spouse_2"]
|
||||
|
||||
household_attrs = {
|
||||
"members": members,
|
||||
"state_name": {CURRENT_YEAR: state}
|
||||
}
|
||||
household_attrs.update({k: {CURRENT_YEAR: v} for k, v in kwargs.items()})
|
||||
|
||||
return {
|
||||
"people": {
|
||||
"spouse_1": {
|
||||
"age": {CURRENT_YEAR: age_1},
|
||||
"employment_income": {CURRENT_YEAR: income_1}
|
||||
},
|
||||
"spouse_2": {
|
||||
"age": {CURRENT_YEAR: age_2},
|
||||
"employment_income": {CURRENT_YEAR: income_2}
|
||||
}
|
||||
},
|
||||
"families": {"family": {"members": members}},
|
||||
"marital_units": {"marital_unit": {"members": members}},
|
||||
"tax_units": {"tax_unit": {"members": members}},
|
||||
"spm_units": {"spm_unit": {"members": members}},
|
||||
"households": {"household": household_attrs}
|
||||
}
|
||||
|
||||
|
||||
def create_family_with_children(
|
||||
parent_income,
|
||||
num_children=1,
|
||||
child_ages=None,
|
||||
state="CA",
|
||||
parent_age=35,
|
||||
married=False,
|
||||
spouse_income=0,
|
||||
**kwargs
|
||||
):
|
||||
"""
|
||||
Create a situation for a family with children.
|
||||
|
||||
Args:
|
||||
parent_income (float): Primary parent's employment income
|
||||
num_children (int): Number of children
|
||||
child_ages (list): List of child ages (defaults to [5, 8, 12, ...])
|
||||
state (str): Two-letter state code
|
||||
parent_age (int): Parent's age
|
||||
married (bool): Whether parents are married
|
||||
spouse_income (float): Spouse's income if married
|
||||
**kwargs: Additional household attributes
|
||||
|
||||
Returns:
|
||||
dict: PolicyEngine situation dictionary
|
||||
"""
|
||||
if child_ages is None:
|
||||
child_ages = [5 + i * 3 for i in range(num_children)]
|
||||
elif len(child_ages) != num_children:
|
||||
raise ValueError("Length of child_ages must match num_children")
|
||||
|
||||
people = {
|
||||
"parent": {
|
||||
"age": {CURRENT_YEAR: parent_age},
|
||||
"employment_income": {CURRENT_YEAR: parent_income}
|
||||
}
|
||||
}
|
||||
|
||||
members = ["parent"]
|
||||
|
||||
if married:
|
||||
people["spouse"] = {
|
||||
"age": {CURRENT_YEAR: parent_age},
|
||||
"employment_income": {CURRENT_YEAR: spouse_income}
|
||||
}
|
||||
members.append("spouse")
|
||||
|
||||
for i, age in enumerate(child_ages):
|
||||
child_id = f"child_{i+1}"
|
||||
people[child_id] = {"age": {CURRENT_YEAR: age}}
|
||||
members.append(child_id)
|
||||
|
||||
household_attrs = {
|
||||
"members": members,
|
||||
"state_name": {CURRENT_YEAR: state}
|
||||
}
|
||||
household_attrs.update({k: {CURRENT_YEAR: v} for k, v in kwargs.items()})
|
||||
|
||||
return {
|
||||
"people": people,
|
||||
"families": {"family": {"members": members}},
|
||||
"marital_units": {
|
||||
"marital_unit": {
|
||||
"members": members if married else ["parent"]
|
||||
}
|
||||
},
|
||||
"tax_units": {"tax_unit": {"members": members}},
|
||||
"spm_units": {"spm_unit": {"members": members}},
|
||||
"households": {"household": household_attrs}
|
||||
}
|
||||
|
||||
|
||||
def add_itemized_deductions(
|
||||
situation,
|
||||
charitable_donations=0,
|
||||
mortgage_interest=0,
|
||||
real_estate_taxes=0,
|
||||
medical_expenses=0,
|
||||
casualty_losses=0
|
||||
):
|
||||
"""
|
||||
Add itemized deductions to an existing situation.
|
||||
|
||||
Adds deductions to the first person in the situation.
|
||||
|
||||
Args:
|
||||
situation (dict): Existing PolicyEngine situation
|
||||
charitable_donations (float): Cash charitable contributions
|
||||
mortgage_interest (float): Mortgage interest paid
|
||||
real_estate_taxes (float): State and local property taxes
|
||||
medical_expenses (float): Medical and dental expenses
|
||||
casualty_losses (float): Casualty and theft losses
|
||||
|
||||
Returns:
|
||||
dict: Updated situation with deductions
|
||||
"""
|
||||
# Get first person ID
|
||||
first_person = list(situation["people"].keys())[0]
|
||||
|
||||
# Add deductions
|
||||
if charitable_donations > 0:
|
||||
situation["people"][first_person]["charitable_cash_donations"] = {
|
||||
CURRENT_YEAR: charitable_donations
|
||||
}
|
||||
|
||||
if mortgage_interest > 0:
|
||||
situation["people"][first_person]["mortgage_interest"] = {
|
||||
CURRENT_YEAR: mortgage_interest
|
||||
}
|
||||
|
||||
if real_estate_taxes > 0:
|
||||
situation["people"][first_person]["real_estate_taxes"] = {
|
||||
CURRENT_YEAR: real_estate_taxes
|
||||
}
|
||||
|
||||
if medical_expenses > 0:
|
||||
situation["people"][first_person]["medical_expense"] = {
|
||||
CURRENT_YEAR: medical_expenses
|
||||
}
|
||||
|
||||
if casualty_losses > 0:
|
||||
situation["people"][first_person]["casualty_loss"] = {
|
||||
CURRENT_YEAR: casualty_losses
|
||||
}
|
||||
|
||||
return situation
|
||||
|
||||
|
||||
def add_axes(situation, variable_name, min_val, max_val, count=1001):
|
||||
"""
|
||||
Add axes to a situation for parameter sweeps.
|
||||
|
||||
Args:
|
||||
situation (dict): Existing PolicyEngine situation
|
||||
variable_name (str): Variable to vary (e.g., "employment_income")
|
||||
min_val (float): Minimum value
|
||||
max_val (float): Maximum value
|
||||
count (int): Number of points (default: 1001)
|
||||
|
||||
Returns:
|
||||
dict: Updated situation with axes
|
||||
"""
|
||||
situation["axes"] = [[{
|
||||
"name": variable_name,
|
||||
"count": count,
|
||||
"min": min_val,
|
||||
"max": max_val,
|
||||
"period": CURRENT_YEAR
|
||||
}]]
|
||||
|
||||
return situation
|
||||
|
||||
|
||||
def set_state_nyc(situation, in_nyc=True):
|
||||
"""
|
||||
Set state to NY and configure NYC residence.
|
||||
|
||||
Args:
|
||||
situation (dict): Existing PolicyEngine situation
|
||||
in_nyc (bool): Whether household is in NYC
|
||||
|
||||
Returns:
|
||||
dict: Updated situation
|
||||
"""
|
||||
household_id = list(situation["households"].keys())[0]
|
||||
situation["households"][household_id]["state_name"] = {CURRENT_YEAR: "NY"}
|
||||
situation["households"][household_id]["in_nyc"] = {CURRENT_YEAR: in_nyc}
|
||||
|
||||
return situation
|
||||
Reference in New Issue
Block a user