rm(list=ls()) #will remove ALL objects


#******** Analytically solving for autarky and free trade equilibriums ****

# Parameters 

A0U = 25     # supply intercept country U
A1U = 1.25   # supply slope country U
B0U = 250    # demand intercept country U
B1U = -2.5   # demand slope country U

A0E = 50    # supply intercept country E
A1E = 2     # supply slope country E
B0E = 125   # demand intercept country E
B1E = -1.75 # demand slope country E

Tariff = 0.1 # Tariff

PU_Au = (-A0U + B0U)/(A1U - B1U); PU_Au # Autarky price for country U
## [1] 60
PE_Au = (-A0E + B0E)/(A1E - B1E); PE_Au # Autarky price for country E
## [1] 20
QU_Au = (-A0U*B1U + A1U*B0U)/(A1U - B1U); QU_Au # Autarky Quantity for country U
## [1] 100
QE_Au = (-A0E*B1E + A1E*B0E)/(A1E - B1E); QE_Au # Autarky Quantity for country E
## [1] 90
PW = ((A0U - B0U) + (A0E - B0E))/((-A1U + B1U) + (-A1E + B1E)); PW # World Price
## [1] 40
NEWEDU = (B0U - A0U) + (B1U - A1U)*(PW + Tariff); NEWEDU
## [1] 74.625
NEWPW = ((B0U - A0U) - (A0E - B0E) + (B1U - A1U)*Tariff)/((A1E - B1E) - (B1U - A1U)); NEWPW
## [1] 39.95
NEWPU = (NEWPW + Tariff);NEWPU
## [1] 40.05
TR = NEWEDU*Tariff  # Tariff Revenue

QDUT = B0U +B1U*NEWPU; QDUT
## [1] 149.875
QSUT = A0U +A1U*NEWPU; QSUT
## [1] 75.0625
QDET = B0E +B1E*NEWPW; QDET
## [1] 55.0875
QSET = A0E +A1E*NEWPW; QSET
## [1] 129.9
#******** Numerically solving for autarky and free traded equilibriums ****

fcnSDU = function(x){
  QU = x[1]
  PU = x[2]
  
  SupplyU = QU - (A0U + A1U*PU) # QU - (A0U + A1U*PU) = 0 in equilibrium
  DemandU = QU - (B0U + B1U*PU) # QU - (B0U + B1U*PU) = 0 in equilibrium
  return(c(SupplyU,DemandU))
}

fcnSDE = function(x){
  QE = x[1]
  PE = x[2]
  
  SupplyE = QE - (A0E + A1E*PE) # QE - (A0E + A1E*PE) = 0 in equilibrium
  DemandE = QE - (B0E + B1E*PE) # QE - (B0E + B1E*PE) = 0 in equilibrium
  return(c(SupplyE,DemandE))
}

fcnESEDw = function(x){
  QW = x[1]
  PUF = x[2]
  PEF = x[3]
  
  ESupplyw = QW - ((B0U + B1U*PEF) - (A0U + A1U*PEF))
  EDemandw = QW - ((A0E + A1E*PUF) - (B0E + B1E*PUF))
  PLINKAGE = PUF - PEF - PEF*Tariff
  
  return(c(ESupplyw,EDemandw, PLINKAGE))
}



library("rootSolve")
solnU = multiroot(fcnSDU, c(1, 1))
QU = solnU$root[1]; QU
## [1] 99.99998
PU = solnU$root[2]; PU
## [1] 60
solnE = multiroot(fcnSDE, c(1, 1))
QE = solnE$root[1]; QE
## [1] 90
PE = solnE$root[2]; PE
## [1] 20
solnw = multiroot(fcnESEDw, c(1, 1, 1))
QW = solnw$root[1]; QW
## [1] 82.14286
PUF = solnw$root[2]; PUF
## [1] 41.90476
PEF = solnw$root[3]; PEF
## [1] 38.09524
QDUF = B0U +B1U*PUF; QDUF
## [1] 145.2381
QSUF = A0U +A1U*PUF; QSUF
## [1] 77.38095
QDEF = B0E +B1E*PEF; QDEF
## [1] 58.33333
QSEF = A0E +A1E*PEF; QSEF
## [1] 126.1905
# ******** Welfare Analysis ****

ChPSU   = A0U*(PUF - NEWPU) + (A1U/2)*(PUF**2 - NEWPU**2); ChPSU  # change in producer surplus for country U
## [1] 141.3732
ChCSU   = B0U*(NEWPU - NEWPW) + (B1U/2)*(NEWPU**2 - NEWPW**2); ChCSU  # change in consumer surplus for country U
## [1] 15
ChPSE   = A0E*(NEWPW - PE_Au) + (A1E/2)*(NEWPW**2 - NEWPU**2); ChPSE  # change in producer surplus for country E
## [1] 989.5
ChCSE   = B0E*(PE_Au - NEWPW) + (B1E/2)*(PE_Au**2 - PW**2); ChCSE  # change in consumer surplus for country E
## [1] -1443.75
NChU = ChPSU + ChCSU; NChU    # net welfare gains for U
## [1] 156.3732
NChE = ChPSE + ChCSE; NChE    # net welfare gains for E
## [1] -454.25
NChW  = NChU + NChE; NChW     # net gains for the world
## [1] -297.8768