##We have three countries
theta <- 8
T <- c(2, 1, 1)
##Trade Cost Parameters
d = matrix(,nrow=3,ncol=3) ##Intialize Trade Cost Matrix
##Enter Trade Costs
d[1,2] <- 1.2
d[2,1] <- d[1,2] ##Trade costs are symmetric
d[1,3] <- 1.5
d[3,1] <- d[1,3]
d[2,3] <- 1.5
d[3,2] <- d[2,3]
##Self trade costs equal to 1
d[1,1] <- 1
d[2,2] <- 1
d[3,3] <- 1
##Instead of using the final conditions, let's see what happens if we use the initial conditions.
##Suppose there are 10000 goods as an approximation to the continuum
num_products = 10000
##The distribution of productivities comes from a Frechet distribution.
##Use the Frechet distribution from the Package EVD
#install.packages("evd") ##If not installed then install the package
library("evd") ##load the package
##Generate productivites for each country
Productivities_Country1 = rfrechet(num_products,scale=T[1],shape=theta)
Productivities_Country2 = rfrechet(num_products,scale=T[2],shape=theta)
Productivities_Country3 = rfrechet(num_products,scale=T[3],shape=theta)
##Plot the density of Country 1 Productivities
plot(density(Productivities_Country1))
##See the log-log CCDF plot of Productivities
Productivity = sort(Productivities_Country1)
CCDF = 1-ecdf(Productivities_Country1)(sort(Productivities_Country1) )
plot(Productivity , CCDF, log="xy")
##Suppose that the unit production cost is 1 in each country (in the problem set it's not). Then calculate each country's cost to export to serve country 1 for each good.
Prices_Exporter1_Importer1 = (1/Productivities_Country1)*d[1,1]
Prices_Exporter2_Importer1 = (1/Productivities_Country2)*d[2,1]
Prices_Exporter3_Importer1 = (1/Productivities_Country3)*d[3,1]
##For each good find the minimum price using pmin
Min_Price_Importer1 = pmin(Prices_Exporter1_Importer1,Prices_Exporter2_Importer1,Prices_Exporter3_Importer1)
##Plot the CCDF for the inverse of prices, see it looks Frechet still
Prices = sort(1/Min_Price_Importer1)
CCDF = 1-ecdf(1/Min_Price_Importer1)(sort(1/Min_Price_Importer1) )
plot(Prices , CCDF, log="xy")