##### Copyright 2022 The Cirq Developers

```
# @title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
```

# Parameter Sweeps

    View on QuantumAI

    Run in Google Colab

    View source on GitHub

    Download notebook

```
try:
    import cirq
except ImportError:
    print("installing cirq...")
    !pip install --quiet cirq
    print("installed cirq.")
    import cirq
```

## Concept of Circuit Parameterization and Sweeps

Suppose you have a quantum circuit and in this circuit there is a gate with some parameter. You might wish to run this circuit for different values of this parameter.  An example of this type of circuit is a Rabi flop experiment. This experiment runs a set of quantum computations which 1) starts in  $|0\rangle$ state, 2) rotates the state by $\theta$ about the $x$ axis, i.e. applies the gate $\exp(i \theta X)$, and 3) measures the state in the computational basis.  Running this experiment for multiple values of $\theta$, and plotting the probability of observing a $|1\rangle$ outcome yields the quintessential $\cos^2$ probability distribution as a function of the parameter $\theta$.  To support this type of experiment, Cirq provides the concept of parameterized circuits and parameter sweeps.

The next cell illustrates parameter sweeps with a simple example.  Suppose you want to compare two quantum circuits that are identical except for a single exponentiated `cirq.Z` gate.

```
q0 = cirq.LineQubit(0)

circuit1 = cirq.Circuit([cirq.H(q0), cirq.Z(q0)**0.5, cirq.H(q0), cirq.measure(q0)])
print(f"circuit1:\n{circuit1}")

circuit2 = cirq.Circuit([cirq.H(q0), cirq.Z(q0)**0.25, cirq.H(q0), cirq.measure(q0)])
print(f"circuit2:\n{circuit2}")
```

You could run these circuits separately (either on hardware or in simulation), and collect statistics on the results of these circuits. However parameter sweeps can do this in a cleaner and more perfomant manner.

First define a parameter, and construct a circuit that depends on this parameter. Cirq uses [SymPy](https://www.sympy.org/en/index.html){:external}, a symbolic mathematics package, to define parameters. In this example the Sympy parameter is `theta`, which is used to construct a parameterized circuit.

```
import sympy

theta = sympy.Symbol("theta")

circuit = cirq.Circuit([cirq.H(q0), cirq.Z(q0)**theta, cirq.H(q0), cirq.measure(q0)])
print(f"circuit:\n{circuit}")
```

Notice now that the circuit contains a `cirq.Z` gate that is raised to a power, but this power is the parameter `theta`.  This is a "parameterized circuit".  An equivalent way to construct this circuit, where the parameter is actually a parameter in the gate constructor's arguments, is:

```
circuit = cirq.Circuit(
    cirq.H(q0), cirq.ZPowGate(exponent=theta)(q0), cirq.H(q0), cirq.measure(q0)
)
print(f"circuit:\n{circuit}")
```

Note: You can check whether an object in Cirq is parameterized using `cirq.is_parameterized`:

```
cirq.is_parameterized(circuit)
```

Parameterized circuits are just like normal circuits; they just aren't defined in terms of gates that you can actually run on a quantum computer without the additional information about the values of the parameters.  Following the example above, you can generate the two circuits (`circuit1` and `circuit2`) by using `cirq.resolve_parameter` and supplying the values that you want the parameter(s) to take:

```
# circuit1 has theta = 0.5
cirq.resolve_parameters(circuit, {"theta": 0.5})
# circuit2 has theta = 0.25
cirq.resolve_parameters(circuit, {"theta": 0.25})
```

More interestingly, you can combine parameterized circuits with a list of parameter assignments when doing things like running circuits or simulating them.  These lists of parameter assignements are called "sweeps".  For example you can use a simulator's `run_sweep` method to run simulations for the parameters corresponding to the two circuits defined above.

```
sim = cirq.Simulator()
results = sim.run_sweep(circuit, repetitions=25, params=[{"theta": 0.5}, {"theta": 0.25}])
for result in results:
    print(f"param: {result.params}, result: {result}")
```

To recap, you can construct parameterized circuits that depend on parameters that have not yet been assigned a value.  These parameterized circuits can then be resolved to circuits with actual values via a dictionary that maps the sympy variable name to the value that parameter should take. You can also construct lists of dictionaries of parameter assignments, called sweeps, and pass this to many functions in Cirq that use circuits to do an action (such as `simulate` or `run`).  For each of the elements in the sweep, the function will execute using the parameters as described by the element.

## Constructing Sweeps

The previous example constructed a sweep by simply constructing a list of parameter assignments, `[{"theta": 0.5}, {"theta": 0.25}]`.  Cirq also provides other ways to construct sweeps.

One useful method for constructing parameter sweeps is `cirq.Linspace` which creates a sweep over a list of equally spaced elements.

```
# Create a sweep over 5 equally spaced values from 0 to 2.5.
params = cirq.Linspace(key="theta", start=0, stop=2.5, length=5)
for param in params:
    print(param)
```

Note: The `Linspace` sweep is composed of `cirq.ParamResolver` instances instead of simple dictionaries. However, you can think of them as effectively the same for most use cases.

If you need to explicitly and individually specify each parameter resolution, you can do it by constructing a list of dictionaries as before. However, you can also use `cirq.Points` to do this more succinctly.

```
params = cirq.Points(key="theta", points=[0, 1, 3])
for param in params:
    print(param)
```

If you're working with parameterized circuits, it is very likely you'll need to keep track of multiple parameters. Two common use cases necessitate building a sweep from two constituent sweeps, where the new sweep includes:
- Every possible combination of the elements of each sweep: A cartesian product.
- A element-wise pairing of the two sweeps: A zip.

The following are examples of using the `*` and `+` operators to combine sweeps by cartesian product and zipping, respectively.

```
sweep1 = cirq.Linspace("theta", 0, 1, 5)
sweep2 = cirq.Points("gamma", [0, 3])
# By taking the product of these two sweeps, you can sweep over all possible
# combinations of the parameters.
for param in sweep1 * sweep2:
    print(param)
```

```
sweep1 = cirq.Points("theta", [1, 2, 3])
sweep2 = cirq.Points("gamma", [0, 3, 4])
# By taking the sum of these two sweeps, you can combine the sweeps
# elementwise (similar to python's zip function):
for param in sweep1 + sweep2:
    print(param)
```

`cirq.Linspace` and `cirq.Points` are instances of the `cirq.Sweep` class, which explicitly supports cartesian product with the `*` operation, and zipping with the `+` operation. The `*` operation produces a `cirq.Product` object, and `+` produces a `cirq.Zip` object, both of which are also `Sweep`s. Other mathematical operations will not work in general *between sweeps*.

## Symbols and Expressions

[SymPy](https://www.sympy.org/en/index.html){:external} is a general symbolic mathematics toolset, and you can leverage this in Cirq to define more complex parameters than have been shown so far. For example, you can define an expression in Sympy and use it to construct circuits that depend on this expression:

```
# Construct an expression for 0.5 * a + 0.25:
expr = 0.5 * sympy.Symbol("a") + 0.25
print(expr)
```

```
# Use the expression in the circuit:
circuit = cirq.Circuit(cirq.X(q0)**expr, cirq.measure(q0))
print(f"circuit:\n{circuit}")
```

Both the exponents and parameter arguments of circuit operations can in fact be any general Sympy expression: The previous examples just used single-variable expressions. When you resolve parameters for this circuit, the expressions are evaluated under the given assignments to the variables in the expression.

```
print(cirq.resolve_parameters(circuit, {"a": 0}))
```

Just as before, you can pass a sweep over variable values to `run` or `simulate`, and Cirq will evaluate the expression for each possible value.

```
sim = cirq.Simulator()
results = sim.run_sweep(circuit, repetitions=25, params=cirq.Points('a', [0, 1]))
for result in results:
    print(f"param: {result.params}, result: {result}")
```

Sympy supports a large number of numeric functions and methods, which can be used to create fairly sophisticated expressions, like cosine, exponentiation, and more:

```
print(sympy.cos(sympy.Symbol("a"))**sympy.Symbol("b"))
```

Cirq can numerically evaluate all of the expressions Sympy can evalute. However, if you are running a parameterized circuit on a service (such as on a hardware backed quantum computing service) that service may not support evaluating all expressions. See documentation for the particular service you're using for details.

As a general workaround, you can instead use Cirq's flattening ability to evaluate the parameters before sending them off to the service.

### Flattening Expressions

Suppose you build a circuit that includes multiple different expressions:

```
a = sympy.Symbol('a')
circuit = cirq.Circuit(cirq.X(q0)**(a / 4), cirq.Y(q0)**(1 - a / 2), cirq.measure(q0))
print(circuit)
```

Flattening replaces every expression in the circuit with a new symbol that is representative of the value of that expression. Additionally, it keeps track of the new symbols and provices a `cirq.ExpressionMap` object to map the old sympy expression objects to the new symbols that replaced them.

```
# Flatten returns two objects, the circuit with new symbols, and the mapping from old to new values.
c_flat, expr_map = cirq.flatten(circuit)
print(c_flat)
print(expr_map)
```

Notice that the new circuit has new symbols, `<a/2>` and `<1-a/2>`, which are explicitly not expressions.  You can see this by looking at the value of the exponent in the first gate:

```
first_gate = c_flat[0][q0].gate
print(first_gate.exponent)
# Note this is a symbol, not an expression
print(type(first_gate.exponent))
```

The second object returned by `cirq.flatten` is an object that can be used to map sweeps over the previous symbols to new sweeps over the new expression-symbols. The values assigned to the new expression symbols in the resulting sweep are the old expressions kept track of in the `ExpressionMap`, but resolved with the values provided by the original input sweep.

```
sweep = cirq.Linspace(a, start=0, stop=3, length=4)
print(f"Old {sweep}")

new_sweep = expr_map.transform_sweep(sweep)
print(f"New {new_sweep}")
```

To reinforce: The new sweep is over two new symbols, which each represent the values of the expressions in the original circuit. The values assigned to these new expression symbols is acquired by evaluating the expressions with `a` resolved to a value in `[0, 4]`, according to the old sweep.

You can use these new sweep elements to resolve the parameters of the flattened circuit:

```
for params in new_sweep:
    print(c_flat, '=>', end=' ')
    print(cirq.resolve_parameters(c_flat, params))
```

Using `cirq.flatten`, you can always take a parameterized circuit with any complicated expressions, plus a sweep, and produce an equivalent circuit with no expressions, only symbols, and a sweep for these new symbols. Because this is a common flow, Cirq provides `cirq.flatten_sweep` to do this in one step:

```
c_flat, new_sweep = cirq.flatten_with_sweep(circuit, sweep)
print(c_flat)
print(new_sweep)
```

You can then directly use these objects to run the sweeps. For example, you can use them to perform a simulation:

```
sim = cirq.Simulator()
results = sim.run_sweep(c_flat, repetitions=20, params=new_sweep)
for result in results:
    print(result.params, result)
```

You can see that the different flattened parameters have corresponding different results for their simulation.

# Summary

- Cirq circuits can handle arbitrary Sympy expressions in place of exponents and parameter arguments in operations.
- By providing one or a sequence of `ParamResolver`s or dictionaries that resolve the Sympy variables to values, `run`, `simulate`, and other functions can iterate efficiently over different parameter assignments for otherwise identical circuits.
- Sweeps can be created succinctly with `cirq.Points` and `cirq.Linspace`, and composed with each other with `*` and `+`, to create `cirq.Product` and `cirq.Zip` sweeps.
- When the service you're using does not support arbitrary expressions, you can flatten a circuit and sweep into a new circuit that doesn't have complex expressions, and a corresponding new sweep.


```python
from IPython.display import Image
Image('../../../python_for_probability_statistics_and_machine_learning.jpg')
```

[Python for Probability, Statistics, and Machine Learning](https://www.springer.com/fr/book/9783319307152)

# Projection Methods

The concept of projection is key to developing an intuition about conditional
probability.  We already have a natural intuition of projection from looking at
the shadows of objects on a sunny day. As we will see, this simple idea
consolidates many abstract ideas in optimization and mathematics.  Consider
[Figure](#fig:probability_001) where we want to find a point along the blue
line (namely, $\mathbf{x}$) that is closest to the black square (namely,
$\mathbf{y}$). In other words, we want to inflate the gray circle until it just
touches the black line. Recall that the circle boundary is the set of points for
which

$$
\sqrt{(\mathbf{y}-\mathbf{x})^T(\mathbf{y}-\mathbf{x})} =\|\mathbf{y}-\mathbf{x} \| = \epsilon
$$

 for some value of $\epsilon$. So we want a point $\mathbf{x}$ along
the line that satisfies this for the smallest $\epsilon$.  Then, that point
will be the closest point on the black line to the black square.
 It may be obvious from the diagram, but the closest point on the line
occurs where the line segment from the black square to the black line is
perpedicular to the line. At this point, the gray circle just touches the black
line. This is illustrated below in [Figure](#fig:probability_002).

Given the point $\mathbf{y}$ (black square) we want to find the $\mathbf{x}$ along the line that is closest to it. The gray circle is the locus of points within a fixed distance from $\mathbf{y}$.

**Programming Tip.**

[Figure](#fig:probability_001) uses the `matplotlib.patches` module.  This
module contains primitive shapes like circles, ellipses, and rectangles that
can be assembled into complex graphics.  As shown in the code in the IPython
Notebook corresponding to this chapter, after importing a particular shape, you
can apply that shape to an existing axis  using the `add_patch` method.  The
patches themselves can by styled using the usual formatting keywords like
`color` and `alpha`.

The closest point on the line occurs when the line is tangent to the circle. When this happens, the black line and the line (minimum distance) are perpedicular.

 Now that we can see what's going on, we can construct the the solution
analytically. We can represent an arbitrary point along the black line as:

$$
\mathbf{x}=\alpha\mathbf{v}
$$

 where $\alpha\in\mathbb{R}$ slides the point up and down the line with

$$
\mathbf{v} = \left[ 1,1 \right]^T
$$

 Formally, $\mathbf{v}$ is the *subspace* onto which we want to
*project* $\mathbf{y}$. At the closest point, the vector between
$\mathbf{y}$ and $\mathbf{x}$ (the *error* vector above) is
perpedicular to the line. This means that

$$
(\mathbf{y}-\mathbf{x} )^T \mathbf{v} = 0
$$

 and by substituting and working out the terms, we obtain

$$
\alpha = \frac{\mathbf{y}^T\mathbf{v}}{ \|\mathbf{v} \|^2}
$$

 The *error* is the distance between $\alpha\mathbf{v}$ and $
\mathbf{y}$.  This is a right triangle, and we can use the Pythagorean
theorem to compute the squared length of this error as

$$
\epsilon^2 = \|( \mathbf{y}-\mathbf{x} )\|^2 = \|\mathbf{y}\|^2 - \alpha^2 \|\mathbf{v}\|^2 = \|\mathbf{y}\|^2 - \frac{\|\mathbf{y}^T\mathbf{v}\|^2}{\|\mathbf{v}\|^2}
$$

 where $ \|\mathbf{v}\|^2 = \mathbf{v}^T \mathbf{v} $. Note that since $\epsilon^2 \ge 0 $, this also shows that

$$
\| \mathbf{y}^T\mathbf{v}\| \le \|\mathbf{y}\|  \|\mathbf{v}\|
$$

 which is the famous and useful Cauchy-Schwarz inequality which we
will exploit later. Finally, we can assemble all of this into the *projection*
operator

$$
\mathbf{P}_v = \frac{1}{\|\mathbf{v}\|^2 } \mathbf{v v}^T
$$

 With this operator, we can take any $\mathbf{y}$ and find the closest
point on $\mathbf{v}$ by doing

$$
\mathbf{P}_v \mathbf{y} = \mathbf{v} \left( \frac{  \mathbf{v}^T \mathbf{y} }{\|\mathbf{v}\|^2} \right)
$$

 where we recognize the term in parenthesis as the $\alpha$ we
computed earlier.  It's called an *operator* because it takes a vector
($\mathbf{y}$) and produces another vector ($\alpha\mathbf{v}$). Thus,
projection unifies geometry and optimization.

## Weighted distance

We can easily extend this projection operator to cases where the measure of
distance between $\mathbf{y}$ and the subspace $\mathbf{v}$ is weighted. We can
accommodate these weighted distances by re-writing the projection operator as

$$
\begin{equation}
\mathbf{P}_v=\mathbf{v}\frac{\mathbf{v}^T\mathbf{Q}^T}{\mathbf{v}^T\mathbf{Q v}}
\end{equation}
\label{eq:weightedProj} \tag{1}
$$

 where $\mathbf{Q}$ is positive definite matrix.  In the previous
case, we started with a point $\mathbf{y}$ and inflated a circle centered at
$\mathbf{y}$ until it just touched the line defined by $\mathbf{v}$ and this
point was closest point on the line to $\mathbf{y}$. The same thing happens
in the general case with a weighted distance except now we inflate an
ellipse, not a circle, until the ellipse touches the line.

In the weighted case, the closest point on the line is tangent to the ellipse and is still perpedicular in the sense of the weighted distance.

Note that the error vector ($\mathbf{y}-\alpha\mathbf{v}$) in [Figure](#fig:probability_003) is still perpendicular to the line (subspace
$\mathbf{v}$), but in the space of the weighted distance.  The difference
between the first projection (with the uniform circular distance) and the
general case (with the elliptical weighted distance) is the inner product
between the two cases.  For example, in the first case we have $\mathbf{y}^T
\mathbf{v}$ and in the weighted case we have $\mathbf{y}^T \mathbf{Q}^T
\mathbf{v}$. To move from the uniform circular case to the weighted ellipsoidal
case, all we had to do was change all of the vector inner products.  Before we
finish, we need a formal property  of projections:

$$
\mathbf{P}_v \mathbf{P}_v = \mathbf{P}_v
$$

 known as the *idempotent* property which basically says that once we
have projected onto a subspace, subsequent projections leave us in the
same subspace. You can verify this by computing Equation ref{eq:weightedProj}.

Thus, projection ties a minimization problem (closest point to a line) to an
algebraic concept (inner product). It turns out that these same geometric ideas
from linear algebra [[strang2006linear]](#strang2006linear) can be translated to the conditional
expectation. How this works is the subject of our next  section.


# Longitudinal Data Analysis

```stata

```

# Panel Data Analysis III

Table of Contents

1  Quick reminder

2  Defining our statistical model

2.1  Decomposition implications

2.2  A word of caution

3  Fixed Effects Model

3.1  Conceptualising the Fixed Effects model

3.2  Estimation

3.3  Interpretation

3.4  Post-estimation

3.5  Benefits of Fixed Effects

3.6  Limitations of Fixed Effects

3.7  Summarising the Fixed Effects model

4  Summary

In this section we estimate a statistical model that leverages some of the main advantages of using panel data: **Fixed Effects**. We show some examples of how to estimate and interpret this model, and reflect on the conditions under which the model is appropriate.

## Quick reminder

Let's briefly recap some essential concepts regarding panel data:

Two sources of variation ([Gould, n.d.](https://www.stata.com/support/faqs/statistics/between-estimator/)):
1. Cross-section information on differences between units
2. Time series information on differences over time within units

So far our panel data models — Pooled OLS and Between Effects — only allow us to examine differences between units.

Two main issues with estimating statistical models:
1. Interdependence of errors
2. Improper model specification

The first can lead to inefficient estimates: under-estimated standard errors and false positive tests of statistical significance.

The second to biased coefficients and incorrect inferences regarding magnitude and direction of effect of explanatory variables.

Therefore we need a statistical model that allows us to **examine change over time** and/or **control for omitted variable bias**.

## Defining our statistical model

Before estimating Fixed Effects and Random Effects models separately, it is worth identifying the key commonality between their respective statistical models.

Let's take a simplified version of our charity income statistical model, this time with only one explanatory variable (*age*) - typically it looks as follows:

\begin{equation} \text{y}_{it} = \beta_0 + \beta_1x_{1it} + \epsilon_{it} \tag{1.2} \end{equation}

However it is possible to **decompose** the residual variation (error term) into two separate terms:

\begin{equation} \text{y}_{it} = \beta_0 + \beta_1x_{1it} + \mu_{i} + \text{e}_{it} \tag{1.3} \end{equation}

In equation 1.3. we have introduced a **unit-specific** term to represent some of the residual variation in the outcome that is unexplained by the explanatory variables.

### Decomposition implications

This term ($\mu_{i}$) captures the effect of *residual heterogeneity* on the outcome i.e., unobserved or immeasurable characteristics of the units that are associated with the outcome (and possibly the explanatory variables), and vary across units.

In our charity data example, these charity-specific effects could be organisational culture, informal connections to government etc. In theory these characteristics could be measured but it's often wildly impractical.

It also controls for the effect of other omitted variables on the outcome (and possibly the explanatory variables).

In our charity data example, we do not include explanatory variables capturing the amount a charity spends on fundraising, how well known it is etc.

### A word of caution

Note the lack of a time subscript *t* in the new term $\mu_{i}$.

The implication is that the unobserved unit-specific effect is **constant** over time (i.e., within units).

Therefore Fixed Effects and Random Effects models only control for omitted variables that do not change within units (e.g., race, sex at birth, natural ability).

## Fixed Effects Model

### Conceptualising the Fixed Effects model

1. The Fixed Effects model focuses on how changes in explanatory variables are associated with changes in the outcome **within units**.

2. It assumes the observed explanatory variables and unobserved unit-specific effect are correlated (i.e., omitted variable bias is an issue).

Mehmetoglu and Jakobsen (2016, p. 241):
> "In other words, we use fixed effects whenever we are only interested in the impact of variables that vary over time. This estimator helps us explore the relationship between the dependent and the explanatory variables within a unit (person, company, country, etc.) Each unit has its own individual characteristics that may or may not influence the predictor variables."

The Fixed Effects model is specified as follows:

\begin{equation} \text{y}_{it} = \beta_0 + \lambda_{i} + \beta_1x_{1it} +...+ \beta_kx_{kit} + \text{e}_{it} \tag{1.4} \end{equation}

Where:

$\lambda_{i}$ represents the unit-specific effect on the outcome.

The value of $\lambda_{i}$ captures the effect of **all** of the unobserved time-invariant explanatory variables that are missing from the model.

As a result, while the value of $\lambda_{i}$ is calculated, it is not of much interest in and of itself.

It's main role is to allow for a more robust (i.e., unbiased) estimation of the effects of the explanatory variables in the model.

In essence the Fixed Effects model produces a unit-specific intercept, which is the sum of the overall constant and the unit-specific effect:

\begin{equation} \text{y}_{it} = \alpha_{i} + \beta_1x_{1it} +...+ \beta_kx_{kit} + \text{e}_{it} \tag{1.5} \end{equation}

Where:

$\alpha_{i} = \beta_0 + \lambda_{i}$

The unit-specific effect shifts the overall intercept up or down the y axis by the value of $\lambda$.

**QUESTION**

Why is the unobserved unit-specific effect incorporated into the constant?

Two reasons:
* It is not of interest in and of itself (who cares what the effect is of being you?)
* It doesn't vary within a unit: remember we are only modelling within-unit variation and the unit-specific effect does not vary within a unit, therefore it does not make sense to interpret the unit-specific term like an explanatory variable.

#### Final thoughts on conceptualisation

Consider it a standard cross-sectional regression model with the addition of a dummy variable being included for every unit in the panel except for one (i.e., *n - 1* dummy variables are added to the model).

### Estimation

```stata
use "../data/charity-panel-analysis-2020-09-10.dta", clear
```

    (Contains annual accounts of charities in E&W for financial years 2006-2017)

```stata
xtreg linc orgage localc west genchar nsources govern_share, fe
```

    note: localc omitted because of collinearity
    note: west omitted because of collinearity
    note: genchar omitted because of collinearity

    Fixed-effects (within) regression               Number of obs     =     23,826
    Group variable: regno                           Number of groups  =      2,166

    R-sq:                                           Obs per group:
         within  = 0.0140                                         min =         11
         between = 0.0425                                         avg =       11.0
         overall = 0.0403                                         max =         11

                                                    F(3,21657)        =     102.28
    corr(u_i, Xb)  = -0.1002                        Prob > F          =     0.0000

    ------------------------------------------------------------------------------
            linc |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          orgage |   .0069072   .0005802    11.90   0.000       .00577    .0080444
          localc |          0  (omitted)
            west |          0  (omitted)
         genchar |          0  (omitted)
        nsources |   .0289886   .0027931    10.38   0.000     .0235139    .0344633
    govern_share |   .0010325   .0001225     8.43   0.000     .0007923    .0012727
           _cons |   14.71504    .026082   564.18   0.000     14.66392    14.76616
    -------------+----------------------------------------------------------------
         sigma_u |  .94534636
         sigma_e |   .2821005
             rho |  .91823289   (fraction of variance due to u_i)
    ------------------------------------------------------------------------------
    F test that all u_i=0: F(2165, 21657) = 120.80               Prob > F = 0.0000

**QUESTION TIME**

1. How much of the variation in the outcome is accounted for by the model? Is this a lot?
2. Why were three of the observed explanatory variables excluded in the estimation of the model?
3. What does the $\text{rho}$ statistic tell us?
4. Is there evidence of correlation between the unit-specific effects and observed explanatory variables?

### Interpretation

The effect of the observed explanatory variables is **net of** the effect of the unit-specific term. That is, we've controlled away the correlation between *X* and $\mu_{i}$.

$\text{_cons}$ is the intercept and represents the average value of the fixed effects + the overall constant.

$\text{orgage}$ is the predicted change in the outcome for a one-unit increase in organisational age.

$\text{rho}$ is the proportion of unexplained variance in the outcome explained by unobserved differences between charities (the unit-specific effects), rather than changes within them.

If $\text{rho}$ > .5 then most of the residual variation in the outcome is due to differences between units, if $\text{rho}$ < .5 then most of the residual variation is accounted for by differences within units (i.e., the effects of the explanatory variables).

$\text{corr(u_i, Xb)}$ is the correlation between the unit-specific effect and the observed explanatory variables in the model.

* $\text{sigma_u}$ (or $\sigma_u$) is the standard deviation of residuals within units.
* $\text{sigma_e}$ (or $\sigma_e$) is the standard deviation of residuals ei.
*
* $\text{R-sq: within}$ is the proportion of variance explained by the observed explanatory variables (i.e., excluding the unit-specific effect).

### Post-estimation

Though it's very rarely of substantive interest, we can recover the unit-specific effects (and other parameter estimates) after estimating a Fixed Effects model:

```stata
capture predict fixed, u
capture predict y_hat, xb
capture predict ei, e
capture predict residuals, ue
capture egen pickone = tag(regno)
```

```stata
l regno fin_year fixed if pickone in 1/100
```

         +-------------------------------+
         |  regno   fin_year       fixed |
         |-------------------------------|
      1. | 200048    2006-07   -.9911593 |
     12. | 200051    2006-07    1.341765 |
     23. | 200069    2006-07   -.4608771 |
     34. | 200222    2006-07   -.1173254 |
     45. | 200424    2006-07   -.4077182 |
         |-------------------------------|
     56. | 200431    2006-07   -.5248324 |
     67. | 200500    2006-07   -.0236679 |
     78. | 201081    2006-07    .0432656 |
     89. | 201321    2006-07   -1.400211 |
    100. | 201911    2006-07   -.7076412 |
         +-------------------------------+

```stata
l regno fin_year linc y_hat residuals fixed ei in 1/11
```

         +-----------------------------------------------------------------+
      1. |  regno | fin_year |     linc |    y_hat | residuals |     fixed |
         | 200048 |  2006-07 | 14.00189 | 15.17772 | -1.175828 | -.9911593 |
         |-----------------------------------------------------------------|
         |                                   ei                            |
         |                            -.1846687                            |
         +-----------------------------------------------------------------+

         +-----------------------------------------------------------------+
      2. |  regno | fin_year |     linc |    y_hat | residuals |     fixed |
         | 200048 |  2007-08 | 14.17788 | 15.18462 | -1.006747 | -.9911593 |
         |-----------------------------------------------------------------|
         |                                   ei                            |
         |                            -.0155877                            |
         +-----------------------------------------------------------------+

         +-----------------------------------------------------------------+
      3. |  regno | fin_year |     linc |    y_hat | residuals |     fixed |
         | 200048 |  2008-09 |  14.1851 | 15.22075 |  -1.03565 | -.9911593 |
         |-----------------------------------------------------------------|
         |                                   ei                            |
         |                            -.0444904                            |
         +-----------------------------------------------------------------+

         +-----------------------------------------------------------------+
      4. |  regno | fin_year |     linc |    y_hat | residuals |     fixed |
         | 200048 |  2009-10 |  14.2326 | 15.19844 | -.9658405 | -.9911593 |
         |-----------------------------------------------------------------|
         |                                   ei                            |
         |                             .0253188                            |
         +-----------------------------------------------------------------+

         +-----------------------------------------------------------------+
      5. |  regno | fin_year |     linc |    y_hat | residuals |     fixed |
         | 200048 |  2010-11 |  14.1709 | 15.20695 | -1.036052 | -.9911593 |
         |-----------------------------------------------------------------|
         |                                   ei                            |
         |                            -.0448926                            |
         +-----------------------------------------------------------------+

         +-----------------------------------------------------------------+
      6. |  regno | fin_year |     linc |    y_hat | residuals |     fixed |
         | 200048 |  2011-12 | 14.14801 | 15.21225 |  -1.06424 | -.9911593 |
         |-----------------------------------------------------------------|
         |                                   ei                            |
         |                            -.0730808                            |
         +-----------------------------------------------------------------+

         +-----------------------------------------------------------------+
      7. |  regno | fin_year |     linc |    y_hat | residuals |     fixed |
         | 200048 |  2012-13 |   14.376 | 15.25097 | -.8749701 | -.9911593 |
         |-----------------------------------------------------------------|
         |                                   ei                            |
         |                             .1161892                            |
         +-----------------------------------------------------------------+

         +-----------------------------------------------------------------+
      8. |  regno | fin_year |     linc |    y_hat | residuals |     fixed |
         | 200048 |  2013-14 | 14.29996 | 15.22607 | -.9261075 | -.9911593 |
         |-----------------------------------------------------------------|
         |                                   ei                            |
         |                             .0650518                            |
         +-----------------------------------------------------------------+

         +-----------------------------------------------------------------+
      9. |  regno | fin_year |     linc |    y_hat | residuals |     fixed |
         | 200048 |  2014-15 | 14.26031 |  15.2623 | -1.001989 | -.9911593 |
         |-----------------------------------------------------------------|
         |                                   ei                            |
         |                            -.0108296                            |
         +-----------------------------------------------------------------+

         +-----------------------------------------------------------------+
     10. |  regno | fin_year |     linc |    y_hat | residuals |     fixed |
         | 200048 |  2015-16 | 14.30113 | 15.23988 | -.9387508 | -.9911593 |
         |-----------------------------------------------------------------|
         |                                   ei                            |
         |                             .0524085                            |
         +-----------------------------------------------------------------+

         +-----------------------------------------------------------------+
     11. |  regno | fin_year |     linc |    y_hat | residuals |     fixed |
         | 200048 |  2016-17 | 14.37021 | 15.24679 | -.8765777 | -.9911593 |
         |-----------------------------------------------------------------|
         |                                   ei                            |
         |                             .1145816                            |
         +-----------------------------------------------------------------+

```stata
di -.9911593 + -.1846687
```

    -1.175828

```stata
tabstat fixed ei, s(mean sd) format(%5.4f)
```

       stats |     fixed        ei
    ---------+--------------------
        mean |   -0.0000   -0.0000
          sd |    0.9451    0.2690
    ------------------------------

### Benefits of Fixed Effects

Analyse change over time [Substantive]

Control for residual heterogeneity [Methodological]

(Mehmetoglu and Jakobsen, 2016)

Coefficient estimates are **consistent** if the key assumption is true.

That is, because we have controlled for the effect of unobserved time-invariant explanatory variables, our coefficients are more robust, which means increasing the sample size increases the likelihood the estimates are converging on their true values.

### Limitations of Fixed Effects

Ignores differences between units [Substantive]

Coefficient estimates are **inefficient**, especially when compared to those from a Random Effects model. As a result, standard errors tend to be larger.

Put simply, the estimates of the coefficients are based on only one source of variation (within) and thus are more uncertain.

Cannot include observed time-invariant explanatory variables [Methodological]

This is due to a very simple reason: if a value does not vary, how can it be associated with variation in the value of another variable?

It is not well suited for variables that rarely change within units.

Think carefully about variables that change little over time - how might these influence the outcome? For example, few individuals in your panel might switch from non-graduate to graduate (let's say you have a sample of older individuals). In a fixed effects model, your estimation of the effect of switching between non-graduate and graduate will be based on a small number of occurrences and care is due in interpreting the coefficient.

Cannot control for unobserved residual heterogeneity that varies over time [Methodological]

Educational ability? Natural resilience?

The last point is worth expanding on: if units differ in an unobserved way that varies over time, this will not be controlled for in the Fixed Effects model.

### Summarising the Fixed Effects model

Focuses on change over time within a unit of analysis.

Can control for the effect of unobserved time-invariant explanatory variables (residual heterogeneity).

Provides robust estimates of observed explanatory variables when said variables are correlated with unobserved effects.

However cannot include observed explanatory variables that do not vary within units.

## Summary

Both the Pooled OLS and Between Effects models provide useful information on the association between an outcome *Y* and a set of explanatory variables *X*.

Fixed Effects provide potentially different information on the association between an outcome *Y* and a set of explanatory variables *X.

Is there a way to combine the *within* and *between* perspectives?


Here are some points to remember:

• Objects share operations according to their category; for instance, strings, lists, and tuples all share sequence operations such as concatenation, length, and
indexing.

• Only mutable objects (lists, dictionaries, and sets) may be changed in-place; you
cannot change numbers, strings, or tuples in-place.

• Files export only methods, so mutability doesn’t really apply to them—their state
may be changed when they are processed, but this isn’t quite the same as Python core type mutability constraints.

• “Numbers” in Table 9-3 includes all number types: integer (and the distinct long
integer in 2.6), floating-point, complex, decimal, and fraction.

• “Strings” in Table 9-3 includes str, as well as bytes in 3.0 and unicode in 2.6; the byte array string type in 3.0 is mutable.

• Sets are something like the keys of a valueless dictionary, but they don’t map to
values and are not ordered, so sets are neither a mapping nor a sequence type; frozenset is an immutable variant of set.

• In addition to type category operations, as of Python 2.6 and 3.0 all the types in Table 9-3 have callable methods, which are generally specific to their type

```python
X = [1,2,3]
L = ['a',X,'b']  # Embed references to X's object
D = {'x':X,'y':2}
```

```python
print(X,L,D)
```

    [1, 2, 3] ['a', [1, 2, 3], 'b'] {'x': [1, 2, 3], 'y': 2}

```python
X[1] = 'surprise'  # Changes all three references!
```

```python
L, D
```

    (['a', [1, 'surprise', 3], 'b'], {'x': [1, 'surprise', 3], 'y': 2})

```python
from wordcloud import WordCloud
import PIL.Image as image

with open("output.txt",'r',encoding='utf-8') as fp:
          text=fp.read()
          #print(text)
          #将文本放入WordCoud容器对象中并分析
          WordCloud = WordCloud(width=1000,height=800).generate(text)
          image_produce = WordCloud.to_image()
          image_produce.show()
```

```python
from sympy import plot_implicit, sin, symbols, pi
x, y = symbols('x y')
my_plot = plot_implicit(x**2 + y**2 <= 1 + 0.5 * sin((x**3 + y**3) * 2 * pi))
```

```python
from __future__ import division
a,b,c = symbols('a b c')
plot2 = plot_implicit( a**2 + b**2 <= 0)
```

```python
(a**2+b**2)/(2*c) + (b**2+c**2)/(2*a) + (c**2+a**2)/(2*b)
```

$\displaystyle \frac{a^{2} + b^{2}}{2 c} + \frac{a^{2} + c^{2}}{2 b} + \frac{b^{2} + c^{2}}{2 a}$

```python
a + b + c
```

$\displaystyle a + b + c$

```python
L = [1,2,3]
D = {'a': 1, 'b': 2}
```

```python
A = L[:] # Instead of A = L (or list(L))
B = D.copy() # Instead of B = D (ditto for sets)
```

```python
A[1] = "Ni"
B['c'] = 'spam'
L, D
```

    ([1, 2, 3], {'a': 1, 'b': 2})

```python
A, B
```

    ([1, 'Ni', 3], {'a': 1, 'b': 2, 'c': 'spam'})

```python
L = [1,2,3]
D = {'a': 1, 'b': 2}
A = L
B = D
A[1] = "Ni"
B['c'] = 'spam'
print(L,D)
print(A,B)
```

    [1, 'Ni', 3] {'a': 1, 'b': 2, 'c': 'spam'}
    [1, 'Ni', 3] {'a': 1, 'b': 2, 'c': 'spam'}

```python
# Case 1
X = [1,2,3]
L = ['a',X[:],'b'] # Embed copies of X's object
D = {'x': X[:],'y':2}
X[1] = 'surprise' # It doesn't change all three references!
```

```python
print(X,L,D)
```

    [1, 'surprise', 3] ['a', [1, 2, 3], 'b'] {'x': [1, 2, 3], 'y': 2}

```python
# case 2
X = [1,2,3]
L = ['a',X,'b']
D = {'x': X, 'y': 2}
X[1] = 'surprise'
```

```python
print(X,L,D)
```

    [1, 2, 3] ['a', [1, 2, 3], 'b'] {'x': [1, 2, 3], 'y': 2}

• Slice expressions with empty limits (L[:]) copy sequences.

• The dictionary and set copy method (X.copy()) copies a dictionary or set.

• Some built-in functions, such as list, make copies (list(L)).

• The copy standard library module makes full copies.

Empty-limit slices and the dictionary copy method only make top-level copies; that is, they do not copy nested data structures, if any are present. If you need a complete, fully independent copy of a deeply nested data structure, use the standard copy module: include an import copy statement and say X = copy.deepcopy(Y) to fully copy an arbitrarily nested object Y. This call recursively traverses objects
to copy all their parts.

```python
E = {'x': X, 'y': {str(X): X}}
```

```python
E
```

    {'x': [1, 2, 3], 'y': {'[1, 2, 3]': [1, 2, 3]}}

```python
F = E.copy()
```

```python
F
```

    {'x': [1, 2, 3], 'y': {'[1, 2, 3]': [1, 2, 3]}}

```python
from sympy import symbols
import sympy
n = symbols('n')
```

```python
from itertools import product
T = product(k,(k,1,1000))
```

```python
from sympy import plot_implicit, sin, symbols, pi
import numpy

x, y = symbols('x y')
my_plot1 = plot_implicit(3*x**2 <= 6-2*y**2)
my_plot2 = plot_implicit(abs(2*x+y)<=numpy.sqrt(11))
```

```python
from chempy import Substance
ferricyanide = Substance.from_formula('Fe(CN)6-3')
ferricyanide.composition == {0: -3, 26: 1, 6: 6, 7: 6}  # 0 for charge
```

    True

```python
print(ferricyanide.unicode_name)
print(ferricyanide.latex_name + ", " + ferricyanide.html_name)
print('%.3f' % ferricyanide.mass)
```

    Fe(CN)₆³⁻
    Fe(CN)_{6}^{3-}, Fe(CN)63-
    211.955

```python
from chempy import balance_stoichiometry  # Main reaction in NASA's booster rockets:
reac, prod = balance_stoichiometry({'NH4ClO4', 'Al'}, {'Al2O3', 'HCl', 'H2O', 'N2'})
from pprint import pprint
pprint(dict(reac))
pprint(dict(prod))
from chempy import mass_fractions
for fractions in map(mass_fractions, [reac, prod]):
    pprint({k: '{0:.3g} wt%'.format(v*100) for k, v in fractions.items()})
```

    {'Al': 10, 'NH4ClO4': 6}
    {'Al2O3': 5, 'H2O': 9, 'HCl': 6, 'N2': 3}
    {'Al': '27.7 wt%', 'NH4ClO4': '72.3 wt%'}
    {'Al2O3': '52.3 wt%', 'H2O': '16.6 wt%', 'HCl': '22.4 wt%', 'N2': '8.62 wt%'}

```python
substances = {s.name: s for s in [
    Substance('pancake', composition=dict(eggs=1, spoons_of_flour=2, cups_of_milk=1)),
    Substance('eggs_6pack', composition=dict(eggs=6)),
    Substance('milk_carton', composition=dict(cups_of_milk=4)),
    Substance('flour_bag', composition=dict(spoons_of_flour=60))
]}
pprint([dict(_) for _ in balance_stoichiometry({'eggs_6pack', 'milk_carton', 'flour_bag'},
                                               {'pancake'}, substances=substances)])
```

    [{'eggs_6pack': 10, 'flour_bag': 2, 'milk_carton': 15}, {'pancake': 60}]

```python
pprint([dict(_) for _ in balance_stoichiometry({'C', 'O2'}, {'CO2', 'CO'})])  # doctest: +SKIP
```

    [{'C': x1 + 1, 'O2': x1 + 1/2}, {'CO': 1, 'CO2': x1}]

```python
pprint([dict(_) for _ in balance_stoichiometry({'C', 'O2'}, {'CO2', 'CO'}, underdetermined=None)])
```

    [{'C': 3, 'O2': 2}, {'CO': 2, 'CO2': 1}]

```python
from chempy import Equilibrium
from sympy import symbols
K1, K2, Kw = symbols('K1 K2 Kw')
e1 = Equilibrium({'MnO4-': 1, 'H+': 8, 'e-': 5}, {'Mn+2': 1, 'H2O': 4}, K1)
e2 = Equilibrium({'O2': 1, 'H2O': 2, 'e-': 4}, {'OH-': 4}, K2)
coeff = Equilibrium.eliminate([e1, e2], 'e-')
print(coeff)
redox = e1*coeff[0] + e2*coeff[1]
print(redox)
autoprot = Equilibrium({'H2O': 1}, {'H+': 1, 'OH-': 1}, Kw)
n = redox.cancel(autoprot)
print(n)
redox2 = redox + n*autoprot
print(redox2)
```

    [4, -5]
    32 H+ + 4 MnO4- + 20 OH- = 26 H2O + 4 Mn+2 + 5 O2; K1**4/K2**5
    20
    12 H+ + 4 MnO4- = 6 H2O + 4 Mn+2 + 5 O2; K1**4*Kw**20/K2**5

```python
from collections import defaultdict
>>> from chempy.equilibria import EqSystem
>>> eqsys = EqSystem.from_string("""HCO3- = H+ + CO3-2; 10**-10.3
... H2CO3 = H+ + HCO3-; 10**-6.3
... H2O = H+ + OH-; 10**-14/55.4
... """)  # pKa1(H2CO3) = 6.3 (implicitly incl. CO2(aq)), pKa2=10.3 & pKw=14
>>> arr, info, sane = eqsys.root(defaultdict(float, {'H2O': 55.4, 'HCO3-': 1e-2}))
>>> conc = dict(zip(eqsys.substances, arr))
>>> from math import log10
>>> print("pH: %.2f" % -log10(conc['H+']))
```

    pH: 8.30

```python
>>> from chempy import Equilibrium
>>> from chempy.chemistry import Species
>>> water_autop = Equilibrium({'H2O'}, {'H+', 'OH-'}, 10**-14)  # unit "molar" assumed
>>> ammonia_prot = Equilibrium({'NH4+'}, {'NH3', 'H+'}, 10**-9.24)  # same here
>>> substances = [Species.from_formula(f) for f in 'H2O OH- H+ NH3 NH4+'.split()]
>>> eqsys = EqSystem([water_autop, ammonia_prot], substances)
>>> print('\n'.join(map(str, eqsys.rxns)))  # "rxns" short for "reactions"

>>> init_conc = defaultdict(float, {'H2O': 1, 'NH3': 0.1})
>>> x, sol, sane = eqsys.root(init_conc)
>>> assert sol['success'] and sane
>>> print(', '.join('%.2g' % v for v in x))
```

    H2O = H+ + OH-; 1e-14
    NH4+ = H+ + NH3; 5.75e-10
    1, 0.0013, 7.6e-12, 0.099, 0.0013

```python
>>> from chempy.electrolytes import ionic_strength
>>> ionic_strength({'Fe+3': 0.050, 'ClO4-': 0.150}) == .3
```

    True

```python
>>> from chempy.henry import Henry
>>> kH_O2 = Henry(1.2e-3, 1800, ref='carpenter_1966')
>>> print('%.1e' % kH_O2(298.15))
```

    1.2e-03

```python
>>> from chempy import ReactionSystem  # The rate constants below are arbitrary
>>> rsys = ReactionSystem.from_string("""2 Fe+2 + H2O2 -> 2 Fe+3 + 2 OH-; 42
...     2 Fe+3 + H2O2 -> 2 Fe+2 + O2 + 2 H+; 17
...     H+ + OH- -> H2O; 1e10
...     H2O -> H+ + OH-; 1e-4""")  # "[H2O]" = 1.0 (actually 55.4 at RT)
>>> from chempy.kinetics.ode import get_odesys
>>> odesys, extra = get_odesys(rsys)
>>> from collections import defaultdict
>>> import numpy as np
>>> tout = sorted(np.concatenate((np.linspace(0, 23), np.logspace(-8, 1))))
>>> c0 = defaultdict(float, {'Fe+2': 0.05, 'H2O2': 0.1, 'H2O': 1.0, 'H+': 1e-2, 'OH-': 1e-12})
>>> result = odesys.integrate(tout, c0, atol=1e-12, rtol=1e-14)
>>> import matplotlib.pyplot as plt
>>> fig, axes = plt.subplots(1, 2, figsize=(12, 5))
>>> for ax in axes:
...     _ = result.plot(names=[k for k in rsys.substances if k != 'H2O'], ax=ax)
...     _ = ax.legend(loc='best', prop={'size': 9})
...     _ = ax.set_xlabel('Time')
...     _ = ax.set_ylabel('Concentration')
>>> _ = axes[1].set_ylim([1e-13, 1e-1])
>>> _ = axes[1].set_xscale('log')
>>> _ = axes[1].set_yscale('log')
>>> _ = fig.tight_layout()
>>> _ = fig.savefig('kinetics.png', dpi=500)
```

```python
>>> from chempy import Substance
>>> from chempy.properties.water_density_tanaka_2001 import water_density as rho
>>> from chempy.units import to_unitless, default_units as u
>>> water = Substance.from_formula('H2O')
>>> for T_C in (15, 25, 35):
...     concentration_H2O = rho(T=(273.15 + T_C)*u.kelvin, units=u)/water.molar_mass(units=u)
...     print('[H2O] = %.2f M (at %d °C)' % (to_unitless(concentration_H2O, u.molar), T_C))
...
```

    [H2O] = 55.46 M (at 15 °C)
    [H2O] = 55.35 M (at 25 °C)
    [H2O] = 55.18 M (at 35 °C)

```python
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plot
target_url = ("https://archive.ics.uci.edu/ml/machine-learning-"
"databases/undocumented/connectionist-bench/sonar/sonar.all-data")
#read rocks versus mines data into pandas data frame
rocksVMines = pd.read_csv(target_url,header=None, prefix="V")
#print head and tail of data frame
print(rocksVMines.head())
print(rocksVMines.tail())
#print summary of data frame
summary = rocksVMines.describe()
print(summary)
```

           V0      V1      V2      V3      V4      V5      V6      V7      V8  \
    0  0.0200  0.0371  0.0428  0.0207  0.0954  0.0986  0.1539  0.1601  0.3109
    1  0.0453  0.0523  0.0843  0.0689  0.1183  0.2583  0.2156  0.3481  0.3337
    2  0.0262  0.0582  0.1099  0.1083  0.0974  0.2280  0.2431  0.3771  0.5598
    3  0.0100  0.0171  0.0623  0.0205  0.0205  0.0368  0.1098  0.1276  0.0598
    4  0.0762  0.0666  0.0481  0.0394  0.0590  0.0649  0.1209  0.2467  0.3564

           V9  ...     V51     V52     V53     V54     V55     V56     V57  \
    0  0.2111  ...  0.0027  0.0065  0.0159  0.0072  0.0167  0.0180  0.0084
    1  0.2872  ...  0.0084  0.0089  0.0048  0.0094  0.0191  0.0140  0.0049
    2  0.6194  ...  0.0232  0.0166  0.0095  0.0180  0.0244  0.0316  0.0164
    3  0.1264  ...  0.0121  0.0036  0.0150  0.0085  0.0073  0.0050  0.0044
    4  0.4459  ...  0.0031  0.0054  0.0105  0.0110  0.0015  0.0072  0.0048

          V58     V59  V60
    0  0.0090  0.0032    R
    1  0.0052  0.0044    R
    2  0.0095  0.0078    R
    3  0.0040  0.0117    R
    4  0.0107  0.0094    R

    [5 rows x 61 columns]
             V0      V1      V2      V3      V4      V5      V6      V7      V8  \
    203  0.0187  0.0346  0.0168  0.0177  0.0393  0.1630  0.2028  0.1694  0.2328
    204  0.0323  0.0101  0.0298  0.0564  0.0760  0.0958  0.0990  0.1018  0.1030
    205  0.0522  0.0437  0.0180  0.0292  0.0351  0.1171  0.1257  0.1178  0.1258
    206  0.0303  0.0353  0.0490  0.0608  0.0167  0.1354  0.1465  0.1123  0.1945
    207  0.0260  0.0363  0.0136  0.0272  0.0214  0.0338  0.0655  0.1400  0.1843

             V9  ...     V51     V52     V53     V54     V55     V56     V57  \
    203  0.2684  ...  0.0116  0.0098  0.0199  0.0033  0.0101  0.0065  0.0115
    204  0.2154  ...  0.0061  0.0093  0.0135  0.0063  0.0063  0.0034  0.0032
    205  0.2529  ...  0.0160  0.0029  0.0051  0.0062  0.0089  0.0140  0.0138
    206  0.2354  ...  0.0086  0.0046  0.0126  0.0036  0.0035  0.0034  0.0079
    207  0.2354  ...  0.0146  0.0129  0.0047  0.0039  0.0061  0.0040  0.0036

            V58     V59  V60
    203  0.0193  0.0157    M
    204  0.0062  0.0067    M
    205  0.0077  0.0031    M
    206  0.0036  0.0048    M
    207  0.0061  0.0115    M

    [5 rows x 61 columns]
                   V0          V1          V2          V3          V4          V5  \
    count  208.000000  208.000000  208.000000  208.000000  208.000000  208.000000
    mean     0.029164    0.038437    0.043832    0.053892    0.075202    0.104570
    std      0.022991    0.032960    0.038428    0.046528    0.055552    0.059105
    min      0.001500    0.000600    0.001500    0.005800    0.006700    0.010200
    25%      0.013350    0.016450    0.018950    0.024375    0.038050    0.067025
    50%      0.022800    0.030800    0.034300    0.044050    0.062500    0.092150
    75%      0.035550    0.047950    0.057950    0.064500    0.100275    0.134125
    max      0.137100    0.233900    0.305900    0.426400    0.401000    0.382300

                   V6          V7          V8          V9  ...         V50  \
    count  208.000000  208.000000  208.000000  208.000000  ...  208.000000
    mean     0.121747    0.134799    0.178003    0.208259  ...    0.016069
    std      0.061788    0.085152    0.118387    0.134416  ...    0.012008
    min      0.003300    0.005500    0.007500    0.011300  ...    0.000000
    25%      0.080900    0.080425    0.097025    0.111275  ...    0.008425
    50%      0.106950    0.112100    0.152250    0.182400  ...    0.013900
    75%      0.154000    0.169600    0.233425    0.268700  ...    0.020825
    max      0.372900    0.459000    0.682800    0.710600  ...    0.100400

                  V51         V52         V53         V54         V55         V56  \
    count  208.000000  208.000000  208.000000  208.000000  208.000000  208.000000
    mean     0.013420    0.010709    0.010941    0.009290    0.008222    0.007820
    std      0.009634    0.007060    0.007301    0.007088    0.005736    0.005785
    min      0.000800    0.000500    0.001000    0.000600    0.000400    0.000300
    25%      0.007275    0.005075    0.005375    0.004150    0.004400    0.003700
    50%      0.011400    0.009550    0.009300    0.007500    0.006850    0.005950
    75%      0.016725    0.014900    0.014500    0.012100    0.010575    0.010425
    max      0.070900    0.039000    0.035200    0.044700    0.039400    0.035500

                  V57         V58         V59
    count  208.000000  208.000000  208.000000
    mean     0.007949    0.007941    0.006507
    std      0.006470    0.006181    0.005031
    min      0.000300    0.000100    0.000600
    25%      0.003600    0.003675    0.003100
    50%      0.005800    0.006400    0.005300
    75%      0.010350    0.010325    0.008525
    max      0.044000    0.036400    0.043900

    [8 rows x 60 columns]

```python
import requests
url = "https://archive.ics.uci.edu/ml/machine-learning-"
"databases/undocumented/connectionist-bench/sonar/sonar.all-data"
r = requests.get(url)
with open('2.csv','wb') as f:
    f.write(r.content)
    f.close()
```

```python
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plot
target_url = ("https://archive.ics.uci.edu/ml/machine-learning-"
"databases/undocumented/connectionist-bench/sonar/sonar.all-data")
#read rocks versus mines data into pandas data frame
rocksVMines = pd.read_csv(target_url,header=None, prefix="V")
for i in range(208):
    #assign color based on "M" or "R" labels
    if rocksVMines.iat[i,60] == "M":
        pcolor = "red"
    else:
        pcolor = "blue"
        #plot rows of data as if they were series data
        dataRow = rocksVMines.iloc[i,0:60]
        dataRow.plot(color=pcolor)
plot.xlabel("Attribute Index")
plot.ylabel(("Attribute Values"))
plot.show()
```

```python
url1 = 'https://haier.ceping.com/Login/Elink?elink=RopfZpaoU63RmcBFDcPl1iIJ4jIYxZou39p1886LYXPfE0rnyFCU/2lWmTWBv5Zidl8EGSLHQjU=&v=1'
url2 = 'https://haier.ceping.com/Login/Elink?elink=RopfZpaoU63RmcBFDcPl1iIJ4jIYxZou39p1886LYXPfE0rnyFCU/2lWmTWBv5Zidl8EGSLHQjU=&v=1'
```

```python
url1 == url2
```

    True

```python
# 导入扩展库
import re # 正则表达式库
import collections # 词频统计库
import numpy as np # numpy数据处理库
import jieba # 结巴分词
import wordcloud # 词云展示库
from PIL import Image # 图像处理库
import matplotlib.pyplot as plt # 图像展示库

# 读取文件
fn = open('1.txt',encoding='utf-8') # 打开文件
string_data = fn.read() # 读出整个文件
fn.close() # 关闭文件

# 文本预处理
pattern = re.compile(u'\t|\n|\.|-|:|;|\)|\(|\?|"') # 定义正则表达式匹配模式
string_data = re.sub(pattern, '', string_data) # 将符合模式的字符去除

# 文本分词
seg_list_exact = jieba.cut(string_data, cut_all = False) # 精确模式分词
object_list = []
remove_words = [u'的', u'，',u'和', u'是', u'随着', u'对于', u'对',u'等',u'能',u'都',u'。',u' ',u'、',u'中',u'在',u'了',
                u'通常',u'如果',u'我们',u'需要'] # 自定义去除词库

for word in seg_list_exact: # 循环读出每个分词
    if word not in remove_words: # 如果不在去除词库中
        object_list.append(word) # 分词追加到列表

# 词频统计
word_counts = collections.Counter(object_list) # 对分词做词频统计
word_counts_top10 = word_counts.most_common(10) # 获取前10最高频的词
print (word_counts_top10) # 输出检查
backgroud_Image = plt.imread('2.jpg')

# 词频展示
mask = np.array(Image.open('2.jpg')) # 定义词频背景
wc = wordcloud.WordCloud(
    font_path='C:/Windows/Fonts/simhei.ttf', # 设置字体格式
    background_color='white',# 设置背景颜色
    mask=backgroud_Image,# 设置背景图片
    max_words=1000, # 最多显示词数
    max_font_size=100, # 字体最大值
    width = 10000,
    height = 10000
)

wc.generate_from_frequencies(word_counts) # 从字典生成词云
image_colors = wordcloud.ImageColorGenerator(mask) # 从背景图建立颜色方案
wc.recolor(color_func=image_colors) # 将词云颜色设置为背景图方案
plt.imshow(wc) # 显示词云
plt.axis('off') # 关闭坐标轴
plt.show() # 显示图像
```

```python
import pickle
from os import path
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
text = ''
with open('1.txt', 'r', encoding='utf-8') as fin:
    for line in fin.readlines():
        line = line.strip('\n')
        # sep’.join（seq）以sep作为分隔符，将seq所有的元素合并成一个新的字符串
        text += ' '.join(jieba.cut(line))
backgroud_Image = plt.imread('2.jpg')
print('加载图片成功！')
'''设置词云样式'''
wc = WordCloud(
    background_color='white',# 设置背景颜色
    mask=backgroud_Image,# 设置背景图片
    font_path='C:\Windows\Fonts\STZHONGS.TTF',  # 若是有中文的话，这句代码必须添加，不然会出现方框，不出现汉字
    max_words=2000, # 设置最大现实的字数
    stopwords=STOPWORDS,# 设置停用词
    max_font_size=75,# 设置字体最大值
    random_state=30,# 设置有多少种随机生成状态，即有多少种配色方案
    width = 10000,
    height = 10000
)
wc.generate_from_text(text)
print('开始加载文本')
#改变字体颜色
img_colors = ImageColorGenerator(backgroud_Image)
#字体颜色为背景图片的颜色
wc.recolor(color_func=img_colors)
# 显示词云图
plt.imshow(wc)
# 是否显示x轴、y轴下标
plt.axis('off')
plt.show()
# 获得模块所在的路径的
print('生成词云成功!')
```

    Building prefix dict from the default dictionary ...
    Dumping model to file cache C:\Users\NickC\AppData\Local\Temp\jieba.cache
    Loading model cost 0.898 seconds.
    Prefix dict has been built successfully.

    加载图片成功！
    开始加载文本

    生成词云成功!

# Comparisons, Equality and Truth

```python
L1 = [1,('a',3)] # Same value, unique objects
L2 = [1,('a',3)]
L1 == L2, L1 is L2 # Equivalent? Same object ?
```

    (True, False)

```python
L = ['grail']
L.append(L)
print(L)
```

    ['grail', [...]]

#### The == operator tests value equivalence. Python performs an equivalence test, comparing all nested objects recursively.

#### The is operator tests object identity. Python tests whether the two are really the same object (i.e., live at the same address in memory).

```python
S1 = 'spam'
S2 = 'spam'
```

```python
S1 == S2, S1 is S2
```

    (True, True)

Here, we should again have two distinct objects that happen to have the same value:
== should be true, and is should be false. But because Python internally caches and
reuses some strings as an optimization, there really is just a single string 'spam' in
memory, shared by S1 and S2; hence, the is identity test reports a true result. To trigger
the normal behavior, we need to use longer strings:

```python
S1 = 'a longer string'
S2 = 'a longer string'
S1 == S2, S1 is S2
```

    (True, False)

```python
L1 = [1, ('a', 3)]
L2 = [1,('a', 2)]
L1 < L2, L1 == L2, L1 > L2
```

    (False, False, True)

In general, Python compares types as follows:

• Numbers are compared by relative magnitude.

• Strings are compared lexicographically, character by character ("abc" < "ac").

• Lists and tuples are compared by comparing each component from left to right.

• Dictionaries compare as equal if their sorted (key, value) lists are equal. Relative magnitude comparisons are not supported for dictionaries in Python 3.0, but they work in 2.6 and earlier as though comparing sorted (key, value) lists.

• Nonnumeric mixed-type comparisons (e.g., 1 < 'spam') are errors in Python 3.0.

They are allowed in Python 2.6, but use a fixed but arbitrary ordering rule. By proxy, this also applies to sorts, which use comparisons internally: nonnumeric mixed-type collections cannot be sorted in 3.0.

# Python 3.0 Dictionary Comparisons

```python
D1 = {'a': 1, 'b': 2}
D2 = {'a': 1, 'b': 3}
print(D1 == D2)
# print( D1 < D2 )
```

    False

```python
# D1 < D2 # In Python 3.0, magnitude comparisons for dictionaries are removed because they incur too much overhead when equality is desired (equality uses an optimized scheme in 3.0 that doesn’t literally compare sorted key/value lists). The alternative in 3.0 is to either write loops to compare values by key or compare the sorted key/value lists manually— the items dictionary methods and sorted built-in suffice
# '<' not supported between instances of 'dict' and 'dict'
```

```python
list(D1.items())
sorted(D1.items())
print(sorted(D1.items()) < sorted(D2.items()))
print(sorted(D1.items()) < sorted(D2.items()))
```

    True
    True

# The None object

```python
L = [None]*100
```

```python
print(L,end='')
```

    [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]

Keep in mind that None does not mean “undefined.” That is, None is something, not
nothing (despite its name!)—it is a real object and piece of memory, given a built-in
name by Python. Watch for other uses of this special object later in the book; it is also
the default return value of functions, as we’ll see in Part IV

# The bool type

```python
bool(1)
bool('spam')
bool({})
```

    False

Python also provides a bool builtin function that can be used to test the Boolean value of an object (i.e., whether it is
True—that is, nonzero or nonempty)

# Type Objects

```python
type([1]) == type([]) # Type of another list
```

    True

```python
type([1]) == list # list type name
```

    True

```python
isinstance([1],list) # List or customization thereof
```

    True

```python
import types
def f(): pass
type(f) == types.FunctionType
```

    True

# Other Types in Python

```python
L = [1,2,3]
M = ['X',L,'Y']
M
L[1] = 0 # Changes M too
M
```

    ['X', [1, 0, 3], 'Y']

```python
L = [1,2,3]
M = ['X',L[:],'Y'] # Embed a copy of L
L[1] = 0 # Changes only L, not M
print(L)
print(M)
```

    [1, 0, 3]
    ['X', [1, 2, 3], 'Y']

# Repetition Adds One Level Deep

```python
L = [4,5,6]
X = L * 4 # Like [4,5,6] + [4,5,6] + ...
```

```python
Y = [L] * 4
```

```python
(X,Y)
```

    ([4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6],
     [[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]])

```python
L[1] = 0 # Impacts Y but not X
print(X, Y)
```

    [4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6] [[4, 0, 6], [4, 0, 6], [4, 0, 6], [4, 0, 6]]

# Beware of Cyclic Data Structures

```python
L = ['grail'] # Append references to same object
L.append(L) # Generates cycle in object: [...]
print(L)
```

    ['grail', [...]]

# Immutable Types Can’t Be Changed In-Place

```python
T = (1,2,3)
# T[2] = 4 # Error！
T = T[:2] + (4,) # Ok: (1,2,4)
```

```python
T
```

    (1, 2, 4)

# Chapter Summary

This chapter explored the last two major core object types—the tuple and the file. We
learned that tuples support all the usual sequence operations, have just a few methods,
and do not allow any in-place changes because they are immutable. We also learned
that files are returned by the built-in open function and provide methods for reading
and writing data. We explored how to translate Python objects to and from strings for
storing in files, and we looked at the pickle and struct modules for advanced roles
(object serialization and binary data). Finally, we wrapped up by reviewing some properties common to all object types (e.g., shared references) and went through a list of
common mistakes (“gotchas”) in the object type domain.
In the next part, we’ll shift gears, turning to the topic of statement syntax in Python—
we’ll explore all of Python’s basic procedural statements in the chapters that follow.
The next chapter kicks off that part of the book with an introduction to Python’s general
syntax model, which is applicable to all statement types. Before moving on, though,
take the chapter quiz, and then work through the end-of-part lab exercises to review
type concepts. Statements largely just create and process objects, so make sure you’ve
mastered this domain by working through all the exercises before reading on.

# Test Your Knowledge: Quiz

```python
T = (4,5,6)
```

```python
len(T)
```

    3

```python
L = list(T)
```

```python
L[0]=1
```

```python
T = tuple()
```

```python

```


## Nuclear Environmental Incidents

## Learning Objectives

After this section, the student will be able to:

- Discuss the history of man-made environmental disasters.
- List signficant nuclear related environmental disasters.
- Describe the timeline of events at TMI, Chernobyl, and Fukushima.
- Understand the scope and magnitude of environmental impacts from such accidents.
- Compare the relative magnitudes of accidents at Chernobyl, TMI, and Fukushima.

- Recall decay chains and the population equation.
- List key isotopes of concern and their biological impacts.
- Apply your understanding of decay and the population equation to decay and in-growth of radionuclide contaminants from a source term.

- Understand cleanup efforts and safety standards.
- Understand the relationship between stress and death.

## Worst Man Made Environmental Disasters?

```python
from IPython.display import IFrame
IFrame(src="https://embed.polleverywhere.com/free_text_polls/D5B05A34g3E3kq6?controls=none&short_poll=true", width="100%", height="700")
```

[//]: # (1940s - 1970s. Chemical company and state of New York dispose of chemicals, elementary school built on top. Love Canal)

[//]: # (2-3 December 1984, Bhopal, thousands of deaths, hundreds of thousands of injuries)

[//]: # (26 April 1986, Chernobyl, 28 people dead from direct radiation exposure, hundreds of radiation injuries to workers, thousands of cases of thyroid cancer)

[//]: # (1989, Exxon Valdez oil spill, 41,000–119,000 m3 oil spilled, 11,000 square miles of ocean)

[//]: # (1991, Kuwaiti Oil Fires, Iraqi military forces set fire to more than 600, possibly more than 700, oil wells)

[//]: # (2008, TVA Kingston Fossil Plant Coal Ash Slurry. 1.7 million cubic yards of coal ash. lead and thallium)

[//]: # (2010 Deepwater Horizon, 11 people dead, leaked for 87 days,  780,000 cubic meters of oil spilled. 2,500 to 68,000 sq mi of ocean)

[//]: # (March 2011, Fukushima Daiichi, none dead due to direct radiation impact, 131 I, 137 Cs releases in the PBq.)

### Superfund Sites

```python

```

## Three Mile Island

### Effects of TMI

> The approximately 2 million people around TMI-2 during the accident are estimated to have received an average radiation dose of only about 1 millirem above the usual background dose. To put this into context, exposure from a chest X-ray is about 6 millirem and the area's natural radioactive background dose is about 100-125 millirem per year for the area. The accident's maximum dose to a person at the site boundary would have been less than 100 millirem above background. -- [http://www.nrc.gov/reading-rm/doc-collections/fact-sheets/3mile-isle.html](http://www.nrc.gov/reading-rm/doc-collections/fact-sheets/3mile-isle.html)

## Chernobyl

The following timeline is adapted from [WNA](http://www.world-nuclear.org/information-library/safety-and-security/safety-of-plants/appendices/chernobyl-accident-appendix-1-sequence-of-events.aspx) .

### 25 April 1986.

- 01:06	Scheduled shutdown started.
- 03:47	Lowering of reactor power halted at 1600 MW(thermal).
- 14:00	(ECCS) was isolated (part of the test procedure) to prevent it from interrupting the test later.
- 23:10	Power reduction recommenced.
- 24:00	Shift change.

### April 26

- 00:05	Power level had been decreased to 720 MWt and continued to be reduced although INSAG-1 stated that operation below 700 MWt was forbidden.
- 00:28	Power at 500 MWt, control transferred to the automatic regulating system. Operator or equipment failed to halt power drop, led to unexpected fall in power, to 30 MWt.
- 00:43:27	Turbogenerator trip signal blocked in accordance with operational and test procedures.
- 01:00	The reactor power had risen to 200 MWt and stabilised. Although the operators may not have known it, the required operating reactivity margin (ORM) of 15 rods had been violated (Positive void coefficient). The decision was made to carry out the turbogenerator rundown tests at a power level of about 200 MWt.
- 01:03	Standby circulation pump was switched into the left hand cooling circuit.
- 01:07	An additional cooling pump was switched into the right hand cooling circuit --> reduced reactivity but problems in the level of steam drum due to overworking pumps.
- 01:19 (approx.)	The steam drum level was still near the emergency level. To compensate, the operator increased feedwater flow. Raised drum level, reduced reactivity, the automatic control rods went up to the upper tie plate to compensate but further withdrawal of manual rods was required to maintain the reactivity balance. System pressure began to fall and, to stabilise pressure, the steam turbine bypass valve was shut off. Since the operators were having trouble with the pressure and level control, **they deactivated the automatic trip systems to the steam drum around this time.**
- 01:23 (approx.)	Reactor parameters stabilised. The unit shift supervisors considered that preparations for the tests had been completed and, having switched on the oscilloscope, gave the order to close the emergency stop valves.
- 01:23:04	Turbine feed valves closed to start turbine coasting. This was the beginning of the actual test. According to Annex I of INSAG-7, for the following approximately 30 seconds of rundown of the four coolant pumps, "the parameters of the unit were controlled, remained within the limits expected for the operating conditions concerned, and did not require any intervention on the part of the personnel."
- 01:23:40	The emergency button (AZ-5) was pressed by the operator. Control rods started to enter the core, increasing the reactivity at the bottom of the core.

- 01:23:43	Power excursion rate emergency protection system signals on; power exceeded 530 MWt.
- 01:23:46	Disconnection of both pairs of pumps (test procedure)
- 01:23:47	Reductions in flow rates, increases in pressure.
- 01:23:48	Restoration of some flow rates of MCPs; further increase of pressure in the steam separator drums and of water level in the steam separator drums; triggering of fast acting systems for dumping of steam to condensers.
- 01:23:49	Emergency protection signal 'Pressure increase in reactor space (rupture of a fuel channel)'; 'No voltage - 48 V' signal (no power supply to the servodrive mechanisms of the EPS); 'Failure of the actuators of automatic power controllers Nos 1 and 2' signals.
- 01:24	From a note in the chief reactor control engineer's operating log: "01:24: Severe shocks; the RCPS rods stopped moving before they reached the lower limit stop switches; power switch of clutch mechanisms is off."
- The two main explosions destroyed the core of Unit 4 and the roof of the reactor building.
- 100s of firefighters control building fire
- Graphite fire continues for days.

### 28 April 1986
- A radiation monitor at Forsmark (Sweden) alarmed. A worker had picking up ground contamination on his morning walk into work.

### Effects
- Two worker deaths that night
- 6 firefighters
- 22 plant staff
- 5% of the radioactive reactor core into the atmosphere and downwind – some 5200 PBq (I-131 eq).
- hundreds of thousands of people relocated from the immediate area, including parts of Belarus.

## Fukushima Accident Sequence

- The Great East Japan Earthquake occurred on 11 March 2011
  - 9.0 magnitude earthquake
  - resulting tsunami struck coastal Japan, many waves higher than 10m
  - More than 15 000 people were killed
  - over 6000 injured
  - approximately 2500 people still reported to be missing

- Fukushima Daiichi (sequence from Corradiini et al, plus IAEA Director General's Report)
  - Units 1-3 were in operation prior to the event
  - Units 4-6 were in outage
  - 14:46 Japan Standard Time (JST)
  - Reactors were shut down based on detection of seismic activity
  - Earthquake resulted in the loss of offsite power due to transmission line damage.
  - Emergency Diesel Generators powered emergency cooling systems.
  - An hour later, the station was struck by the tsunami. The tsunami took out all multiple sets of the Emergency Diesel generator, AC buses, DC batteries (U1) and damaged service water that provide heat rejection to the sea.
  - Delayed cooling caused substantial fuel damage as portable power supplies and pumps were being brought on-site to re-establish cooling with fresh & seawater.
  -  Containments leakage (U1-3) occurred as fuel cladding oxidized and hydrogen released from these processes combusted in the surrounding buildings
  -  Spent fuel pools didn’t suffer direct damage although it was incorrectly assumed

> As a reference, the global pre-accident deposition of 137 Cs as of 1970 is estimated at 290 ± 30 PBq and the typical (background) level of 137 Cs in the North Pacific Ocean was approximately 69 PBq [187, 188]. -- IAEA Dir. Gen. Rep.

### Task: Google Images of "Fukushima Nuclear Disaster"

What comes up? What is that? Is that a reactor on fire?

## Calculations

[](https://3.bp.blogspot.com/-cMi-1LwfHI4/TYohZDkiXWI/AAAAAAAAACA/qjJLSmuTVP8/s1600/fukushima_map2.png)

# Radiation Dosimetry
Much of this section relies on Turner : Atoms, Radiation, and Radiation Protection.
Here is another helpful image, for keeping track of units.

Finally, here is another useful table:

## Exposure

Ionization in the air created by gammas and X Rays. ICRP defines exposure as:

\begin{align}
X &= \frac{\Delta Q}{\Delta m}\\
\Delta Q &= \mbox{net sum of charge produced in air when electrons liberated by photons are stopped completely}\\
\Delta m &= \mbox{mass of air in which ionization occurs}\\
R &= \mbox{Roentgen}\\
& = 2.58\times 10^{-4} \left[\frac{C}{kg}\right]
\end{align}

## Absorbed Dose

Energy absorbed per unit mass from ionizing radiation in any target. There are many units.

\begin{align}
1 Gy = \frac{1 J}{kg} = \frac{10^7 erg}{10^3 g} = 100 rad
\end{align}

We can say that the energy needed to make an ion pair in the air is $W=33.97\frac{eV}{\mbox{ion pair}}= 33.97 \frac{J}{C}$. Thus, dose absorbed by the air in 1 R is :

\begin{align}
D &= \mbox{Absorbed Dose}\\
 &= X\cdot W\\
\Rightarrow D &= 1 R = \frac{2.58\times 10^{-4}C}{kg} \times \frac{33.97 J}{C} = 8.76\times10^{-3}\left[\frac{J}{kg}\right]
\end{align}

## Dose Equivalent

If dose is expressed in Gy, the dose equivalent is expressed in Sv. If dose is expressed in the older unit of rad, the older unit, rem, is used for dose equivalent.

Thus:

\begin{align}
1 Gy = 100 rad \Rightarrow 1Sv = 100rem
\end{align}

Absorbed dose is different for different kinds of radiation. We describe this as the Linear Energy Transfer of different types of radiation. The "quality factor" was once the ICRP factor differentiating dose equivalent due to LET.

\begin{align}
H &= \mbox{dose equivalent}\\
 &= QD\\
 \mbox{where }&\\
Q &= \mbox{Quality Factor, depends on LET [-]}\\
\end{align}

(Figure above from Turner)

However, Q is no longer used. It has been replaced by weighting factor such that:

\begin{align}
H = w_RD
\end{align}

- (1991). "1990 Recommendations of the International Commission on Radiological Protection". Annals of the ICRP 21 (1-3). Retrieved on 17 May 2012.
- (2007). "The 2007 Recommendations of the International Commission on Radiological Protection". Annals of the ICRP 37 (2-4). Retrieved on 17 May 2012.

| Radiation | Radiation Energy Weighting Factor $W_R$ (formerly Q)            |
|:-----------:|:-----------------------------------------------------------:|
| x-rays, gammas, betas, muons | $1$ |
| neutrons (< 1 MeV)                  | $2.5 + 18.2\cdot e^{-\frac{[ln(E)]^2}{6}}$ |
| neutrons (1 - 50 MeV) | $5.0 + 17.0\cdot e^{-\frac{[ln(2\cdot E)]^2}{6}}$ |
| neutrons (> 50 MeV)      | $2.5 + 3.25\cdot e^{-\frac{[ln(0.04\cdot E)]^2}{6}}$  |
| protons, charged pions | 2 |
| alphas, fission products, heavy nuclei | 20 |

## Example
A worker receives a whole-body dose of 0.1mGy from 2MeV neutrons. Using the table above, estimate the dose equivalent.

```python
import math
d = 0.1 # mGy

def q(e):
    exponent = -pow(math.log(2*e), 2)/6
    return 5.0 + 17.0*math.exp(exponent)

print("dose equivalent = ", d*q(2), " [mSv]")
```

    dose equivalent =  1.7340806029467286  [mSv]

So, this has all been review. Recall this image we saw a while back:

Recall also:

Epidemiological studies vary on the factors for both dose conversion and risk from dose from various sources. There is a nice recent study on what we know. [http://www.ncbi.nlm.nih.gov/pubmed/27334644](http://www.ncbi.nlm.nih.gov/pubmed/27334644)

```python
from pyne import data
print("data.Bq_per_Ci= ", data.Bq_per_Ci)
print("data.Ci_per_Bq= ", data.Ci_per_Bq)

```

    data.Bq_per_Ci=  37000000000.0
    data.Ci_per_Bq=  2.7027027e-11

    /Users/huff/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
      """Entry point for launching an IPython kernel.
    /Users/huff/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: QAWarning: pyne.data is not yet QA compliant.
      return f(*args, **kwds)

```python
data.inhale_dose?
```

```python
print("data.inhale_dose('129I') =",data.inhale_dose('129I'))
print("data.inhale_dose('137Cs') =", data.inhale_dose('137Cs'))
print("data.inhale_dose('134Cs') =", data.inhale_dose('134Cs'))
```

    data.inhale_dose('129I') = 0.000174
    data.inhale_dose('137Cs') = 3.19e-05
    data.inhale_dose('134Cs') = 4.63e-05

```python
data.ingest_dose?
```

```python
print("data.ingest_dose('129I') =",data.ingest_dose('129I'))
print("data.ingest_dose('137Cs') =", data.ingest_dose('137Cs'))
print("data.ingest_dose('134Cs') =", data.ingest_dose('134Cs'))
```

    data.ingest_dose('129I') = 0.000276
    data.ingest_dose('137Cs') = 5e-05
    data.ingest_dose('134Cs') = 7.33e-05

```python

```

## Recall Yields

## Consider Decay

\begin{align}
\frac{dN_i}{dt} &= \sum_{m=1}^{M}l_{im}\lambda_mN_m + \phi\sum_{m=1}^{M}f_{im}\sigma_mN_m - (\lambda_i + \phi\sigma_i + r_i - c_i)N_i + F_i\Big|_{i\in [1,M]}\\
\end{align}
\begin{align}
N_i &= \mbox{atom density of nuclide i}\\
M &= \mbox{number of nuclides}\\
l_{im} &= \mbox{fraction of decays of nuclide m that result in formation of nuclide i}\\
\lambda_i &= \mbox{radioactive decay constant of nuclide i}\\
\phi &= \mbox{neutron flux, averaged over position and energy}\\
f_{im} &= \mbox{fraction of neutron absorption by nuclide m leading to the formation of nuclide i}\\
\sigma_m &= \mbox{average neutron absorption cross section of nuclide m}\\
r_i &= \mbox{continuous removal rate of nuclide i from the system}\\
c_i &= \mbox{continuous feed rate of nuclide i}\\
F_i &= \mbox{production rate of nuclide i directly from fission}\\
\end{align}

But, we're not talking about fission production after the initial release, usually, just decay, daughters, and some removal from cleanup.

\begin{align}
\frac{dN_i}{dt} &= \sum_{m=1}^{M}l_{im}\lambda_mN_m - (\lambda_i + r_i - c_i)N_i\\
\end{align}
\begin{align}
N_i &= \mbox{atom density of nuclide i}\\
M &= \mbox{number of nuclides}\\
l_{im} &= \mbox{fraction of decays of nuclide m that result in formation of nuclide i}\\
\lambda_i &= \mbox{radioactive decay constant of nuclide i}\\
r_i &= \mbox{continuous removal rate of nuclide i from the system}\\
c_i &= \mbox{continuous feed rate of nuclide i}\\
\end{align}

## 131I

\begin{align}
\tau_{1/2} &= 8.02 [d]\\
\end{align}

- TMI : $20 Ci = 7.4\times10^{11} Bq = 7.4\times 10^{-4}PBq$
- Fukushima: $100-400 PBq$ (The direct releases and discharges of 131 I into the sea were estimated to be 10–20 PBq.)
- Chernobyl: $1760 PBq$

```python
# If we talk about this in Ci

print("TMI ",20, "Ci")
print("Fukushima ", (100E15)*data.Ci_per_Bq, "Ci")
print("Chernobyl ", (1760E15)*data.Ci_per_Bq, "Ci")
```

    TMI  20 Ci
    Fukushima  2702702.6999999997 Ci
    Chernobyl  47567567.519999996 Ci

## 137 Cs

\begin{align}
\tau_{1/2} &= 30.17 [y]\\
\end{align}

- TMI : N/A
- Fukushima : $7-20 PBq$ atmospheric, 1-6 PBq direct releases and discharges [into the sea].
- Chernobyl : $85 PBq$

```python
from pyne import pyne
data.dose_lung_model
data.dose_lung_model('137Cs')
s_per_d = pyne.utils.time_conv_dict['day']

isos = ['137Cs', '134Cs', '131I','129I']
for i in isos:
    print(i,
         data.half_life(i)/s_per_d,
         data.decay_children(i),
         data.ext_soil_dose(i),
         data.ext_air_dose(i))

```

    137Cs 10986.72 set([561370000]) 1530.0 3.63e-07
    134Cs 754.3143 set([541340000, 561340000]) 4240.0 1.01e-06
    131I 8.0252 set([541310000]) nan nan
    129I 5734425000.0 set([541290000]) 6.57 5.06e-09

## Banana For Scale

Banana Equivalent Dose ([BED](https://en.wikipedia.org/wiki/Banana_equivalent_dose)) is commonly accepted to be $0.1 \mu Sv$.

## Release Pathways

We have talked about the "source term" of each of these releases, but the amount that actually gets to people depends on the pathway.

### Sea Release Pathway

- source term
- flow patterns (water temperature, depth, currents, geography, weight of isotope)
- water chemistry (salinity, etc.)
- plant life (kelp)
- fish population
- fishing frequency
- fish consumption

#### Atmospheric release

- source term
- flow patterns (air temperature, weather, height of release, geography, weight of isotope)
- plants, animals, humans where deposited
- consumption of plants/animals/animal products

### Cleanup

### Dose in Children

### Dose to Workers

### Stress and Death

[http://www.nytimes.com/2015/09/22/science/when-radiation-isnt-the-real-risk.html](http://www.nytimes.com/2015/09/22/science/when-radiation-isnt-the-real-risk.html)  <--- How do you feel about this?

```python

```