\documentclass[12pt]{article}

\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{float}
\usepackage{tabularx}
\usepackage{amsmath}

\setlength{\parskip}{10pt}
\setlength{\parindent}{0pt}

\title{A Short Introduction to Macroeconomic Modeling}
\author{Will Clark\\
	International Monetary Fund}

\date{August 15-23, 2013}

\begin{document}

\maketitle

\tableofcontents

\section{A Simple Two-Period Model of Consumption}

We will begin by looking at a very simple model to gain some intuition for modeling with representative agents and how to think about intertemporal decision making.

\subsection{Setup of the model}

In this framework, we will assume that the economy is populated with representative households with an exogenous endowment of income (for example, a fruit tree grows in their yard, and the household has no control over how much fruit it produces).

We will assume that households live for two periods: today and tomorrow. In the first period (today), the household can split its endowment between consumption and savings. Whatever is not consumed today must be saved, and whatever is saved today must be consumed tomorrow. The household can also dis-save in order to consume more than its endowment today, but must then pay back tomorrow what it borrowed today. Savings are done with riskless bonds, and we are going to abstract away from financial intermediaries (banks).

There will be a government that collects lump-sum taxes from households and spends its revenue in some non-productive way. That is, households derive no utility from government spending.

\subsection{Analyzing the first period problem}

Let's now consider the decision households must make in the first period. The household has a budget constraint:

\[ c_1 + s = y_1 - t_1 \]

Consumption is given by \(c\), savings given by \(s\), income given by \(y\), and taxes given by \(t\). We will use the subscript 1 for today's variables and the subscript 2 for tomorrow's variables. Note that savings is not subscripted, because the household can only save today. Tomorrow, there is no saving, because there is no third period in which the household can consume.

The budget constraint says that expenditures (consumption and saving) must equal after-tax income. If \(s\) is greater than zero, the household is a saver/lender, foregoing consumption today to consume more tomrrow. If \(s\) is less than zero, the household is a borrower, consuming more today and promising to pay it back tomorrow. If \(s\) is equal to zero, then the household consumes its entire endowment today and does not substitute consumption across time.

How exactly can the household save income? We are assuming that households have access to a riskless bond. By saving with a riskless bond, the household promises to give up one unit of consumption today, but will recieve \((1+r)\) units of consumption tomorrow, where \((1+r)\) is the real interest rate of the economy. There is no risk of default, so the household is guaranteed to recieve the extra consumption tomorrow by forgoing consumption today.

Alternatively, a household can borrow using a riskless bond, where the extra unit of consumption today must be paid back tomorrow at a rate of \((1+r)\). Note that we are implicitly assuming that anyone who wants to lend or borrow can do it at the given interest rate of \(r\).

The real interest rate for lenders and borrowers allows us to express the trade-off between consumption today and consumption tomorrow. Taking the real interest rate as given, the household can express the relative price of future consumption in terms of current consumption, and vice-versa:

\[ c_1 = (1+r) c_2 \]

or

\[ c_2 = \left( \frac{1}{1+r} \right) c_1 \]

\subsection{Analyzing the second period problem}

The household has a very similar budget constraint in the second period. Again, it must equate after-tax income with total expenditures. The main difference is that income tomorrow can come from repaid bonds and expenditures can no longer include savings, since there is no future period to save for. The budget constraint is:

\[ c_2 = y_2 - t_2 + (1+r) s \]

Note that if the household was a borrower in the first period, \((1+r) s \) will be negative in the second period and will represent an expense for the household rather than extra income.

\subsection{Deriving the household's lifetime budget constraint}

We can now combine the household's first period budget constraint and second period budget constraint to derive a lifetime budget constraint. We begin by solving the second period budget constraint for the household's level of savings:

\[ c_2 = y_2 - t_2 + (1+r) s \]

\[ s = \frac{c_2 - (y_2 - t_2)}{1 + r} \]

We can plug this into the first period budget constraint, and then simplify the result:

\[ c_1 + \frac{c_2 - (y_2 - t_2)}{1 + r} = y_1 - t_1 \]

\[ c_1 + \frac{c_2}{1+r} = (y_1 - t_1) + \frac{(y_2-t_2)}{1+r} \]

This lifetime budget constraint equates the present value of lifetime income with the present value of lifetime consumption. In this context, ``present value'' means the value of all consumption expressed in terms of consumption in the first period, using the inter-temporal relative price of consumption derived above. In simpler terms, it states that over the course of the household's life span (two periods), consumption must be equal to after-tax income. The only trick is that everything is measured in terms of the value of consumption in the first period, using the interest rate to denote value across time.

\subsection{A graphical analysis of the lifetime budget constraint}

We can express the budget constraint graphically and visualize the trade-off between consumption today and consumption tomorrow that the household faces. Let's do this by expressing the lifetime budget constraint in terms of tomorrow's consumption as a function of today's consumption:

\[ c_1 + \frac{c_2}{1+r} = (y_1 - t_1) + \frac{(y_2-t_2)}{1+r} \]

becomes

\[ c_2 = (1+r) (y_1 - t_1) + (y_2 - t_2) - (1+r) c_1 \]

We can express lifetime income as wealth, represented by \(w\):

\[ w = y_1 - t_1 + \frac{y_2 - t_2}{1+r} \]

This allows us to express the budget constraint in the following form:

\[ c_2 = (1_r) w - (1+r) c_1 \]

We can plot this on a coordinate plane, where we place \(c_2\) on the vertical axis and \(c_1\) on the horizontal axis:

\begin{figure}[H]
    \includegraphics{figure_1.pdf}
\end{figure}

The slope of the budget constraint is \(-(1+r)\). The horizontal intercept is the point at which the household consumes all its wealth (current and future consumption) in the first period, with no consumption in the second period. The vertical intercept is the point at which the household consumes all its wealth in the second period, with no consumption in the first period.

The line connecting the points is the set of all the household's possible consumption baskets (where a basket is some combination of future and current consumption). The point \(E\) is the endowment point, where the household consumes only the current period income and does not borrow or save. Households above point \(E\) are net lenders/savers, and households below point \(E\) are net borrowers.

\subsection{A graphical analysis of household utility}

Let's start by making an assumption about lifetime utility: it is the sum of today's utility and tomorrow's utility, discoutned by a parameter \(\beta\) that is between 0 and 1. That is, the household has a slight preference for consuming today over consuming the same amount tomorrow. Formally, we can write:

\[ U(c_1,c_2) = u(c_1) + \beta u(c_2) \]

We are implicitly assuming that more consumption is always better than less consumption, and that the household prefers to smooth its level of consumption over time. We can express the household's lifetime utility (given by capital \(U\)) as a function of today's consumption only by solving the lifetime budget constraint for tomorrow's consumption and plugging this in. First, recall that:

\[ w = y_1 - t_1 + \frac{y_2-t_2}{1+r} \]

Now we can write:

\[ U(c_1) = u(c_1) + \beta u((1+r)w-(1+r)c_1) \]

The household wants to maximize lifetime utility, so we can solve for the optimal level of today's consumption by taking the first derivative of utility with respect to today's consumption and setting that value equal to zero. Intuitively, we know that the household could derive some small level of utility from consuming its lifetime wealth today, or by consuming its lifetime wealth tomorrow, but there is some happy medium in between that would deliver the highest level of utility. This is what we are trying to solve for:

\[ U'(c_1) = u'(c_1) + \beta u'((1+r)w-(1+r)c_1)(-1)(1+r) = 0 \]
\[ 0 = u'(c_1) - (1+r) \beta u'((1+r)w-(1+r)c_1) \]
\[ 0 = u'(c_1) - (1+r) \beta u'(c2) \]
\[ u'(c_1) = (1+r) \beta u'(c_2) \]

In the first step, we take the derivative of lifetime utility with respect to consumption and set it equal to zero, utilizing the chain rule. In the second step, we simplify, and in the third step we plug the value for tomorrow's consumption back into the equation. The last step rearranges the terms and gives us the \textbf{Euler equation}, the fundamental intertemoral relationship of this model.

Let's get some intution for this Euler equation. First, note that \(u'(c_1)\) is the marginal utility of consumption: that is, the extra utility the household derives from one more unit of consumption. If the household increases its savings today, its utility goes down by \(u'(c_1)\). Today's foregone consumption is earned back tomorrow, with interest, and the household derives \((1+r)u'(c_2)\) more utility. But that utility is dicounted by the factor \(\beta\) because of the household's impatience.

The Euler equation says that these two marginal utilities must be the same. In other words, in order for the household to maximize its lifetime utility, it must derive the same marginal utility from extra consumption today as it does from extra consumption tomorrow. Otherwise, it would be possible for the household to make itself better off by substituting consumption today for consumption tomorrow (or vice-versa).

Now we can add an indifference curve to the grapical budget constraint. The marginal rate of substitution between \(c_1\) and \(c_2\) is derived from the Euler equation:

\[ u'(c_1) = (1+r)\beta u'(c_2) \]
\[ \frac{\beta u'(c_2)}{u'(c_1)} = \frac{1}{1+r} \]

The left-hand side of this equation is the marginal rate of substitution of consumption and the right-hand side is the relative price of consumption. The slope of the indifference curve will be the negative of the marginal rate of substitution:

\begin{figure}[H]
    \includegraphics{figure_2.pdf}
\end{figure}

The slope of the budget constraint is the negative of the relative price of consumption, and the household's optimal basket of consumption is the point at which the indifference curve is tangent to the budget constraint. This point represents the highest level of utility the household can obtain given its lifetime budget constraint.

\subsection{A comparative statics exercise}

We can use this graphical setup of the model to analyze what would happen if the household's income stream were to change. Suppose that the household's income stream is exogenously increased (either today's income or tomorrow's income will be higher; it doesn't matter which, since the intuition is the same in either case).

The household's lifetime wealth will be higher, pushing the boundaries of the budget constraint father out. Let's call the original lifetime wealth \(w\) and the new lifetime wealth \(w^{hi}\).

\[ w = (y_1 - t_1) + \frac{y_2-t_2}{1+r} \]
\[ w^{hi} = (y_1^{hi} - t_1) + \frac{y_2-t_2}{1+r} \]

The new budget constraint is shifted out, and the household's optimal solution is now on a different (and better) indifference curve. Consumption today and consumption tomorrow both increase as a result of this positive wealth shock:

\begin{figure}[H]
    \includegraphics{figure_3.pdf}
\end{figure}

Note that the increase in current consumption is \textit{less} than the increase in current income. The household is choosing to spread out the higher consumption over both periods (remember that we are assuming the household prefers smooth to volatile consumption). If the positive shock to income were expected tomorrow, the result would be the same: some of the higher income would be consumed today and some would be consumed tomorrow. The household is always trying to maximize its \textit{lifetime} utility, and that is the basis for the observed consumption smoothing behavior in this model.

\subsection{Next steps}

This simple model is a powerful tool for building intuition but has some obvious problems. For one, it assumes perfect credit markets: whenever a household wants to lend, it can lend, and whenever a household wants to borrow, it can borrow. A bigger issue, though, is how the interest rate is set. When all households try to lend, shouldn't interest rates fall? This is not what we observe in this model, because there is no general equilibrium framework for lending and borrowing. That is, we need to consider both sides of the market for credit in order to understand the dynamics more accurately. We will do this, and incorporate more general equilibrium features, in the next section with a small real business cycle model.

\section{A Small Real Business Cycle Model}

We will now consider a small real business cycle to gain some intuition for modeling complete markets (in this case, markets for capital and labor) and to understand how exogenous shocks can impact a dynamic model. The model will be represented with a small system of non-linear difference equations describing the path of the economy over time.

\subsection{Setupt of the model}

Let's first consider how the real business cycle model will be similar to the stochastic growth model. Both models will assume a representative household that tries to maximize its utiility subject to a constraint. Decisions are made intertemporally, with an eye towards maximizing lifetime utility (whether the lifetime is two periods or an infinite number of periods).

In the real business cycle model, the time horizon will be infinite; households will be ``infinitely-lived.'' We will utilize a general equilibrium framework, which means we must carefully consider both the supply side and the demand side of the market for goods and services. This economy will be populated with households and firms, and income will be earned rather than exogenous. It is a much more realistic framework that allows us to consider some real policy-relevant issues.

\subsubsection{Firms stucture}

The economy is populated by a representative firm that produces a generic output good. Production of output requires two inputs--capital and labor--both supplied by households. Production technology is given by a Cobb-Douglas function:

\begin{equation}
y_t = a_t k_t^{\alpha} n_t^{1-\alpha}
\end{equation}

where \(a_t\) is aggregate factor productivity (think of it as economy-wide technology), \(y_t\) is output, \(k_t\) is capital, \(n_t\) is labor, and \(\alpha\) is the capital's share of total output.

Inputs are rented on perfectly competitive markets, and firms have no price power, so they must pay whatever price the market dictates. The firm's goal is to maximize its profit, which is defined as produced output minus the cost of inputs. The firm maximization problem is static (not intertemporal) and can be expressed as:

\[ \max_{k_t, n_t} \Pi_t = y_t - R_t k_t - w_t n_t \]

where \(\Pi_t\) is firm profit, \(R_t\) is the rental rate of capital, and \(w_t\) is the wage paid to labor. To solve this problem, we can substitute in the production function specified above and then take the derivitave with respect to the firm's choice variables (capital and labor). We will end up solving for the optimal prices the firm should pay for each of its inputs.

\[ \max_{k_t, n_t} \Pi_t = a_t k_t^{\alpha} n_t^{1-\alpha} - R_t k_t - w_t n_t \]

First, we'll sovle for the rental rate of capital:
\[ \frac{\partial \Pi_t}{\partial k_t} = 0 \iff \alpha a_t k_t^{\alpha-1} n_t^{1-\alpha} - R_t = 0 \]
\[ R_t = \alpha a_t k_t^{\alpha-1} n_t^{1-\alpha} \]
\begin{equation}
R_t = \frac{\alpha y_t}{k_t}
\end{equation}

Now we'll solve for the wage:
\[ \frac{\partial \Pi_t}{\partial n_t} = 0 \iff (1-\alpha) k_t^{\alpha} n_t^{-\alpha} - w_t = 0 \]
\[ w_t = (1-\alpha) k_t^{\alpha} n_t^{-\alpha} \]
\begin{equation}
w_t = \frac{(1-\alpha) y_t}{n_t}
\end{equation}

Note that because markets for inputs are competitive, firms pay exactly the marginal value of each input and therefore have no profit.

\subsubsection{Household structure}

In this economy, households derive income from labor (firms pay them a wage) and from renting capital (rental rate paid by the firms). They also receive firm profits, though because of the competitive market assumption, firm profits are zero. Households can spend their income on consumption and on investment in capital. For now, we will assume that capital investment is the only way that households can save their income. The household's budget constraint is:

\[ c_t + I_t = R_t k_t + w_t n_t + \Pi_t \]

where \(c_t\) is consumption and \(I_t\) is investment in capital. Capital accumulates according to the rate of depreciation (given by \(\delta\)) and the level of investment. The accumulation equation is given by:

\begin{equation}
k_{t+1} = I_t + (1-\delta) k_t
\end{equation}

which simply states that tomorrow's stock of capital is equal to today's stock of capital, less depreciation, plus today's flow of investment.

Households are trying to maximize the present value of their lifetime stream of consumption. We will assume that utility comes only from consumption and that functional form of the utility function is logarithmic. That is:

\[ u(c_t) = \ln(c_t) \]

This functional form says that utility increases with consumption, though it increases at a decreasing rate. In other words, there are always positive but decreasing returns to increased consumption. The household problem is to maximize its lifetime utility subject to a budget constraint. It chooses how much to consume, how much to invest, and what the stock of capital should be, and is subject to a budget constraint. For the sake of analytical simplicity, we will assume that the household always provides a fixed quantity of labor:

\begin{equation}
n_t = 1
\end{equation}

In order to simplify the problem so that there is only one constraint, we will substitute in the capital accumulation equation to the budget constraint:

\[ c_t + k_{t+1} - (1-\delta) k_t = R_t k_t + w_t n_t + \Pi_t \]

This allows us to act as if there are only two choice variables, since the level of capital chosen by the household implicitly defines how much the flow of investment should change. We can solve the problem analytically by setting up a Lagrangian:

\[ \max_{c_t,k_{t+1}} \mathcal{L}  = E_0 \sum_{t=0}^{\infty} \beta^t \Bigl[ \ln(c_t) + \lambda_t (R_t k_t + w_t n_t + \Pi_t - c_t - k_{t+1} + (1-\delta) k_t) \Bigr] \]

We can solve this as we did for the firm problem, by taking the derivative with respect to the two choice variables and setting them equal to zero. First, we will take the derivative with respect to consumption:

\[ \frac{\partial \mathcal{L}}{\partial c_t} = 0 \iff \beta^t \frac{1}{c_t} - \beta^t \lambda_t = 0 \]
\[ \frac{1}{c_t} = \lambda_t \]

The Lagrange multiplier \(\lambda_t\) is the shadow value of consumption: that is, the utility that the household derives from an additional unit of consumption. Next, we can find the optimality condition for the capital stock. Note that since we are solving for \(k_{t+1}\) and the capital stock shows up in our equation at time \(t\) and time \(t+1\) we have to take the derivative at time \(t\) and time \(t+1\) as well:

\[ \frac{\partial \mathcal{L}}{\partial k_{t+1}} = 0 \iff \beta^t \lambda_t + \beta^{t+1} E_t \lambda_{t+1} (R_{t+1} + (1-\delta)) = 0 \]
\[ \lambda_t = \beta E_t \lambda_{t+1} (R_{t+1} + (1-\delta)) \]

I can combine these two optimality conditions to get the \textbf{Euler equation} that determines the household's consumption path across time:

\begin{equation}
\frac{1}{c_t} = \beta E_t \frac{1}{c_{t+1}}(R_{t+1} + (1-\delta))
\end{equation}

The intuition for this is the same as in the small two-period model: the household must optimally choose its consumption today and consumption tomorrow so that it recieves the same utility (discounted in the future) for consumption across time. Otherwise, the household could be better off by changing its consumption path and its behavior would be sub-optimal.

\subsubsection{Total factor productivity}

We assumed above that the productivity of private firms is dependent on an aggregate technological process that applies to the whole economy. We will assume that this process is exogenous and that it follows an autoregressive process in the log (so that it is equal to 1 when the model is in equilibrium). Think of this as a catch-all factor for every possible causal mechanism in the economy that could change the productivity of private firms: droughts, floods, new technologies, political conflict, etc. We will model it like this:

\begin{equation}
\ln(a_t) = \rho \ln(a_{t-1}) + \epsilon_t
\end{equation}

The persistence of this AR(1) process is determined by the parameter \(\rho\). The exogenous shock \(\epsilon_t\) is a mean-zero shock with no auto-correlation. This shock is how our model economy will vary from its equilibrium.

Lastly, need to keep track of the total resource contraint in the economy. This is simply an identity stating that total consumption and investment in any given period must be equal to total output. Since our model is of a closed economy, there are no imports or exports that you would otherwise see in a GDP identity:

\begin{equation}
y_t = c_t + I_t
\end{equation}

\subsubsection{Equilibrium of the model}

The model equilibrium is defined by the eight equations identified above and the eight endogenous variables we have defined. The eight variables are:

\begin{center}
\begin{tabular}{llll}
	\(y_t\) & output & \(n_t\) & labor \\
	\(c_t\) & consumption & \(R_t\) & price of capital \\
	\(I_t\) & investment & \(w_t\) & wage \\
	\(k_t\) & capital & \(a_t\) & total factor productivity \\
\end{tabular}
\end{center}

and the eight equations are (1)-(8) from above:

\[ k_{t+1} = I_t + (1-\delta) k_t \]
\[ y_t = a_t k_t^{\alpha} n_t^{1-\alpha} \]
\[ R_t = \frac{\alpha y_t}{k_t} \]
\[ w_t = \frac{(1-\alpha) y_t}{n_t} \]
\[ n_t = 1 \]
\[ \frac{1}{c_t} = \beta E_t \frac{1}{c_{t+1}}(R_{t+1} + (1-\delta)) \]
\[ \ln(a_t) = \rho \ln(a_{t-1}) + \epsilon_t \]
\[ y_t = c_t + I_t \]

\subsection{Finding the model equilibrium}

Let's first think about what it means for the model to have an equilibrium. It means that there is a certain value for each variable that allows the system to remain constant over time. In other words, the endogenous variables are time invariant and the system does not move. The equilibrium point is the point to which the model will always return after being subjected to an exogenous shock, given enough time to stabilize.

In mathematical terms, the equilibrium is the point at which all variables are the same, or more formally:

\[ x_t = x_{t+1} = x^* \]

where \(x_t\) is a stand-in for all the endogenous variables of the model. We will use the star notation to indicate that variables are at their equilibrium value. Our goal is to solve for the steady state values of all endogenous variables in terms of the deep parameters of the model, which are constant across time.

In the system of eight equations that describe the model, we can substitute the time-invariant equilibrium values and derive an analytical solution that describes the steady state. We can start with the easiest equation:

\begin{equation}
n^* = 1
\end{equation}

We are simply assuming that labor is constant across time anyway. Next, we can solve for the steady state value of factor productivity, recalling that the natural log of one is zero and that the exogenous shock is mean-zero (i.e. it's equilibrium value is zero):

\[ \ln(a^*) = \rho \ln(a^*) + \epsilon_t \]
\begin{equation}
a^* = 1
\end{equation}

Next, let's look at the Euler equation. Note that when I plug in equilibrium values for consumption, they cancel out on each side of the equation, and I am left with \(R*\) and some parameters, which allows me to isolate the endogenous variable:

\[ \frac{1}{c^*} = \beta E_t \frac{1}{c^*}(R^* + (1-\delta)) \]
\[ 1 = \beta E_t (R^* + (1-\delta)) \]
\begin{equation}
R^* = \frac{1}{\beta}-(1-\delta)
\end{equation}

Now we can use the value for the rental rate of capital and solve for the stock of capital (I am dropping labor from this expession as it is equal to 1 in equilirbium):

\[ R^* = \alpha k^{*\alpha} \]
\[ \frac{1}{\beta}-(1-\delta) = \alpha k^{*\alpha} \]
\begin{equation}
k^* = \left(\frac{\alpha}{\frac{1}{\beta}-(1-\delta)}\right)^{\frac{1}{1-\alpha}}
\end{equation}

We can plug this value of the capital stock into our production function and find the equilibrium level of output (again dropping labor from the expression):

\[ y^* = k^{*\alpha} \]
\begin{equation}
y^* = \left(\frac{\alpha}{\frac{1}{\beta}-(1-\delta)}\right)^{\frac{\alpha}{1-\alpha}}
\end{equation}

The level of output can be used to solve for the equilibrium wage:

\[ w^* = (1-\alpha) y^* \]
\begin{equation}
w^* = (1-\alpha)\left(\frac{\alpha}{\frac{1}{\beta}-(1-\delta)}\right)^{\frac{\alpha}{1-\alpha}}
\end{equation}

We can use the capital accumulation equation to solve for the level of investment in terms of the capital stock:

\[ k^* = I^* + (1-\delta) k^* \]
\[ I^* = \delta k^* \]
\begin{equation}
I^* = \delta \left(\frac{\alpha}{\frac{1}{\beta}-(1-\delta)}\right)^{\frac{1}{1-\alpha}}
\end{equation}

Finally, we can solve for the level of consumption by rearranging the economy-wide resource constraint:

\[ y^* = c^* + I^* \]
\[ c^* = y^* - I^* \]
\begin{equation}
c^* = \left(\frac{\alpha}{\frac{1}{\beta}-(1-\delta)}\right)^{\frac{\alpha}{1-\alpha}} - \delta \left(\frac{\alpha}{\frac{1}{\beta}-(1-\delta)}\right)^{\frac{1}{1-\alpha}}
\end{equation}

With equations (9)-(16), we can now find numerical values to describe the model's equilibrium for a given parameterization. This will be important when we are using the computer to simulate the model, and also when we are finding the linearized solution to the model, as we will do in the next section.

\subsection{Finding a linearized solution to the model}

Our model, represented by a system of non-linear difference equations, does not have a closed-form analytic solution (i.e. we cannot solve it by hand, with pencil and paper). One way to use the model, then, is to rely on a linear approximation of the model that can be solved analytically. A common technique for doing this is by log-linearizing the model around a point (the steady state, which we have been calling the equilibrium). The result will give us a system of linear difference equations, which we can use to derive a policy function that expresses the dynamics of the system.

What we'll do first is collapse our system into only three equations that describe completely all the dynamic relationships we have captured above. We will do this by plugging in values and dropping those that are constant (i.e. labor). First, we plug in the price of capital to the Euler equation:

\[ \frac{1}{c_t} = \beta E_t \frac{1}{c_{t+1}}(R_{t+1} + (1-\delta)) \]
\begin{equation}
\frac{1}{c_t} = \beta E_t \frac{1}{c_{t+1}}(\alpha a_{t+1} k_{t+1}^\alpha + (1-\delta))
\end{equation}

Next, we will combine the capital accumulation equation and the aggregate resource constraint:

\[ k_{t+1} = I_t + (1-\delta) k_t \]
\[ k_{t+1} = y_t - c_t + (1-\delta) k_t \]

Then we will plug in our value for output in terms of the capital stock:

\begin{equation}
k_{t+1} = a_t k_t^\alpha - c_t + (1-\delta) k_t
\end{equation}

The last equation will be the AR(1) process that describes the path of factor productivity:
\begin{equation}
\ln(a_t) = \rho \ln(a_{t-1}) + \epsilon_t
\end{equation}

Now equations (17)-(19) describe the system in three equations by keeping track of three variables (consumption, capital, and technology). The rest of the variables can be described in terms of these three, which we will do after deriving the policy function in order to simulate our entire model.

\subsubsection{Log-linearizing the model}

In many cases, finding the closed-form solution to a discrete time problem like this can be exceedingly difficult. One useful approximation technique is to log-linearize the model around a point, usually the steady state of the model. The end result will be a system of linear equations where variables are expressed as percentage deviations from their steady state value.

Recall that Taylor's theorem tells us that we can express a function \(f(x)\) as follows:

\[ f(x) = f(x^*) + \frac{f'(x^*)}{1!}(x-x^*) + \frac{f^{(2)}(x^*)}{2!}(x-x^*) + \frac{f^{(3)}(x^*)}{3!}(x-x^*) + ...\]

For a sufficiently smooth function, the higher-order derivitaves will be small and the function can be well-approximated as follows:

\[ f(x) = f(x^*) + f'(x^*)(x-x^*) \]

We will perform this approximating technique on the logs of our three equations, so that we can simplify the system into expressions of deviations from steady state. First, we will log-linearize equation (17):

\[ \frac{1}{c_t} = \beta E_t \frac{1}{c_{t+1}}(\alpha a_{t+1} k_{t+1}^\alpha + (1-\delta)) \]
\[ -\ln(c_t) = -\ln(c_{t+1}) + \ln(\beta) + \ln(\alpha a_{t+1} k_{t+1}^\alpha + (1-\delta)) \]

Now do the first-order Taylor approximation:

\[ -\ln(c^*)  - \frac{c_t-c^*}{c^*} = -\ln(c^*)  - \frac{c_{t+1}-c^*}{c^*} + \ln(\beta) + \frac{\beta \alpha k^{*(\alpha-1)}(a_{t+1}-a^*)}{a^*} + \frac{\beta(\alpha-1)\alpha k^{*(\alpha-2)}(k_{t+1}-k^*)}{k^*} \]

For notational simplicity, let's say that \(\tilde{x} = \frac{(x_t-x^*)}{x^*} \) to express deviation from steady state. We can simplify further:

\[ -\tilde{c}_t = -\tilde{c}_{t+1} + \left(\beta \alpha k^{*(\alpha-1)}\right) \tilde{a}_{t+1} + \left(\beta(\alpha-1)\alpha k^{*(\alpha-1)}\right)\tilde{k}_{t+1} \]

\begin{equation}
\tilde{c}_{t+1} = \tilde{c}_{t} + \left(\beta \alpha k^{*(\alpha-1)}\right) \tilde{a}_{t+1} + \left(\beta(\alpha-1)\alpha k^{*(\alpha-1)}\right)\tilde{k}_{t+1}
\end{equation}

Now let's repeat the process (take logs, then linearize) for equation (18):

\[ k_{t+1} = a_t k_t^\alpha - c_t + (1-\delta) k_t \]
\[ \ln(k_{t+1}) = \ln(a_t k_t^\alpha - c_t + (1-\delta) k_t) \]

In this next step, I take advantage of the fact that in steady state, \(k^* = a^* k^{*\alpha} - c^* + (1-\delta) k^*\):

\[ \ln(k^*) + \frac{k_t-k^*}{k^*} = \ln(a^* k^{*\alpha} - c^* + (1-\delta) k^*) + \frac{k^{*\alpha(a_t-a^*)}}{k^*} + \frac{\alpha a^* k^{*(\alpha-1)}(k_t-k^*)}{k^*} - \frac{c_t-c^*}{k^*} + \frac{(1-\delta)(k_t-k^*)}{k^*} \]

We can simplify again using our tilde notation:

\[ \tilde{k}_{t+1} = (\alpha a^* k^{*(\alpha-1)})\tilde{k}_t - \left(\frac{c^*}{k^*}\right)\tilde{c}_t + (1-\delta) \tilde{k}_t + k^{*(\alpha-1)}\tilde{a}_t \]

\begin{equation}
\tilde{k}_{t+1} = \left(k^{*(\alpha-1)}\right)\tilde{a}_t - \left(\frac{c^*}{k^*}\right)\tilde{c}_t + \left(\frac{1}{\beta}\right)\tilde{k}_t
\end{equation}

Last, we repeat the process for equation (19), noting that it is already in logs:

\[ \ln(a_t) = \rho \ln(a_{t-1}) + \epsilon_t \]
\[ \ln(a^*) + \frac{a_{t+1}-a^*}{a^*} = \rho\ln(a^*) + \frac{a_{t}-a^*}{a^*} + \epsilon^* + (\epsilon_{t+1} - \epsilon^*) \]

\begin{equation}
\tilde{a}_{t+1} = \rho \tilde{a}_t + \epsilon_{t+1}
\end{equation}

Now we want to use equations (20)-(22) to express our time \(t+1\) variables only in terms of time \(t\) variables and the steady state values (which we can treat as parameters since they are time invariant). For equations (21) and (22), this is already done for us, so we just need to substitute and rearrange equation (20). We do this by plugging in values for \(k_{t+1}\) and \(a_{t+1}\) and simplifying. I will show only the final step (the in-between steps are nothing more than simple algebra):

\begin{equation}
\tilde{c}_{t+1} = \tilde{c}_t \left( 1 - \left(\frac{c^*}{k^*}\right)\beta(\alpha-1)R^* \right) + \tilde{a}_t \left( \rho \beta R^* + \beta(\alpha-1)R^* k^{*(\alpha-1)} \right) + \tilde{k}_t \left( R^*(\alpha-1) \right)
\end{equation}

Now, since our time \(t+1\) variables are exprsesed only in terms of time \(t\) variables, we can write the system as a VAR(1):

\[ E_t \begin{bmatrix}
	\tilde{c}_{t+1} \\
	\tilde{k}_{t+1} \\
	\tilde{a}_{t+1}
 \end{bmatrix}  =
\begin{bmatrix}
	1 - \left(\frac{c^*}{k^*}\right)\beta(\alpha-1)R^* & R^*(\alpha-1) & \rho \beta R^* + \beta(\alpha-1)R^* k^{*(\alpha-1)} \\
	-\frac{c^*}{k^*} & \frac{1}{\beta} & k^{*(\alpha-1)} \\
	0 & 0 & \rho
\end{bmatrix}
\begin{bmatrix}
	\tilde{c}_{t} \\
	\tilde{k}_{t} \\
	\tilde{a}_{t}
 \end{bmatrix} \]

The rest of the solution is best found on a computer. Basically, we will do an eigenvalue decomposition of this matrix of coefficients and then sort the eigenvalues. We will use the sorted eigenvalues to form a policy function, which is an expression for \(c_t\) in terms of \(k_t\) and \(a_t\). We can then shock \(a_t\) (remember, it is a function of its own lag and a white noise process) and watch how the entire system reacts.

\subsection{The Lucan critique; or: why do this the hard way?}

Take a look at the matrix of coefficients above. Notice that every element of the matrix is a function of the model steady state or of the ``deep parameters'' of the model. This is a very important point, and gets to the core of why we chose to solve our model this way.

Let's think about one alternative: an econometric model estimated on past data. Implicit in such an estimated model is the assumption that past action will have some predictive power of future outcomes. The problem comes in determining how people will react to a change in economic policy. Since an econometric model is estimated on a past policy regime, there is no way to know how people's behavioral responses will change in a new policy regime.

This is the core of the Lucas critique: if you want to do predictive analysis on a change in policy, you must use a model estimated on deep and policy-invariant parameters, like people's discount factor, capital depreciation, and consumers' relative risk aversion. These parameters do not depend on any given policy regime, so we can use them to estimate a model that will have some predictive power.

Let's look at how these parameters will be estimated for our simple model. First, we know that \(\alpha\) is capital's share of income, so \(1-\alpha\) will be labor's share of income. We calculate this by finding:

\[ \frac{\textrm{wage income}}{\textrm{all income}} \approx \frac{2}{3} \]

So we set \(\alpha\approx0.33\). Next, we consider the time preference parameter \(\beta\). Note that in steady state, \(\beta=1+r^*\). We know that \(r^* = i^*-\pi^*\), so by taking long-run values of inflation and the nominal interest rate, we can solve for the real inflation rate and the time preference parameter. For the US, this comes to around 0.95.

Finally, we can solve for \(\delta\) by finding the share of investment in total income. Note that:

\[ \frac{I^*}{y^*} = \frac{\delta k^*}{y^*} \]

We can re-arrange to get:

\[ \frac{I^*}{y^*} = \delta k^* k^{* -\alpha} = \delta k^{*(1-\alpha)} = \delta \left( \frac{\alpha}{\frac{1}{\beta}-(1-\delta)} \right) \]

\[ \delta = \frac{\frac{I^*}{k^*}\left(\frac{1}{\beta}-1 \right)}{\alpha - \frac{I^*}{k^*}} \]

We can estimate \(\rho\) via a growth accounting exercise. Note that all these parameters are time-invariant, so our model will be useful for estimating the effects of future policy changes. This is why we will use a DSGE model rather than a large-scale macroeconometric model for policy analysis.

\end{document}


\subsection{The DPLL Procedure}

\begin{frame}
  \frametitle{The DPLL Procedure (Revised)}

  \scriptsize

  \begin{tabbing}
  as \= a \= a \= as \= asasdfsdfasdfasdfasdfasdfasdfasdfasdfasdf \= \kill
  DPLL( $V$, $\claset$ ) \\ \\
  \> if ( ``a clause has all $\bot$ literals'' ) return $unsat$; \> \> \> \>  // Failure \\
  \> if ( ``all clauses has a $\top$ literal'' ) return $sat$; \> \> \> \>  // Model found \\ \\
  \pause
  \> if ( ``a clause has all but one literal $l$ to $\bot$'' )  \\
  \> \> return DPLL( $V \cup \{ l \}$, $\claset$ );   \> \> \>               // BCP \\ \\
  \pause
  \> $l =$ ``pick an unassigned literal'' \> \> \> \>                          // Decision \\ \\
  \pause
  \> if ( DPLL( $V \cup \{ l \}$, $\claset$ ) == $sat$ )        \> \> \> \>  // Left Branch\\
  \> \> return $sat$; \\
  \> else \\
  \> \> return DPLL( $V \cup \{ \neg l \}$, $\claset$ );           \> \> \>  // Backtracking (Rigth Branch)
  \end{tabbing}

  \vfill
  \pause

  To be called as DPLL( $\{\ \}$, $\claset$ )

\end{frame}

\subsection{The Iterative DPLL Procedure}

\begin{frame}
  \frametitle{The Iterative DPLL Procedure (Revised)}

  \scriptsize

  \vfill

  \begin{tabbing}
  as \= a \= a \= a \= as \= asdfsdfasdfasdfasdfasdfasdfasdfasdfasdf \= \kill
  \> $dl = 0$; $flipped = \{\ \}$; $trail = \{\ \}$;    \> \> \> \> \> // Decision level, flipped vars, assignment\\ \\
  \> while ( $true$ ) \\ \\
  \> \> if ( BCP( ) == conflict )                          \> \> \> \> // Do BCP until possible \\
  \> \> \> $done = false$; \\
  \> \> \> do \\
  \> \> \> \> if ( $dl == 0$ ) return $unsat$;                   \> \> // Unresolvable conflict \\
  \> \> \> \> $l = $ {\sc GetDecisionLiteralAt}( $dl$ ); \\
  \> \> \> \> {\sc BacktrackTo}( $dl - 1$ );                         \> \> // Backtracking (shrinks $trail$) \\
  \> \> \> \> if ( var($l$) $\in flipped$ ) \\
  \> \> \> \> \> $dl = dl - 1$; \\
  \> \> \> \> else \\
  \> \> \> \> \> $trail = trail \cup \{ \neg l \}$; \\
  \> \> \> \> \> $flipped = flipped \cup \{ $var($l$)$ \}$; \\
  \> \> \> \> \> $done = true$; \\
  \> \> \> while ( !$done$ ); \\ \\
  \> \> else \\
  \> \> \> if ( ``all variables assigned'' ) return $sat$;    \> \> \> // $trail$ holds satisfying assignment \\
  \> \> \> $l = $ {\sc Decision}( );                                \> \> \> // Do another decision \\
  \> \> \> $trail = trail \cup \{ l \}$ \\
  \> \> \> $dl = dl + 1$;                                     \> \> \> // Increase decision level \\
  \end{tabbing}

  \vfill

\end{frame}

\begin{frame}
  \frametitle{The DPLL Procedure (Revised)}

  Some characteristics

  \begin{itemize}
    \vfill
    \item BCP is called with highest priority, as much as possible
    \vfill
    \item Backtracking is performed {\bf chronologically}: search backtracks to
          the previous decision-level
    \vfill
  \end{itemize}

  \pause
  Some differences w.r.t. ``original'' DPLL Procedure

  \begin{itemize}
    \vfill
    \item $\claset$ is not touched: non-destructive data-structures (memory efficient)
    \vfill
    \item Pure Literal Rule is not used: expensive to implement (!)
  \end{itemize}

\end{frame}


\section{Inference using Variational Bayes} \label{sec:vb}

Given a set of annotations, $\bs c$,
%=\{\bs c^{(1)}, .., \bs c^{(K)} \}$, from $K$ annotators,
we obtain a posterior distribution over
%the parameters,
%$\bs T$, $\bs\theta = \{ \bs\theta^{(1)},..,\bs\theta^{(S)} \}$,
%$\bs A=\{ A^{(1)},..,A^{(K)} \}$ and
%$\bs B = \{ B^{(1)},..,B^{(S)} \}$,
%and thereby compute the posterior over
sequence labels, $\bs t$, using
\emph{variational Bayes} (\emph{VB})~\cite{attias_advances_2000}.
Unlike maximum likelihood methods such as standard expectation maximization (EM),
VB considers prior distributions
and accounts for parameter uncertainty due to
noisy or sparse data.
In comparison to other Bayesian approaches such as Markov chain Monte Carlo (MCMC),
VB is often faster, readily allows incremental learning, and provides easier ways
to determine convergence~\cite{Bishop2006}.
It has been successfully applied to a wide range of methods,
including being used as the standard learning procedure for LDA~\cite{blei2003},
and to combining non-sequential crowdsourced classifications~\cite{simpsonlong}.

The trade-off is that we must approximate the posterior distribution with
%the \emph{mean field} assumption,
an approximation that
factorises between subsets of latent variables:
% so that each subset, $z$, has a \emph{variational factor}, $q(z)$:
%over $\bs t$ and the model parameters,
%$\bs T$, $\bs\theta = \{ \bs\theta^{(1)},..,\bs\theta^{(S)} \}$,
%$\bs A=\{ A^{(1)},..,A^{(K)} \}$ and
%$\bs B = \{ B^{(1)},..,B^{(S)} \}$.
%The labels produced by the sequence taggers, $\bs d$,
%can be marginalized, so do not appear in the approximate posterior, which is given by:
\begin{flalign} \label{eq:vb_posterior}
& p(\bs t, \bs A, \bs T, \bs\rho | \bs c, \bs x, \bs \alpha, \bs\gamma, \bs\kappa ) &   \nonumber\\
&  \approx \prod_{k=1}^K  q(A^{(k)}) \prod_{j=1}^J \left\{
q(\bs T_j) q(\bs \rho_j)\right\} \prod_{n=1}^N q(\bs t_n) .
\end{flalign}
VB performs approximate inference by
%we optimize Equation \ref{eq:vb_posterior}
%using coordinate ascent to
updating each variational factor, $q(z)$, in turn,
optimising the approximate posterior distribution until it converges.
%taking expectations with respect to the current estimates of the other variational factors.
%(see Algorithm \ref{al:vb_bac}).
%Each iteration reduces the KL-divergence between the true and approximate posteriors
%of Equation \ref{eq:vb_posterior}, and hence optimizes a lower bound on the
%log marginal likelihood, also called the evidence lower bound or ELBO
Details of the theory are beyond the scope of this paper, but are
explained by ~\citet{Bishop2006}.
The VB algorithm is described in Algorithm \ref{al:vb_bac},
making use of update equations for the variational factors given below.
% We now provide the variational factors,
% which can be used to approximate the marginal posterior distributions for the parameters and sequence
% labels,
% and explain how to incorporate existing sequence taggers into the algorithm.
%that
%predict only sequence labels rather than full posterior distributions.
\begin{algorithm}
\DontPrintSemicolon
 \KwIn{ Annotations $\bs c$, tokens $\bs x$}
 \nl Compute initial values of $\mathbb{E}\ln A^{(k)},\forall k$,
 $\mathbb{E}\ln \rho_j,\forall j$,
 $\mathbb{E}\ln \bs T_j,\forall j$
 from their prior distributions.\;
 \While{$\mathrm{not\_converged}(r_{n,\tau,j},\forall n,\forall \tau, \forall j)$}
 {
 \nl Update $r_{j,n,\tau}$, $s_{t_{j,n,\tau\!-\!1}, t_{\iota,n,\tau}}$, $\!\forall j,\!\forall \tau,\!\forall n,\!\forall \iota$,
 %given $\bs c$,
 %$\mathbb{E}\left[\ln \bs A \right]$, $\mathbb{E}\left[\ln \bs B \right]$ and $\mathbb{E}\left[\ln \bs T \right]$
 using forward-backward algorithm
 given $\bs x$, $\bs c$, $\mathbb{E}\ln \bs T_j,\forall j$,
 $\mathbb{E}\ln\bs\rho_j,\forall j$,
  $\mathbb{E}\ln\bs A^{(k)},\forall k$.\;%~\cite{ghahramani2001introduction}\;
 %\nl Set current true label predictions $\mathbb{E}\left[t_{n,\tau}=j\right] = r_{j,n,\tau}$ \;
%  \nl Retrain all sequence taggers using $\tilde{\bs d}^{(s)}$ as training labels for
%  each tagger $s$\;
%  \nl Use sequence taggers to predict $\hat{d}^{(s)}_{n,\tau}(i)$, $\forall s,\forall n,\forall \tau,\forall i$\;
 \nl Update $\mathbb{E}\ln A^{(k)},\forall k$,
 given $\bs c$, $r_{j,n,\tau}$.\;
%  \nl Update $\ln q(B^{(s)})$ and $\mathbb{E}\ln B^{(s)},\forall s$ ,
%  given current $\hat{\bs d}$, $r_{j,n,\tau}$\;
 \nl Update $\mathbb{E}\ln T_{j,\iota},\forall j,\forall \iota$,
 given $s_{t_{j,n,\tau\!-\!1}, t_{\iota,n,\tau}}$.\;
 \nl Update $\mathbb{E}\ln \rho_j,\forall j$ given $\bs x$, $r_{j,n,\tau}$.
 }
\KwOut{ Label posteriors, $r_{n,\tau,j},\!\forall n,\!\forall \tau, \!\forall j$, %\mathbb{E}[\bs t]$,
most probable sequence of labels, $\hat{\bs t}_n,\forall n$ using Viterbi algorithm }
\;
\caption{The VB algorithm for BSC.}
\label{al:vb_bac}
\end{algorithm}

We compute the posterior probability of each true token label,
$r_{n,\tau,j}= \mathbb{E}[p(t_{n,\tau}=j | \bs c)]$,  %_{\bs T,\bs A,\bs B,\bs d}
and of each label transition, $s_{n,\tau,j,\iota} =
\mathbb{E}%_{\bs T,\bs A,\bs B,\bs d}
[p(t_{n,\tau\!-\!1}=j, t_{n,\tau}=\iota | \bs c)]$,
%
% terms provide the output predictions of the class labels.
% To compute $q(\bs T_j)$, $q(\bs A^{(k)} )$, and $q(\bs B^{(s)})$,
% we require expectations for the individual
% true labels $r_{n,\tau,j}
using the forward-backward algorithm~\cite{ghahramani2001introduction}.
In the forward pass, we compute:
\begin{flalign}
   & \ln r^{-}_{n,\tau,j} = \ln \sum_{\iota=1}^J \left\{ r^{-}_{n,\tau-1,\iota} \mathrm{e}^{\mathbb{E}\ln T_{\iota,j}} \right\} + ll_{n,\tau}(j), & \nonumber \\
% \end{flalign}
%  where the log likelihood $ll(j,n,\tau)$ of the annotations for token $\tau$ in document $n$ given
%  label $j$ is:
% \begin{flalign}
   & ll_{n,\tau}(j) = \!\sum_{k=1}^K \!\mathbb{E}\!%_{\bs A}
   \ln A^{(k)}\!\!\left(j, c_{n,\tau}^{(k)}, c_{n,\tau\!-\!1}^{(k)} \right)
   \!\mathbb{E}\!\ln\rho_{j,x_{n,\tau}}, &
   %+  \sum_{s=1}^S
%    & \nonumber \\
%    &  \sum_{i=1}^J\sum_{\iota=1}^J \mathbb{E}%_{\bs B}
%    \ln B^{(s)} \!\left(j, i, \iota \right)
%    \hat{d}_{n,\tau}^{(s)}(i) \hat{d}_{n,\tau-1}^{(s)}(\iota), &
 \end{flalign}
 and in the backward pass we compute:
  \begin{flalign}
  %& \ln \lambda_{n,L_n,j} = 0, \hspace{1cm}
   \ln \lambda_{n,\tau,j} & = \ln\sum_{\iota=1}^J \exp \big\{
   \ln \lambda_{i,\tau+1,\iota}
   & \nonumber \\
&  + \mathbb{E}\ln T_{j,\iota} + ll_{n,\tau+1}(\iota) \big\} .&
 \end{flalign}
 Then we can calculate the posteriors as follows:
 \begin{flalign}
 r_{n,\tau,j} &\propto r^{-}_{n,\tau,j}\lambda_{n,\tau,j}, &\\
 s_{n,\tau,j,\iota} &\propto  r^{-}_{n,\tau-1,j} \lambda_{n,\tau,\iota} \exp\{\mathbb{E}\ln T_{j,\iota}+ ll_{n,\tau}(\iota)\}. &
 \end{flalign}

%The variational factor $q(\bs t)$ requires the following expectation:
The expectations of $\ln \bs T$ and $\ln\bs\rho$
can be computed using standard equations for a Dirichlet distribution:
 \begin{flalign}
& \mathbb{E}\ln T_{j,\iota} = \Psi\!\left(N_{j,\iota} \!\!+ \gamma_{j,\iota}\right)
 - \Psi\!\left(\sum_{i=1}^J (N_{j,i} \!\!+ \gamma_{j,i}) \!\right), & \\
 & \mathbb{E} \ln\rho_j = \Psi\!\left(o_{j,w} \!\!+ \kappa_{j,w}\right)
 - \Psi\!\left(\sum_{v=1}^J (o_{j,v} \!\!+ \kappa_{j,v}) \!\right), &
\end{flalign}
 where $\Psi$ is the digamma function,
  $N_{j,\iota} = \sum_{n=1}^N \sum_{\tau=1}^{L_n}  s_{n,\tau,j,\iota}$ is the expected number of times that label $\iota$ follows label $j$,
 and $o_{j,w}$ is the expected number of times that word $w$
 occurs with sequence label $j$.
Similarly, for the \emph{seq} annotator model, the expectation terms are:
 \begin{flalign}
 \label{eq:elna}
 \mathbb{E}\ln A^{(k)}(j,l,m) &= \Psi\!\left(N^{(k)}_{j,l,m}\right)  & \\
& - \Psi\left(\sum_{\;m'\!=1}^J \left( N^{(k)}_{j,l,m'} \right) \right). & \nonumber \\
N^{(k)}_{j,l,m} =  \alpha_{j,l,m}^{(k)} + & \sum_{n=1}^N \sum_{\tau=1}^{L_n}
r_{n,\tau,j} \delta_{l,c^{(k)}_{n,\tau-1}}\delta_{m,c^{(k)}_{n, \tau}}, &
 \end{flalign}
 where $\delta$ is the Kronecker delta.
For other annotator models, this equation is simplified as the values of
the previous labels $c^{(k)}_{n,\tau-1}$ are ignored.
% counts are be simplified for the other annotator models

% For \emph{CM}, the equation can be adapted by omitting the $l$ subscripts on the right-hand side, which refer to the previous annotation in the sequence.
% The variational factor, $q(B^{(s)} )$, for each sequence tagger's annotator model
%  has the same form as $q(A^{(k)} )$, substituting $\delta_{l,c^{(k)}_{n,\tau\!-\!1}}$
%  for $\hat{d}_{n,\tau}^{(s)}(i)$, as defined in below in Equation \ref{eq:hatp}.

% \textbf{Black-box sequence taggers}: the parameters of tagger $s$ have
% %Our inference approach can incorporate either pre-trained sequence taggers, or
% %train the sequence tagger using the crowdsourced data while performing inference over the complete BSC model.
% %In both cases, the tagger's reliability will be modeled by an annotator model, $B^{(s)}$,
% %so it is possible to incorporate noisy sequence taggers into the ensemble.
% %----
% % can be pre-trained, in which case their parameters, $\bs\theta^{(s)}$,
% % are fixed and we do not update the variational factor $q(\bs\theta^{(s)} )$
% % or trained as part of our VB algorithm.
% the following variational factor:
% \begin{flalign}
%  \ln q\Big( & \bs\theta^{(s)}\!\Big) \!=\! %\ln p(\bs\theta^{(s)}) + \mathbb{E}%_{\bs d_n^{(s)}}
% %\left[ \ln p(\bs x | \bs\theta^{(s)}, \bs d_n^{(s)} ) \right] & \nonumber \\
% %& \approx
% \ln p\!\left(\!\bs x | \bs\theta^{(s)}\!\!, \tilde{\bs d}^{(s)} \!\right) \!+\! \ln p\!\left(\!\bs\theta^{(s)}\!\right) \!+\! \mathrm{const}, & \nonumber \\
% % \end{flalign}
% % where $\tilde{\bs d_n}$ is an expectation:
% % %takes the place of training labels
% % %for obtaining a posterior distribution over $\bs \theat^{(s)}$.
% % %The approximation above enables us to train the sequence tagger using its standard training or fitting function:
% % %we compute $\ln q(\bs\theta^{(s)})$ by running the training function of the black-box sequence tagger,
% % %passing in a set of expectations over the labels in place of gold labels:
% % \begin{flalign}
% \tilde{d}_{n,\tau} &=
% \mathbb{E}\left[ p(d_{n,\tau}^{(s)} = i | B^{(s)}, t_{n,\tau} ) \right]
% & \nonumber\\
% & = \sum_{j=1}^J \sum_{\iota=1}^J
% r_{n,\tau,j} \tilde{d}_{n,\tau\!-\!1} \mathbb{E}B^{(s)}(j, i, \iota) .\!
%  \label{eq:tildepd}
% \end{flalign}
% % d can be marginalized analytically so does not need a separate variational factor.
% % This results in the phat terms in the update equations.
% %The term $d_{n,\tau}^{(s)}$ can be marginalized without recourse to its own variational factor.
% %since it
% %is independent of all other variables given $t_{n,\tau}$, $\bs x_n$, $B^{(s)}$,
% %$d_{n,\tau\!-\!1}^{(s)}$
% %and $\bs\theta^{(s)}$.
% The expectations, $\tilde{\bs d}_n^{(s)}$, fill the role of training labels,
% allowing us to use the training function of the black-box sequence taggers
% to update the variational factor, $q\left(\theta^{(s)}\right)$.
% %hence we can marginalize $\bs \theta^{(s)}$
% %by training the sequence labeller using its own training procedure.
% Many black-box sequence taggers, including most neural networks,
% use maximum likelihood (ML) to find optimal point values, $\hat{\bs\theta}^{(s)}$,
% rather than their posterior distribution.
% If we integrate such sequence taggers,
% our complete inference procedure becomes
% a hybrid between VB and ML expectation maximization (EM) (see Bishop~\shortcite{Bishop2006}).
% The sequence tagger may also require training using discrete labels,
% in which case we introduce a further ML step and
% approximate $\tilde{\bs d}_n^{(s)}$
% by the most probable values at each token.
% % instead of Equation \ref{eq:tildepd}:
% %\begin{flalign}
% %& \tilde{d}_{n,\tau}^{(s)} \!= \argmax_{i} \;
% %\mathbb{E}\!\left[ p(d_{n,\tau}^{(s)} \!= i | B^{(s)}\!, t_{n,\tau}) \right]. & \label{eq:discrete1}
% %\end{flalign}
%
% The update equations for other factors require
% expectations of $\bs d_{n}$
% with respect to $\bs\theta^{(s)}$, or their ML approximation:
% %over $\bs d_n^{(s)}$, defined as:
% \begin{flalign}
%  \hat{d}_{n,\tau}^{(s)}(i) & = \mathbb{E}%_{\bs\theta^{(s)}}
% \left[p(d_{n,\tau}^{(s)}=i | \bs x_n, \bs\theta^{(s)}) \right] \nonumber\\
% & \approx p\left(d_{n,\tau}^{(s)}=i | \bs x_n, \hat{\bs\theta}^{(s)}\right) & \label{eq:hatp}
% \end{flalign}
% These values are the predictions obtained from the black-box sequence tagger given tokens $\bs x$.
% % If possible, we obtain this posterior through the prediction function of the sequence tagger.
% % However, some sequence tagger implementations may output only discrete predictions of the following form:
% % \begin{flalign}
% % & \hat{d}_{n,\tau}^{(s)}(i) = \argmax_{i} \;p\left(d_{n,\tau}^{(s)}=i | \bs x_n, \hat{\bs\theta}^{(s)}\right), &
% % \end{flalign}
% % where $\hat{\bs\theta}^{(s)}$ is the value of $\bs\theta^{(s)}$ learned using maximum likelihood or MAP optimization.
% % As in Equation \ref{eq:discrete1}, we can use these discrete predictions in place of probabilities to perform an M-step from maximum likelihood-EM in place of taking  expectations over $\bs d^{(s)}$.
% Therefore, our method requires only training and prediction functions
% to integrate a sequence tagger,
% while its annotator model, $B^{(s)}$, accounts for the sequence tagger's reliability.
% %error rates and
% %provides confidence estimates based on their reliability,
% This means we can treat sequence taggers as black boxes, %and ignore their internal details,
% even if their predictions are noisy or over-confident.
% Pre-trained taggers can also be used, for example, to make use of taggers that were trained on different domains with more annotated data.
%

%TODO: provide the equations in the appendix
\subsection{Most Likely Sequence Labels}

%Two types of output from the BSC inference algorithm are of particular interest: (1)
The approximate posterior probabilities of the true labels, $r_{j,n,\tau}$, provide confidence estimates for the labels. However, it is often useful to  compute
the most probable sequence of labels, $\hat{\bs t}_n$, using the
Viterbi algorithm~\cite{viterbi1967error}.
%To apply the algorithm, we use the converged variational factors to compute
%$\mathbb{E}[\bs T]$ and $\mathbb{E}[A^{(k)}],\forall k$. %$\mathbb{E}[B^{(s)}],\forall s$ and
% $\hat{d}_{n,\tau}^{(s)}(i), \forall s, \forall n, \forall \tau, \forall i$.
The most probable sequence is particularly useful because, unlike $r_{j,n,\tau}$,
the sequence will be consistent with any transition
constraints imposed by the priors on the transition matrix $\bs T$,
such as preventing `O'$\rightarrow$`I' transitions by assigning them zero probability.
% We can also make predictions for unlabeled documents in a similar manner,
% simply omitting the human annotations, $\bs c$, and relying only on the predictions
% of the black-box sequence taggers, $\hat{\bs d}^{(s)}$.

%TODO provide the lower bound in the appendix

% \subsection{Variational Lower Bound}
%
% The VB algorithm optimises the lower bound on model evidence, so it is useful to compute the lower bound
% to check for convergence, or to compare models with different hyperparameters when performing model selection. The lower bound for Bayesian annotator combination is:
% \begin{flalign}
%  &\mathcal{L}  = \mathbb{E}_{q} \left[ \ln p\left(\bs c, \bs t | \bs A, \bs\pi^{(1)},..,\bs\pi^{(K)}\right)
%  - \ln q(\bs t) \right] & \nonumber \\
%  & + \sum_{j=1}^L \bigg\{ \mathbb{E}_{q} \left[ \ln p\left(\bs A_j | \bs\beta_j\right)  -  \ln q(\bs A_j ) \right] & \nonumber\\
%  & + \sum_{l=1}^J \sum_{k=1}^K
%  \mathbb{E}_{q}
%  \left[ \ln p \left( \bs\pi_{j,l}^{(k)} | \bs\alpha_{j,l}^{(k)} \right)
%  - \ln q \left( \bs\pi_{j,l}^{(k)} \right) \right]  \&& nonumer\\
% &  + \mathcal{L}_v \left(\bs\theta_v; \bs V, \bs c^{(b)} \right) \bigg\},&&
% \end{flalign}
% where $\mathcal{L}_v$ is provided by the text classifier implementation and
% is the marginal log-likelihood of the text data $\bs V$, either given the maximum likelihood estimate of parameters $\theta_v$, or, in the case of a Bayesian classifier, an expectation over $\theta$.
% The lower bound computation uses the equations described in the previous section for the variational
% factors, $q(\bs A_j)$ and $q \left( \bs\pi_{j,l}^{(k)} \right)$, and the prior distributions for the parameters,
% and inserts the expectations $\mathbb{E}\left[\ln \bs A_j \right]$ and $\mathbb{E}\left[\ln\bs\pi_{j,l}^{(k)} \right]$. The first term of $\mathcal{L}$ makes use of auxiliary variables from the
% forward-backward algorithm:
% \begin{align}
%  && \mathbb{E}_q \left[ \ln p\left(\bs c, \bs t | \bs A, \bs\pi^{(1)},..,\bs\pi^{(K)}\right)\right] =
%  \nonumber\\
%  && \sum_{i=1}^N \sum_{\tau=1}^{T_i} \sum_{j=1}^L r_{i,\tau,j} \ln r^{-}_{i,\tau,j} &
% \end{align}


\documentclass[11pt, letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{bm}

\newcommand{\e}{\epsilon}
\newcommand{\dl}{\delta}
\newcommand{\dij}{\delta_{ij}}
\newcommand{\1}{\bm{1}}
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\uu}[1]{\underline{\underline{#1}}}
\newcommand{\un}[1]{\underline{#1}}

\title{Lecture 6: Irreducible tensors and the Green's Function for the Laplacian}
\begin{document}
\maketitle
This lecture includes 3 key ideas-
\begin{itemize}
\item Few applications of isotropy of tensors
\item Symmetric irreducible tensors
\item Green's function for the Laplace equation and spherical harmonics
\end{itemize}

\section{Isotropic Tensors}
The moment of inertia tensor is a symmetric second order tensor. For a spheroid, it is given by $\un{I}_{ij} = c_1 \dij + c_2 p_ip_j$ while for an ellipsoid it is given by $\un{I}_{ij} = c_1 p_ip_j + c_2 q_iq_j + c_3 r_ir_j$. It seems that the fore-aft symmetry of the spheroid or ellipsoid rules out the cross terms i.e. since inverting $\un{p}$ to $-\un{p}$ does not change the orientation of the spheroid (or ellipsoid) hence we get the term $p_ip_j$ for spheroid (and do not get terms like $q_i r_j$ for ellipsoid). However, note that the moment of inertia tensor is itself invariant to inversions of the principal directions. Therefore, even in cases when changing the direction of anisotropy \textit{does indeed change} the orientation of the body, the moment of inertia tensor will not contain terms that can reflect this change.

Consider an example- an elliptic cone pointing towards the positive x-axis, will point towards negative x-axis if $\un{p}$ is changed to $-\un{p}$ but still our ansatz for the moment of inertia of the cone will look exactly like the one for the ellipsoid. The ellipsoid is fore-aft symmetric whereas the cone isn't but this difference cannot lead to appreance of cross-terms in the moment of inertia tensor because $\dij$  and $x_ix_j$ (and therefore $I_{ij}$) remain invariant if $\un{p}$ is changed to $-\un{p}$.

On the other hand, integrals like
$$
\int x_ix_jx_k dV
$$

will indeed change if one of the principal directions is reversed. These integrals are like moments of the mass disribution also known as virial integrals.

A second key idea about the moment of inertia tensor is as follows. If a second order tensor has three orthogonal directions where its behaviour is identical, then it must be proportional to the isotropic tensor $\dij$. As an example, consider the integral $\int x_ix_j dV$ taken over a sphere. It is immediately clear that it should be proportional to $\dij$. But the idea is that, this integral will be proportional to $\dij$ even if it is carried over a cube because a cube has three independent identical directions.

\subsection{Example: Particle sedimentation}
Assume that a small sphere (radius $= a$) is sedimenting slowly $(Re=0)$ in a fluid with viscosity $\mu$. What will be the drag force $F$ on the sphere? Stokes law states that this force is given by $F=6\pi \mu a u$. In vector form, for a general solid, this law takes the form
$$
F_i = R_{ij} u_j
$$
where $R_{ij}$ is called the resistance tensor and it depends on the geometry of the body. For a sphere it is ofcourse isotropic but in line with our previous argument, it is isotropic for a cube as well. The scalar pre-factor relevant to a particular orientation can be exctracted from $R_{ij}$ by contracting it with $p_ip_j$ where $\un{p}$ is a unit vector denoting the orientation of the sedimenting cube. But the  isotropic tensor $\dij$ treats every $p_i$ identically and hence the scalar pre-factor will be the same for any orientation. Therefore, the drag on the cube is independent of its orientation.

However, for a spheroid, orientation matters and the pre-factor differs with the orientation.

\subsection{Example: Newton's law of viscosity}
Assume a suspension where the collidal particles are spherical. Then the fourth order viscosity tensor in Newton's law of viscosity involves the integral
$$
\int x_ix_jx_kx_l dV = c(\dl_{ij}\dl_{kl}+\dl_{ik}\dl_{jl}+\dl_{il}\dl_{jk})
$$
There is only one constant and hence only one corresponding viscosity. However for a cube, consider $\dij$ to be constructed through three mutually orthogonal unit vectors $\dij = p_ip_j + q_iq_j + r_ir_j$. Constructing the fourth order isotropic tensor ($\dl_{ij}\dl_{kl}+\dl_{ik}\dl_{jl}+\dl_{il}\dl_{jk}$) using this form of $\dij$ results in two types of terms - single face terms ($q_iq_jq_kq_l$) and two-faced terms ($q_iq_jr_kr_l$). The constant multiplying the single-faced terms is different from the constant for the two-faced terms and hence a suspension of cubic particles would exhibit two distinct viscosities.

\section{Symmetric irreducible tensors}

These are symmetric tensors which vanish upon contracting with $\dij$. A symmetric irreducible tensor can be constructed from a general second order tensor by first symmetrizing it ($B_{ij}\rightarrow\frac{B_{ij}+B_{ji}}{2}$) and then enforcing that $\widetilde{B} =\frac{B_{ij}+B_{ji}}{2} + b \dij$ goes to zero when contracted with $\dij$. This gives us the value of $b$ for which $\widetilde{B}$ becomes irreducible. As an example, $\dij \widetilde{B_{ij}} = \widetilde{B_{ii}} = B_{ii} + 3b = 0 \Rightarrow b= -B_{ii}/3$. Therefore the corresponding symmetric irreducible tensor for $B_{ij}$ is
$$
\widetilde{B_{ij}} = \frac{B_{ij}+B_{ji}}{2} - \frac{B_{kk}}{3}\dij
$$
Similar procedure can also be adopted for higher order tensors.

\subsection{Example: The velocity gradient tensor}
\begin{align*}
\tau_{ij} &= A_{ijkl}e_{kl}\\
 &= \underbrace{c_1(\pd{u_i}{x_j}+\pd{u_j}{x_i})}_{\text{volume \textit{and} shape change}} + \underbrace{c_2\pd{u_k}{x_k}}_{\text{only volume change}}
\end{align*}

The first part contains information about shape change as well as the volumetric change of a fluid parcel. Since the trace of the tensor $\pd{u_i}{x_j}+\pd{u_j}{x_i}$ is not zero, it allows for a volumetric change. This can easily be converted to a symmetric irreducible tensor as shown above to obtain the following expression
\begin{align*}
\tau_{ij} &= c_1\bigg(\pd{u_i}{x_j}+\pd{u_j}{x_i}\bigg) +c_2\pd{u_k}{x_k} \\
&=\underbrace{c_1\bigg(\pd{u_i}{x_j}+\pd{u_j}{x_i} - \frac{2}{3}\pd{u_k}{x_k}\dl_{ij}}_{\text{only shape change}}\bigg) + \underbrace{(c_2+\frac{2 c_1}{3})\pd{u_k}{x_k}\dij}_{\text{only volume change}}
\end{align*}

Now the traceless component associated with $c_1$ only deals with shape changes of a fluid element while that with $c_2+\frac{2 c_1}{3}$ deals with volumetric changes. This leads to the definition of the two types of viscosities - shear viscosity $(c_1)$ and bulk viscosity $(c_2+\frac{2 c_1}{3})$

\section{Green's function for the Laplacian}
The Green's function for an operator is its response to a point source. Hence, Green's function of the Laplacian is the solution to the differential equation
$$
\nabla ^2\phi = \delta(\un{x}-\un{x_0})
$$

Using basic ideas from electrostatics, we can immediately determine the Green'd function for Laplacian. Consider the statement for Gauss' Law
$$
\nabla \cdot \un{E} = \frac{\rho}{\epsilon_0}
$$
But $\un{E}=\nabla\phi$
$$
\Rightarrow \nabla ^2\phi = \frac{\rho}{\epsilon_0}
$$
Here $\rho$ is the charge density. Since we want to find the response due to a point source, we will consider the charge density due to a point charge $q$ placed at $\un{x_0}$. (The scalar delta function has units of length inverse and hence for vectors it will be 'per-unit-volume')
\begin{align*}
&\Rightarrow \nabla ^2\phi = \frac{q \delta(\un{x}-\un{x_0})}{\epsilon_0}\\
&\Rightarrow \nabla ^2 (\frac{\epsilon_0\phi}{q}) = \delta(\un{x}-\un{x_0})
\end{align*}
But we already know the potential due to point charge which is the solution to this equation. Hence,
\begin{align*}
&\Rightarrow \phi = \frac{q}{4\pi\epsilon_0 r}\\
&\Rightarrow \frac{\epsilon_0\phi}{q} = G(\un{x}|\un{x}_0) = \frac{1}{4\pi |\un{x}-\un{x}_0|}
\end{align*}

which is the Green's function for the three dimensional Laplacian. We can also assume a line charge with field $\propto 1/r$ (instead of a point charge) and similarly evaluate the 2D Green's function for Laplacian as $\propto\ln(r)$

\subsection{Green's function for higher order tensors}

Assume that instead of a point charge, we wish to calculate the potential due to a dipole (charges $q$ and $-q$ seperated by a distance $\epsilon$ about the center $\un{x_0}$). Then charge density is given by
$$
\rho = q\delta(\un{x}- (\un{x}_0+\un{p} \frac{\epsilon}{2})) - q\delta(\un{x}-(\un{x}_0-\un{p}\frac{\epsilon}{2}))
$$
Assuming that seperation between charges $\epsilon$ is much less than the distances at which we are observing the potential due to the dipole, we can linearize the above expression. The leading order terms cancel because at leading order $q$ and $-q$ are not seperated at all and the net effect is zero. Hence the forcing term appears at first order and we get a gradient of the delta function. The expression is
$$
\nabla ^2\phi_d = -D p_i\pd{}{x_i}\delta(\un{x}-\un{x}_0)
$$
where D is the dipole strength and $\phi_d$ is the potential due to the dipole. This equation is different from the one for the point charge because we get a gradient and a dot product with $\un{p}$ on the RHS. How can we solve this equation? We know the solution to the equation $\nabla ^2 \phi_m = \delta(\un{x})$ where $\phi_m$ is the potential due to a monopole (point charge). Let's take the gradient of this equation and then dot it with $-D\un{p}$. We get
$$
\frac{\partial ^2}{\partial x_j^2}\underbrace{(-Dp_i\pd{\phi_m}{x_i})}_{\phi_d} = -D p_i\pd{}{x_i}\delta(\un{x}-\un{x}_0)
$$
This equation is identical to the one for dipole which implies that
$$
\phi_d = -Dp_i\pd{\phi_m}{x_i} = -Dp_i\pd{}{x_i}\frac{1}{4\pi r} = \frac{D}{4 \pi \epsilon_0}\frac{p_i x_i}{r^3}
$$
Which gives us the vector solution to the Laplace equation. Similarly we can construct second and higher order tensor solutions. The key idea here is that \textit{the Gradient commutes with the Laplacian}. Therefore, if $\phi$ is a solution to Laplace equation, then so is $\nabla \phi$, $\nabla \nabla \phi$ and so on. Three such solutions are written below
\begin{itemize}
\item Scalar solution: $\frac{1}{r}$
\item Vector solution: $\frac{x_i}{r^3}$
\item Second order tensor: $\frac{\dij}{r^3} - \frac{3x_ix_j}{r^5}$
\end{itemize}

In hindsight then, we can say that since the equation for potential for a dipole involved a vector on the RHS, hence $\phi_d$ must have been proportional to $\frac{x_i}{r^3}$. Since potential is a scalar, this must be contracted with a vector. The only relevant vector here is the orientation vector of the dipole $p_i$ and hence we must have $\phi_d \propto \frac{p_ix_i}{r^3}$. Similarly scalar potential due to quadrupole can be constructed using the second order solution.

All these solutions to the Laplacian are in fact symmetric irreducible tensors. To see this, note that $\nabla ^2 (1/r) = 0$ and that the gradient commutes with the Laplacian. Suppose we contract $\frac{\partial ^3}{\partial x_i \partial x_j \partial x_k} \frac{1}{r}$ with $\dij$, then it immediately results in a Laplacian and hence proves the irreducibility of the tensor solution.

Using these tensor solutions, we can write a general solution to the Laplace equation as
$$
\phi = \frac{a_0}{r} + a^1_i\pd{}{x_i}\frac{x_i}{r^3} + a^2_{ij}\frac{\partial ^2}{\partial x_i \partial x_j}\frac{1}{r}+...
$$

\section{Appendix}

\end{document}


\section{Introduction}
\subsection{Problem Description}
From the group project assignment:
\vspace{0.4cm}

\textit{In a \textbf{slotted random-access network}, \textbf{N couples transmitter-receiver} share the same communication
medium, which consists of \textbf{C separate channels}. Multiple attempts to use the same channel in the
same slot by different transmissions will lead to collision, hence no receiver listening on that
channel will be able to decode the message.
Assume that each of the N transmitters generate packets according to an \textbf{exponential inter-arrival
distribution}, and picks its channel at random on every new transmission. Before sending a packet, it
keeps extracting a value from a \textbf{Bernoullian RV with success probability p} on every slot, until it
achieves success. Then it transmits the packet and starts over. If a collision occurs, then the
transmitter backs off for a random number of slots (see later), and then starts over the whole
Bernoullian experiment.
The number of back-off slots is extracted as $U(1, 2^{x+1})$, where x is the number of collisions
experienced by the packet being transmitted.}
\subsection{Objectives}
The aim of the project report is the \textit{Assessment of the Effectiveness of the Slotted Random-Access Network Protocol} described in the latter paragraph.
\subsection{Performance Indexes}
In order to define a metric of performance of the objective, the following Performance Indexes are defined:
\begin{itemize}
	\item \textbf{Throughput}: let $Tp$ be the Throughput to be measured, $N_{p}$ the number of packets successfully sent to the corresponding receiver, $N_{t}$ the number of time-slot considered in the count of $N_{p}$, $T_{slot}$ the period (in seconds) of a time slot, the Throughput(per slot) can be measured as:
	\begin{align*}
	Tp(slot) = \frac{N_{p}}{N_{t}} \quad [packets/slot]
	\end{align*}
	During all the documentation the latter will be used because more meaningful w.r.t. the scenario that will be defined.
	We can also convert this performance metric in a more standard form, dealing with packets per second:
	\begin{align*}
	Tp = \dfrac{Tp(slot)}{T_{slot}} \quad [packets/s]
	\end{align*}

	\item \textbf{Response Time}: defined as the time that occurs from the first appearance of one packet at the Transmitter up to the reception of the packet at the Receiver.

	%\item \textbf{Mean Number of Packets in the queues}: due to the fact that the this metric is time dependent, will be computed as:
	%\begin{align*}
	%E[N] = \frac{\sum_{i = 0}^{N}N_{i}*(t_{i+1} - t_{i})}{t_{N} - t_{0}} \quad
	%\end{align*}
	%Where $N_{i}$ are the number of packets in the buffer on the interval $[t_{i}, t_{i+1})$.
\end{itemize}


\documentclass[../../main.tex]{subfiles}

\begin{document}

\subsection{Motivation}

%Each report must be created in a separate subfolder. Take the "example" folder as an example of a report (please do not change).
%
%Here it is required to describe the problematic of the problem. Basic premises and results. It is important to indicate a list of references on this topic~\cite{AuthorYear}. All refernces put to the main.bib file.
%
%After writing your report, include the report to main.tex similar to the sample report.

%All changes are made through GitHub\footnote{\url{https://github.com/Intelligent-Systems-Phystech/GeometricDeepLearning}}.

The work investigates the problem of predicting a complex structured target variable. The problem is supposed to be solved by higher-order partial least squares \cite{zhao2012higher}. This model predicts tensor $\underline{\mathbf{Y}}$ from tensor $\underline{\mathbf{X}}$ using projection into the latent space and solving regression problem on latent variables. The aim of computational experiment is to predict gyroscore data using accelerometer data. The experiment is held on a synthetic dataset.

\subsection{Problem statement}

Given a dataset $\mathfrak{D} = (\underline{\mathbf{X}}, \underline{\mathbf{Y}})$, where tensors $\underline{\mathbf{X}} \in \mathbb{R}^{I_1\times \ldots\times I_N}$ and $\underline{\mathbf{Y}} \in \mathbb{R}^{J_1\times \ldots \times J_M}$, $I_1 = J_1$. We also require that $N \geq 3, ~M\geq 3$. Assume that $\underline{\mathbf{X}}$ is decomposed as a sum of rank-$(1, L_2, \ldots, L_N)$ Tucker-blocks, and $\underline{\mathbf{Y}}$ is decomposed as a sum of rank-$(1, K_2, \ldots, K_M)$ Tucker-blocks. This can be written in a following form:
\begin{equation}
\underline{\mathbf{X}} \approx \sum_{r=1}^R\underline{\mathbf{G}}_r\times_1\mathbf{t}_r\times_2\mathbf{P}^{(1)}_r\times_3\ldots \times_N\mathbf{P}_r^{(N - 1)},
\end{equation}

\begin{equation}
\underline{\mathbf{Y}} \approx \sum_{r=1}^R\underline{\mathbf{D}}_r\times_1\mathbf{t}_r\times_2\mathbf{Q}^{(1)}_r\times_3\ldots \times_M\mathbf{Q}_r^{(M - 1)},
\end{equation}
where $R$ is a number of latent vectors, $\mathbf{t}_r \in \mathbf{R}^{I_1}$ is a $r$-th latent vector,
$\mathbf{P}_r^{(n)} \in \mathbb{R}^{I_{n+1}\times L_{n+1}}, ~\mathbf{P}_r^{(n)\top}\mathbf{P}_r^{(n)} = \mathbf{I}, ~\mathbf{Q}_r^{(m)} \in \mathbb{R}^{J_{m+1}\times K_{m + 1}}, ~\mathbf{Q}_r^{(m)\top}\mathbf{Q}_r^{(m)} = \mathbf{I}$, and $\underline{\mathbf{G}} \in \mathbb{R}^{1\times L_1\times \ldots \times L_N}, ~\underline{\mathbf{D}}\in \mathbb{R}^{1\times J_1\times K_1\times \ldots \times K_M}$ are core tensors.

For simplicity, let us define the following notation of Tucker decomposition:
\[
 \sum_{r=1}^R\underline{\mathbf{G}}_r\times_1\mathbf{t}_r\times_2\mathbf{P}^{(1)}_r\times_3\ldots \times_N\mathbf{P}_r^{(N - 1)} = [\underline{\mathbf{G}};\mathbf{t}, \mathbf{P}^{(1)}, \ldots, \mathbf{P}^{(N-1)}].
\]

Let $\underline{\mathbf{C}} \in \mathbb{R}^{I_2\times\ldots\times I_N\times J_2\times\ldots\times J_M}$, where:
\[
c_{i_2, \ldots, i_N, j_1, \ldots, j_M} = \sum_{i_1 = 1}^{I_1}x_{i_1, i_2, \ldots, i_N}y_{j_1, \ldots, j_M}.
\]
The problem is to find matrices $\underline{\mathbf{P}}_r^{(n)}, ~\underline{\mathbf{Q}}_r^{(m)}$ and latent vectors $\mathbf{t}_r$ from the following optimization problem:
\begin{equation}
\label{optim}
\max_{\mathbf{P}^{(n)}, \mathbf{Q}^{(m)}}\|[\underline{\mathbf{C}};\mathbf{P}^{(1)\top},\ldots,\mathbf{P}^{(N-1)\top}, \mathbf{Q}^{(1)\top}, \ldots, \mathbf{Q}^{(M-1)\top}]\|_F^2
\end{equation}
\begin{equation}
\mathrm{s.t.}, \quad \mathbf{P}^{(n)\top}\mathbf{P}^{(n)} = \mathbf{I}, ~\mathbf{Q}^{(m)\top}\mathbf{Q}^{(m)} = \mathbf{I}.
\end{equation}
Based on the found matrices $\underline{\mathbf{P}}^{(n)}, ~\underline{\mathbf{Q}}^{(m)}$ from \eqref{optim}, we find a latent vector $\mathbf{t}$ from the following optimization problem:
\begin{equation}
\mathbf{t} = \arg\min_{\mathbf{t}}\|\underline{\mathbf{X}} - [\underline{\mathbf{G}}; \mathbf{t}, \mathbf{P}^{(1)}, \ldots, \mathbf{P}^{(N - 1)}]\|_F^2.
\end{equation}
The following procedure should be carried $R$ times. The next step will be performed with a residual tensor:
\[
\underline{\mathbf{X}} - [\underline{\mathbf{G}}; \mathbf{t}, \mathbf{P}^{(1)}, \ldots, \mathbf{P}^{(N - 1)}].
\]

\subsection{Problem solution}
The optimization algorithm is described in \cite{zhao2012higher}. On each of $R$ steps it  computes orthogonal Tucker decomposition of $\underline{\mathbf{C}}_r$ and performs SVD decomposition in order to find $\mathbf{t}_r$. The algorithm may stop earlier when frobenius norm of both residuals is less than $\varepsilon$.

Prediction from a new observation $\underline{\mathbf{X}}^\text{test}$ can be written in matricized form:
\begin{equation}
\underline{\mathbf{Y}}_{(1)}^{\text{test}} = \mathbf{X}_{(1)}^\text{test}\mathbf{W}\mathbf{Q}^{*\top},
\end{equation}
where matrices $\mathbf{W}$ and $\mathbf{Q}$ have $R$ columns, which are the following:
\[
\mathbf{w}_r = (\mathbf{P}_r^{(N-1)}\otimes\ldots\otimes \mathbf{P}_r^{(1)})\underline{\mathbf{G}}_{r(1)}^+,
\]
\[
\mathbf{q}_r^* = \underline{\mathbf{D}}_{r(1)}(\mathbf{Q}_r^{(M-1)}\otimes\ldots\otimes\mathbf{Q}_r^{(1)})^\top.
\]

\subsection{Code analysis}

The implementation of HOPLS was taken from GitHub repository\footnote{https://github.com/arthurdehgan/HOPLS}. The computational experiment can be found on GitHub repository\footnote{https://github.com/Konstantin-Iakovlev/MathMethodsOfForecasting}.

\subsection{Experiment on synthetic dataset}

Consider an accelerometer tensor $\underline{\mathbf{X}}\in \mathbb{R}^{P\times T \times C}$, where $P = 10$ is a number of multivariate time series, $T = 40$ is a number of time steps, $C = 10$ is the number of channels. The task is to predict gyroscope data $\underline{\mathbf{Y}} \in \mathbb{R}^{P\times T \times C}$. Tensor $\underline{\mathbf{X}}$ was generated as:
\begin{equation}
x_{p, t, c} = \sin(\omega_{p, t, c} (t / T) + \varphi_{p,t, c}), \quad \omega_{p, t, c} \sim \mathcal{U}(20, 30), \quad \varphi_{p, t, c} \sim \mathcal{U}(0, \pi).
\end{equation}
Tensor $\underline{\mathbf{Y}}$ was generated as:
\begin{equation}
y_{p,t,c} = x_{p,t,c'}w_{c', c} + \varepsilon_{p,t,c},
\end{equation}
where $\mathbf{W} \in \mathbb{R}^{C\times C}, ~w_{c', c}\sim \mathcal{U}(0, 1), ~\varepsilon_{p,t,c}\sim \mathcal{N}(0, 0.01)$.

The task is to predict $\underline{\mathbf{Y}}$ by $\underline{\mathbf{X}}$, using method described above. Define a similarity between real $\underline{\mathbf{Y}}$ and $\underline{\mathbf{Y}}^\text{pred}$ as $Q^2$:
\begin{equation}
Q^2 = 1 - \frac{\|\underline{\mathbf{Y}}^\text{pred} - \underline{\mathbf{Y}}\|_F^2}{\|\underline{\mathbf{Y}}\|_F^2}.
\end{equation}

In order to measure quality of proposed method we performed cross validation. Here we split accelerometer data $\underline{\mathbf{X}}$ by the first dimension on 5 folds. In our model $L_2 = K_2 = 6, ~L_3 = K_3 =1$.

The goal of the computational experiment is to get dependence of validation quality on number of latent vectors $R$. We also compare proposed method with N-PLS. In fact, HOPLS can be simplified to N-PLS if we define all $L_n = 1$ and $K_m = 1$.

\begin{table}
\caption{Dependence of $Q^2$ on $R$ on validation dataset}
\label{table}
\begin{tabular}{c|c|c|c|c|c|c|c|}
Method & $R= 1$ & $R= 4$ & $R = 10$ & $R = 13$ & $R = 16$ & $R = 19$\\ \hline
HOPLS  &0.41 $\pm$ 0.12 & 0.51 $\pm$ 0.19&  0.41 $\pm$ 0.12& 0.04 $\pm$ 0.47 & -0.22 $\pm$ 0.68&  -0.32 $\pm$ 0.68 \\
N-PLS & 0.42 $\pm$ 0.15 & 0.45 $\pm$ 0.11 & 0.35 $\pm$ 0.18 & 0.35 $\pm$ 0.19 & 0.33 $\pm$ 0.20 & 0.30 $\pm$ 0.20
\end{tabular}
\end{table}

From Table \ref{table} it can be seen that HOPLS method gives better solution in terms of $Q^2$. In addition, variance of both methods is comparable.

From Figure \ref{fig:exp_1} it can be seen that the optimal $R^* = 4$. Moreover, the higher $R$ is, the more the variance of $Q^2$ is.
\begin{figure}[h!]
\centering
\includegraphics[width=1\textwidth]{figures/cross_val}
\caption{Dependence of $Q^2$ on number of latent vectors $R$.}
\label{fig:exp_1}
\end{figure}

%\begin{figure}[h!]
%\centering
%\includegraphics[width=0.6\textwidth]{figures/fig1}
%\caption{Some description}
%\label{fig:example:1}
%\end{figure}
%
%This section requires some experiment analysis. The chapter assumes some graphs with their descriptions~\ref{fig:example:1}.

\end{document}


    \filetitle{apct}{Annualised percent rate of change}{tseries/apct}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
X = apct(X)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{X} {[} tseries {]} - Input tseries object.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{X} {[} tseries {]} - Annualised percentage rate of change in
  the input data.
\end{itemize}

\paragraph{Description}\label{description}

\paragraph{Example}\label{example}


\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{geometry}
\usepackage{enumerate}
\usepackage{natbib}
\usepackage{float}%稳定图片位置
\usepackage{graphicx}%画图
\usepackage[english]{babel}
\usepackage{a4wide}
\usepackage{indentfirst}%缩进
\usepackage{enumerate}%加序号
\usepackage{multirow}%合并行

\begin{document}
\newpage
\section{(a)}
\subsection{(i)}
When the nmos is in the saturation region, $I_x=\frac{1}{2}\mu_nC_{ox}(\frac{W}{L_{eff}})_1(V_{a}-V_{TH})^2$
$$R_{out}=\frac{\mathrm{d}V_x}{\mathrm{d}I_x}=r_{01}=\frac{1}{I_x*\lambda}=\frac{1}{\frac{1}{2}\mu_nC_{ox}(\lambda\frac{W}{L_{eff}})_1(V_{a}-V_{TH})^2}$$
\subsection{(ii)}
$$R_{out}=\frac{\mathrm{d}V_x}{\mathrm{d}I_x}=r_{o1}+r_{o2}+gm_2r_{o1}r_{o2}$$
Assume the voltage between the two nmos is $V_D$, then
$$R_{out}=\frac{1}{I_{d1}\lambda_1}+\frac{1}{I_{d2}\lambda_2}+\frac{2I_x}{I_{d1}I_{d2}\lambda_1\lambda_2(V_b-V_D-V_{TH})}$$
$$I_x=\frac{1}{2}\mu_nC_{ox}(\frac{W}{L_{eff}})_1(V_{a}-V_{TH})^2(1+\lambda V_D)=\frac{1}{2}\mu_nC_{ox}(\frac{W}{L_{eff}})_2(V_{b}-V_{D}-V_{TH})^2(1+\lambda (V_X-V_D))$$
$$I_{d1}=\frac{1}{2}\mu_nC_{ox}(\frac{W}{L_{eff}})_1(V_{a}-V_{TH})^2$$
$$I_{d2}=\frac{1}{2}\mu_nC_{ox}(\frac{W}{L_{eff}})_2(V_{b}-V_{TH}-V_D)^2$$
\section{(b)}
\subsection{(i)}
$$R_{out}=\frac{1}{0.5*0.035*\frac{3.9*8.85*10^{-12}}{9*10^{-9}}*\frac{20}{2-2*0.08}*(1.2-0.7)^2*0.1}=54833\Omega$$
\subsection{(ii)}
$$20*(1.2-0.7)^2*(1+0.1*V_D)=100*(2.2-0.7-V_D)^2*(1+0.1*(2-V_D))$$
$$V_D=1.271V$$
$$I_{d1}=0.5*0.035*\frac{3.9*8.85*10^{-12}}{9*10^{-9}}*\frac{20}{2-2*0.08}*(1.2-0.7)^2=1.824\times10^{-4}A$$
$$I_{d2}=0.5*0.035*\frac{3.9*8.85*10^{-12}}{9*10^{-9}}*\frac{100}{2-2*0.08}*(2.2-0.7-1.271)^2=1.913\times10^{-4}A$$
$$I_x=I_{d1}*(1+0.1*1.271)=2.06\times10^{-4}A$$
$$R_{out}=\frac{1}{0.1\times1.824\times10^{-4}}+\frac{1}{0.1\times1.913\times10^{-4}}+\frac{2\times2.06\times10^{-4}}{0.01\times1.824\times1.913\times10^{-8}\times(2.2-1.27-0.7)}$$
$$R_{out}=5240688\Omega想·$$
\section{(c)}
\subsection{(i)}
$V_X>V_A-V_{TH}=1.2-0.7=0.5V$
\subsection{(ii)}
Assume both of the nmos are in saturation region, then $V_D>0.5V$, $V>1.5V$
$$I_x=\frac{1}{2}\mu_nC_{ox}(\frac{W}{L_{eff}})_1(V_{a}-V_{TH})^2(1+\lambda V_D)=\frac{1}{2}\mu_nC_{ox}(\frac{W}{L_{eff}})_2(V_{b}-V_{D}-V_{TH})^2(1+\lambda (V_X-V_D))$$
$$20*0.5^2*(1+0.1V_D)=100*(1.5-V_D)^2(1+0.1*(V-V_D))$$
When $V_D=0.5V$, $V_x=-9.375V$
\\ When $V_x=1.5V$, $V_D=1.265V$
\\ So the minimum voltage for $V_x$ is $1.5V$
\section{(d)}
\subsection{(i)}
\begin{figure}[H]
\centering
\includegraphics[scale=0.25]{P1.png}
\end{figure}
The voltage for saturation is about 5V and the output impedance is $\frac{1}{18.246\times10^{-6}}=54806\Omega$, which is very close to my calculated value.
\subsection{(ii)}
\begin{figure}[H]
\centering
\includegraphics[scale=0.25]{P2.png}
\end{figure}
The voltage for saturation is about $1.5V$ and the output impedance is $\frac{1}{1.024\times10^{-6}}=976562\Omega$, which is very much smaller than my calculated value.
\end{document}


 \input{"/$dstroot"/econtexPaths.tex} \documentclass{\econtex}\usepackage{\econtexSetup}\usepackage{\econtexShortcuts}\input{"/$dstroot"/Switches.tex}\provideboolean{StandAlone}\setboolean{StandAlone}{true}\setboolean{BigAndWide}{true}   \begin{document}\bibliographystyle{\econtexBibStyle} \setboolean{DocVersion}{false}  \newcommand{\texname}{Muth}\hfill{\tiny \today} \large
  \section{Formulae Derived from \cite{muthOptimal}}\label{appendix:Muth}

  \begin{verbatimwrite}{"/$dstroot"/LaTeX/appendices/MuthPre-muthOptimal.texinput}
  \cite{muthOptimal}, pp.\ 303--304,  shows that the signal-extracted estimate of permanent income is
  \begin{eqnarray}
  \perc{\PLev}_{t} & = & v_{1}Y_{t}+v_{2}Y_{t-1}+v_{3}Y_{t-2}+...
  \end{eqnarray}
  for a sequence of $v$'s given by
  \begin{eqnarray}
    v_{k} & = & (1-\lambda_{1})\lambda_{1}^{k-1}
  \end{eqnarray}
  for $k = 1,2,3,...$.  So:
  \begin{eqnarray}
    \perc{\PLev}_{t} & = & (1-\lambda_{1})(\phantom{\YLev_{t+1}+\lambda_{1}^{0}}\YLev_{t}+\lambda_{1} \YLev_{t-1} + \lambda_{1}^{2} \YLev_{t-2} ...)
\\    \perc{\PLev}_{t+1} & = & (1-\lambda_{1})(\YLev_{t+1}+\lambda_{1} \YLev_{t} + \lambda_{1}^{2} \YLev_{t-1}+ \lambda_{1}^{3} \YLev_{t-2} ...)
\\     & = & (1-\lambda_{1})\phantom{(}\YLev_{t+1}+\lambda_{1} \underbrace{(1-\lambda_{1})(\YLev_{t} + \lambda_{1}^{2} \YLev_{t-1}+ \lambda_{1}^{3} \YLev_{t-2} ...)}_{\perc{\PLev}_{t}}
\\    & = & (1-\lambda_{1})\YLev_{t+1}+\lambda_{1} \perc{\PLev}_{t}
  \end{eqnarray}

This compares with \eqref{eq:PischkeP} in the main text
  \begin{eqnarray}
    \perc{\PLev}_{t+1} & = & \Pi \YLev_{t+1} + (1-\Pi) \perc{\PLev}_{t}
  \end{eqnarray}
so the relationship between our $\Pi$ and Muth's $\lambda_{1}$ is:
  \begin{eqnarray}
    \lambda_{1} & = & 1-\Pi
  \end{eqnarray}

  Defining the signal-to-noise ratio $\varphi = \sigma_{\pmb{\psi}}/\sigma_{\pmb{\theta}}$, starting with equation (3.10) in \cite{muthOptimal} we have
  \begin{eqnarray}
\notag    \lambda_{1} & = & 1 + (1/2) \varphi^{2} - \varphi \sqrt{1+\varphi^{2}/4}
\notag \\ (1-\Pi) & = & 1 + (1/2) \varphi^{2} - \varphi \sqrt{1+\varphi^{2}/4}
                        \notag\\ -\Pi & = & (1/2) \varphi^{2} - \varphi \sqrt{1+\varphi^{2}/4}
  \end{eqnarray}
  yielding equation \eqref{eq:muthOptimal} in the main text.
\end{verbatimwrite}
\input{"/$dstroot"/LaTeX/appendices/MuthPre-muthOptimal.texinput}
  \begin{verbatimwrite}{/Volumes/Data/Papers/cAndCwithStickyE/cAndCwithStickyE-AEJM/Equations/muthOptimal.tex}
  \begin{eqnarray}
\Pi & = & \varphi \sqrt{1+\varphi^{2}/4} - (1/2) \varphi^{2} \label{eq:muthOptimal}
  \end{eqnarray}
\end{verbatimwrite}
\input{/Volumes/Data/Papers/cAndCwithStickyE/cAndCwithStickyE-AEJM/Equations/muthOptimal.tex}

\bibliography{economics,\texname,\texname-Add}

\end{document}


\documentclass[12pt]{article}
\author{David Alves}

\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{dirtytalk}
\usepackage[a4paper, total={6.5in, 8.5in}]{geometry}
\usepackage{forest}
\usepackage{skak}
\usepackage{tikz}
\usepackage{titling}
\usepackage{wrapfig}

\def\multichoose#1#2{\ensuremath{\left(\kern-.3em\left(\genfrac{}{}{0pt}{}{#1}{#2}\right)\kern-.3em\right)}}

\title{Math 142 Problem Set 5}
\author{David Alves}
\date{2016-09-27}

\begin{document}
\pagenumbering{gobble}

\begin{center}
\Large \thetitle \\
\large \theauthor \\
\thedate
\end{center}

\subsection*{Sources}

    \begin{itemize}
    \item http://tex.stackexchange.com and https://www.sharelatex.com for help with \LaTeX
    \end{itemize}

\section{Edges in a Complete Graph }

\subsection*{Problem Statement}
Consider a complete graph on 6 vertices. This means that every pair of vertices has exactly one edge between them. How many edges are there in the graph? What if the graph has $n$ vertices?
\subsection*{Solution}

Consider a complete undirected graph $G$ with $n$ vertices. Each edge connects two distinct vertices from the vertex set, and since it is an undirected graph the order of those vertices does not matter. Therefore there is a bijection between the set of edges in $G$ and the set of subsets of length two on a in the vertex set. Therefore there are $\binom{n}{2}$ edges on a complete graph with $n$ vertices. For $n=6$, there are 15 such edges.

\section{Triangles in a Grid}
\subsection*{Problem Statement}
How many triangles can be made with vertices from a $4\times4$ square grid of points? A $2\times2$ square grid would allow 4 triangles. Triangles must use three distinct points and the points cannot be colinear.
\subsection*{Solution}
516 valid triangles can be formed from a grid of $4 \times 4$ points.
\begin{proof}

In a $4 \times 4$ grid there are 16 points. A valid triangle is formed by three distinct noncolinear points. There are $\binom{16}{3} = 560$ ways to choose the three distinct points for a triangle. We know that 560 is equal to the set of valid triangles plus the set of triangles which contain three colinear points by the sum principle. Therefore we can subtract the number of colinear triangles from 560 to determine the number of valid triangles.

    The number of colinear triangles is equal to the number of colinear triangles on rows plus the number of colinear triangles on columns plus the number of colinear triangles on NW-SE diagonals plus the number of colinear triangles on NE-SW diagonals.
    Each of the four rows contains $\binom{4}{3} = 4$ ways of choosing three colinear points, giving a total of 16 colinear triangles in the rows. By symmetry, there are also 16 colinear triangles in columns. The NE-SW long diagonals also has $\binom{4}{3} = 4$ ways of choosing three colinear points. There are two length-3 NE-SW diagonals, shown here:

\begin{center}
\begin{tikzpicture}
\foreach \x in {0,1,2,3}
    \foreach \y in {0,1,2,3}
    {
    \fill (\x,\y) circle (0.1cm);
    }
\draw[rounded corners=8pt,rotate around={45:(0,1)}] (-.3,.7) rectangle (3.242,1.3);
\draw[rounded corners=8pt,rotate around={45:(1,0)}] (.7,-.3) rectangle (4.242,.3);
\end{tikzpicture}
\end{center}

Each of the length-3 diagonals has $\binom{3}{3} = 1$ way of choosing points for a colinear triangle. This gives $4+1+1 = 6$ colinear triangles on NE-SW diagonals, and therefore 6 on the NW-SE diagonal by symmetry. Thus we have 560 - 16 - 16 - 6 - 6 = 516 noncolinear triangles on a $4 \times 4$ grid of points.
\end{proof}

\section{Stirling Numbers of the Second Kind}
\subsection*{Problem Statement}
Prove that
\[
    S(n, m) = \sum_{i=m}^{n}m^{n-i}S(i-1, m-1)
\]
\subsection*{Solution 1}
\begin{proof}
Consider $n$ distinguishable balls being placed into $k$ indistinguishable bins such that each bin contains at least one ball. Without loss of generality, number the balls $b_1, b_2, \ldots, b_n$. Let $b_i$ be the first ball such that $b_1, b_2, \ldots, b_i$ puts at least one ball into each bin. $k \leq i \leq n$ because we need at least $k$ balls to place one in each of $k$ bins. Thus we know that $b_1, b_2, \ldots, b_{i-1}$ require $k-1$ bins. $b_i$ can only be placed into one bin (the empty one). All balls $b_{i+1}, b_{i+2}, \ldots, b_n$ can be placed into any bin since all bins already contain at least one ball.

The number of ways to place $n$ distinguishable balls into $k$ indistinguishable bins is defined as $S(n,k)$. Thus we can sum over all possible choices of $i$ from $k$ to $n$. For each value of $i$, there are $S(i-1, k-1)$ ways to place the balls before $b_i$ into $k-1$ bins, and $k^{n-i}$ ways to place the balls that come after $b_i$ since they can go into any bin. Thus $S(n,k) = \sum_{i=k}^{n}k^{n-1}S(i-1, k-1)$.
\end{proof}

\subsection*{Solution 2}
\begin{proof}
    The Stirling number of the second kind $S(n,k)$ is defined as the number of ways to divide $n$ items into $k$ groups. Without loss of generality, consider a single item. If that item is in a group by itself, then there are $S(n-1, k-1)$ to partition the remaining items. If that item is not in a group by itself, there are $S(n-1, k)$ ways to partition the remaining items, and the item can go into any of the existing $k$ groups. This leads to the following recurrence relation:

\[
S(n,k) = S(n-1, k-1) + kS(n-1,k)
\]

This recurrence relation can be applied recursively to the $kS(n-1,k)$ term, giving

\[
S(n,k) = S(n-1, k-1) + k\Big(   S(n-2, k-1) + k\big(   S(n-3, k-1) + k(\ldots )   \big)   \Big)
\]

Multiplying through gives

\begin{multline*}
    S(n,k) = S(n-1, k-1) + kS(n-2, k-1) + k^2S(n-3, k-1) + \ldots + k^{n-k}S(k-1,k-1)
\end{multline*}

which can be written as:

\[
\sum_{i=k}^{n}k^{n-1}S(i-1, k-1)
\]
\end{proof}

\section{Squares and Cubes}

\subsection*{Problem Statement}
Show that for all positive integers $n$,

\[
    \big(1 + 2 + \ldots + n\big)^2 = 1^3 + 2^3 + \ldots + n^3
\]

\subsection*{Solution}

\begin{proof}
    We prove this by induction on the statement $p(k) = \big(1 + 2 + \ldots + k\big)^2 = 1^3 + 2^3 + \ldots + k^3$. For $k = 1$, $p(1)$ gives $1^2 = 1^3$, which is true. For $k > 1$, we show that if $p(k-1)$ is true, then $p(k)$ is true. First note that the sum of integers $1 + 2 + \ldots + x = \frac{x(x+1)}{2} $. Thus $p(k-1)$ can be written as

    \begin{equation}\label{eq:squares1}
    \left(\frac{(k-1)k}{2}\right)^2 = 1^3 + 2^3 + \ldots + (k-1)^3
    \end{equation}

    and $p(k)$ can be written as
    \begin{equation}\label{eq:squares2}
    \left(\frac{k(k+1)}{2}\right)^2 = 1^3 + 2^3 + \ldots + k^3
    \end{equation}

    Subtracting equation \ref{eq:squares1} from equation \ref{eq:squares2} gives

    \begin{equation}\label{eq:squares3}
    \frac{k^2(k+1)^2 - k^2(k-1)^2}{4} = k^3
    \end{equation}

    Which can be further simplified as follows:

    \begin{equation}
    \frac{(k^2+2k+1) - (k^2-2k+1)}{4} = k
    \end{equation}

    or $\frac{4k}{4} = k$. Thus we have shown that if $p(k-1)$ is true, then $p(k)$ is true, which completes the inductive proof.

\end{proof}

\section{Multisets}
\subsection*{Problem Statement}
Define a \emph{multiset} to be a set that allows multiple copies of any element, knows how many copies of each element are present, but does not know the order. (Example:  $\{2,3,2\} = \{2,2,3\}$, but $\{2,2,3\} \neq \{2,3\}$). What is the number of multisets of size 3 using elements from $[5]$? What about in general (size $m$ using elements from $[n]$?)
\subsection*{Solution}

There are $\binom{7}{5} = 21$ multisets on $[5]$ with 3 elements. More generally, there are $\binom{m+n-1}{n}$ multisets on $[n]$ with $m$ elements.
\begin{proof}
    There is a bijection between multisets on $[n]$ with $m$ elements and lists of length $m+n-1$ where all elements are either 0 or 1 and exactly $n$ elements are 1. A multiset can be represented as a list of $m$ \say{stars} (0s) and $n-1$ \say{bars} (1s) such that the number of stars before the first bar is the the number of 1s in the multiset, the number of stars before the second bar is the number of 2s in the multiset, etc. For example, the multiset $\{1,3,3\}$ on $[5]$ would be represented as $0,1,1,0,0,1,1$. Thus there are $\binom{m+n-1}{n}$ multisets on $[n]$ with $m$ elements.
\end{proof}

\section{Time Spent}

I spent about six hours on this problem set. I got stuck for a while on how to intuitively understand the Stirling numbers recurrence. Overall this wasn't a very difficult problem set, but it was useful practice.
\end{document}