Comparative advantage is a microeconomics concept that stipulates that individuals, countries or firms will trade an item with another entity when they can produce a good at a lower opportunity cost than their trading partner. The principle explains numerous economic phenomena across the world, like countries' specializations in goods like coffee or rice. Understanding the math behind comparative advantage might seem difficult, but can easily be distilled with some algebra.

### Understand Opportunity Costs

The first step in understanding the math behind comparative advantage is to first understand the math behind opportunity costs, which are fundamental to comparative advantage. An opportunity cost is the cost someone bears by not doing something. So, in general, economics reduces a person's choice to two choices: choice A or choice B. A person may produce A and B at different rates, with one possibly being produced at a faster rate. So, if a person can produce 12 B's in an hour, but only six A's in an hour, his opportunity cost of producing A is the cost of the foregone B's he could produce in one hour, or 12 B's in favor of producing only six A's. As such, his opportunity cost is 12-6= 6 units of foregone production.

### Production Possibility Sets

Understanding production possibilities sets is the second step in successfully navigating the mathematics of comparative advantage. Given a particular unit of time, say one hour, and the varying speeds at which a single person can produce two goods, how many could she produce if she devoted x amount of time to one good and y to another? For example, say Susan has 60 minutes. For each 10 minutes, she can produce one chair or two tables. So, in 20 minutes, she can produce either two chairs or four tables. In 30 minutes, she can produce three chairs or six tables, and this continues until at 60 minutes, she can either devote all her hour to producing six chairs or 12 tables.

### Comparative Matrices

Now that we understand how to compare how a single person can produce two goods, let's also conduct the math so that we can compare between two persons. Say now that John can produce at the exact opposite rate of Susan, who can produce one chair in 10 minutes or two tables in 10 minutes. John, therefore, can produce two chairs in 10 minutes or one table in 10 minutes. As such, he has the exact opposite production possibilities set as Susan, and can produce either 12 chairs in an hour or six tables if he devotes all of his time to one or the other activity.

### Greater Than or Less Than

Now that we have our possibility production sets for each person, our comparative advantage calculation is quite straightforward. Clearly, based on the fact the John can produce 12 chairs in an hour, while Susan can only produce six, means that he has the comparative advantage in chairs. Susan, meanwhile, has the advantage in tables because she can produce 12 in an hour as opposed to John's six. Each should specialize in their respective advantage, and trade, which will yield a total market outcome of 24 units of chairs and tables, which is more than they would have been able to produce if each person split their time producing both items.