---
title: "Solving for Elasticity in a Notch"
author: "Itai Trilnick"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Solving for Elasticity in a Notch}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
  This vignette is meant to explain how `bunchr` estimates the earning elasticity
from bunching induced by a notch. These calculations derive the formulas used for
notch analysis. It closely follows the discussion by Kleven^[Kleven, H.J., 2016.
  _"Bunching"_, Annual Review of Economics 8(1)] with one main difference: Kleven
discusses a notch created by a change in average tax rates on income. `bunchr’
is inspired by another setting: tax rates are marginal, and the notch is created
by a “cash cliff” - where a fixed sum of money is taken from the agent for crossing
a threshold. This “cash cliff” is observed in many settings of government transfers
in developed countries (e.g. disability insurance in the US).

## Setup
The agent faces the following budget line:

  \[ c(z) =
       \begin{cases}
     z \cdot (1-t_1) & \quad \text{if} \quad z \leq z^* \\
     z^* \cdot (1-t_1) + (z - z^*)\cdot (1-t_2) - T   & \quad \text{if} \quad z > z^*\\
     \end{cases}
     \]

Where $c$ is consumption (net earnings after tax), $z$ are pre-tax earnings,
$z^*$ is the notch point, $t_1$ and $t_2$ are the marginal tax rates before and
after the notch point, and $T$ is the Taxed "penalty" for crossing the notch.

An agent has an ability measure $n_i$, and an elasticity of earnings w.r.t.
net-of-tax rate $e_i$. We assume a smooth ability distribution in the population.
We also assume that elasticity is constant among all agents, or that its mean,
conditional on ability, is constant (in the latter case, we are estimating the
mean elasticity in the population). The agent has quasi-linear, iso-elastic utility:

  $$u(c,z) = c - \frac{n}{1 + 1/e } \cdot \left( \frac{z}{n} \right)^{1+1/e}$$

Which has a first order condition of:
  $$ z = n \cdot (1-t)^e$$

Where $z$ is the level of earnings. Note that when the marginal tax rate is zero,
earnings equal ability. Thus we can interpret the ability parameter as the level
of income this individual would earn in a world where marginal tax rate is zero.

## Estimating $e$
There is an agent with ability $n^*$, who optimally earns exactly the sum of money
where the notch kicks in. This agent's tangency condition for maximizing utility
is $z^* = n^* \cdot (1-t_1)^e$.
There is another agent, the marginal buncher, with ability
$n^* + \Delta n^*$. This agent is indifferent between earning at the notch point
$z^*$ or earning at the point satisfying his first order condition, which we call
$z^* + \Delta z^*$. The marginal buncher is indifferent between two bundles, as
the budget line is not convex. Agents with higher ability have only one optimal
point, a tangency point to the right of the notch. They are unaffected by the 
notch.

When estimating elasticity for a notch, `bunchr` first tries to get an estimate
of $\Delta z^*$, using the amount of bunching and assuming that all that bunching
comes from the right side of the distribution. After estimating this $\Delta z^*$,
or being provided one by the user with the _force\_after_ option,`bunchr` finds
the elasticity that would equate utilities of this agent at both point:
$z^*$ and $z^* + \Delta z^*$. To do so, it uses the convenient connection
between ability and earnings given by the first order condition:
$z^* + \Delta z^* = (n^* + \Delta n^*)\cdot (1-t_2)^e$

### Utility of Marginal Buncher at Bunch Point $z^*$
\begin{align*}
u(c,z^*) &= c - \frac{n^* + \Delta n^*}{1+1/e} \cdot \left( \frac{z^*}{n^* + \Delta n^*} \right)^{1+1/e}\\
&= c - \frac{1}{1+1/e} \cdot (n^* + \Delta n^*)^{-1/e} \cdot \left(z^* \right)^{1+1/e}\\
&= c - \frac{1}{1+1/e} \cdot \left(\frac{z^*+\Delta z^*}{(1-t_2)^e} \right)^{-1/e} \cdot \left(z^* \right)^{1+1/e}\\
&= c - \frac{1}{1+1/e} \cdot \frac{(1-t_2)}{(z^*+\Delta z^*)^{1/e}} \cdot \left(z^* \right)^{1+1/e}\\
&= z^* \cdot (1-t_1) - \frac{1}{1+1/e} \cdot \frac{(1-t_2)}{(z^*+\Delta z^*)^{1/e}} \cdot \left(z^* \right)^{1+1/e}
\end{align*}

### Utility of Marginal Buncher at Tangency Point $z^* + \Delta z^*$
\begin{align*}
u(c, z^* + \Delta z^*) &= c - T -  \frac{n^* + \Delta n^*}{1+1/e} \cdot \left( \frac{z^*+\Delta z^*}{n^* + \Delta n^*} \right)^{1+1/e}\\
&= c - T - \frac{1}{1+1/e} \cdot (n^* + \Delta n^*)^{-1/e} \cdot \left(z^* + \Delta z^* \right)^{1+1/e}\\
&= c - T - \frac{1}{1+1/e} \cdot \left(\frac{z^*+\Delta z^*}{(1-t_2)^e} \right)^{-1/e} \cdot \left(z^* + \Delta z^*\right)^{1+1/e}\\
&= c - T - \frac{1}{1+1/e} \cdot \frac{(1-t_2)}{(z^*+\Delta z^*)^{1/e}} \cdot \left(z^* + \Delta z^* \right)^{1+1/e}\\
&= c - T - \frac{1}{1+1/e} \cdot (1-t_2) \cdot \left(z^* + \Delta z^* \right)\\
&= z^* \cdot (1-t_1) + \Delta z^* \cdot (1-t_2) - T - \frac{1}{1+1/e} \cdot (1-t_2) \cdot \left(z^* + \Delta z^* \right)\\
\end{align*}

 ### Equating these two, we can numerically solve for elasticity.
After calculating $\Delta z^*$, `bunchr` solves for elasticity, by minimizing
the difference between these two utilities. The function `elas_equalizer`, included
in `bunchr`, takes the marginal taxes, the Tax variable, and $\Delta z^*$, returning
the squared difference between the utilities defined with some $e$. Using the
`optimize` function in the stats package, `bunchr` finds the elasticity that
minimizes the squared distance between these utilities. Note that, while by definition
of the utility function, earnings cause disutility (from work). Hence elasticity should
be positive. `bunchr` bounds the elasticity search between 0 and 5, the latter being
a very high elasticity in most settings, let alone labor supply.
