> For the complete documentation index, see [llms.txt](https://zavalab.gitbook.io/statschemebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zavalab.gitbook.io/statschemebook/chapter-1-introduction/01-motivation.md).

# Motivation

{% hint style="success" %}
**Slides:** [stats\_ch1\_intro.pdf](https://github.com/vzavala/StatsBook/blob/master/Slides/stats_ch1_intro.pdf) · [4-up handout](https://github.com/vzavala/StatsBook/blob/master/Slides/handouts/stats_ch1_intro_4up.pdf) · **Code:** [Python notebooks](https://github.com/vzavala/StatsBook/tree/master/Python) · [MATLAB scripts](https://github.com/vzavala/StatsBook/tree/master/Matlab) · [MATLAB live scripts](https://github.com/vzavala/StatsBook/tree/master/Matlab-Live)
{% endhint %}

As engineers, we often use *fundamental laws* to make *decisions*:

{% hint style="info" %}

* Discovery of fundamental laws has been the result of extensive collection and analysis of observations (data)
* Fundamental law is often expressed in the form of a mechanistic model
* Mechanistic model provides a concise summary of observations (knowledge) that allows us to predict and generalize
  {% endhint %}

{% hint style="info" %}
Can you think of fundamental laws used in chemical engineering?
{% endhint %}

Fundamental laws are powerful but only provide limited descriptions of phenomena:

{% hint style="info" %}

* Laws are applicable under specific settings (e.g., continuum vs. atomistic)
* Discovering laws and new mechanistic models might be challenging or cost-prohibitive (e.g., climate)
  {% endhint %}

To account for this, we also often build predictive models based purely on observations (data); such models are known as *empirical models* and also embed knowledge.

Engineering decisions rely on a combination of mechanistic and empirical knowledge.

{% hint style="info" %}
Can you think of empirical models used in chemical engineering?
{% endhint %}

However:

{% hint style="info" %}

* Model predictions will *always* face a certain degree of *uncertainty* (due to limited knowledge/understanding/data or due to *random* phenomena affecting systems).
* Despite these limitations, we still need to be able to *make decisions*. In fact, we (as humans) make predictions and decisions in our daily lives.
* The human brain naturally gathers knowledge (learns) from observations by building empirical models and has the ability to blend such empirical knowledge with mechanistic knowledge.
* The human brain has a natural ability to hedge against uncertainty and to adapt decisions based on new knowledge.
  {% endhint %}

{% hint style="info" %}
**Examples of random phenomena:** time to failure of a material, “pop” time of a corn kernel, molecular fluctuations.
{% endhint %}

{% hint style="info" %}
**Examples of brain learning:** riding a bicycle, cooking.
{% endhint %}

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-6348e9a97720a5c4f5025ce5a2d16d385b0e45ee%2Fphen_mech_rand.png?alt=media" alt="" width="630"><figcaption></figcaption></figure>

Decision-making relies on the ability to characterize what is known (predictable) and not known (not predictable).

*Statistics* is the branch of mathematics that offers tools to:

* Collect, analyze, and extract knowledge (models) from data
* Characterize and model the unknown (uncertainty)
* Systematically make decisions in the face of uncertainty

{% hint style="info" %}
From an engineering perspective, *statistics* aids the discovery of fundamental laws and the development of mechanistic and empirical models as well as the characterization of random phenomena.
{% endhint %}

{% hint style="info" %}
From a scientific perspective, *statistics* provides a framework for thinking about the world that can help us understand how humans extract knowledge from data and use this to make decisions in the face of uncertainty.
{% endhint %}

## Example: Flow Mixer

{% hint style="danger" icon="flask" %}

* Consider a mixing system with input flows $$F\_1$$, $$F\_2$$ and output flow $$F\_3$$
* Sensors measure flows at time $$\omega$$: $$f\_1(\omega)=101.5$$, $$f\_2(\omega)=50.5$$, $$f\_3(\omega)=151$$ gpm
* Conservation laws tell us that $$f\_3(\omega)=f\_1(\omega)+f\_2(\omega)$$ (but this is not true). Why?
  {% endhint %}

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-c2025a7489654552772885e73d1f492268b14350%2Fmixing_sensor.png?alt=media" alt="" width="350"><figcaption></figcaption></figure>

{% hint style="info" %}

* What is known and unknown about this system?
* What is the source of uncertainty?
  {% endhint %}

{% hint style="danger" icon="flask" %}
Collect 100 observations and monitor mismatch $$\epsilon(\omega)=f\_3(\omega)-f\_1(\omega)-f\_2(\omega)$$
{% endhint %}

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-e866e83371628884ee49fa3184d54fa3d464b8b0%2Fch1_mixer_flows.png?alt=media" alt="" width="350"><figcaption></figcaption></figure>

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-c33eda4b6cbcdd06141457b786605bb376c12582%2Fch1_mixer_flows_error.png?alt=media" alt="" width="350"><figcaption></figcaption></figure>

#### Python implementation

{% hint style="success" %}
Notebook [`ch1_mixer_example.ipynb`](https://colab.research.google.com/github/vzavala/StatsBook/blob/master/Python/ch1_mixer_example.ipynb) — open it in Colab and run the cells alongside this section.

MATLAB live script: [`ch1_mixer_example.mlx`](https://github.com/vzavala/StatsBook/blob/master/Matlab-Live/ch1_mixer_example.mlx)
{% endhint %}

Flow mixer example (Victor Z and Ugochukwu Ikegwu, UW-Madison, 2024).

| Slides                                               | Python           | Meaning                                  |
| ---------------------------------------------------- | ---------------- | ---------------------------------------- |
| $$\omega$$                                           | `omega`          | observation (time) index                 |
| $$S$$                                                | `S`              | number of observations                   |
| $$f\_1(\omega)$$, $$f\_2(\omega)$$, $$f\_3(\omega)$$ | `f1`, `f2`, `f3` | measured flows \[gpm]                    |
| $$\epsilon(\omega)$$                                 | `epsilon`        | conservation mismatch $$f\_3-f\_1-f\_2$$ |

**Note:** This notebook reproduces Example 1.1 (Section 1.1) of *Statistics for Chemical Engineers*: three flow sensors measure the flows of a mixer, and mass conservation says that f3 = f1 + f2.

```python
import numpy as np
import matplotlib.pyplot as plt
```

Simulate $$S=100$$ sensor readings of each flow (sensor noise of about 1%) and compute the mismatch $$\epsilon(\omega)=f\_3(\omega)-f\_1(\omega)-f\_2(\omega)$$.

```python
np.random.seed(0)  # reproducibility
S = 100
omega = np.arange(S)
f1 = np.random.normal(100, 100*0.01/2.5, S)
f2 = np.random.normal(50, 50*0.01/2.5, S)
f3 = np.random.normal(150, 150*0.01/2.5, S)

epsilon = f3 - f1 - f2   # conservation mismatch
```

```python
fig, axs = plt.subplots(1, 3, figsize=(14, 4))
for ax, f_k, nominal, label in zip(axs, (f1, f2, f3), (100, 50, 150), ('f_1', 'f_2', 'f_3')):
    ax.scatter(omega, f_k, marker='o', s=20, color='white', edgecolor='black')
    ax.plot(omega, nominal * 1.01 * np.ones(S), 'k--')
    ax.plot(omega, nominal * 0.99 * np.ones(S), 'k--')
    ax.set_xlabel(r'Observation $\omega$')
    ax.set_ylabel(rf'${label}(\omega)$ [gpm]')
    ax.axis([0, S, nominal * 0.98, nominal * 1.02])
    ax.grid(True)
plt.tight_layout()
plt.show()
```

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-1830101fa83b050cdc3c5dc447e7bbcb2bbbb6e2%2Fch1_mixer_example-06-0.png?alt=media" alt="" width="520"><figcaption></figcaption></figure>

```python
plt.scatter(omega, epsilon, marker='o', s=20, color='white', edgecolor='black')
plt.plot(omega, np.zeros(S), 'k--')
plt.xlabel(r'Observation $\omega$')
plt.ylabel(r'$\epsilon(\omega)$ [gpm]')
plt.grid(True)
plt.show()
```

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-4a6f243cdaf25a38d89f6b6fabe0cd0943e4cbb9%2Fch1_mixer_example-07-0.png?alt=media" alt="" width="520"><figcaption></figcaption></figure>

**Note:** As in the book, the mismatch in mass conservation comes from random sensor errors, not from a flaw in the conservation law. Enough data is needed to see this, and both engineering knowledge (conservation) and data are needed to understand the system.

***

## Example: Gibbs Reactor

{% hint style="danger" icon="flask" %}

* Consider a reactor in which the reaction $$CO+2H\_2\leftrightarrow CH\_3OH$$ takes place
* Data seem to indicate that the reaction is favored (achieves higher conversion $$\xi$$) at high pressure ($$P$$) and low temperature ($$T$$) but this is blurred by variability.
  {% endhint %}

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-43232c44a80401648ba471fb9877a4c3e5ba39c7%2Fgibbs_diagram.png?alt=media" alt="" width="210"><figcaption></figcaption></figure>

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-a3433d5e007bdd82e182f1031ff268205abebc66%2Fch1_gibbs_data.png?alt=media" alt="" width="350"><figcaption></figcaption></figure>

{% hint style="info" %}

* What is known and unknown about this system?
* Note that trends are blurred by uncertainty/noise.
  {% endhint %}

#### Python implementation

{% hint style="success" %}
Notebook [`ch1_gibbs_data.ipynb`](https://colab.research.google.com/github/vzavala/StatsBook/blob/master/Python/ch1_gibbs_data.ipynb) — open it in Colab and run the cells alongside this section.

MATLAB live script: [`ch1_gibbs_data.mlx`](https://github.com/vzavala/StatsBook/blob/master/Matlab-Live/ch1_gibbs_data.mlx)
{% endhint %}

Data for the Gibbs reactor (Victor Z and Ugochukwu Ikegwu, UW-Madison, 2024).

| Slides  | Python | Meaning          |
| ------- | ------ | ---------------- |
| $$P$$   | `P`    | pressure \[bar]  |
| $$T$$   | `T`    | temperature \[K] |
| $$\xi$$ | `xi`   | conversion \[-]  |

**Note:** This notebook creates the reactor data of Example 1.2 (Section 1.1) of *Statistics for Chemical Engineers*; the Gibbs reactor is used throughout the book.

```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
```

Each file has columns $$(T, P, \xi)$$; we stack the high- and low-temperature data and add a little noise to blur the pattern, as in measured data.

```python
data_high = pd.read_csv(path+'Data/gibbs_hightemp_class.dat', sep="\t", header=None)
data_low = pd.read_csv(path+'Data/gibbs_lowtemp_class.dat', sep="\t", header=None)
data = pd.concat([data_high, data_low])
n = len(data)

np.random.seed(1)  # reproducibility
T = data.iloc[:, 0].to_numpy() + np.random.randn(n)*0.1    # temperature
P = data.iloc[:, 1].to_numpy() + np.random.randn(n)*0.1    # pressure
xi = data.iloc[:, 2].to_numpy() + np.random.randn(n)*0.1   # conversion
```

```python
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
axs[0].scatter(P, xi, marker='o', s=20, color='white', edgecolor='black')
axs[0].set_xlabel('$P$ [bar]')
axs[0].set_ylabel(r'$\xi$ [-]')
axs[0].grid(True)
axs[1].scatter(T, xi, marker='o', s=20, color='white', edgecolor='black')
axs[1].set_xlabel('$T$ [K]')
axs[1].set_ylabel(r'$\xi$ [-]')
axs[1].grid(True)
plt.tight_layout()
plt.show()
```

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-d3e47066bb144ac0f30ebe7112e430fb4be6f36b%2Fch1_gibbs_data-06-0.png?alt=media" alt="" width="520"><figcaption></figcaption></figure>

**Note:** As discussed in the book, data reveal trends but cannot explain them alone; a model (thermodynamics) helps, and some aspects of the system remain unknown, so new experiments may still be needed.

***

{% hint style="danger" icon="flask" %}
$$
\begin{aligned}
\mu^{out}*k&=\mu\_k+\gamma\_k\cdot \xi\cdot \mu*{CO},;k\in K\\
\xi&=1-\frac{\mu^{out}*{CO}}{\mu*{CO}}\\
K\_{eq}&=\frac{(\mu\_{CH\_3OH}+\xi\cdot \mu\_{CO})}{(\mu\_{CO}-\xi\mu\_{CO})(\mu\_{H\_2}-2\cdot \xi\cdot \mu\_{CO})^2}\left(\frac{\mu\_{out}}{P}\right)^2\\
\frac{\partial \log K\_{eq} }{\partial T}&=\frac{\Delta H\_0}{R\cdot T^2}
\end{aligned}
$$

* We know fundamental conservation and thermodynamic laws hold and the mechanistic model allows us to predict $$\xi$$ from $$P$$ and $$T$$.
* This knowledge, however, is limited (e.g., makes assumptions, cannot explain random behavior).
* How to predict $$\xi$$ as a function of $$P$$ and $$T$$ without any mechanistic knowledge?
* What are trade-offs between mechanistic and empirical knowledge?
  {% endhint %}

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-43232c44a80401648ba471fb9877a4c3e5ba39c7%2Fgibbs_diagram.png?alt=media" alt="" width="700"><figcaption></figcaption></figure>

***

## Motivation

{% hint style="info" %}
**Don't forget:**

Inherent limitation of engineering practice: no matter how sophisticated our predictive models and sensing devices are, we will *always* have a certain degree of uncertainty.

Characterizing both known and unknown aspects of a system is key.
{% endhint %}
