Fun with empirical and function-based derivatives in R

tl;dr: Use functions like Deriv::Deriv(), splinefun(), approxfun(), and uniroot() to do things with derivatives in R, both with actual functions and with existing empirical data

A typical microeconomics problem involves finding the optimal price and quantity of a product, given its demand and cost across different quantities. You can optimize this price and quantity and maximize profit by finding the point where the marginal cost and the marginal revenue (or the first derivatives of the cost and revenue functions) are equal to each other.

For instance, the demand for some product can be defined as \(Q = 10 - 2P\) (where \(Q =\) quantity and \(P =\) price). The revenue you get from selling that product is defined as \(R = PQ\) (just multiplying price × quantity), so through some algebraic trickery and rearranging of Ps and Qs, you can create a revenue function for this demand curve: \(R = 5Q - 0.5Q^2\). The cost function for this product can be defined as \(C = 0.25Q + 0.5Q^2\).

To figure out the optimal profit, we set the marginal cost and marginal revenue equations equal to each other and solve for Q. Here, \(\frac{dC}{dQ} = MC = 0.25 + 0.5Q\) and \(\frac{dR}{dQ} = MR = 5 - Q\), so with algebra we can find the optimal point:

Finally, use the uniroot() function to look for the point where mc and mr intersect within a given range (here I’m looking between 1 and 10 since the demand curve goes negative after \(Q =\) 10):

optimal_q <- uniroot(function(x) mc(x)- mr(x),c(1,10))
optimal_q$root

## [1] 3.166667

It’s the same answer!

We can then plug optimal_q$root back into the marginal revenue and demand functions to find the optimal price (in a competitive market, the price should be equal to the marginal revenue, but this happens to be a monopoly, so the actual price is higher, but that’s totally unrelated to the topic here):

mr(optimal_q$root)

## [1] 1.833333

demand(optimal_q$root)

## [1] 3.416667

# oh noes monopolies

However! Wait! Stop! This is all well and fine if you have precise formulas for demand and cost. But real life is far messier than this. What if you don’t know the underlying equations?

Often in economics, you have a set of quantities and prices based on empirical data. Market research and surveys can estimate the demand for a product, and tracking how fixed and variable costs change over time can estimate the costs for a product, but this data is all empirically based and not based in actual formulas.

For instance, suppose you have this table of prices, quantities, and costs (which is actually really based on the demand and cost functions from earlier):

We can still use R to find the optimal quantity, even without actual formulas. R has two base functions for approximating functions based on existing data. approxfun() will try to fit data linearly, and splinefun() will try to fit data with cubic splines (i.e. it can handle curvy lines better than approxfun()).

If we compare the empirically-based functions with their real-life counterparts, we can see that the approximation worked great:

cost(1:10)

## [1] 0.5 1.5 3.0 5.0 7.5 10.5 14.0 18.0 22.5 27.5

cost_empirical(1:10)

## [1] 0.5 1.5 3.0 5.0 7.5 10.5 14.0 18.0 22.5 27.5

revenue(1:10)

## [1] 4.5 8.0 10.5 12.0 12.5 12.0 10.5 8.0 4.5 0.0

revenue_empirical(1:10)

## [1] 4.5 8.0 10.5 12.0 12.5 12.0 10.5 8.0 4.5 0.0

Determining the marginal cost and revenue functions from these approximations is surprisingly easy because splinefun() objects have a built-in mechanism for returning derivatives with a deriv argument:

mc(1:10)

## [1] 0.75 1.25 1.75 2.25 2.75 3.25 3.75 4.25 4.75 5.25

cost_empirical(1:10, deriv =1)

## [1] 0.75 1.25 1.75 2.25 2.75 3.25 3.75 4.25 4.75 5.25

mr(1:10)

## [1] 4 3 2 1 0 -1 -2 -3 -4 -5

revenue_empirical(1:10, deriv =1)

## [1] 4 3 2 1 0 -1 -2 -3 -4 -5

Magic!

We can plot these empirically-approximated marginal functions and see that they intersect, as expected:

And just like before, we can find the optimal price, given this quantity. But first we have to create an empirical function for the demand. The demand variable is linear here, so we can use approxfun(), but splinefun() works just fine too (and it has built-in derivative capabilities, while approxfun() doesn’t).

In this case, the empirical solution and the function-based solution are identical, but that’s only because I created the empirical data from the functions. In real life, though, this same process should work on any empirical price, quantity, and cost data.