\section{Access paths}
Access paths can be seen as a general concept for indexes on relations: the DBMS needs several data structures, mainly for space management and retrieval.

A common problem in database implementation consists in where to store incoming data, especially since not all tuples have the same size. A traditional solution for that is the free space bitmap, in which each nibble (an array of half a byte per page) indicates the fill status for each page.

This means that information on space utilization has to be encoded in 4 bits, approximating the status with the linear formula $data\ size / \frac{page\ size}{2^{bits}}$, which however leads to accuracy loss.

Logarithmic scale is often better ($\lceil log_2(free\ size)\rceil$), or interpolation, or a combination of methods since it is unlikely that each page only has a couple of tuples.

When inserting the data, the required FSI entry is computed and FSI gets scanned for a match, then data is inserted. Searching leads to linear runtime, which can be too expensive, especially since the size is so small. This method therefore gets slower and slower, as FSI gets longer.

\subsection{Allocation}
Performing allocation benefits from application knowledge: larger pieces are often inserted with short amount of time in between, or there can be one single huge item.

Allocation should be contiguous, and to achieve this the interface $allocate(min, max)$ contains size parameters to improve layout, help deciding location and reduce fragmentation. Tuples are more difficult to store, and can cause over-allocation. Unfortunately, most programming languages do not support such options.

Taking a vector and continuously increasing its size, for example, will at some point lead to some misalignment if memory hasn't been allocated before: in this case, pointers can be employed to know next location, but MALLOC cannot give bounds on the overhead.

\subsection{Slotted Pages}
Slotted pages are useful to allocate pages a fixed size on a segment, using slots supporting combinations of page number and slot number to retrieve information.

It can happen that a record overflows: in that case, it is moved to a new page, and a pointer gets stored in the previous slot. This forward mechanism enables to keep a structured storage while not having to perform cascading updates.

When a record which has already been moved overflows again, in fact, there is no need to create a chain: the existing link can just be replaced, only forming a chain in case of an index. This system, however, relies on the hope that most tuples will not be moved, instead they will be never updated.

Slotted pages are implemented by storing tuples in a page in which data grows from one side and slots from the other. Each page will have a header and a new slot getting added as new data items get added, until it is full when two sides meet.

In reality, data items have variable size, and it is unknown how many slots will be there. Updates and deletions can be complicated as well, since they might change the size: in this case, some data needs to be moved with periodic compactification to optimize space consumption.

Header parameters are:
\begin{itemize}
	\item LSN, flag to check whether there have been crashes;
	\item slotCount, number of used slots;
	\item firstFreeSlot, pointer to the first usable slot;
	\item dataStart, lower end of data, which should not be updated;
	\item freeSpace, available space after compactification, which can be performed when this number is bigger than the effective free space.
\end{itemize}
The effective free space can be calculated with the following formula:
$$EFS = pageSize - headerSize - slots \cdot slotSize - (pageSize - dataStart)$$

Scanning slots to find a free one can take quadratic runtime, therefore pointers to free slots are employed; however, performance of slotted pages is still not ideal.

Conceptually, each slot can be assigned an offset and length of the respective data item, even if empty tuples must be distinguished from deleted one through offset length.

The main issue with this method is the lack of available space in the case a transaction to shrink a page aborts and the following to add data commits, making it necessary to store a further bit (flag) in the TID which would be stored inside the slot: if the TID is valid, the entry is redirected.

\subsection{Record layout}
A record layout has the purpose of materialize and serialize the tuples, trying to avoid the linear worst-case runtime of accessing an attribute.

Instead of offset, the length is stored:
\begin{enumerate}
	\item Each tuple is split in two parts;
	\item The header has fixed size, while tail is variable;
	\item Header contains pointers to the tail (beginning can just be computed);
	\item Attributes are accessed in constant time.
\end{enumerate}
For even better performance, attributes can be reordered by decreasing alignment, i. e. the number of bits of its type. Sorting works since alignment is always a power of two, and it helps identifying the data type.

Variable length-data, which modern processors easily handle, is placed at the end and given alignment 1 by default, without wasting space on padding.

NULL values can either be stored as invalid or identified with an additional bit, which allows to omit actual information to save more space. This is useful when NULL values are common.

\subsection{Compression}
Some DBMS store data in a compressed way, with the aim of saving space and most importantly improve performance: reducing size reduces bandwidth consumption as well, decreasing I/O costs.

General compression schemes work better as the data gets larger (for instance LZ77), but every DBMS should be able to update information without decompressing and recompressing everything. Page size is also a problem, since it can vary.

Compressing large chunks is therefore not an option, so it is performed tuple by tuple. Huffman encodings are a possibility (building decision trees to take advantage of similarities in data) except for the problem of not knowing the tree to decompress, which is usually not serialized to avoid the additional overhead.

To remedy this, adapted Huffman encoding is an algorithm in order to reconstruct trees by changing encoding to make frequent letters cheaper, so that trees will only depend on frequencies.

Performance of such technique is quite bad, and might not balance the time saved from I/O. A reasonable compromise is byte-wise encoding through VLE, a bit worse in terms of size but faster in the long run and able to handle null values.

Compressed data, however, has variable length, and sometimes depend on precedent compression. Lookup tables help precomputing and locating quickly.

Dictionary encoding is a particular method used for strings, since data tends to be redundant: it stores strings in a dictionary, and only the ID is contained in the tuple, greatly reducing the total size.

\subsection{Long records}
Long records consist in a long-term solution in case the previously mentioned methods cannot be used: the root of the idea are pages which do not fit into a single page (BLOB, for instance).

These could be stored with a pointer to the attribute to a whole page, or eventually a long chain of pointers, but this implementation is problematic since it implies reading a potentially unbounded amount of pages (troublesome for buffering or locking). Furthermore, updates involve multiple allocations or releases in the system.

Instead, tuples are stored separately from BLOB objects, and not parsed until a query is actually performed; sometimes IDs are assigned to BLOBs, hiding their complexity.

In most systems, it is not easily possible to perform many operations on such data types, for instance looking at the content, and costs are generally high even though processing is simple.

When objects are in the order of MBs, data is again mapped to pages, storing information in a B-tree like fashion. Each node contains a part of the BLOB and a key representing a range (offset) for searching. Relative offsets allow logarithmic worst-case runtime when updating, the only downside is the overly complicated interface.

Using an extent list is simpler, having a chain of real tuples pointing to BLOB tuples with unlikely worst-case scenarios. BLOBS can be manipulated as a whole piece, but are harder to update in pieces.

Postgres employs TOAST, a mechanism to shorten long tuples.

Other approaches encode short BLOBs in the TID, and uses the full procedure only for actually large objects. The ones smaller than the page size are stored in records.

\subsection{Index structures}
Index structures come in handy when a small part of all the available tuples need to be fetched (either point or range queries), or when making sure primary keys exist.

\subsection{B-trees}
B-trees are the most popular data structure used for external storage: every tree has a degree $k$ and each nodes has $k \leq n \leq 2k$ entries. All leaves must have the same depth, which can be bounded with the logarithm of the size (usually 3 or 4).

Given they are particularly easier to implement and generally faster than red-black trees, B-trees are widely used in their variant of B+-trees, which are even simpler and store TIDs only in leaves. Inner nodes contain separators which may or may not occur in the data.

Each node contains:
\begin{itemize}
	\item LSN for recovery;
	\item Upper node or leaf node marker, the latter being a value which cannot appear in the former;
	\item Number of entries;
	\item Key/TID/child;
	\item Next leaf node (only for leaves, a flag).
\end{itemize}

% todo insert lookup delete

\subsection{B+-trees}
\subsubsection{Insertion}
Insertion is performed in the following way:
\begin{itemize}
	\item The leaf page is looked up;
	\item If there is available space, the entry is inserted;
\end{itemize}

Bulk insertions can be optimized, since the tree needs to be rebuilt several times and there is a lot of random I/O. Each database system implements it in its own way (Postgres for instance loads a whole file through a COPY instruction), trying to improve locality.

Sorting is an option since the order is not random, but it triggers the worst-case scenario of space consumption: all the pages will be half-full, since the rightmost one is split.

Since a database is usually aware of bulk loading, this can be optimized further:
\begin{itemize}
	\item Data is sorted;
	\item Data is spooled into leaf pages, filling them completely and storing the largest value in each;
	\item Separators are spooled into inner pages, again remembering inner separators;
	\item This is repeated until one page is left (the root).
\end{itemize}
This method provides a compact B+-tree, performing only sequential I/O with all full pages except for the rightmost and improving locality on disk.

Inserting in an existing tree requires a level of merging to be employed in the above procedure, beginning with merging all data and then starting a new chunk once a page would contain only entries from the original tree and then merging separators.

Under-full pages can therefore be accepted to avoid moving all data already contained in old pages but obviously not guaranteeing adjacent pages anymore.

\subsubsection{Deletion}
Deletion is the hardest operation to perform in B+-trees, since it may violate the strong criterion of having at least half the pages half full.

In case the removal of an entry violates this property, logarithmic cost is not guaranteed anymore, therefore the tree needs to be rebalanced: this happens through merging pages with their neighboring ones, updating separators.

Finding neighbors, however, can be a difficult task: the succeeding value can be located far away in the structure, hence a simpler solution is to just choose another child of the parent, achieving constant time.

Many systems do not implement a proper deletion logic, since it is so complicated, and just accept pages under-full, eventually deleting them completely. In the long run, however, if the value of newly-inserted entries is much bigger than the deleted ones, free space may be never used.

\subsubsection{Range scans}
Range scan is the operation reading all entries within a range. This is efficient thanks to pointers to next element, therefore only the first lookup needs to be performed.

This works particularly well when leaf nodes are stored consequently on disk, or cached (clusters).

\subsection{Compound keys}
Compound keys are compared lexicographically according to the following rule:
$$(a_1, a_2) < (b_1, b_2) \leftrightarrow (a_1 < b_1) \lor (a_1 = b_1 \land a_2 < b_2)$$
Other than this, they are quite similar to atomic keys. Search can be performed by imposing a prefix of the compound and using anything else as a range, however the other way around can be difficult to implement.

\subsection{Non-unique values}
Users may decide to apply an index on columns containing many duplicates, which need to be retrieved for updates and deletions.

The solution in this case is to only index unique values, appending a TID to the non-key attributes working as a tie-breaker. Space complexity grows sightly, but logarithmic access is still guaranteed.

\subsection{Concurrent access}
Concurrent access cannot be handled through simple page locking or latching, since this method only works for leaf nodes; inner pages locking might cause missed values.

The classical technique is lock coupling, working in the following way:
\begin{itemize}
	\item A thread locks both the page and its parent;
	\item The parent is released as a new lock is acquired on the child, and so on;
\end{itemize}
This prevents conflicts and deadlocks, as latches are ordered and pages can only be split when the parent is latched.

Concurrent insertions might cause the split to propagate up, until the root, making lock coupling not an option anymore since the parent may need to be locked, causing deadlocks.

Another solution is safe inner pages: while going down, a check if the inner page has enough space is performed, and if not the page is split. This method ensures going up of never more than a step.

An alternative to this is just restarting the process, releasing all latches only if the inner node must be split, and keeping all latches up to the root in the new execution. It is not ideal since it involves less concurrency, yet happens extremely rarely.

% todo blink

\subsection{Partitioned B-trees}
Bulk insertions in trees work well if they are not too frequent, but usually require taking the index offline and impacting optimization.

Partitioned B-trees are created by adding an artificial column (partitioning key) causing all new values to be larger than the existing ones. Partitions are largely independent and can be efficiently merged, but lookups need to access all partitions and deletions are complicated to implement.

\subsection{Variable length records}
In reality, entries can have variable length, making it harder to check whether a page is half full. Having keys of different length (or alternating) can produce multiple tree structures and depths, depending on the separator.

This emphasizes on the importance of choosing the right separator, a non-trivial issue to achieve logarithmic height. The main idea behind algorithms is to pick the smallest value within 20\% around the median whenever a page overflows.

% todo prexix btree

\subsection{Hashing}
Hash tables are a data structure used for single, infrequent lookups, providing amortized expected constant runtime through hashing keys either with chaining or open addressing.

To map keys to their hashed values, a prime module function is used. Calculations can be sped up with the following methods:
\begin{itemize}
	\item Making the modulo operation cheap by precomputing some magic numbers to then perform a sequence of multiplications and shifting;
	\item Making sure that $N$ is a power of two;
	\item Converting 32 bits to 64, multiplying by the hash table size and shifting again.
\end{itemize}
DBMS benefit from this implementing hash tables as indexes, but downsides are many, such as random I/O and expensive rehashing. In practice, hash tables are mainly used for primary keys, since the number of collisions should be less due to uniqueness of values. Duplicate values can be handled by reindexing them, or just with a tree.

In real life it is hard to predict the table size beforehand, and rehashings are to be avoided, due to their expensiveness: extendible hashing is a technique in which pointers lead to buckets (pages), even the same one, obtaining variable depth.

When a bucket overflows, it is split, increasing the depth and adding a bit. Pointers must be un-shared and items are redistributed. Exponential growth, however, should stop at some point, falling back to chaining.

Database Systems on Modern CPU Architectures/lectures/access.tex


In this section we present our causal estimand, identifying assumptions, estimation strategy, and inferential procedure.

\subsection{Estimand}

Our goal is to estimate the average effect 2014 Medicaid expansion would have had on the non-elderly adult uninsurance rate in states that did not expand Medicaid. Let $A$ indicate treatment assignment, $c$ index a CPUMA, $s$ index the state, and $t$ index the time period. Let $n_1$ be the number of treated CPUMAs, $n_0$ be the number of control CPUMAs, and $n$ be the total number of CPUMAs. Similarly, let and $m = m_1 + m_0$ states (with $m_1$ and $m_0$ defined analogously). Each state has $p_s$ CPUMAs. $A_s=0$ indicates untreated states and $A_s=1$ indicates treated states, when this notation is a superscript if it indicates the potential outcome. Since we are only interested in the counterfactual at time $T = 2014$, we simplify notation by removing this variable and the subscript and write our formal estimand as:

\begin{equation}
\psi = \psi^1 - \psi^0 &= n_0^{-1}\sum_{s, c: A_s = 0} Y_{sc}^{A_s = 1} - Y_{sc}^{A_s = 0}
\end{equation}

The challenge is that we do not observe the counterfactual outcomes for non-expansion CPUMAs had they been in states that expanded their Medicaid programs. We therefore require causal assumptions to tie this counterfactual quantity to our observed data.\footnote{The 2014 Medicaid expansion occurred simultaneously with the implementation of several other major ACA provisions, including (but not limited to) the creation of the ACA-marketplace exchanges, the individual mandate, health insurance subsidies, and community-rating and guaranteed issue of insurance plans (\cite{courtemanche2017early}). Almost all states broadly implemented these reforms beginning January 2014. Conceptually we think of the other ACA components as a state-level treatment ($R$) separate from Medicaid expansion ($A$). Therefore, our total estimated effect may also include interactions between these policy changes; however, we do not attempt to separately identify these effects. Because the ACA implementation and Medicaid expansion may vary over time, we do not try to generalize these results beyond 2014.}

\subsection{Identification}

The following causal assumptions are necessary (though insufficient) to identify our target parameter from our observed data: the stable unit treatment value assumption (SUTVA), no unmeasured confounding, and no anticipatory treatment effects. We explain these assumptions in detail and their consequences below. We additionally invoke several parametric assumptions to help us identify our causal parameter given the measurement error in our covariates. These assumptions in total are sufficient to identify our causal estimand.

SUTVA has two implications: first, that there is only one version of treatment; second, that $Y_{sc}^{\mathbf{a}} = Y_{sc}^{\mathbf{a}'}$ when $a_{sc} = a_{sc}'$, where $\mathbf{a}$ is the vector of all treatment assignments. We discussed potential violations of the first assumption previously when considering how to reduce Medicaid Expansion to a binary treatment classification. Our solution is to remove states with less restrictive Medicaid eligibility requirements prior to 2014 to approximately satisfy this condition. The second part of this assumption implies that the potential outcomes in each region do not depend on another region's treatment assignment. This is a standard assumption, but is often not realistic in practice. Violations are likely in our setting: for example, \cite{frean2017premium} find evidence that Medicaid expansion drove previously eligible but uninsured individuals to enroll in Medicaid in both expansion and non-expansion states. Signing the potential bias from this violation requires redefining the causal estimand: for example, we might consider the treatment effect on the untreated given that all states have expanded Medicaid, where the contrast is against where only the observed expansion states expanded Medicaid. If the spillover effects were equal in each region, and the magnitude of the spillovers increase with the total number of treated regions, then the true effect would be larger in absolute magnitude than the estimated effect using the observed data. We could consider other estimands or assumptions to get different predictions about the sign of the bias; however, this is beyond the scope of this paper.

We next assume that there were no anticipatory treatment effects. Letting treatment occur at time $T$, we have that for $t < T$:

\begin{align*}
Y_{sct} = Y_{sct}^0
\end{align*}

This assumption is necessary because we will condition on pre-treatment outcomes. If these outcomes were affected by the treatment before it were implemented, these covariates would be endogenous. Anticipatory treatment effects may occur if plans to expand Medicaid induce uninsured but Medicaid-eligible individuals to enroll in Medicaid prior to expansion. We do not think these violations occurred in large enough numbers to substantially affect our results. Instead, we address a more concerning version of this violation: the fact that several states allowed certain counties to expand Medicaid prior to 2014. We test the sensitivity of our results to the exclusion of these states.

Third, we assume no unmeasured confounding; that is, that at time $T$ the potential outcomes for each CPUMA are independent of the state-level treatment assignment conditional on the population-level CPUMA and state-level covariates $X_{sc}$, a $q$ dimensional vector of covariates (which includes pre-treatment outcomes):

\begin{align*}
Y_{sc}^a \perp A_{sc} \mid X_{sc}
\end{align*}

While unverifiable, we believe it is reasonable here given our rich covariate set. To be explicit, we believe that the pair of potential uninsurance rates for each CPUMA are independent of the treatment assignment conditional on the percentage of uninsured individuals in each year of the pre-treatment period, the percentage of unemployed individuals in each year of the pre-treatment period, the average population growth, the average ratio of households to non-elderly adult population, the state's political composition, the average proportion of households with one, two, or three or more children during the pre-treatment period, the average proportion of households who did not respond about their number children, and the average proportion of individuals during the pre-treatment period with given demographics noted above (age group, sex, white, Hispanic ethnicity, U.S. citizenship, foreign born, income-to-poverty group (including non-response), disability status, urban residence, and educational attainment group).

A key problem we address in this paper is the violation of this assumption due to measurement error in our covariates. Let
$X = (X_0, X_1)^T$ be the $n$ by $q$ matrix of true covariates (and separating the control from treated units using $X_a$). Because our covariates are estimated using the ACS data, rather than $(Y, X)$, we instead observe $(J,W)$, which consist of estimates of the true outcome and covariate values, where $W$ is structured analogously to $X$. Importantly, $Y_{sc}^a \perp A_{sc} \mid X_{sc} \centernot\implies J_{sc}^a \perp A_{sc} \mid W_{sc}$. The use of these proxies may therefore bias our estimates. We rely on several modeling assumptions to correct for this.

We first model our observed data as functions of the true values plus mean-zero Gaussian noise: $J_{sc} = Y_{sc} + \xi_{sc}$ and $W_{sc} = X_{sc} + v_{sc}$, where we assume $\xi_{sc}$ and $v_{sc}$ are independent though not identically distributed.\footnote{Our covariates are almost all ratio estimates, which will in general be biased. This bias, however, decreases quickly with the sample size (is $O(n^{-1})$). Given that our CPUMA sample sizes are all over 300, we treat these estimates as unbiased in our analysis.} We assume that these errors are uncorrelated with the true values, i.e. $\mathbb{E}\{\xi_{sc} \mid Y_{sc}\} = 0$ (and similarly for all elements of $X$). Second, we assume that $\xi_{sc}$ are uncorrelated with the errors in the covariate measurements. These assumptions and our model for the observed data are reasonable given that the measurement error in this context is sampling variability. Moreover, our outcomes are measured on a different cross-section than our covariates, so it is reasonable to assume that they are uncorrelated with the measurement errors in the covariates.

We next assume that the true potential outcomes are linear in the true covariates $X_{sc}$. Specifically, we assume that the following model generates the potential non-elderly adult uninsurance rate under treatment $A = a$:

\begin{equation}\label{eqn:outcomemodel}
Y_{sc}^a = \alpha_a + X_{sc}^T\beta_a + \epsilon_{sc} + c_s
\end{equation}

We assume that the errors $\epsilon_{sc}$ and $c_s$ are mean-zero, independent from each other and across time (i.e., we rule out serial-correlation), and are uncorrelated with the true covariates and the treatment assignment, i.e. $\mathbb{E}\{\epsilon_{sc}c_s \mid X_{sc}, A_s\} = \mathbb{E}\{\epsilon_{sc} \mid X_{sc}, A_s\} = \mathbb{E}\{c_s \mid X_{sc}, A_s\} = 0$. We can then identify $\psi^a$ in terms of our model parameters (see Appendix A); specifically, we have that $\psi^a = \alpha_a + \bar{X}_0^T\beta_a$, where $\bar{X}_0$ is the vector of mean covariate values among the control units. Moreover, we can substitute $J_{sc}$ for $Y_{sc}$ in Equation~\ref{eqn:outcomemodel}, and add $\xi_{sc}$ to the error term without affecting identification.

We still have the problem that we observe $W_{sc}$ instead of $X_{sc}$. Let $\eta_a = \mathbb{E}\{X_{sc} \mid W_{sc}, A_s = a\}$. By linearity, we know that

\begin{equation}
    J_{sc} = \eta_a(W_{sc})^T\beta + (X_{sc} - \eta_a(W_{sc}))^T\beta + \xi_{sc} + \epsilon_{sc} + c_s
\end{equation}

If we knew $\eta_a$, we could estimate this model using the observed data $(J, W)$. The approach we follow here is known as ``regression calibration'' in the measurement-error literature. In particular, we assume a linear model for $\eta_a$:

\begin{align*}
\eta_a(W_{sc}) = \upsilon_a + \kappa_a^T(W_{sc} - \upsilon_a)
\end{align*}

where $\kappa_a = (\Sigma_{XX \mid A = a} + \Sigma_{vv \mid A = a})^{-1}\Sigma_{XX \mid A = a}$. The assumption motivating this model is that $(X_{sc}, v_{sc}) \stackrel{iid}\sim MVN((\upsilon_a, 0), \Sigma_a)$ and $\Sigma_a$ is a $2q$ by $2q$ block-diagonal matrix consisting of $q$ by $q$ matrices $\Sigma_{XX \mid A = a}$ and $\Sigma_{vv \mid A = a}$ and $0$ in the off-diagonals. Given sufficient auxillary data to estimate $\kappa_a$, we can then estimate $\psi$. We discuss this further below and in Appendices A and B (see also \cite{gleser1992importance}).

\subsection{Estimation}

We outline our estimation strategy first emphasizing how estimating the ETC differs from estimating the ETT with respect to variable selection under the ``synthetic controls'' framework and associated assumptions required. Second, we explain our estimation procedure, which modifies the SBW criterion to address the hierarchical data structure. This objective, which we call H-SBW, reduces the variance of our estimator under our assumption of constant variance and constant within-state correlation of model errors. Third, we connect our estimator to the regression calibration literature by generating weights that balance a linear prediction of the true covariates $\hat{\eta}_a(W_{sc})$ using the observed covariates $W_{sc}$. Fourth, we test the sensitivity of our estimator to a regression-augmented version, using ridge-regression weights following the suggestion of \cite{ben2018augmented}; this allows us to achieve better covariate balance by extrapolating beyond the support of the data. We conclude by proposing a model validation procedure that uses pre-treatment outcomes to compare the performance of our estimators on pre-treatment data.

\subsubsection{Variable selection}

We seek to generate a set of positive weights that balance the means of covariates for the treated units to the mean covariates for the control units. Assume that we observe the true covariate matrices for the treated data $X_a = (X_{a,1}, ..., X_{a, q})$, and the true outcomes $Y$. Let $\bar{X}_{0, r}$ be the mean covariate value for the $r$-th covariate in the non-expansion region. Ideally, there exists some $\gamma^\star \in \Gamma$ satisfying:

\begin{equation}\label{eqn:constraint}
\Gamma = \{\gamma \in \mathbb{R}^{n_1}: &\lvert \gamma^TX_{1, r} - \bar{X}_{0, r} \lvert \le \delta_r \ \ (r = 1, ..., q), \ \gamma_{sc} > 0, \sum_{s, c: A_{sc} = 1}\gamma_{sc} = 1\}
\end{equation}

for $\delta_r = 0$ for all $r = 1, ..., q$. We could then estimate $\psi$ as

\begin{equation}\label{eqn:psi}
\hat{\psi} = \sum_{s: A_s = 1}^{m_1}\sum_{c = 1}^{p_s}\gamma_{sc}^\star Y_{sc} - n_0^{-1}\sum_{s: A_s = 0}^{m_0}\sum_{c = 1}^{p_s}Y_{sc}
\end{equation}

Again assuming that the potential outcomes are a linear function of the true covariates: $\mu_a(X_{sc}) = \alpha_a + X_{sc}^T\beta_a$, the bias of our estimate of $\psi^1$ (again assuming we observed $X_{sc}$), is less than or equal to $\lvert\beta_1\rvert^T\delta = 0$ (see, e.g., \cite{zubizarreta2015stable}). The challenge is that for any given dataset we have no guarantee that any such $\gamma^\star$ exists that exactly balances the covariates. We therefore often require some method of determining which parts of the covariate distribution we wish to prioritize balancing - or which covariates to balance at all - to minimize this bias.

The synthetic controls approach chooses the $\gamma$ that minimizes the weighted L2-squared distance of the covariates using a diagonal weighting matrix $V$. $V$ is then chosen to minimize the mean-square error of the weighted difference in pre-treatment outcomes. Letting $Z_a$ be the matrix of pre-treatment outcomes for treatment group $A = a$, the synthetic controls algorithm solves the following optimization problem for a fixed $V$:

\begin{align}
\gamma(V) = \arg\min_{\tilde{\gamma}(V^\star)} = (\bar{X}_1 - X_0^T\tilde{\gamma})'V(\bar{X}_1 - X_0^T\tilde{\gamma})
\end{align}

This is the ``inner'' optimization. $V^\star$ is then determined in an ``outer'' optimization to minimize the imbalances in the pre-treatment outcomes $Z$:

\begin{align}
    V^\star = \arg\min_V (\bar{Z}_1 - Z_0^T\gamma(V))'(\bar{Z}_1 - Z_0^T\gamma(V))
\end{align}

In applications the covariate matrix $X_a$ may contain some elements of $Z_a$. In cases where $X_a$ contains all pre-treatment outcomes, \cite{kaul2015synthetic} has shown that the predictor weights $V^\star$ will give no weight to auxillary covariates (covariates that are not the pre-treatment outcomes).

While in practice $V$ is often learned on the same data as the weights, we consider the case where we use cross-validation to choose $V$, as proposed by \cite{abadie2015comparative}. Assume we can divide our pre-treatment data into a training data from periods $T = 1, ..., T - l - 1$, a validation period from periods $T - l, ..., T - 1$, and a post-treatment period at time $T$. To make this discussion more general, assume that we are evaluating a set of candidate models $\mathcal{M}$ on the validation data (where for the synthetic controls algorithm we can think of this as the set of all possible weighting matrices $V$). Let $\bar{Y}^a_{a', t}$ be the mean potential outcome under treatment $A = a$ for treatment group $A = a'$ at time $t$ (where $t$ occurs during the validation period). Let $\hat{\bar{Y}}^a_{a'', t}(m)$ be an estimator of that potential outcome at time $t$ using model $m$, which was trained during the training period using data from treatment group $A = a''$. Finally, let $\bar{Y}_{a'}^a_T$ be the post-treatment estimand, where $\hat{Y}^a_{a'', T}(m)$ is the estimator using model $m$ trained using validation period data. This learning procedure implicitly assumes that:

\begin{align*}
m^\star = \min_{m \in \mathcal{M}}\sum_{T - l}^{T-1}\|\hat{Y}^0_{0, t}(m) - \bar{Y}^0_{1, t}\| = \min_{m \in \mathcal{M}}\mathbb{E}\{\|\hat{Y}^0_{0, T}(m) - \bar{Y}^0_{1, T}\|\}
\end{align*}

In other words, we select our model using the empirical loss in the validation period as a proxy for the expected loss in the post-treatment time-period.\footnote{It is possible that multiple models in $\mathcal{M}$ either perfectly predict the pre-treatment outcomes, or predict them equally well. In this case we would require an additional criteria to choose the optimal model (see, e.g, \cite{becker2017cross}}. This makes intuitive sense in the typical synthetic controls setting where the estimand is the ETT since we observe $Y^0_{sct}$ for $t < T$. When synthetic controls are used to estimate the ETC, this strategy alone is insufficient as we never observe $Y^1_{sct}$ (or a mean-unbiased proxy) prior to treatment for any unit. We therefore cannot easily use pre-treatment outcomes to optimally select variables or determine relative covariate importance without stronger assumptions.

One such assumption is the following:

\begin{align*}\label{assumption:second}
m^\star = \min_{m \in \mathcal{M}}\sum_{T - l}^{T-1}\|\hat{Y}^0_{1, t}(m) - \hat{Y}^0_{0, t}\| = \min_{m \in \mathcal{M}}\mathbb{E}\{\|\hat{Y}^1_{1, T}(m) - \bar{Y}^1_{0, T}\|\}
\end{align*}

We call this assumption ``counterfactual risk invariance.'' In other words, we assume that the model that minimizes the validation-period risk also minimizes the post-treatment risk. This is a very strong assumption for conducting any form of variable selection or covariate weighting in this setting. As a simple example, assume that we can partition $X = (R, S)$, where, for simplicity, we assume $S$ is univariate. Further assume that $Y^0_t \perp A \mid R$ for all $t = 1, ..., T$ but that $Y^1_T \perp A \mid X$. Again assume that $\mu_a$ are linear in the covariates with (time-invariant) coefficients $\beta_{a, r}$ ($r = 1, ..., q$). These assumptions imply that $\beta_{0, s} = 0$. If our goal is to predict the ETT, then we wish to use the untreated data to predict $\bar{Y}_{1, T}^0$. We may then conduct some variable selection procedure using our pre-treatment data, learn that covariate $S$ is unimportant, and estimate a model that downweights imabalances in $S$ (or ignores the covariate entirely) but perfectly balances the remaining covariates. This model would give an unbiased estimate of the counterfactual outcome for the treated group absent treatment in time-period $T$. However, if our goal were instead to predict $\bar{Y}^1_{0, T}$ -- the counterfactual outcome under treatment for the untreated units -- the same procedure applied to the treated data would again downweight $S$ and result in a biased estimate, with bias equal to $(\gamma^TS_1 - \bar{S}_0) \beta_{1, s}$. If $S$ is a strong predictor of treatment assignment, this could lead to substantial bias. \footnote{We conflate two issues in this discussion: the synthetic controls variable weighting algorithm and variable selection more generally. Provided $S$ is contained in $X$ and exact balancing weights exist with high probability as $n \to \infty$, then under our modeling assumptions, the synthetic controls procedure is still consistent for $\psi^1$ even if the variable weights $V$ are sub-optimal. However, if $S$ is not contained in $X$ at all, then this procedure will not balance these covariates even asymptotically provided that $\mathbb{E}\{S \mid A = 1\} \ne \mathbb{E}\{S \mid A = 0\}$. However, here we take a finite-sample perspective. Imbalances in $S$ will lead to bias, whether or not $S$ is imbalanced due to receiving low weights on the weighting matrix $V$, or omitted from the objective entirely. Moreover, these two conditions are in the case where the optimal weight on covariate $S$, $V_s = 0$.}

As a practical example, we highlight the potential confounding role of Republican governance for our counterfactual estimate. Republican governance is a strong predictor of a state's decision to expand Medicaid \cite{courtemanche2017early}. Moreover, existing evidence prior to Medicaid expansion showed that Medicaid take-up rates were lower in more conservative states \cite{sommers2012understanding}. Yet when generating their synthetic control weights to estimate the ETT, \cite{courtemanche2017early} and \cite{kaestner2017effects} do not control for these factors. \footnote{\cite{courtemanche2017early} does control for Republican governor in their regression model and they find that it is a statistically significant predictor of 2013 uninsurance rates. One reason they may not control for this in the synthetic control model is practical: it is much harder to balance this covariate using control data without extrapolating from the data.} However, it is clear that if take-up rates depend on governance, we may expect this to be a strong confounder of $Y^1$ and hence confound the ETC, even if arguably it is not a confounder of $Y^0$ (and hence not a confounder for the ETT).

We demonstrate this in our application by conducting a variable importance analysis. Specifically, we remove the balance constraints from the Republican governance indicators and examine how our estimates of $\hat{\psi}^1$ change. Letting $\hat{\psi}^1_s$ be the estimate when removing the Republican governance indicators (or more generally, the covariate matrix $S$ where $X = (R, S)$). We subtract our original point estimate $\hat{\psi}^1_0$ from $\hat{\psi}^1_s$ to generate the difference $\hat{\Delta}^1$. This difference tells us about the direction of the bias our estimate of $\hat{\psi}^1$ would incur when we do attempt to constrain the imbalance in covariate $S$. Our hypothesis implies that we should expect $\hat{\Delta}_s^1 < 0$: that is, keeping all other covariates (roughly) fixed, we expect the predicted uninsurance rate will decrease when as the level of Republican governance decreases. In addition to the Republican governance indicators, we also examine four other covariate groups: pre-treatment uninsurance rates and pre-treatment unemployment rates, and three sets of different demographic indicators, which we detail in Appendix E.\footnote{We caution that our results do not imply that Republican governance is not an important confounder of $Y^0_{1, T}$ since we do not analyze this directly.}

Overall we emphasize that predicting the outcome under treatment is different, and perhaps more challenging, than predicting the outcome absent treatment. The former requires understanding which covariates matter most to predicting treatment response, which we cannot as naturally learn from pre-treatment outcomes. We instead rely on our prior knowledge and modeling assumptions to choose which covariates to balance and which covariates to prioritize in this balancing. We point out that modeling the ETC requires greater justification of the covariates used to predict treatment response than for the ETT, and that using the standard synthetic controls variable weighting procedure is unlikely to be optimal for this purpose.\footnote{Our analysis assumes no unmeasured confounding and a linear model for $\mu_a$. By contrast, synthetic controls are frequently motivated by a linear factor model for $\mu_0$. \cite{abadie2010synthetic} and \cite{ferman2016revisiting} outline conditions where this method is consistent as the number of pre-treatment outcomes goes to infinity, in particular because the method balances the unobserved factor loadings. Analogous to our analysis, if we assume $\mu_{a, T}$ both follow a linear factor model, identification of the ETC requires that the factors that confound $Y^1_T$ are the same that confound $Y^0_t$. Under this assumption, we might be able to show that the synthetic control estimator is consistent in this setting. However, the tuning procedure to determine the predictor weights may again be sub-optimal from a finite-sample bias perspective, depending again on how the covariates (or unobserved factors) that are most predictive of treatment response vary between treatment groups and on their associations with the potential outcomes.}

Given these challenges, we therefore use a variation of SBW to estimate the ETC.\footnote{Specifically, we use a modified implementation of Noah Griefer's ``optweight'' package in R, available on github.com/mrubinst757} SBW minimizes the variance of the weights subject to user-specified balance constraints. The primary advantages of H-SBW over synthetic controls in this setting are that it gives the user finer control over the desired levels of covariate balance, allowing the user to navigate a bias-variance tradeoff with respect to balance and the variability of the weights. Specifically, SBW weights solve the following minimization:

\begin{equation}
\gamma &= \arg\min_{\tilde{\gamma} \in \Gamma} \quad \sum_{s: A_s = 1}^{m_1}\sum_{c = 1}^{p_s} \tilde{\gamma}_{sc}^2
\end{equation}

where $\Gamma$ is defined in Equation~\ref{eqn:constraint}. We can then estimate $\psi$ using Equation~\ref{eqn:psi}, substituting $J_{sc}$ for $Y_{sc}$ and plugging in the weights $\gamma$. By contrast, the synthetic controls algorithm will minimize the weighted L2 distance between the treated and control units in the criterion; this algorithm in general may lead to lower imbalances, but the balance tradeoffs are difficult to control, and the resulting weights may be more extreme. Finally, the algorithm, as formulated in \cite{abadie2010synthetic} and presented above, may not have a unique solution (but see \cite{ben2018augmented}, \cite{becker2017cross}).

For our primary estimates we lean heavily on assumptions to justify our choice of $\delta$. We use a priori domain knowledge about which covariates are most likely to be important predictors of treatment response when setting $\delta$, but also choose $\delta$ to avoid generating overly extreme weights. For our application, we constrain $\delta$ to be 0.05 percentage points (out of 100) for pre-treatment outcomes, 0.15 percentage points for pre-treatment unemployment rates, and 25 percentage points for the Republican governance indicators. We believe these covariates are most likely to predict treatment response. While we believe that Republican governance is an important covariate to balance, we are unable to reduce the constraints further given the support of the data. For the remaining covariates, we let $\delta$ be 0.5 percentage points for average population growth and household to adult ratio, 1 percentage point for female, Hispanic ethnicity, white race, age category, disability, and number of children category; 2 percentage points for urban, citizenship, education category, income-to-poverty category, student, and foreign-born, again choosing these constraints with respect to both feasibility and extreme weight concerns.

\subsubsection{H-SBW objective}

The motivation of the SBW criterion is to produce the minimum variance weights for a fixed $\delta$. This produces the minimum variance estimator within the constraint set if, for example, the errors in the outcome model are independent and identically distributed \cite{zubizarreta2015stable}. In our setting we allow for possible state-level dependencies, potentially reducing the efficiency of the SBW estimator. To address this possibility, we add the tuning parameter $\rho \in [0, 1)$ in the objective below. Assuming a constant variance across units for each error component, $\rho$ represents a constant (and known) within-state correlation of the errors.

\begin{equation}\label{eqn:objective}
\gamma &= \arg\min_{\tilde{\gamma} \in \Gamma} \quad \sum_{s: A_s = 1}^{m_1}(\sum_{c = 1}^{p_s} \tilde{\gamma}_{sc}^2 + \sum_{c \ne d}\rho \tilde{\gamma}_{sc}\tilde{\gamma}_{sd})\\
\end{equation}

For $\delta \to \infty$, this objective yields the solution:

\begin{equation}\label{eqn:sbwsol}
\gamma_{sc} \propto \frac{1}{(p_s - 1)\rho + 1}
\end{equation}

Setting $\rho = 0$ returns the SBW solution: $\gamma_{sc} \propto 1$. By contrast, when setting $\rho \approx 1$, we see that $\gamma_{sc} \propto \frac{1}{p_s}$. In other words, as we increase $\rho$, this objective downweight CPUMAs in states with large numbers of CPUMAs and upweight CPUMAs in states with small numbers of CPUMAs (all while assigning each CPUMA within a state equal weight). Moreover, when $p_s$ is constant across states this objective will also the SBW solution. In short, as we increase $\rho$, the objective will attempt to more uniformly disperse weights across states.

As a brief illustration, we simulate $N = 800$ observations in $m = 40$ regions each with $p_s = 20$ units, and draw $X_{sc} \sim N(\mu_s, 1)$, for $\mu \in \{0, 1\}$ (drawn from a Bernoulli with equal probability); $A_s \sim Bern(expit(\bar{X}_s))$, where $\bar{X}_s$ is the mean covariate value in group $s$. We then generate weights to balance the control to the treated group mean. We run H-SBW variants setting $\rho = 0$ (which is equivalent to SBW), $\rho = 0.5$, and $\rho = 0.99$ all while keeping $\delta$ fixed at zero. Figure 1 shows the weights summed to the group level for all control regions. The color of each set of bars is the overall variance of the weights. We can see that the weights in general are uniform across units for SBW (the variance is lower); however, the weights are not uniformly dispersed across regions (the sum of weights within each region is not even). As we increase $\rho$, the weights disperse more evenly across regions. Despite the increase in the variance of the weights, these weights are optimal under our assumed covariance structure.

\begin{figure}
\begin{center}
    \includegraphics[scale=0.5]{01_Plots/proofofconcept.png}
    \caption{Comparison of SBW and H-SBW: within group sum of weights}
    \label{oatepref}
\end{center}
\end{figure}

The particular covariance structure we assume is identical to the one proposed by \cite{kloek1981ols}. In Appendix A, we show that this objective produces the minimum variance estimator under the constraint set for this correlation structure. We note that theoretically we could incorporate any other assumed covariance structure into this objective, though the number of tuning parameters might change. Broadly speaking, we can think of H-SBW being to SBW what generalized least squares (GLS) is to ordinary least squares (OLS): both SBW and OLS can produce unbiased estimates of model parameters; however, H-SBW and GLS can improve the efficiency of our estimates under different assumed correlation structures of the outcome errors.

\subsubsection{Measurement error}

A second advancement in our estimation procedure comes in our balance constraints: rather than balancing on the observed covariate values $W_{sc}$, we instead balance on the imputed covariate estimates $\hat{\eta}_1(W_{sc})$ (we refer to these as the ``adjusted covariates''). This procedure attempts to correct for the estimation error in these CPUMA-level covariates that may bias our estimate of $\psi^1$. In Appendix A, we consider the super-population target $\psi^{1, sp} = \mathbb{E}\{Y^1 \mid A = 0\}$ and show that under the classical errors-in-variables model, the bias for the SBW estimator that balances on the observed covariates $W$ and sets $\delta = 0$ is equivalent to the bias of a linear combination of coefficient estimates from the OLS-based regression estimator. Specifically, the bias for either estimator is:

\begin{equation}
\mathbb{E}\{\hat{\psi}^{1} - \psi^{1, sp}\} = (\upsilon_0 - \upsilon_1)^T(\kappa - I_d)\beta_1
\end{equation}

The intuition for this result is as follows: exact balancing weights implicitly estimate $\beta_1$ on a subset of the data where we have sufficient covariate overlap. We can therefore think of SBW as returning a solution to some weighted-least squares problem. Assuming that the outcome model holds across all of the data, WLS and OLS are estimating the same $\beta_1$; therefore, the bias that effects the least squares solution will have the same effect on the WLS, and therefore SBW, solution. In Appendix A, Proposition 2, we show that if we had access to $\eta_1$, we can obtain an unbiased estimate of $\psi^{1, sp}$ by reweighting $\eta_1(W_{sc})$.\footnote{For the finite-sample parameter we're targeting, this estimator will have finite-sample bias conditional on $W$ and viewing $X$ as fixed.} Of course, in practice we do not know $\eta_1$ but must instead estimate it using auxillary data. In Appendix A, Proposition 3, we show that we can obtain a consistent of $\psi^{1, sp}$ when balancing on an estimate of $\eta_1$ using auxillary data.

The key in our application is to estimate $\eta_a$: at a high-level, we use the ACS micro-data replicate survey weights to estimate the covariance matrix of CPUMA sampling-variability $\Sigma_{vv, sc}$. Using our observed data to estimate $\Sigma_{WW \mid A = a}$ and $\bar{W}$, we combine these estimates to generate an estimate of $\eta_a$. This is a technique that comes from the regression-calibration literature (see, e.g., \cite{gleser1992importance}). We also consider an adjustment procedure that further accounts for the differential measurement error due to the highly variable sample sizes used to calculate each covariate. This procedure allows our adjustment to differentially adjust covariate values depending on the sample-sizes involved in the adjustment procedure. We refer to the first procedure the ``homogeneous adjustment'' and the second procedure the ``heterogeneous adjustment'' because the adjustment is constant for all units in the first case, but varies by unit in the second case. Further details about these procedures are available in Appendix B.

This is the first application we are aware of to use regression calibration in the context of balancing weights to address the problem of measurement error. We emphasize two critical assumptions for using this procedure in our context: (1) the outcome model is linear in the true covariates; and (2) the measurement error in the outcome is uncorrelated with the measurement error in the covariates. The first assumption is strong, though often used in practice. The second assumption is reasonable, because our outcomes are estimated from a different cross-section than our covariates.

\subsubsection{Bias-correction for imbalances}

Because we are unable to reduce the balance constraints to our preferred level without generating very extreme weights, following the recent literature on synthetic controls, we test the sensitivity of our results to the imbalances in the observed (or adjusted) covariates using ridge-regression augmented weights \cite{ben2018augmented}. Letting $\matr{\hat{X}}_1$ be the matrix of adjusted covariates, and $\gamma^{hsbw}$ be our H-SBW weights, we consider the regression-augmented weights:

\begin{equation}
\gamma^{aug} = \gamma^{hsbw} + (\gamma^{hsbw}\hat{X}_1 - \bar{W}_0)^T(\hat{X}_1^T\Omega^{-1}\hat{X}_1 + \lambda I_q)^{-1}\hat{X}_1^T\Omega^{-1}
\end{equation}

where $\Omega$ is a block diagonal matrix with diagonal entries equal to one and the within-group off diagonals equal to $\rho$. We choose $\lambda$ so that the remaining imbalances all fall within 0.5 percentage points. The cost of this procedure is that we must extrapolate off the support of the data, and therefore rely more heavily on our outcome modeling assumptions. We refer to \cite{ben2018augmented} for more details about this procedure. In our results we consider estimators using SBW ($\rho = 0$), H-SBW ($\rho = 1/6$), and ridge-augmented versions of SBW and H-SBW that we call BC-SBW and BC-HSBW.

\subsection{Model validation}

Earlier we argued that we cannot easily use pre-treatment outcomes to conduct variable selection or to learn about the relative importance of covariates. The challenge with using pre-treatment data in this setting is that it is not obvious why the best model of $\bar{Y}_{0, t}^0$ $(t = T-l,..., T-1)$ should also be the best model, or even a good model, of $\bar{Y}^1_{0,T}$. However, for fixed covariates and targeted levels of imbalance $\delta$, we can use the heuristic that a good model of $\bar{Y}^1_{0,T}$ should also be a good model of $\bar{Y}_{0, t}^0$ to compare our models. We can also assume that when comparing two models, one with uniformly better covariate balance than the other, the model with better covariate balance should have lower bias both for $\bar{Y}_{0, T}^1$ and $\bar{Y}_{0, t}^0$ if our model assumptions are correct. We justify these comparisons by again assuming that $\mu_{a, t}$ are linear in $X$ for all time periods $t$. This is a stronger assumption than we require to estimate the ETC, which only requires that $\mu_{1, T}$ is linear in $X$. However, with this stronger assumption we can use pre-treatment data to at least heuristically compare our models. While we caution that these comparisons may not indicate the best model, we can still use these comparisons to see which models we may trust less.

We therefore rerun our procedures on pre-treatment data to compare the performance of our models for a fixed level of imbalances $\delta$. In particular, we train our model on 2009-2011 data to predict 2012 outcomes, and 2010-2012 data to predict 2013 outcomes. We limit to one-year prediction error since our estimand is only one-year forward. We then examine the performance of the H-SBW versus SBW estimators, which only vary with respect to the tuning parameter $\rho$, the bias-corrected versions, and the covariate adjustment procedure used to determine the weights.

We expect that the estimators trained on the adjusted data should perform better than the estimators trained on the unadjusted data. If our outcome model is correct, these estimators should achieve better balance across the true covariates and therefore have lower bias than the estimators trained on the unadjusted data. We assume that any difference between the performance of the two adjustments is due to better covariate balance on the true covariates. We therefore use the performance on this data to select which adjustment we prefer in our final results. Conditional on the adjustment, we assume that if the bias-corrected estimators all have uniformly better balance than the uncorrected estimators, these estimators should also perform better than the uncorrected estimators. However, if the assumed outcome models are incorrect, these estimators may suffer from extrapolation bias and perform worse despite achieving better covariate balance. Finally, we expect in general that H-SBW and SBW should have similar performance if we knew the true covariate values. However, in Appendix A, we show that for fixed (unobserved) $X$, these weighting estimators have a finite sample bias that reduces with the square of the weights, suggesting the SBW may have less bias than H-SBW. On the other hand, H-SBW should have less variability given within-state dependencies in the outcomes. From an MSE perspective it is unclear which should be optimal, and given only two-years of pre-treatment data we do not believe we can select which model is better.

\subsection{Inference}

We consider $W$ to be fixed (and $X$ as fixed unknown parameters), and we consider inference over repeated samples from some super-population of CPUMAs with a state-level dependency structure.\footnote{Alternatively, viewing the potential outcomes as fixed and treatment assignment as random, we could consider inference over the randomization distribution of treatment at the state-level.} While placebo tests are frequently used in the synthetic controls literature for inference, we view these as qualitative statistical tests (see, e.g., \cite{arkhangelsky2019synthetic}) and instead use the leave-one-state-out jackknife to estimate the variance of $\hat{\psi}^1$ (\cite{cameron2015practitioner}). Specifically, we exclude each state and re-calculate the weights holding our targeted mean fixed at $\bar{W}_0$.\footnote{When our preferred initial choice of $\delta$ does not converge, we gradually reduce the constraints until it does.} We compute this estimator in two ways: first, we condition on our covariate adjustment $\hat{\eta}_1$. This is our preferred estimator; however, it does not account for the randomness in $\hat{eta}_1$. We therefore also conduct a second procedure where we re-estimate $\hat{\eta}_1$ for each state omitted in the jackknife procedure and provide these results in the Appendix.

To estimate $Var(\hat{\psi}^0 \mid X, W)$ we use an auxillary regression model and use the CR-2 standard error adjustment (using the ``clubSandwich'' package in R) to estimate the variance of the linear combination $\bar{W}_0^T\hat{\beta}_0$. We can estimate this quantity using the original (unadjusted) data given that $\mathbb{E}\{\bar{W}_0^T\hat{\beta}_0\} = \psi^0$ (since the regression line runs through the point $(\bar{W}_0, \bar{J}_0)$, which are unbiased estimates of $(\bar{X}_0, \bar{Y}_0)$). Our total estimate $\hat{Var}(\hat{\psi})$ is simply the sum of these two variance estimates. We use the standard normal quantiles to generate confidence intervals.


\BoSSSopen{ParameterStudy/ParameterStudy}
\graphicspath{{ParameterStudy/ParameterStudy.texbatch}}

\BoSSScmd{
restart;
 }
\BoSSSexeSilent
\BoSSScmd{
/// This guide will give you an example of how to conduct a parameter study with
/// all the necessary steps.
/// \section{Initialization of solver, processor and workflow}
/// We start with initializing of the workflow
 }
\BoSSSexe
\BoSSScmd{
/// \section{Initialization of solver, processor and workflow}
/// We start with initializing of the workflow.
 }
\BoSSSexe
\BoSSScmd{
BoSSSshell.WorkflowMgm.Init("Name of Workflow");
 }
\BoSSSexe
\BoSSScmd{
/// This line helps us manage the sessions later on while evaluating the results.
/// Next, we connect to the database.
 }
\BoSSSexe
\BoSSScmd{
var myDb = CreateTempDatabase();
 }
\BoSSSexe
\BoSSScmd{
/// To check all the sessions in the current workflow, use the line:
 }
\BoSSSexe
\BoSSScmd{
BoSSSshell.WorkflowMgm.Sessions;
 }
\BoSSSexe
\BoSSScmd{
/// Now, all the necessary libraries need to be loaded
 }
\BoSSSexe
\BoSSScmd{
using System.Diagnostics;\newline
using BoSSS.Foundation.Grid.RefElements;\newline
using BoSSS.Application.XNSE\_Solver;\newline
using BoSSS.Platform.LinAlg;\newline
using BoSSS.Solution.XdgTimestepping;
 }
\BoSSSexe
\BoSSScmd{
/// As an execution queue, we select the first queue defined in
/// the {\tt $\sim$/.BoSSS/etc/BatchProcessorConfig.json}-file:
 }
\BoSSSexe
\BoSSScmd{
var myBatch = ExecutionQueues[0];
 }
\BoSSSexe
\BoSSScmd{
/// \section{Grid Generation}
///Firstly, we need to determine the boundaries of our grid/control volume.
///Is it important to know that the number of nodes (in our case $/code{k}$)
///needed are equal
/// to the number of cells $+1$. For instance, for $10$ cells we need $11$ nodes.
/// In this example we will use the Cartesian $2D$ grid from the database
/// which requires $x$- and $y$-Nodes. The J term in the code is for doing
/// a check if the desired resolution of the volume is correctly typed.
 }
\BoSSSexe
\BoSSScmd{
int k = 10;\newline
double[] xNodes = GenericBlas.Linspace(0, 1, k + 1);\newline
double[] yNodes = GenericBlas.Linspace(0, 1, k + 1);\newline
int J           = (xNodes.Length - 1)*(yNodes.Length - 1);\newline
string GridName = string.Format(BoSSSshell.WorkflowMgm.CurrentProject + "\_J" +J);\newline
 \newline
Console.WriteLine("Creating grid with " + J + " cells. ");\newline
 \newline
GridCommons g;\newline
g      = Grid2D.Cartesian2DGrid(xNodes, yNodes);\newline
g.Name = GridName;
 }
\BoSSSexe
\BoSSScmd{
/// \section{Define geometrical boundaries}
/// After loading the grid and giving the dimensions, we need to adjust
/// the edges and their names. With the following code we assign every edge with
/// a number and name. Keep in mind that the name corresponds to the boundary
///condition (in this case "Pressure Dirichlet").
/// In this particular case we will use inflow profile represented
/// via tan-function and the angle of inflow will be $30$ degrees.
 }
\BoSSSexe
\BoSSScmd{
GridCommons g;\newline
g      = Grid2D.Cartesian2DGrid(xNodes, yNodes);\newline
g.Name = GridName;\newline
 \newline
g.EdgeTagNames.Add(1, "wall");\newline
g.EdgeTagNames.Add(2, "Velocity\_Inlet");\newline
g.EdgeTagNames.Add(3, "Pressure\_Dirichlet\_back");\newline
g.EdgeTagNames.Add(4, "Pressure\_Dirichlet\_top");\newline
 \newline
g.DefineEdgeTags(delegate (double[] X) \{\newline
\btab byte ret = 0;\newline
\btab if (Math.Abs(X[1]-(0.0))<= 1.0e-8)\newline
\btab \btab ret = 1;\newline
\btab if (Math.Abs(X[0]-(0.0))<= 1.0e-8)\newline
\btab \btab ret = 2;\newline
\btab if (Math.Abs(X[1]-(1.0))<= 1.0e-8)\newline
\btab \btab ret = 3;\newline
\btab if (Math.Abs(X[0]-(1.0))<= 1.0e-8)\newline
\btab \btab ret = 4;\newline
\btab return ret;\newline
 \newline
 \});
 }
\BoSSSexe
\BoSSScmd{
/// \section{Angle/Velocity Profile}
/// In this particular case we will use inflow profile represented via tan-function and the angle of inflow will be $30$ degrees.
 }
\BoSSSexe
\BoSSScmd{
string caseName = string.Format("k\{0\}\_\{1\}", k, g);\newline
 \newline
Console.WriteLine("setting up: " + caseName);\newline
 \newline
double beta    = 30;\newline
string CosBeta = Math.Cos(beta*Math.PI/180.0).ToString();\newline
string SinBeta = Math.Sin(beta*Math.PI/180.0).ToString();
 }
\BoSSSexe
\BoSSScmd{
/// These code lines set up the case name and introduce the sine and cosine
/// functions to our simulation. Next, we define the velocities in
/// $x$- and $y$-direction via a tan-function. These velocities and angles are only for this particular example and would not be suited for your simulation.
 }
\BoSSSexe
\BoSSScmd{
var UX = new Formula\newline
\btab (string.Format("X=> \{0\}*Math.Atan(X[1]*5)*2.0/Math.PI",CosBeta),false);\newline
var UY = new Formula \newline
\btab (string.Format("X=> \{0\}*Math.Atan(X[1]*5)*2.0/Math.PI",SinBeta),false);
 }
\BoSSSexe
\BoSSScmd{
///After the velocities and boundary conditions are set.
///We need to determine all other simulation parameters needed to proceed.
//The variable $\textbackslash code\{ctrl\}$ is used to store the $\textbackslash code\{IBM\_Control\}$-object.\newline
/// All other parameters are selfexplanatory.
 }
\BoSSSexe
\BoSSScmd{
var ctrl = new XNSE\_Control();\newline
//controls.Add(ctrl);\newline
 \newline
ctrl.SessionName = caseName;\newline
ctrl.SetDatabase(myDb);\newline
ctrl.SetGrid(g);\newline
ctrl.SetDGdegree(k);\newline
ctrl.NoOfMultigridLevels = int.MaxValue;
 }
\BoSSSexe
\BoSSScmd{
/// \section{Boundary conditions/Initial values}
/// We move on to the part where we define
/// the boundary conditions and initial values.
 }
\BoSSSexe
\BoSSScmd{
ctrl.AddBoundaryValue("wall");\newline
ctrl.AddBoundaryValue("Velocity\_Inlet");\newline
ctrl.AddBoundaryValue("Pressure\_Dirichlet\_back");\newline
ctrl.AddBoundaryValue("Pressure\_Dirichlet\_top");\newline
ctrl.AddBoundaryValue("Velocity\_Inlet","VelocityX",UX);\newline
ctrl.AddBoundaryValue("Velocity\_Inlet","VelocityY",UY);
 }
\BoSSSexe
\BoSSScmd{
/// and for the initial values
 }
\BoSSSexe
\BoSSScmd{
ctrl.InitialValues.Add("VelocityX", new Formula ("X=> 0.0", false));\newline
ctrl.InitialValues.Add("VelocityY", new Formula ("X=> 0.0", false));\newline
ctrl.InitialValues.Add("Pressure", new Formula ("X=> 0.0", false));\newline
ctrl.InitialValues.Add("Phi", new Formula ("X=> -1.0", false));
 }
\BoSSSexe
\BoSSScmd{
/// \section{Fluid properties}
/// Here we set up the density and the Reynolds number,
/// keep in mind that the calculations are dimensionles,
/// so leave the values as seen above ($100$ is an example value)
 }
\BoSSSexe
\BoSSScmd{
double reynolds               = 100;\newline
ctrl.PhysicalParameters.rho\_A = 1;\newline
ctrl.PhysicalParameters.mu\_A  = 1.0/reynolds;
 }
\BoSSSexe
\BoSSScmd{
/// \section{Simulation options}
/// We set the simulation parameters, such as time-step size,
/// end time and number of time-steps.
 }
\BoSSSexe
\BoSSScmd{
ctrl.TimeSteppingScheme = TimeSteppingScheme.ImplicitEuler;\newline
double dt               = 7e-2;\newline
ctrl.dtMax              = dt;\newline
ctrl.dtMin              = dt;\newline
ctrl.Endtime            = 1e16;\newline
ctrl.NoOfTimesteps      = 100;
 }
\BoSSSexe
\BoSSScmd{
/// for the time-stepping scheme, you can choose either BDF2 or ImplicitEuler.
/// \section{Starting of simulation}
/// You have two possible ways to start a simulation
/// - locally on the PC via $\code{myBatch}$
/// or on the network cluster $\code{myHPC}$.
 }
\BoSSSexe
\BoSSScmd{
 \newline
//Console.WriteLine(" Submitting to Cluster: " + ctrl.SessionName);\newline
//ctrl.RunBatch(myHPC);\newline
 \newline
Console.WriteLine(" Submitting " + ctrl.SessionName);\newline
ctrl.RunBatch(myBatch);
 }
\BoSSSexe
\BoSSScmd{
/// \section{Evaluation and Error Calculation}
/// After all of the desired simulation are finished,
/// you need to evaluate the different parameters and their effect on
///the whole system. Typing the following command gives you a list of all
///simulations with their status (FinishedSuccessful or with certain errors)
 }
\BoSSSexe
\BoSSScmd{
BoSSSshell.WorkflowMgm.AllJobs.Select(kv => kv.Key + ": \textbackslash t" + kv.Value.Status);
 }
\BoSSSexe
\BoSSScmd{
/// With the next command line you are able to select a certain
///session(simulation) and see the different time-steps for control purposes.
 }
\BoSSSexe
\BoSSScmd{
BoSSSshell.WorkflowMgm.AllJobs.ElementAt(1).Value.Stdout;
 }
\BoSSSexe
\BoSSScmd{
/// \subsection{$L^2$-Error}
/// This section introduces the calculation of the $L^2$-Error.
 }
\BoSSSexe
\BoSSScmd{
 ITimestepInfo[] AllSolutionS = BoSSSshell.WorkflowMgm.AllJobs.Select(kv => kv.Value.LatestSession.Timesteps.Last()).ToArray();
 }
\BoSSSexe
\BoSSScmd{
ITimestepInfo[] k1\_SolutionS = AllSolutionS.Where(\newline
\btab  ts = > ts.Fields.Single(\newline
\btab \btab    f = > f.Identification == "Pressure").Basis.Degree == 0).ToArray();\newline
ITimestepInfo[] k2\_SolutionS = AllSolutionS.Where(\newline
\btab  ts = > ts.Fields.Single(\newline
\btab \btab    f = > f.Identification == "Pressure").Basis.Degree == 1).ToArray();\newline
ITimestepInfo[] k3\_SolutionS = AllSolutionS.Where(\newline
\btab  ts = > ts.Fields.Single(\newline
\btab \btab    f = > f.Identification == "Pressure").Basis.Degree == 2).ToArray();
 }
\BoSSSexe
\BoSSScmd{
k1\_SolutionS.Select(\newline
\btab  ts => ts.Fields.Single(\newline
\btab \btab    f = > f.Identification == "Pressure").Basis.Degree);
 }
\BoSSSexe
\BoSSScmd{
double[] GridRes;\newline
Dictionary<string, double[]> L2Errors;\newline
DGFieldComparison.ComputeErrors(\newline
\btab  new[]\{"VelocityX","VelocityY"\}, k1\_SolutionS, out GridRes, out L2Errors);
 }
\BoSSSexe
\BoSSScmd{
/// To check the particular errors, type
 }
\BoSSSexe
\BoSSScmd{
GridRes;
 }
\BoSSSexe
\BoSSScmd{
L2Errors["VelocityX"];
 }
\BoSSSexe
\BoSSScmd{
L2Errors["VelocityY"];
 }
\BoSSSexe
\BoSSScmd{
/// \section{Plotting of errors}
/// This section gives a brief example of how to plot the erros
/// and all the data from the previous simulations.
 }
\BoSSSexe
\BoSSScmd{
Plot(GridRes,L2Errors["VelocityX"],"VelXErr","-oy",\newline
\btab  GridRes,L2Errors["VelocityY"],"VelXErr","-xb",logX:true,logY:true);
 }
\BoSSSexe
\BoSSScmd{
/// for a plot with more specifics and more possible adjustments
 }
\BoSSSexe
\BoSSScmd{
var FancyPlot = new Plot2Ddata();
 }
\BoSSSexe
\BoSSScmd{
FancyPlot.LogX = true;\newline
FancyPlot.LogY = true;
 }
\BoSSSexe
\BoSSScmd{
var k1plot = new Plot2Ddata.XYvalues(\newline
\btab "VelXErr-k1",GridRes,L2Errors["VelocityY"]);
 }
\BoSSSexe
\BoSSScmd{
ArrayTools.AddToArray(k1plot, ref FancyPlot.dataGroups);
 }
\BoSSSexe
\BoSSScmd{
var CL = FancyPlot.ToGnuplot().PlotCairolatex();
 }
\BoSSSexe
\BoSSScmd{
CL.PlotNow();
 }
\BoSSSexe
\BoSSScmd{
/// \section{Exporting the session table}
 }
\BoSSSexe
\BoSSScmd{
static class AddCols \{\newline
\btab static public object SipMatrixAssembly\_time(ISessionInfo SI) \{\newline
\btab \btab var mcr = SI.GetProfiling()[0];\newline
\btab \btab var ndS = mcr.FindChildren("SipMatrixAssembly");\newline
\btab \btab var nd  = ndS.ElementAt(0);\newline
\btab \btab return nd.TimeSpentInMethod.TotalSeconds  / nd.CallCount;\newline
\btab \}\newline
\btab static public object Aggregation\_basis\_init\_time(ISessionInfo SI) \{\newline
\btab \btab var mcr = SI.GetProfiling()[0];\newline
\btab \btab var ndS = mcr.FindChildren("Aggregation\_basis\_init");\newline
\btab \btab var nd  = ndS.ElementAt(0);\newline
\btab \btab return nd.TimeSpentInMethod.TotalSeconds  / nd.CallCount;\newline
\btab \}\newline
\btab static public object Solver\_Init\_time(ISessionInfo SI) \{\newline
\btab \btab var mcr = SI.GetProfiling()[0];\newline
\btab \btab var ndS = mcr.FindChildren("Solver\_Init");\newline
\btab \btab var nd  = ndS.ElementAt(0);\newline
\btab \btab //Console.WriteLine("Number of nodes: " + ndS.Count() + " cc " + nd.CallCount );\newline
\btab \btab return nd.TimeSpentInMethod.TotalSeconds / nd.CallCount;\newline
\btab \}\newline
\btab static public object Solver\_Run\_time(ISessionInfo SI) \{\newline
\btab \btab var mcr = SI.GetProfiling()[0];\newline
\btab \btab var ndS = mcr.FindChildren("Solver\_Run");\newline
\btab \btab var nd  = ndS.ElementAt(0);\newline
\btab \btab return nd.TimeSpentInMethod.TotalSeconds  / nd.CallCount;\newline
\btab \}\newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// this code adds additional/user-defined colums. Now, we want to export he
/// saved session table in a file.
 }
\BoSSSexe
\BoSSScmd{
var SessTab = BoSSSshell.WorkflowMgm.SessionTable;
 }
\BoSSSexe
\BoSSScmd{
SessTab = SessTab.ExtractColumns(AllCols.ToArray());
 }
\BoSSSexe
\BoSSScmd{
using System.IO;
 }
\BoSSSexe
\BoSSScmd{
/// Here, we define the filename
 }
\BoSSSexe
\BoSSScmd{
var now           = DateTime.Now;\newline
SessTab.TableName = "SolverRuns--" + now.Year + "-" + now.Month + "-" + now.Day;\newline
string docpath    = Path.Combine(CurrentDocDir, SessTab.TableName + ".json");
 }
\BoSSSexe
\BoSSScmd{
/// saving the session table as a file could also be done in our git reposatory
 }
\BoSSSexe
\BoSSScmd{
SessTab.SaveToFile(docpath);
 }
\BoSSSexe
\BoSSScmd{
///
 }
\BoSSSexe


\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{braket}%量子力学符号
\usepackage{geometry}
\usepackage{enumerate}
\usepackage{natbib}
\usepackage{float}%稳定图片位置
\usepackage{graphicx,subfig}%画图
\usepackage{caption}
\usepackage[english]{babel}
\usepackage{indentfirst}%缩进
\usepackage{enumerate}%加序号
\usepackage{multirow}%合并行
\usepackage{hyperref}
\hypersetup{hypertex=true, colorlinks=true, linkcolor=black, anchorcolor=black, citecolor=black}
\title{\Large \textbf{VP390 Problem Set 3}\\
\author{\textbf{Pan, Chongdan ID:516370910121}\\
}
}
\begin{document}
\maketitle
\section{Problem 1}
\begin{enumerate}[(a)]
    \item $|\Psi(x,t)|^2=\Psi(x,t)\Psi^*(x,t)=Ce^{-\frac{i}{\hbar}Et}(x^2-a^2)Ce^{\frac{i}{\hbar}Et}(x^2-a^2)=C^2(x^2-a^2)^2$
    \\ $|\Psi(x,t)|^2=\Phi(x)$ So $\Psi$ represents a stationary state.
    \item $\int_{-a}^a|\Psi(x,t)|^2\mathrm{d}x=\int_{-a}^a C^2(x^2-a^2)^2\mathrm{d}x=C^2(\frac{2a^5}{5}-\frac{4a^5}{3}+2a^5)=C^2\frac{16a^5}{15}=1$
    \\$C^2a^5=\frac{15}{16}$
    \item $\int_{-a/2}^{a/2} C^2(x^2-a^2)^2\mathrm{d}x=C^2(\frac{a^5}{80}-\frac{a^5}{6}+a^5)=\frac{203}{256}\approx0.793$
    \item Since $\Phi(x)$ is symmetric about $x$-axis, the probability is 0.5
    \item $\braket{x}_\Psi=\int_{-\infty}^\infty x|\Psi(x,t)|^2\mathrm{d}x=\int_{-a}^a C^2x(x^2-a^2)^2\mathrm{d}x=0$
    So it's time independent
    \item $\bigtriangleup_x=\sqrt{\braket{x^2}_\Psi-\braket{x}_\Psi^2}=C\sqrt{\int_{-a}^a x^2(x^-a^2)^2\mathrm{d}x}=C\sqrt{\frac{30a^7}{105}-\frac{84a^7}{105}+\frac{70a^7}{105}}=\sqrt{\frac{a^2}{7}}$
\end{enumerate}
\section{Problem 2}
\begin{enumerate}[(a)]
    \item $E_1=13.6$eV, $a_0=\frac{4\pi\epsilon_0\hbar^2}{me^2}\approx5.29\times10^{-2}$nm
    \\$\int_{-\pi/2}^{\pi/2}\int_0^{2\pi}\int_0^\infty\Psi_{100}(r,\varphi,\theta,t)|^2\mathrm{d}r\mathrm{d}\theta\mathrm{d}\varphi=C^2\int_{-\pi/2}^{\pi/2}\int_0^{2\pi}\int_0^\infty r^2\sin\varphi\exp(-\frac{2r}{a_0})\mathrm{d}r\mathrm{d}\theta\mathrm{d}\varphi=2\times2\pi C^2\frac{a^3}{4}$
    \\$C=\sqrt{\frac{1}{\alpha_0^3\pi}}=1.467\times10^{15}$
    \item $C^2\int_{-\pi/2}^{\pi/2}\int_0^{2\pi}\int_0^{a_0} r^2\sin\varphi\exp(-\frac{2r}{a_0})\mathrm{d}r\mathrm{d}\theta\mathrm{d}\varphi=\pi C^2(a^3-5a^3e^{-2})=1-5e^{-2}$
    \item The probability for the electron radius is $r$ can be expressed :
    \\$C^2\int_{-\pi/2}^{\pi/2}\int_0^{2\pi} r^2\sin\varphi\exp(-\frac{2r}{a_0})\mathrm{d}\theta\mathrm{d}\varphi=4\pi C^2r^2\exp(-\frac{2r}{a_0})$
    \\$\frac{\mathrm{d}r^2\exp(-\frac{2r}{a_0})}{\mathrm{d}r}=2r\exp(-\frac{2r}{a_0})-\frac{2r^2}{a_0}\exp(-\frac{2r}{a_0})=0$
    \\$r_1=0, r_2=a_0, r_3=\infty$, since $r_1$ and $r_3$ can lead to the minium value of $r^2\exp(-\frac{2r}{a_0})$, only $r=\alpha_0$ has the most probability value to find the electron
    \item $r_a=\int_{-\pi/2}^{\pi/2}\int_0^{2\pi}\int_0^\infty r\Psi_{100}(r,\varphi,\theta,t)|^2\mathrm{d}r\mathrm{d}\theta\mathrm{d}\varphi=C^2\int_{-\pi/2}^{\pi/2}\int_0^{2\pi}\int_0^\infty r^3\sin\varphi\exp(-\frac{2r}{a_0})\mathrm{d}r\mathrm{d}\theta\mathrm{d}\varphi$
    \\$=4\pi C^2\frac{3a_0^4}{8}=\frac{3a_0}{2}$
    \\$\bigtriangleup_r=\sqrt{\braket{r^2}_\Psi-r_a^2}$
    \\$\braket{r^2}=4\pi C^2\int_0^\infty r^4\exp(-\frac{2r}{a_0})\mathrm{d}r=3\pi C^2 a_0^5=3a_0^2$
    \\$\bigtriangleup_r=\sqrt{3a_0^2-\frac{9a_0^2}{4}}=\frac{\sqrt{3}a_0}{2}$
    \\So the average value of $r$ is $\frac{3a_0}{2}$, its standard deviation is $\frac{\sqrt{3}a_0}{2}$
\end{enumerate}
\section{Problem 3}
\begin{enumerate}[(a)]
    \item $\braket{f,g}=\int_{-\infty}^\infty f^*(u)g(u)\mathrm{d}u=\int_{-\infty}^\infty (g^*(u))^*f^*(u)\mathrm{d}u=(\int_{-\infty}^\infty g^*(u)f(u)\mathrm{d}u)^*=\braket{g,f}^*$
    \item $\braket{f,\alpha g}=\int_{-\infty}^\infty f^*(u)\alpha g(u)\mathrm{d}u=\alpha\int_{-\infty}^\infty f^*(u)g(u)\mathrm{d}u=\alpha\braket{f,g}$
    \item $\braket{\alpha f,g}=\braket{g,\alpha f}^*=\alpha^*\braket{g,f}^*=\alpha^*\braket{f,g}$
\end{enumerate}
\section{Problem 4}
    \begin{enumerate}
        \item $\int_{-\infty}^\infty|\psi|^2\mathrm{d}x=\int_0^L\frac{2}{L}\sin^2(\frac{n\pi x}{L})\mathrm{d}x=\frac{2}{L}(\frac{L}{2}-\frac{L\sin 2n\pi}{4n\pi})=1$
        \\So it is normalized
        \item $\braket{\psi_n,\psi_m}=\int_0^L\frac{2}{L}\sin\frac{n\pi x}{L}\sin\frac{m\pi x}{L}\mathrm{d}x=\frac{1}{L}\int_0^L\cos\frac{(n-m)\pi x}{L}-\cos\frac{(n+m)\pi x}{L}\mathrm{d}x$
        \\$=\frac{1}{L}[\frac{L\sin(n-m)\pi}{(n-m)\pi}-\frac{L\sin(n+m)\pi}{(n+m)\pi}]=0$
        \item $\braket{i\psi_1,-3\psi_1+2i\psi_2-\psi_3}=3i\braket{\psi_1,\psi_1}-2\braket{\psi_1,\psi_2}-i\braket{\psi_1,\psi_3}=3i$
        \item Assume $x=x_1\psi_1+x_2\psi_2,y=y_1\psi_1+y_2\psi_2$ where
        \\$x_1^2+x_2^2=1,y_1^2+y_2^2=1,x_1y_1+x_2y_2=0$
        \\$x=\frac{\sqrt{2}}{2}\psi_1-\frac{\sqrt{2}}{2}\psi_2,y=\frac{\sqrt{2}}{2}\psi_1+\frac{\sqrt{2}}{2}\psi_2$
    \end{enumerate}
\section{Problem 5}
    \begin{enumerate}
        \item $\braket{\psi,\psi}=\frac{1}{2}[\braket{e^{-\frac{i}{\hbar}E_1t}\psi_1(x),e^{-\frac{i}{\hbar}E_1t}\psi_1(x)}+\braket{e^{-\frac{i}{\hbar}E_2t}\psi_2(x),e^{-\frac{i}{\hbar}E_2t}\psi_2(x)}]$
        \\$\frac{1}{2}(\braket{\psi_1,\psi_1}+\braket{\psi_2,\psi_2})=1$
        \\So it's normalized
        \item $\Psi(x,t)^*\Psi(x,t)=\frac{1}{2}(e^{\frac{i}{\hbar}E_1t}\psi_1^*(x)+e^{\frac{i}{\hbar}E_2t}\psi_2^*(x))(e^{-\frac{i}{\hbar}E_1t}\psi_1(x)+e^{-\frac{i}{\hbar}E_2t}\psi_2(x))$
        \\$=\frac{1}{2}(\psi_1^*(x)\psi_1(x)+\psi_2^*(x)\psi_2(x))+\frac{1}{2}(e^{\frac{i}{\hbar}(E_1-E_2)t}\psi_1^*(x)\psi_2(x)+e^{\frac{i}{\hbar}(E_2-E_1)t}\psi_1(x)\psi_2^*(x))$
        \\So the $\Psi$ only describe a particle in a stationary state when $E_1=E_2$
        \item $\Psi(x,t)=\frac{1}{\sqrt{2}}(\cos\frac{E_1t}{\hbar}\psi_1(x)-i\sin\frac{E_1t}{\hbar}\psi_1(x)+\cos\frac{4E_1t}{\hbar}\psi_2(x)-i\sin\frac{4E_1t}{\hbar}\psi_2(x))$
        \\$T=\frac{2\pi}{\omega}=\frac{h}{E_1}$
        \\if $|\Psi(x,t+T)|^2=|\Psi(x,t)|^2=$ then
        \\$\frac{1}{2}(e^{-\frac{i}{\hbar}3t}\psi_1^*(x)\psi_2(x)+e^{\frac{i}{\hbar}3t}\psi_1(x)\psi_2^*(x)=\frac{1}{2}(e^{-\frac{i}{\hbar}3E_1(t+T)}\psi_1^*(x)\psi_2(x)+e^{\frac{i}{\hbar}3E_1(t+T)}\psi_1(x)\psi_2^*(x)$
        \\$T=\frac{2\pi}{\omega}=\frac{h}{3E_1}$

    \end{enumerate}
\end{document}