Example #1
0
def TheWholeSmack(S, I, sigma):
    #Parameters Zone
    #I = 10 #Number of countries
    #S = 80 #Upper bound of age for agents
    #I=rc.I
    #S=rc.S
    T = int(
        round(2.5 * S)
    )  #Number of time periods to convergence, based on Rick Evans' function.

    T_1 = S  #This is like TransYear in the FORTRAN I think
    if S > 50:
        T_1 = 50
    StartFertilityAge = int(S / 80. *
                            23)  #The age when agents have their first children
    EndFertilityAge = int(S / 80. *
                          45)  #The age when agents have their last children
    StartDyingAge = int(S / 80. * 68)  #The first age agents can begin to die
    MaxImmigrantAge = int(
        S / 80. * 65)  #All immigrants are between ages 0 and MaxImmigrantAge
    g_A = 0.001  #Technical growth rate

    beta_ann = .95  #Starting future consumption discount rate
    delta_ann = .08  #Starting depreciation rate
    beta = beta_ann**(70 / S)  #Future consumption discount rate
    #sigma = 2 #Utility curvature parameter
    #sigma=rc.Sigma
    delta = 1 - (1 - delta_ann)**(70 / S)  #Depreciation Rate
    alpha = .3  #Capital Share of production
    e = np.ones((I, S, T + S + 1))  #Labor productivities
    #A = np.ones(I) #Techonological Change, used for idential countries
    A = np.linspace(
        1, 2,
        num=I)  #Techonological Change, used for when countries are different

    diff = 1e-6  #Convergence Tolerance
    distance = 10  #Used in taking the norm, arbitrarily set to 10
    xi = .99  #Parameter used to take the convex conjugate of paths
    MaxIters = 3000  #Maximum number of iterations on TPI.

    #Program Levers
    PrintAges = False  #Prints the different key ages in the demographics
    PrintSS = False  #Prints the result of the Steady State functions
    CalcTPI = True  #Activates the calculation of Time Path Iteration
    #NOTE: Graphing only works if CalcTPI is activated.
    Graphs = False  #Activates graphing the graphs.
    CountryNamesON = False  #Turns on labels for the graphs. Replaces "Country x" with proper names.

    #MAIN CODE

    #Gets demographic data
    demog_params = (I, S, T, T_1, StartFertilityAge, EndFertilityAge,
                    StartDyingAge, MaxImmigrantAge, g_A, PrintAges)
    FertilityRates, MortalityRates, Migrants, N_matrix, Nhat_matrix = Stepfuncs.getDemographics(
        demog_params)
    #Stepfuncs.plotDemographics((S,T),0,[0,19],"USA", N_matrix)

    #Initalizes initial guesses
    assets_guess = np.ones((I, S - 1)) * .15
    kf_guess = np.zeros((I))

    #Gets the steady state variables
    params_ss = (I, S, beta, sigma, delta, alpha, e, A)
    assets_ss, kf_ss, kd_ss, n_ss, y_ss, r_ss, w_ss, c_vec_ss = Stepfuncs.getSteadyState(
        params_ss, assets_guess, kf_guess)

    if PrintSS == True:  #Prints the results of the steady state, line 23 activates this
        print "assets steady state", assets_ss
        print "kf steady state", kf_ss
        print "kd steady state", kd_ss
        print "n steady state", n_ss
        print "y steady state", y_ss
        print "r steady state", r_ss
        print "w steady state", w_ss
        print "c_vec_ss steady state", c_vec_ss

    if CalcTPI == True:  #Time Path Iteration, activated by line 24
        print "Beginning TPI"
        initialguess_params = (I, S, T, delta, alpha, e, A)
        assets_init, kf_init, w_initguess, r_initguess, kd_init, n_init, y_init, c_init = \
         Stepfuncs.get_initialguesses(initialguess_params, assets_ss, kf_ss, w_ss, r_ss)

        tp_params = (I, S, T, T_1, beta, sigma, delta, alpha, e, A,
                     StartFertilityAge, StartDyingAge, N_matrix,
                     MortalityRates, distance, diff, xi, MaxIters)
        wpath, rpath, cpath, Kpath, ypath = Stepfuncs.get_Timepath(
            tp_params, w_initguess, r_initguess, assets_init, kd_ss, kf_ss,
            w_ss, r_ss)
        #print "Cohorts:",S,"Countries:", I,"Curvature:", sigma

        if Graphs == True:
            Stepfuncs.plotTimepaths(I, S, T, wpath, rpath, cpath, Kpath, ypath,
                                    CountryNamesON)
Example #2
0
MaxIters=300 #Maximum number of iterations on TPI.

#Program Levers
PrintAges = False #Prints the different key ages in the demographics
PrintSS = False #Prints the result of the Steady State functions
CalcTPI = False #Activates the calculation of Time Path Iteration
#NOTE: Graphing only works if CalcTPI is activated.
Graphs = True #Activates graphing the graphs.
CountryNamesON = False #Turns on labels for the graphs. Replaces "Country x" with proper names.
DiffDemog = True #Turns on different demographics over countries. 

#MAIN CODE

#Gets demographic data
demog_params = (I, S, T, T_1, StartFertilityAge, EndFertilityAge, StartDyingAge, MaxImmigrantAge, g_A)
FertilityRates, MortalityRates, Migrants, N_matrix, Nhat_matrix = Stepfuncs.getDemographics(demog_params, PrintAges, DiffDemog)

for i in range(I):
	Stepfuncs.plotDemographics((S,T),i,[0,24],str("Country "+str(i)), N_matrix)

#Initalizes initial guesses
assets_guess = np.ones((I, S-1))*.15
kf_guess = np.zeros((I))

#Gets the steady state variables
params_ss = (I, S, beta, sigma, delta, alpha, e, A)
assets_ss, kf_ss, kd_ss, n_ss, y_ss, r_ss, w_ss, c_vec_ss = Stepfuncs.getSteadyState(params_ss, assets_guess, kf_guess)

if PrintSS==True: #Prints the results of the steady state, line 23 activates this
	print "assets steady state", assets_ss
	print "kf steady state", kf_ss
Example #3
0
def TheWholeSmack(S,I,sigma):
    T = int(round(2.5*S)) #Number of time periods to convergence, based on Rick Evans' function.

    T_1 = S #This is like TransYear in the FORTRAN I think
    if S > 50:
	    T_1 = 50
    StartFertilityAge = int(S/80.*23)#The age when agents have their first children
    EndFertilityAge = int(S/80.*45)#The age when agents have their last children
    StartDyingAge = int(S/80.*68)#The first age agents can begin to die
    MaxImmigrantAge = int(S/80.*65)#All immigrants are between ages 0 and MaxImmigrantAge
    g_A = 0.015#Technical growth rate

    beta_ann=.95 #Starting future consumption discount rate
    delta_ann=.08 #Starting depreciation rate
    beta = beta_ann**(70/S) #Future consumption discount rate
    #sigma = 1 #Utility curvature parameter
    delta = 1-(1-delta_ann)**(70/S) #Depreciation Rate
    alpha = .3 #Capital Share of production
    e = np.ones((I, S, T+S+1)) #Labor productivities
    A = np.ones(I) #Techonological Change, used for idential countries
    #A=np.array([1,4,2,5,6]) #Techonological Change, used for when countries are different

    diff=1e-8 #Convergence Tolerance
    distance=10 #Used in taking the norm, arbitrarily set to 10
    xi=.95 #Parameter used to take the convex conjugate of paths
    MaxIters=1000 #Maximum number of iterations on TPI.

    #Program Levers
    PrintAges = False #Prints the different key ages in the demographics
    PrintSS = False #Prints the result of the Steady State functions
    CalcTPI = True #Activates the calculation of Time Path Iteration
    #NOTE: Graphing only works if CalcTPI is activated.
    Graphs = False #Activates graphing the graphs.
    CountryNamesON = False #Turns on labels for the graphs. Replaces "Country x" with proper names.
    DiffDemog = True #Turns on different demographics over countries. 

    #MAIN CODE

    #Gets demographic data
    demog_params = (I, S, T, T_1, StartFertilityAge, EndFertilityAge, StartDyingAge, MaxImmigrantAge, g_A)
    FertilityRates, MortalityRates, Migrants, N_matrix, Nhat_matrix = Stepfuncs.getDemographics(demog_params, PrintAges, DiffDemog)

    #Initalizes initial guesses
    assets_guess = np.ones((I, S-1))*.15
    kf_guess = np.zeros((I))

    #Gets the steady state variables
    params_ss = (I, S, beta, sigma, delta, alpha, e, A)
    assets_ss, kf_ss, kd_ss, n_ss, y_ss, r_ss, w_ss, c_vec_ss = Stepfuncs.getSteadyState(params_ss, assets_guess, kf_guess)

    if PrintSS==True: #Prints the results of the steady state, line 23 activates this
	    print "assets steady state", assets_ss
	    print "kf steady state", kf_ss
	    print "kd steady state", kd_ss
	    print "n steady state",n_ss
	    print "y steady state", y_ss
	    print "r steady state",r_ss
	    print "w steady state", w_ss
	    print "c_vec_ss steady state",c_vec_ss

    if CalcTPI==True: #Time Path Iteration, activated by line 24
	    print "Beginning TPI"
	    initialguess_params = (I, S, T, delta, alpha, e, A)
	    assets_init, kf_init, w_initguess, r_initguess, kd_init, n_init, y_init, c_init = \
		    Stepfuncs.get_initialguesses(initialguess_params, assets_ss, kf_ss, w_ss, r_ss)

	    tp_params = (I, S, T, T_1, beta, sigma, delta, alpha, e, A, StartFertilityAge, StartDyingAge, N_matrix, MortalityRates, distance, diff, xi, MaxIters)
	    wpath, rpath, cpath, Kpath, ypath, apath = Stepfuncs.get_Timepath(tp_params, w_initguess, r_initguess, assets_init, kd_ss, kf_ss, w_ss, r_ss)
	
	    if Graphs==True:
		    Stepfuncs.plotTimepaths(I, S, T, wpath, rpath, cpath, Kpath, ypath, CountryNamesON)
            
            print wpath.shape
            print rpath.shape
            print apath.shape

            outfile = TemporaryFile()

            apath=np.reshape(apath,(I*(S+1)*(T+S+1)))

            np.savetxt("apath.csv",apath,delimiter=",")
            np.savetxt("wpath.csv",wpath,delimiter=",")
            np.savetxt("rpath.csv",rpath,delimiter=",")
Example #4
0
def Multi_Country(S,I,sigma):
    I_dict = {"usa":0,"eu":1,"japan":2,"china":3,"india":4,"russia":5,"korea":6}
    #Parameters Zone
    T = int(round(4*S)) #Number of time periods to convergence, based on Rick Evans' function.
    I_touse = ["eu","russia","usa","japan","korea","china","india"]

    T_1 = S #This is like TransYear in the FORTRAN I think

    if S > 50:
        T_1 = 50

    g_A = 0.015 #Technical growth rate

    beta_ann=.95 #Starting future consumption discount rate
    delta_ann=.08 #Starting depreciation rate
    beta = beta_ann**(70./S) #Future consumption discount rate
    delta = 1-(1-delta_ann)**(70./S) #Depreciation Rate
    alpha = .3 #Capital Share of production
    chi = 1.5 #New Parameter
    rho = 1.3 #Other New Parameter

    tpi_tol = 1e-8 #Convergence Tolerance
    demog_ss_tol = 1e-8 #Used in getting ss for population share
    xi = .9999 #Parameter used to take the convex conjugate of paths
    MaxIters = 10000 #Maximum number of iterations on TPI.

    #Program Levers
    CalcTPI = False #Activates the calculation of Time Path Iteration

    PrintAges = False #Prints the different key ages in the demographics
    PrintLoc = False #Displays the current locations of the program inside key TPI functions
    PrintEulErrors = False #Prints the euler errors in each attempt of calculating the steady state
    PrintSS = True #Prints the result of the Steady State functions
    Print_cabqTimepaths = False #Prints the consumption, assets, and bequests timepath as it gets filled in for each iteration of TPI
    CheckerMode = False #Reduces the number of prints when checking for robustness

    DemogGraphs = False #Activates graphing graphs with demographic data and population shares
    TPIGraphs = True #Activates graphing the graphs.

    UseStaggeredAges = True #Activates using staggered ages
    UseDiffDemog = True #Turns on different demographics for each country
    UseSSDemog = False #Activates using only steady state demographics for TPI calculation
    UseDiffProductivities = False #Activates having e vary across cohorts
    UseTape = True #Activates setting any value of kd<0 to 0.001 in TPI calculation
    SAVE = False #Saves the graphs
    SHOW = True #Shows the graphs
    ADJUSTKOREAIMMIGRATION = True

    LeaveHouseAge, FirstFertilityAge, LastFertilityAge, MaxImmigrantAge, FirstDyingAge, agestopull = Stepfuncs.getkeyages(S, PrintAges, UseStaggeredAges)

    if len(I_touse) < I:
        print "WARNING: We are changing I from", I, "to", len(I_touse), "to fit the length of I_touse. So the countries we are using now are", I_touse
        I = len(I_touse)
        time.sleep(2)
    elif len(I_touse) > I:
        print "WARNING: We are changing I_touse from", I_touse, "to", I_touse[:I], "so there are", I, "regions"
        I_touse = I_touse[:I]
        time.sleep(2)

    if UseDiffDemog:
        A = np.ones(I)+np.cumsum(np.ones(I)*.05)-.05 #Techonological Change, used for when countries are different
        #A = np.ones(I)
    else:
        A = np.ones(I) #Techonological Change, used for idential countries

    if UseDiffProductivities:
        e = np.ones((I, S, T))
        e[:,FirstDyingAge:,:] = 0.3
        e[:,:LeaveHouseAge,:] = 0.3
    else:
        e = np.ones((I, S, T)) #Labor productivities

    #MAIN CODE

    #Gets demographic data
    demog_params = (I, S, T, T_1, LeaveHouseAge, FirstFertilityAge, LastFertilityAge, FirstDyingAge, MaxImmigrantAge, agestopull, g_A, demog_ss_tol)
    demog_levers = PrintLoc, UseStaggeredAges, UseDiffDemog, DemogGraphs, CheckerMode

    MortalityRates, Nhat_matrix, Nhat_ss, lbar = Stepfuncs.getDemographics(demog_params, demog_levers, I_dict, I_touse, ADJUSTKOREAIMMIGRATION)

    #Initalizes initial guesses
    assets_guess = np.ones((I, S-1))*.1
    kf_guess = np.zeros((I))

    #Gets the steady state variables
    params_ss = (I, S, beta, sigma, delta, alpha, chi, rho, e[:,:,-1], A,\
                 FirstFertilityAge, FirstDyingAge, Nhat_ss, MortalityRates[:,:,-1],\
                 g_A, lbar[-1], PrintEulErrors, CheckerMode)
    #assets_ss, kf_ss, kd_ss, n_ss, y_ss, r_ss, w_ss, c_vec_ss = Stepfuncs.getSteadyState(params_ss, assets_guess, kf_guess)


    #NEW CODE BEGINS HERE
    r_ss_guess = .2
    bq_ss_guess = np.ones(I)*.2
    bq_ss, r_ss, w_ss, cvec_ss, avec_ss, kd_ss, kf_ss, n_ss, y_ss = Stepfuncs.getSteadyStateNEWEST(params_ss, bq_ss_guess, r_ss_guess, I_touse)
    #NEW CODE ENDS HERE

    if PrintSS==True: #Prints the results of the steady state, line 23 activates this
        print "assets steady state", avec_ss
        print "kf steady state", kf_ss
        print "kd steady state", kd_ss
        print "n steady state",n_ss
        print "y steady state", y_ss
        print "r steady state",r_ss
        print "w steady state", w_ss
        print "c_vec_ss steady state",cvec_ss

    if UseSSDemog == True:
        print "NOTE: USING SS DEMOGRAPHICS FOR TIMEPATH\n"
        Nhat_matrix = np.einsum("is,t->ist", Nhat_matrix[:,:,-1],np.ones(T))
        MortalityRates = np.einsum("is,t->ist", MortalityRates[:,:,-1],np.ones(T))
        time.sleep(2)

    if CalcTPI==True: #Time Path Iteration, activated by line 24
        print "Beginning TPI..."
        #Gets initial guesses for TPI
        initialguess_params = (I, S, T, delta, alpha, e[:,:,0], lbar, A, FirstFertilityAge, FirstDyingAge, Nhat_matrix[:,:,0], MortalityRates[:,:,0], g_A)
        assets_init, wpath_initguess, rpath_initguess = \
            Stepfuncs.get_initialguesses(initialguess_params, assets_ss, kf_ss, w_ss, r_ss, PrintLoc)

        #Gets timepaths for w, r, C, K, and Y
        tp_params = (I, S, T, T_1, beta, sigma, delta, alpha, rho, chi, e, A, FirstFertilityAge, FirstDyingAge, Nhat_matrix, MortalityRates, g_A, lbar, tpi_tol, xi, MaxIters, CheckerMode)
        wpath, rpath, Cpath, Kpath, Ypath = Stepfuncs.get_Timepath(tp_params, wpath_initguess, rpath_initguess, assets_init, kd_ss, kf_ss, PrintLoc, Print_cabqTimepaths, UseTape)
    	
        if TPIGraphs==True:
            Stepfuncs.plotTimepaths(I, S, T, sigma, wpath, rpath, Cpath, Kpath, Ypath, I_touse, SAVE, SHOW, CheckerMode)