! global.f90 -- global variables shared across modules
! (grids, value/policy functions, distributions, aggregate paths).

MODULE GLOBAL
!!!!!!!!!!!!!!!!!!!!!!!!
!!   USE STATEMENTS   !!
!!!!!!!!!!!!!!!!!!!!!!!! 
USE PARAMETERS

IMPLICIT NONE

!!!!!!!!!!!!!!!!!!!!!!!!
!!    DECLARATIONS    !!
!!!!!!!!!!!!!!!!!!!!!!!!   
    
!! GRIDS
REAL(DP), DIMENSION(N_k)        :: k_grid			! capital
REAL(DP), DIMENSION(N_kfine)    :: kfine			! capital fine grid
REAL(DP), DIMENSION(N_l)        :: l_grid			! labor supply grid
REAL(DP), DIMENSION(N_z)        :: z_grid			! persistent income shocks (z)
REAL(DP), DIMENSION(N_z,N_z)    :: ptran            ! transition matrix (z,z)
REAL(DP), DIMENSION(jret-1,N_z) :: wfh_z            ! share of hours that can be worked from home (z)
REAL(DP), DIMENSION(N_s)        :: s_grid           ! savings rate
REAL(DP), DIMENSION(N_ceq)      :: ceq_grid
REAL(DP), DIMENSION(N_cfh)      :: cfh_grid

!! Demographics
REAL(DP), DIMENSION(jmax)       :: psi_j            ! aging probability (j,h)
REAL(DP), DIMENSION(jmax,N_h)   :: eta_j            ! age-income profile (j,e,h)
REAL(DP), DIMENSION(jmax)       :: mu_j             ! age distribution (j)

!! Other variables
REAL(DP)                        :: beta_adj         ! period-adjusted discount factor
REAL(DP)                        :: vsl, ubar, utilsick, cbar, gamgam, utilsui
REAL(DP)                        :: rho_z, sigma_eps
REAL(DP)                        :: wage, r, rnew, R_adj, delta_adj
INTEGER, DIMENSION(N_z)         :: Nzmin, Nzmax

!! Aggregates
REAL(DP)    :: aggk, aggc, aggh, aggl, aggy, bigs, bigi, bigr, bequest, betaet
REAL(DP)    :: aggkinit, aggcinit, agghinit, agglinit, aggyinit, aggocinit, aggohinit
REAL(DP)    :: wss, rss
REAL(DP)    :: pvdef, sui_init, medk
INTEGER     :: idx_kmed
        
!! VALUE FUNCTION & POLICY FUNCTIONS (Steady state)
REAL(DP), DIMENSION(jmax,N_z,N_k)   :: vss, vssh, vssl, v_vsl   ! value function (j,e,h,z,k)
REAL(DP), DIMENSION(jmax,N_z,N_k)   :: vhat_s, welf         ! pandemic value function
REAL(DP), DIMENSION(jmax,N_z,N_k,N_ceq) :: vhati, vhati_old
REAL(DP), DIMENSION(jmax,N_z,N_k)   :: kss          ! savings policy in levels (j,e,h,z,k)
REAL(DP), DIMENSION(jmax,N_z,N_k)   :: sss          ! savings policy in rate (j,e,h,z,k)
REAL(DP), DIMENSION(jmax,N_z,N_k)   :: css          ! consumption policy (j,e,h,z,k)
REAL(DP), DIMENSION(jmax,N_z,N_k)   :: lss          ! labor policy (j,e,h,z,k)

!! STEADY STATE DISTRIBUTION
REAL(DP), DIMENSION(jmax,N_z,N_k)       :: muss      ! distribution (j,e,h,z,k)
REAL(DP), DIMENSION(N_z)                :: Erd		        ! ergodic productivity distribution
REAL(DP)                                :: MSE, umse, wmse  ! Mean squared errors
REAL(DP), DIMENSION(jmax,N_z,N_kfine)   :: mussfine, mu_old, lssfine

!! TRANSITION PRICES AND QUANTITIES
REAL(DP), DIMENSION(ncaptdist)  :: aggkpath, aggcpath, aggocpath,       &
                                    agghpath, aggohpath, agglpath, aggypath
REAL(DP), DIMENSION(ncaptdist)  :: aggc1path, aggc2path, aggc3path,     &
                                    aggoc1path, aggoc2path, aggoc3path, &
                                    aggh1path, aggh2path, aggh3path,    &
                                    aggoh1path, aggoh2path, aggoh3path
REAL(DP), DIMENSION(ncaptdist)  :: aggocinfpath, aggocinfpathnew,       &
                                    aggohinfpath, aggohinfpathnew,      &
                                    betaepath, betaepathnew
REAL(DP), DIMENSION(ncaptdist)  :: bigspath, bigipath, bigrpath, ipathnew
REAL(DP), DIMENSION(ncaptdist)  :: s1path, s2path, s3path, i1path, i2path, i3path,  &
                                    r1path, r2path, r3path, dpath, d1path, d2path, d3path
REAL(DP), DIMENSION(ncaptdist)  :: aggcs1path, aggcs2path, aggcs3path,  &
                                    aggci1path, aggci2path, aggci3path, &
                                    aggcr1path, aggcr2path, aggcr3path
REAL(DP), DIMENSION(ncaptdist)  :: agghs1path, agghs2path, agghs3path,  &
                                    agghi1path, agghi2path, agghi3path, &
                                    agghr1path, agghr2path, agghr3path
REAL(DP), DIMENSION(ncaptdist)  :: agghtots1path, agghtots2path, agghtots3path,     &
                                    agghtoti1path, agghtoti2path, agghtoti3path,    &
                                    agghtotr1path, agghtotr2path, agghtotr3path
REAL(DP), DIMENSION(ncaptdist)  :: aggtaxpath, aggsuipath, mu_takeuppath
REAL(DP), DIMENSION(ncaptdist)  :: wpath, rpath, rpathnew

!! EPIDEMIOLOGY MODULE
REAL(DP), DIMENSION(N_h)            :: muh
REAL(DP), DIMENSION(jmax)           :: pid
REAL(DP), DIMENSION(jmax,N_h,N_h)   :: pih
REAL(DP)                            :: beta_e, beta_c, beta_l

CHARACTER(LEN=19)                   :: path

END MODULE GLOBAL