A Small Example Computing Bilateral Trade with a Gravity Model
October 24, 2025
According to USA Trade, Louisiana exported approximately $22 billion worth of soybeans in 2022, accounting for 42.6% of all soybeans exported from the United States that year. However, Louisiana only produced about $575 million of soybeans. This discrepancy is due to the United States recording exports as originating from the last port of exit, rather than the actual location of production. In Louisiana’s case, a significant portion of the soybeans exported through its ports are grown in other states, particularly in the Midwest, and then transported via the Mississippi to Louisiana for export. Our goal is trace this production back to its source using a gravity model. These methods can be extended to see the value soybeans traveling from Wisconsin to China.
Example Setup
We are going to consider a model with three states , one good (), and three trade partners (). For each state, we assume we have the following data for state , the for good:
- Production demand -
- Foreign Export demand -
- Domestic Supply -
- Foreign Import Supply -
Where total demand equals total supply, or
Additionally, we have the following data for each trade partner :
- Total exports going to partner -
- Total imports coming from partner -
Finally, for the gravity model we need some metric determining trade impedances. We will use to denote the trade impedance between locations and , where
- for if and are trade connected
- if and are not trade connected.
In our example we take the other countries to be exogenous trade partners, or for .
| Region | ||||
|---|---|---|---|---|
| 30 | 15 | 40 | 5 | |
| 20 | 3 | 18 | 5 | |
| 15 | 17 | 30 | 2 | |
| 5 | 10 | |||
| 7 | 15 | |||
| 10 | 20 |
If you would like to follow along with the code, you can find it in
the GitHub
repository, in particular the file blog_example.jl.
Estimating Trade Flow
In order to apply the gravity model, we need to estimate the trade flows between each state and trade partner. To do this we will use trade and production shares of the input data. Let be the estimated flow of goods from location to location . Note that for all since we are treating the trade partners as exogenous.
We will first consider the flow of goods between states. For this calculation we use domestic demand in state which can be calculated as . The domestic flow is then given using production shares as follows:
The import flow (country to state) is calculated using foreign import supply shares:
Similarly, the export flow (state to country) is calculated using foreign export demand shares:
Notice the following properties preserved by the flow function: - Domestic supply: - Domestic Demand: - Foreign Import Supply: - Foreign Export Demand:
We now have an estimate for the trade flows. Why not stop here? Why is the gravity model needed? There are a few issues with this data. First, production do not take into account trade impedances. For example, if state is very far from trade partner , we would expect less trade between them than production shares estimate. Second, the production shares are too high. Most soybeans grown in Wisconsin are used in Wisconsin as cattle feed. A gravity model solves both of these issues by adjusting the flows based on trade impedances and scaling the flows to match observed trade data.
An Overview of the Gravity Model
Our gravity model is a CGE model implemented in MPSGE. The general idea is to apply trade impedances to the network of flows given by and find a new equilibrium point .
To further this discussion we need to introduce prices. Define the following:
- be the output price in state
- be the import price coming from country
- be the foreign exchange price
- be the price of absorption in state
Absorption in state is defined as all goods that are consumed within the state, regardless of origin. This includes both domestic goods produced in other states and foreign goods imported from trade partners. It is given by
This identity defines the key sector in our gravity model, the absorption sector in state . This sector outputs goods at the following price and quantity,
- Price:
- Reference Quantity:
For inputs into this sector, we have a choice we can either use domestic goods or foreign goods. This choice introduces a nest in our cost function. For each choice we’ll either specify an elasticity or a price and quantity.
- Domestic:
- Within State
- Price:
- Reference Quantity:
- Reference Price:
- Domestically:
,
for each state
- Price:
- Reference Quantity:
- Reference Price:
- Within State
- Foreign:
,
for each country
- Price:
- Reference Quantity:
- Reference Price:
This is written in MPSGE syntax, and one natural question is what do
Reference Quantity and Reference Price mean?
The Reference Price is adjusts the price of the good, in
particular this appears in the cost function as:
Since we set a reference price of
,
this has the effect of multiplying the price by the trade impedance.
Thus higher trade impedances lead to higher effective prices, which
leads to less consumption of that good. The
Reference Quantity is the total value of the good consumed.
Since we increase the price by the trade impedance, we also need to
scale the quantity by the trade impedance to keep the total value of the
good consumed the same.
In addition to the absorption sector, we have several consumers in the model:
- Exports for each country
- Demands:
for each state
- Price:
- Reference Quantity:
- Reference Price:
- Endowments:
- Price: , Quantity:
- Demands:
for each state
- Representative Agent
- Demands
- Price:
- Reference Quantity: Sum of endowment quantities
- Endowments:
- Price: , Quantity: Total flow of goods
- Price: , Quantity:
- Price: , Quantity:
- Demands
- State Demands: For each state
- Demands
- Price:
- Reference Quantity:
- Endowments
- Price: , Quantity:
- Demands
Two auxiliary variables were used in the model to help with scaling, for each state and for each country . These variables let us link and , holding them both into a ratio with . The constraint for is given by:
Where is a model parameter that allows us to scale the output price relative to the foreign exchange price . The constraint for is given by:
This fully describes the gravity model. The final step is to run the model and extract the new trade flows . These new trade flows will take into account trade impedances and will be scaled to match observed trade data.
The process to run the model is as follows:
- At the benchmark to ensure we starting at an equilibrium.
- Set trade impedances and fix and to maintain a ratio between prices. This ensures the model stays convex.
- Unfix and to allow prices to adjust.
- Finally, we extract the new trade flows .
Results
Let’s start by identifying locations of the states and countries. The following graph shows the locations of each state and country in our example.
We define the trade impedance to be one plus the Euclidean distance between locations. We offset by one to ensure that trade impedances are at least one.
Before we implement our gravity model, let’s make some predictions.
- Within state trade should be much higher in the gravity model. We expect production shares to underestimate within state trade as most goods are consumed locally.
- We expect overall trade between and to decrease as they are the furthest apart.
- should prioritize trade with
- The share of goods flowing from to should be higher than the share of goods flowing from to since is closer to .
Here is what the model predicts for import values:
This matches really well with our predictions. Trade between and has decreased significantly, while within state trade has increased. Additionally, is primarily trading with , and is trading proportionally more with than .
For completeness, here are the export values predicted by the model:
Similar conclusions can be drawn from the export values.
I am working on an interactive dashboard to explore the results of this model. However, given other responsibilities, this may take some time. I will post an update here when it is ready.
Next Steps
This is a small example meant to illustrate the mechanics of applying a gravity model to trace production back to its source. The next step is to scale this up to a full model with all US states and major trade partners. Additionally, I would like to explore different methods for estimating initial trade flows beyond production shares.