-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moving some functions for general model use into a utils module
- Loading branch information
Showing
3 changed files
with
81 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import division | ||
|
||
import itertools | ||
import math | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import pyomo.environ as mo | ||
|
||
|
||
from salamanca import ineq | ||
from salamanca.models.utils import below_threshold | ||
|
||
# | ||
# Constraints | ||
# | ||
|
||
|
||
# | ||
# Objectives | ||
# | ||
|
||
def theil_total_sum_obj(m): | ||
_t_w = lambda m, idx: m.i[idx] * \ | ||
m.data['n'][idx] / m.data['G'] * m.t[idx] | ||
T_w = sum(_t_w(m, idx) for idx in m.idxs) | ||
T_b = sum(_t_b(m, idx) for idx in m.idxs) | ||
return (m.data['T'] - T_w - T_b) ** 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import math | ||
|
||
import pyomo.environ as mo | ||
|
||
|
||
# | ||
# Pyomo-enabled helper functions | ||
# | ||
|
||
|
||
def below_threshold(x, i, t, mean=True): | ||
"""Compute the CDF of the lognormal distribution at x using an approximation of | ||
the error function: | ||
.. math:: | ||
erf(x) \approx \tanh(\sqrt \pi \log x) | ||
Parameters | ||
---------- | ||
x : numeric | ||
threshold income | ||
i : numeric | ||
mean income (per capita) | ||
t : numeric or Pyomo variable | ||
theil coefficient | ||
mean : bool, default: True | ||
treat income as mean income | ||
""" | ||
# see | ||
# https://math.stackexchange.com/questions/321569/approximating-the-error-function-erf-by-analytical-functions | ||
sigma2 = 2 * t # t is var | ||
# f(var), adjust for mean income vs. median | ||
mu = math.log(i) | ||
if mean: | ||
mu -= sigma2 / 2 | ||
# f(var), argument for error function | ||
arg = (math.log(x) - mu) / mo.sqrt(2 * sigma2) | ||
# coefficient for erf approximation | ||
k = math.pi ** 0.5 * math.log(2) | ||
# definition of cdf with tanh(kx) approximating erf(x) | ||
return 0.5 + 0.5 * mo.tanh(k * arg) | ||
|
||
|
||
def model_T_w(m, income_from_data=True): | ||
def _t_w(m, idx): | ||
i = m.data['i'] if income_from_data else m.i | ||
return i[idx] * m.data['n'][idx] / m.data['G'] * m.t[idx] | ||
T_w = sum(_t_w(m, idx) for idx in m.idxs) | ||
return T_w |