コード例 #1
0
ファイル: test_CyClpSimplex.py プロジェクト: HerrKevin/CyLP
class TestCyClpSimplex(unittest.TestCase):

    def setUp(self):
        self.s = CyClpSimplex()
        self.s.readMps(join(currentFilePath, '../input/p0033.mps'))

    def test_PE(self):
        #pivot = PositiveEdgePivot(self.s)
        self.s.setPivotMethod(PositiveEdgePivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_Dantzig(self):
        #pivot = DantzigPivot(self.s)
        self.s.setPivotMethod(DantzigPivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_LIFO(self):
        #pivot = LIFOPivot(self.s)
        self.s.setPivotMethod(LIFOPivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_MostFrequent(self):
        #pivot = MostFrequentPivot(self.s)
        self.s.setPivotMethod(MostFrequentPivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_initialSolve(self):
        self.s.initialSolve()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_initialPrimalSolve(self):
        self.s.initialPrimalSolve()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_initialDualSolve(self):
        self.s.initialDualSolve()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_direction(self):
        self.assertEqual(self.s.optimizationDirection, 'min')
        self.s.optimizationDirection = 'max'
        self.assertEqual(self.s.optimizationDirection, 'max')
コード例 #2
0
class TestCyClpSimplex(unittest.TestCase):
    def setUp(self):
        self.s = CyClpSimplex()
        self.s.readMps(join(currentFilePath, '../input/p0033.mps'))

    def test_PE(self):
        #pivot = PositiveEdgePivot(self.s)
        self.s.setPivotMethod(PositiveEdgePivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_Dantzig(self):
        #pivot = DantzigPivot(self.s)
        self.s.setPivotMethod(DantzigPivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_LIFO(self):
        #pivot = LIFOPivot(self.s)
        self.s.setPivotMethod(LIFOPivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_MostFrequent(self):
        #pivot = MostFrequentPivot(self.s)
        self.s.setPivotMethod(MostFrequentPivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_initialSolve(self):
        self.s.initialSolve()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_initialPrimalSolve(self):
        self.s.initialPrimalSolve()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_initialDualSolve(self):
        self.s.initialDualSolve()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_direction(self):
        self.assertEqual(self.s.optimizationDirection, 'min')
        self.s.optimizationDirection = 'max'
        self.assertEqual(self.s.optimizationDirection, 'max')
コード例 #3
0
ファイル: solve_lp_for_year.py プロジェクト: yic242/taxdata
def solve_lp_for_year(puf, Stage_I_factors, Stage_II_targets, year, tol):

    puf_length = len(puf.s006)

    print("Preparing coefficient matrix for year {} .....".format(year))

    s006 = np.where(puf.e02400 > 0,
                    puf.s006 * Stage_I_factors[year]["APOPSNR"] / 100,
                    puf.s006 * Stage_I_factors[year]["ARETS"] / 100)

    single_return = np.where((puf.mars == 1) & (puf.filer == 1), s006, 0)
    joint_return = np.where(
        ((puf.mars == 2) | (puf.mars == 3)) & (puf.filer == 1), s006, 0)
    hh_return = np.where((puf.mars == 4) & (puf.filer == 1), s006, 0)
    return_w_SS = np.where((puf.e02400 > 0) & (puf.filer == 1), s006, 0)

    dependent_exempt_num = (puf.xocah + puf.xocawh + puf.xoodep +
                            puf.xopar) * s006
    interest = puf.e00300 * s006
    dividend = puf.e00600 * s006
    biz_income = np.where(puf.e00900 > 0, puf.e00900, 0) * s006
    biz_loss = np.where(puf.e00900 < 0, -puf.e00900, 0) * s006
    cap_gain = np.where(
        (puf.p23250 + puf.p22250) > 0, puf.p23250 + puf.p22250, 0) * s006
    annuity_pension = puf.e01700 * s006
    sch_e_income = np.where(puf.e02000 > 0, puf.e02000, 0) * s006
    sch_e_loss = np.where(puf.e02000 < 0, -puf.e02000, 0) * s006
    ss_income = np.where(puf.filer == 1, puf.e02400, 0) * s006
    unemployment_comp = puf.e02300 * s006

    # Wage distribution
    wage_1 = np.where(puf.e00100 <= 0, puf.e00200, 0) * s006
    wage_2 = np.where(
        (puf.e00100 > 0) & (puf.e00100 <= 10000), puf.e00200, 0) * s006
    wage_3 = np.where(
        (puf.e00100 > 10000) & (puf.e00100 <= 20000), puf.e00200, 0) * s006
    wage_4 = np.where(
        (puf.e00100 > 20000) & (puf.e00100 <= 30000), puf.e00200, 0) * s006
    wage_5 = np.where(
        (puf.e00100 > 30000) & (puf.e00100 <= 40000), puf.e00200, 0) * s006
    wage_6 = np.where(
        (puf.e00100 > 40000) & (puf.e00100 <= 50000), puf.e00200, 0) * s006
    wage_7 = np.where(
        (puf.e00100 > 50000) & (puf.e00100 <= 75000), puf.e00200, 0) * s006
    wage_8 = np.where(
        (puf.e00100 > 75000) & (puf.e00100 <= 100000), puf.e00200, 0) * s006
    wage_9 = np.where(
        (puf.e00100 > 100000) & (puf.e00100 <= 200000), puf.e00200, 0) * s006
    wage_10 = np.where(
        (puf.e00100 > 200000) & (puf.e00100 <= 500000), puf.e00200, 0) * s006
    wage_11 = np.where(
        (puf.e00100 > 500000) & (puf.e00100 <= 1000000), puf.e00200, 0) * s006
    wage_12 = np.where(puf.e00100 > 1000000, puf.e00200, 0) * s006

    # Set up the matrix
    One_half_LHS = np.vstack(
        (single_return, joint_return, hh_return, return_w_SS,
         dependent_exempt_num, interest, dividend, biz_income, biz_loss,
         cap_gain, annuity_pension, sch_e_income, sch_e_loss, ss_income,
         unemployment_comp, wage_1, wage_2, wage_3, wage_4, wage_5, wage_6,
         wage_7, wage_8, wage_9, wage_10, wage_11, wage_12))

    # Coefficients for r and s
    A1 = np.matrix(One_half_LHS)
    A2 = np.matrix(-One_half_LHS)

    print("Preparing targets for year {} .....".format(year))

    APOPN = Stage_I_factors[year]["APOPN"]

    b = []

    b.append(Stage_II_targets[year]["Single Returns"] - single_return.sum())
    b.append(Stage_II_targets[year]["Joint Returns"] - joint_return.sum())
    target_name = "Head of Household Returns"
    b.append(Stage_II_targets[year][target_name] - hh_return.sum())
    target_name = "Number of Returns w/ Gross Security Income"
    b.append(Stage_II_targets[year][target_name] - return_w_SS.sum())
    target_name = "Number of Dependent Exemptions"
    b.append(Stage_II_targets[year][target_name] - dependent_exempt_num.sum())

    AINTS = Stage_I_factors[year]["AINTS"]
    INTEREST = (Stage_II_targets[year]["Taxable Interest Income"] * APOPN /
                AINTS * 1000 - interest.sum())

    ADIVS = Stage_I_factors[year]["ADIVS"]
    DIVIDEND = (
        Stage_II_targets[year]["Ordinary Dividends"] * APOPN / ADIVS * 1000 -
        dividend.sum())

    ASCHCI = Stage_I_factors[year]["ASCHCI"]
    BIZ_INCOME = (Stage_II_targets[year]["Business Income (Schedule C)"] *
                  APOPN / ASCHCI * 1000 - biz_income.sum())

    ASCHCL = Stage_I_factors[year]["ASCHCL"]
    BIZ_LOSS = (Stage_II_targets[year]["Business Loss (Schedule C)"] * APOPN /
                ASCHCL * 1000 - biz_loss.sum())

    ACGNS = Stage_I_factors[year]["ACGNS"]
    CAP_GAIN = (Stage_II_targets[year]["Net Capital Gains in AGI"] * APOPN /
                ACGNS * 1000 - cap_gain.sum())

    ATXPY = Stage_I_factors[year]["ATXPY"]
    target_name = "Taxable Pensions and Annuities"
    ANNUITY_PENSION = (
        Stage_II_targets[year][target_name] * APOPN / ATXPY * 1000 -
        annuity_pension.sum())

    ASCHEI = Stage_I_factors[year]["ASCHEI"]
    target_name = "Supplemental Income (Schedule E)"
    SCH_E_INCOME = (
        Stage_II_targets[year][target_name] * APOPN / ASCHEI * 1000 -
        sch_e_income.sum())

    ASCHEL = Stage_I_factors[year]["ASCHEL"]
    SCH_E_LOSS = (Stage_II_targets[year]["Supplemental Loss (Schedule E)"] *
                  APOPN / ASCHEL * 1000 - sch_e_loss.sum())

    ASOCSEC = Stage_I_factors[year]["ASOCSEC"]
    APOPSNR = Stage_I_factors[year]["APOPSNR"]
    SS_INCOME = (Stage_II_targets[year]["Gross Social Security Income"] *
                 APOPSNR / ASOCSEC * 1000 - ss_income.sum())

    AUCOMP = Stage_I_factors[year]["AUCOMP"]
    UNEMPLOYMENT_COMP = (Stage_II_targets[year]["Unemployment Compensation"] *
                         APOPN / AUCOMP * 1000 - unemployment_comp.sum())

    AWAGE = Stage_I_factors[year]["AWAGE"]
    target_name = "Wages and Salaries: Zero or Less"
    WAGE_1 = (Stage_II_targets[year][target_name] * APOPN / AWAGE * 1000 -
              wage_1.sum())
    target_name = "Wages and Salaries: $1 Less Than $10,000"
    WAGE_2 = (Stage_II_targets[year][target_name] * APOPN / AWAGE * 1000 -
              wage_2.sum())
    target_name = "Wages and Salaries: $10,000 Less Than $20,000"
    WAGE_3 = (Stage_II_targets[year][target_name] * APOPN / AWAGE * 1000 -
              wage_3.sum())
    target_name = "Wages and Salaries: $20,000 Less Than $30,000"
    WAGE_4 = (Stage_II_targets[year][target_name] * APOPN / AWAGE * 1000 -
              wage_4.sum())
    target_name = "Wages and Salaries: $30,000 Less Than $40,000"
    WAGE_5 = (Stage_II_targets[year][target_name] * APOPN / AWAGE * 1000 -
              wage_5.sum())
    target_name = "Wages and Salaries: $40,000 Less Than $50,000"
    WAGE_6 = (Stage_II_targets[year][target_name] * APOPN / AWAGE * 1000 -
              wage_6.sum())
    target_name = "Wages and Salaries: $50,000 Less Than $75,000"
    WAGE_7 = (Stage_II_targets[year][target_name] * APOPN / AWAGE * 1000 -
              wage_7.sum())
    target_name = "Wages and Salaries: $75,000 Less Than $100,000"
    WAGE_8 = (Stage_II_targets[year][target_name] * APOPN / AWAGE * 1000 -
              wage_8.sum())
    target_name = "Wages and Salaries: $100,000 Less Than $200,000"
    WAGE_9 = (Stage_II_targets[year][target_name] * APOPN / AWAGE * 1000 -
              wage_9.sum())
    target_name = "Wages and Salaries: $200,000 Less Than $500,000"
    WAGE_10 = (Stage_II_targets[year][target_name] * APOPN / AWAGE * 1000 -
               wage_10.sum())
    target_name = "Wages and Salaries: $500,000 Less Than $1 Million"
    WAGE_11 = (Stage_II_targets[year][target_name] * APOPN / AWAGE * 1000 -
               wage_11.sum())
    target_name = "Wages and Salaries: $1 Million and Over"
    WAGE_12 = (Stage_II_targets[year][target_name] * APOPN / AWAGE * 1000 -
               wage_12.sum())

    temp = [
        INTEREST, DIVIDEND, BIZ_INCOME, BIZ_LOSS, CAP_GAIN, ANNUITY_PENSION,
        SCH_E_INCOME, SCH_E_LOSS, SS_INCOME, UNEMPLOYMENT_COMP, WAGE_1, WAGE_2,
        WAGE_3, WAGE_4, WAGE_5, WAGE_6, WAGE_7, WAGE_8, WAGE_9, WAGE_10,
        WAGE_11, WAGE_12
    ]
    for m in temp:
        b.append(m)

    targets = CyLPArray(b)
    print("Targets for year {} are:".format(year))
    print(targets)

    LP = CyLPModel()

    r = LP.addVariable("r", puf_length)
    s = LP.addVariable("s", puf_length)

    print("Adding constraints for year {} .....".format(year))
    LP.addConstraint(r >= 0, "positive r")
    LP.addConstraint(s >= 0, "positive s")
    LP.addConstraint(r + s <= tol, "abs upperbound")

    c = CyLPArray((np.ones(puf_length)))
    LP.objective = c * r + c * s

    LP.addConstraint(A1 * r + A2 * s == targets, "Aggregates")

    print("Setting up the LP model for year {} .....".format(year))
    model = CyClpSimplex(LP)

    print("Solving LP for year {} .....".format(year))
    model.initialSolve()

    print("DONE solving LP for year {}".format(year))
    z = np.empty([puf_length])
    z = (1.0 + model.primalVariableSolution["r"] -
         model.primalVariableSolution["s"]) * s006 * 100
    return z
コード例 #4
0
if __name__ == "__main__":
    mip_model = MIPModel(GurobiSolver())
    starttime1 = timeit.default_timer()
    load_mps_primitive(mip_model, path="../../examples/mps_files/10teams.mps")
    endtime1 = timeit.default_timer()
    mip_model.set_mip_sense(LPSense.MIN)
    starttime2 = timeit.default_timer()
    mip_model.lp_model.optimize()
    endtime2 = timeit.default_timer()
    first_value = mip_model.lp_model.get_objective_value()
    mip_model = MIPModel(GurobiSolver())
    starttime3 = timeit.default_timer()
    load_mps_advanced(mip_model, path="../../examples/mps_files/10teams.mps")
    endtime3 = timeit.default_timer()
    mip_model.set_mip_sense(LPSense.MIN)
    starttime4 = timeit.default_timer()
    mip_model.lp_model.optimize()
    endtime4 = timeit.default_timer()
    second_value = mip_model.lp_model.get_objective_value()
    print(
        "Primitive Load: {}s loading time, {}s optimization time, objective value: {}\nAdvanced Load: {}s loading time, {}s optimization time, objective value: {}"
        .format(endtime1 - starttime1, endtime2 - starttime2, first_value,
                endtime3 - starttime3, endtime4 - starttime4, second_value))

    from cylp.cy import CyClpSimplex
    s = CyClpSimplex()
    s.readMps("../../examples/mps_files/10teams.mps")
    s.initialSolve()
    print(s.objectiveValue)
    print(s.solution)
コード例 #5
0
def Single_Year_Stage_II(puf, Stage_I_factors, Stage_II_targets, year, tol):


    length = len(puf.s006)


    print("Preparing coefficient matrix...")

    s006 = np.where(puf.e02400>0,
                    puf.s006*Stage_I_factors[year]["APOPSNR"]/100,
                    puf.s006*Stage_I_factors[year]["ARETS"]/100)



    single_return = np.where(puf.mars==1, s006, 0)
    joint_return = np.where((puf.mars==2)|(puf.mars==3), s006, 0)
    hh_return = np.where(puf.mars==4,s006,0)
    return_w_SS = np.where(puf.e02400>0,s006,0)

    dependent_exempt_num = (puf.xocah+puf.xocawh+puf.xoodep+puf.xopar)*s006
    interest = puf.e00300*s006
    dividend = puf.e00600*s006
    biz_income = np.where(puf.e00900>0, puf.e00900, 0)*s006
    biz_loss = np.where(puf.e00900<0, -puf.e00900, 0)*s006
    cap_gain = np.where(puf.e01000>0, puf.e01000, 0)*s006
    annuity_pension = puf.e01700*s006
    sch_e_income = np.where(puf.e02000>0, puf.e02000, 0)*s006
    sch_e_loss = np.where(puf.e02000<0, -puf.e02000, 0)*s006
    ss_income = puf.e02400*s006
    unemployment_comp = puf.e02300*s006


    # Wage distribution

    wage_1 = np.where(puf.e00100<=0, puf.e00200,0)*s006
    wage_2 = np.where((puf.e00100>0)&(puf.e00100<=10000), puf.e00200,0)*s006
    wage_3 = np.where((puf.e00100>10000)&(puf.e00100<=20000), puf.e00200,0)*s006
    wage_4 = np.where((puf.e00100>20000)&(puf.e00100<=30000), puf.e00200,0)*s006
    wage_5 = np.where((puf.e00100>30000)&(puf.e00100<=40000), puf.e00200,0)*s006
    wage_6 = np.where((puf.e00100>40000)&(puf.e00100<=50000), puf.e00200,0)*s006
    wage_7 = np.where((puf.e00100>50000)&(puf.e00100<=75000), puf.e00200,0)*s006
    wage_8 = np.where((puf.e00100>75000)&(puf.e00100<=100000), puf.e00200,0)*s006
    wage_9 = np.where((puf.e00100>100000)&(puf.e00100<=200000), puf.e00200,0)*s006
    wage_10 = np.where((puf.e00100>200000)&(puf.e00100<=500000), puf.e00200,0)*s006
    wage_11 = np.where((puf.e00100>500000)&(puf.e00100<=1000000), puf.e00200,0)*s006
    wage_12 = np.where((puf.e00100>1000000), puf.e00200,0)*s006


    # Set up the matrix
    One_half_LHS = np.vstack((single_return, joint_return, hh_return, return_w_SS,
                              dependent_exempt_num, interest, dividend,
                              biz_income,biz_loss, cap_gain, annuity_pension,
                              sch_e_income, sch_e_loss, ss_income, unemployment_comp,
                              wage_1, wage_2, wage_3, wage_4, wage_5, wage_6,
                              wage_7, wage_8, wage_9, wage_10, wage_11, wage_12))


    # Coefficients for r and s
    A1 = np.matrix(One_half_LHS)
    A2 = np.matrix(-One_half_LHS)


    print("Preparing targets for ", year)

    APOPN = Stage_I_factors[year]["APOPN"]

    b = []

    b.append(Stage_II_targets[year]['Single']-single_return.sum())
    b.append(Stage_II_targets[year]['Joint']-joint_return.sum())
    b.append(Stage_II_targets[year]['HH']-hh_return.sum())
    b.append(Stage_II_targets[year]['SS_return']-return_w_SS.sum())

    b.append(Stage_II_targets[year]['Dep_return'] -  dependent_exempt_num.sum())

    AINTS = Stage_I_factors[year]["AINTS"]
    INTEREST = Stage_II_targets[year]['INTS']*APOPN/AINTS*1000-interest.sum()

    ADIVS = Stage_I_factors[year]["ADIVS"]
    DIVIDEND = Stage_II_targets[year]['DIVS']*APOPN/ADIVS*1000 - dividend.sum()

    ASCHCI = Stage_I_factors[year]["ASCHCI"]
    BIZ_INCOME = Stage_II_targets[year]['SCHCI']*APOPN/ASCHCI*1000 - biz_income.sum()


    ASCHCL = Stage_I_factors[year]["ASCHCL"]
    BIZ_LOSS = Stage_II_targets[year]['SCHCL']*APOPN/ASCHCL*1000 - biz_loss.sum()

    ACGNS = Stage_I_factors[year]["ACGNS"]
    CAP_GAIN = Stage_II_targets[year]['CGNS']*APOPN/ACGNS*1000 - cap_gain.sum()

    ATXPY = Stage_I_factors[year]["ATXPY"]
    ANNUITY_PENSION = Stage_II_targets[year]['Pension']*APOPN/ATXPY*1000 - annuity_pension.sum()

    ASCHEI = Stage_I_factors[year]["ASCHEI"]
    SCH_E_INCOME = Stage_II_targets[year]["SCHEI"]*APOPN/ASCHEI*1000 - sch_e_income.sum()

    ASCHEL = Stage_I_factors[year]["ASCHEL"]
    SCH_E_LOSS = Stage_II_targets[year]["SCHEL"]*APOPN/ASCHEL*1000 - sch_e_loss.sum()

    ASOCSEC = Stage_I_factors[year]["ASOCSEC"]
    APOPSNR = Stage_I_factors[year]["APOPSNR"]
    SS_INCOME = Stage_II_targets[year]["SS"]*APOPSNR/ASOCSEC*1000 - ss_income.sum()

    AUCOMP = Stage_I_factors[year]["AUCOMP"]
    UNEMPLOYMENT_COMP = Stage_II_targets[year]["UCOMP"]*APOPN/AUCOMP*1000 - unemployment_comp.sum()

    AWAGE = Stage_I_factors[year]["AWAGE"]
    WAGE_1 = Stage_II_targets[year]["WAGE_1"]*APOPN/AWAGE*1000 - wage_1.sum()
    WAGE_2 = Stage_II_targets[year]["WAGE_2"]*APOPN/AWAGE*1000 - wage_2.sum()
    WAGE_3 = Stage_II_targets[year]["WAGE_3"]*APOPN/AWAGE*1000 - wage_3.sum()
    WAGE_4 = Stage_II_targets[year]["WAGE_4"]*APOPN/AWAGE*1000 - wage_4.sum()
    WAGE_5 = Stage_II_targets[year]["WAGE_5"]*APOPN/AWAGE*1000 - wage_5.sum()
    WAGE_6 = Stage_II_targets[year]["WAGE_6"]*APOPN/AWAGE*1000 - wage_6.sum()
    WAGE_7 = Stage_II_targets[year]["WAGE_7"]*APOPN/AWAGE*1000 - wage_7.sum()
    WAGE_8 = Stage_II_targets[year]["WAGE_8"]*APOPN/AWAGE*1000 - wage_8.sum()
    WAGE_9 = Stage_II_targets[year]["WAGE_9"]*APOPN/AWAGE*1000 - wage_9.sum()
    WAGE_10 = Stage_II_targets[year]["WAGE_10"]*APOPN/AWAGE*1000 - wage_10.sum()
    WAGE_11 = Stage_II_targets[year]["WAGE_11"]*APOPN/AWAGE*1000 - wage_11.sum()
    WAGE_12 = Stage_II_targets[year]["WAGE_12"]*APOPN/AWAGE*1000 - wage_12.sum()



    temp = [INTEREST,DIVIDEND, BIZ_INCOME, BIZ_LOSS, CAP_GAIN, ANNUITY_PENSION, SCH_E_INCOME, SCH_E_LOSS, SS_INCOME, UNEMPLOYMENT_COMP,
            WAGE_1,WAGE_2, WAGE_3,WAGE_4, WAGE_5, WAGE_6, WAGE_7,WAGE_8,WAGE_9, WAGE_10, WAGE_11, WAGE_12]
    for m in temp:
        b.append(m)

    targets = CyLPArray(b)
    print("Targets for year ", year, " is ", targets)

    LP = CyLPModel()

    r = LP.addVariable('r', length)
    s = LP.addVariable('s', length)

    print("Adding constraints")
    LP.addConstraint(r >=0, "positive r")
    LP.addConstraint(s >=0, "positive s")
    LP.addConstraint(r + s <= tol, "abs upperbound")

    c = CyLPArray((np.ones(length)))
    LP.objective = c * r + c * s




    LP.addConstraint(A1 * r + A2 * s == targets, "Aggregates")

    print("Setting up the LP model")
    model = CyClpSimplex(LP)


    print("Solving LP......")
    model.initialSolve()

    print("DONE!!")
    z = np.empty([length])
    z = (1+model.primalVariableSolution['r'] - model.primalVariableSolution['s'])*s006
    return z
コード例 #6
0
def Single_Year_Stage_II(puf, Stage_I_factors, Stage_II_targets, year, tol):


    length = len(puf.s006)


    print("Preparing coefficient matrix...")

    s006 = np.where(puf.e02400>0,
                    puf.s006*Stage_I_factors[year]["APOPSNR"]/100,
                    puf.s006*Stage_I_factors[year]["ARETS"]/100)



    single_return = np.where((puf.mars==1) & (puf.filer==1), s006, 0)
    joint_return = np.where(((puf.mars==2)|(puf.mars==3)) & (puf.filer==1), s006, 0)
    hh_return = np.where((puf.mars==4) & (puf.filer==1),s006,0)
    return_w_SS = np.where((puf.e02400>0) & (puf.filer==1),s006,0)

    dependent_exempt_num = (puf.xocah+puf.xocawh+puf.xoodep+puf.xopar)*s006
    interest = puf.e00300*s006
    dividend = puf.e00600*s006
    biz_income = np.where(puf.e00900>0, puf.e00900, 0)*s006
    biz_loss = np.where(puf.e00900<0, -puf.e00900, 0)*s006
    cap_gain = np.where((puf.p23250+puf.p22250)>0, (puf.p23250+puf.p22250), 0)*s006
    annuity_pension = puf.e01700*s006
    sch_e_income = np.where(puf.e02000>0, puf.e02000, 0)*s006
    sch_e_loss = np.where(puf.e02000<0, -puf.e02000, 0)*s006
    ss_income = puf.e02400*s006
    unemployment_comp = puf.e02300*s006


    # Wage distribution

    wage_1 = np.where(puf.e00100<=0, puf.e00200,0)*s006
    wage_2 = np.where((puf.e00100>0)&(puf.e00100<=10000), puf.e00200,0)*s006
    wage_3 = np.where((puf.e00100>10000)&(puf.e00100<=20000), puf.e00200,0)*s006
    wage_4 = np.where((puf.e00100>20000)&(puf.e00100<=30000), puf.e00200,0)*s006
    wage_5 = np.where((puf.e00100>30000)&(puf.e00100<=40000), puf.e00200,0)*s006
    wage_6 = np.where((puf.e00100>40000)&(puf.e00100<=50000), puf.e00200,0)*s006
    wage_7 = np.where((puf.e00100>50000)&(puf.e00100<=75000), puf.e00200,0)*s006
    wage_8 = np.where((puf.e00100>75000)&(puf.e00100<=100000), puf.e00200,0)*s006
    wage_9 = np.where((puf.e00100>100000)&(puf.e00100<=200000), puf.e00200,0)*s006
    wage_10 = np.where((puf.e00100>200000)&(puf.e00100<=500000), puf.e00200,0)*s006
    wage_11 = np.where((puf.e00100>500000)&(puf.e00100<=1000000), puf.e00200,0)*s006
    wage_12 = np.where((puf.e00100>1000000), puf.e00200,0)*s006


    # Set up the matrix
    One_half_LHS = np.vstack((single_return, joint_return, hh_return, return_w_SS,
                              dependent_exempt_num, interest, dividend,
                              biz_income,biz_loss, cap_gain, annuity_pension,
                              sch_e_income, sch_e_loss, ss_income, unemployment_comp,
                              wage_1, wage_2, wage_3, wage_4, wage_5, wage_6,
                              wage_7, wage_8, wage_9, wage_10, wage_11, wage_12))


    # Coefficients for r and s
    A1 = np.matrix(One_half_LHS)
    A2 = np.matrix(-One_half_LHS)


    print("Preparing targets for ", year)

    APOPN = Stage_I_factors[year]["APOPN"]

    b = []

    b.append(Stage_II_targets[year]['Single Returns']-single_return.sum())
    b.append(Stage_II_targets[year]['Joint Returns']-joint_return.sum())
    b.append(Stage_II_targets[year]['Head of Household Returns']-hh_return.sum())
    b.append(Stage_II_targets[year]['Number of Returns w/ Gross Security Income']-return_w_SS.sum())

    b.append(Stage_II_targets[year]['Number of Dependent Exemptions'] -  dependent_exempt_num.sum())

    AINTS = Stage_I_factors[year]["AINTS"]
    INTEREST = Stage_II_targets[year]['Taxable Interest Income']*APOPN/AINTS*1000-interest.sum()

    ADIVS = Stage_I_factors[year]["ADIVS"]
    DIVIDEND = Stage_II_targets[year]['Ordinary Dividends']*APOPN/ADIVS*1000 - dividend.sum()

    ASCHCI = Stage_I_factors[year]["ASCHCI"]
    BIZ_INCOME = Stage_II_targets[year]['Business Income (Schedule C)']*APOPN/ASCHCI*1000 - biz_income.sum()


    ASCHCL = Stage_I_factors[year]["ASCHCL"]
    BIZ_LOSS = Stage_II_targets[year]['Business Loss (Schedule C)']*APOPN/ASCHCL*1000 - biz_loss.sum()

    ACGNS = Stage_I_factors[year]["ACGNS"]
    CAP_GAIN = Stage_II_targets[year]['Net Capital Gains in AGI']*APOPN/ACGNS*1000 - cap_gain.sum()

    ATXPY = Stage_I_factors[year]["ATXPY"]
    ANNUITY_PENSION = Stage_II_targets[year]['Taxable Pensions and Annuities']*APOPN/ATXPY*1000 - annuity_pension.sum()

    ASCHEI = Stage_I_factors[year]["ASCHEI"]
    SCH_E_INCOME = Stage_II_targets[year]["Supplemental Income (Schedule E)"]*APOPN/ASCHEI*1000 - sch_e_income.sum()

    ASCHEL = Stage_I_factors[year]["ASCHEL"]
    SCH_E_LOSS = Stage_II_targets[year]["Supplemental Loss (Schedule E)"]*APOPN/ASCHEL*1000 - sch_e_loss.sum()

    ASOCSEC = Stage_I_factors[year]["ASOCSEC"]
    APOPSNR = Stage_I_factors[year]["APOPSNR"]
    SS_INCOME = Stage_II_targets[year]["Gross Social Security Income"]*APOPSNR/ASOCSEC*1000 - ss_income.sum()

    AUCOMP = Stage_I_factors[year]["AUCOMP"]
    UNEMPLOYMENT_COMP = Stage_II_targets[year]["Unemployment Compensation"]*APOPN/AUCOMP*1000 - unemployment_comp.sum()

    AWAGE = Stage_I_factors[year]["AWAGE"]
    WAGE_1 = Stage_II_targets[year]["Wages and Salaries: Zero or Less"]*APOPN/AWAGE*1000 - wage_1.sum()
    WAGE_2 = Stage_II_targets[year]["Wages and Salaries: $1 Less Than $10,000"]*APOPN/AWAGE*1000 - wage_2.sum()
    WAGE_3 = Stage_II_targets[year]["Wages and Salaries: $10,000 Less Than $20,000"]*APOPN/AWAGE*1000 - wage_3.sum()
    WAGE_4 = Stage_II_targets[year]["Wages and Salaries: $20,000 Less Than $30,000"]*APOPN/AWAGE*1000 - wage_4.sum()
    WAGE_5 = Stage_II_targets[year]["Wages and Salaries: $30,000 Less Than $40,000"]*APOPN/AWAGE*1000 - wage_5.sum()
    WAGE_6 = Stage_II_targets[year]["Wages and Salaries: $40,000 Less Than $50,000"]*APOPN/AWAGE*1000 - wage_6.sum()
    WAGE_7 = Stage_II_targets[year]["Wages and Salaries: $50,000 Less Than $75,000"]*APOPN/AWAGE*1000 - wage_7.sum()
    WAGE_8 = Stage_II_targets[year]["Wages and Salaries: $75,000 Less Than $100,000"]*APOPN/AWAGE*1000 - wage_8.sum()
    WAGE_9 = Stage_II_targets[year]["Wages and Salaries: $100,000 Less Than $200,000"]*APOPN/AWAGE*1000 - wage_9.sum()
    WAGE_10 = Stage_II_targets[year]["Wages and Salaries: $200,000 Less Than $500,000"]*APOPN/AWAGE*1000 - wage_10.sum()
    WAGE_11 = Stage_II_targets[year]["Wages and Salaries: $500,000 Less Than $1 Million"]*APOPN/AWAGE*1000 - wage_11.sum()
    WAGE_12 = Stage_II_targets[year]["Wages and Salaries: $1 Million and Over"]*APOPN/AWAGE*1000 - wage_12.sum()



    temp = [INTEREST,DIVIDEND, BIZ_INCOME, BIZ_LOSS, CAP_GAIN, ANNUITY_PENSION, SCH_E_INCOME, SCH_E_LOSS, SS_INCOME, UNEMPLOYMENT_COMP,
            WAGE_1,WAGE_2, WAGE_3,WAGE_4, WAGE_5, WAGE_6, WAGE_7,WAGE_8,WAGE_9, WAGE_10, WAGE_11, WAGE_12]
    for m in temp:
        b.append(m)

    targets = CyLPArray(b)
    print("Targets for year ", year, " is ", targets)

    LP = CyLPModel()

    r = LP.addVariable('r', length)
    s = LP.addVariable('s', length)

    print("Adding constraints")
    LP.addConstraint(r >=0, "positive r")
    LP.addConstraint(s >=0, "positive s")
    LP.addConstraint(r + s <= tol, "abs upperbound")

    c = CyLPArray((np.ones(length)))
    LP.objective = c * r + c * s




    LP.addConstraint(A1 * r + A2 * s == targets, "Aggregates")

    print("Setting up the LP model")
    model = CyClpSimplex(LP)


    print("Solving LP......")
    model.initialSolve()

    print("DONE!!")
    z = np.empty([length])
    z = (1+model.primalVariableSolution['r'] - model.primalVariableSolution['s'])*s006 * 100
    return z