Exam 24/25 Q1¶

No description has been provided for this image No description has been provided for this image

CEGM1000 MUDE

Exercise 1: Programming¶

Programming part 1: Object-oriented programming¶

Consider the following piece of code and output, and reflect on the object oriented programming (OOP) that is used:

1a

A. Which of the following are OOP attributes? (You can select more than 1)

  • data
  • np
  • numpy
  • genfromtext
  • print
  • shape
  • mean
  • none of the above

Model answer

  • shape

B. which of the following are OOP methods? (You can select more than 1)?

  • data
  • np
  • numpy
  • genfromtext
  • print
  • shape
  • mean
  • none of the above

Model answer:

  • genfromtext and mean

Programming part 2: Errors¶

A friend has sent you a piece of code (not provided) that is supposed to compute the numerical solution to an ordinary differential equation using Taylor series, an iteration scheme and a numpy array. However, instead of getting the expected answer of 5, the code returns 9

C. What type of error is this? Add an explanation for your reasoning.

Model answer: Logical error.

Reasoning given: the code executes without any syntax errors or exceptions but produces an incorrect result. This indicates that the logic used to compute the numerical solution is flawed, leading to an incorrect output (9 instead of the expected 5).


Consider each of the following approaches to fixing the error:

  • Read the traceback
  • Use list comprehension
  • Try pandas instead of numpy
  • Use assert statements

D. What would be a useful approach to help figure out and solve the problem? Add an explanation for your selection.

Model answer: Use assert statements.

Reasoning: Because they allow you to verify assumptions at specific points in the code. As there is no error message or traceback, the logic of the code needs to be checked.

By placing assertions at critical steps (e.g., checking intermediate results or ensuring values remain within expected ranges), you can quickly identify where the computation diverges from expectations.


Exercise 2: Uncertainty propagation¶

Let $X_1,X_2,X_3$, be independent random variables, each following a standard normal distribution. These random variables are combined into a 3-dimensional random vector $Y=\left\lbrack Y_1,Y_2,Y_3\right\rbrack^{T}$ , defined as:

$$Y=\begin{bmatrix}X_1\\ X_2+X_3\\ X_1+X_2+X_3\end{bmatrix}$$

A. What is the expectation of $Y$ ?.

Model answer:

Linear propagation law, expectation is 0 for all the $X_i$.

$ E(Y) = [0, 0, 0]^T $


B. Apply the propagation law to show that $cov\left(Y_{1,}Y_2\right)=0$ and explain why this is the case?

Model answer:

$$Y=\begin{bmatrix}1 & 0 & 0\\ 0 & 1 & 1\\ 1 & 1 & 1\end{bmatrix}\begin{bmatrix}X_1\\ X_2\\ X_3\end{bmatrix}$$$$\Sigma_{Y}=\begin{bmatrix}1 & 0 & 0\\ 0 & 1 & 1\\ 1 & 1 & 1\end{bmatrix}\begin{bmatrix}1 & 0 & 0\\ 0 & 1 & 1\\ 1 & 1 & 1\end{bmatrix}^{T}=\begin{bmatrix}1 & 0 & 1\\ 0 & 2 & 2\\ 1 & 2 & 3\end{bmatrix}$$

The covariance between the first and second element is zero because the second element contains $X_2, X_3$ which are independent of $Y_1=X_1$


C. If the second element of 𝑌 is changed to $𝑋_2−𝑋_3$, which elements in covariance matrix of the changed vector $Y$, $\Sigma_{Y}$, will be changed? Write out the new $\Sigma_{Y}$.

Model answer:

The covariance between the second and third element will be changed. The variance of the second element will not be changed. The new matrix is shown below (not necessary to be computed), where the elements in red are changed:

$$\Sigma_Y = \begin{bmatrix} 1 & 0 & 1 \\ 0 & 2 & \textbf{0} \\ 1 & \textbf{0} & 3 \end{bmatrix}$$

Exercise 3: Observation Theory¶

An object is moving along a straight line with an unknown speed $v$. It starts at $t0 = 0 [s]$ with the known location $x_0 = 0 [m]$. We measured its location every $\Delta t$ seconds and obtained $m$ measurements $y$, where $i = [1, 2, ..., m]$ measured at $t=i * \Delta t$.

Model 1: $y_i = x_0 + vt_i$

We assume the measurements $y_i$ are independent and follow a normal distribution with standard deviation $σ_i=i ⋅ σ$


A. Specify the functional model and the covariance matrix of the observable, $Σ_Y$ , according to Model 1.

Model answer:

$$ E \begin{bmatrix} Y_1 \\ \vdots \\ Y_m \end{bmatrix} = \begin{bmatrix} \Delta t \\ \vdots \\ m \Delta t \end{bmatrix} v$$

$$\Sigma_Y = \begin{bmatrix} \sigma^2 & \cdots & 0 \\ \vdots & \ddots & \vdots \\ 0 & \cdots & m^2 \sigma^2 \end{bmatrix} $$


B.How many measurements $m$ are required if we want the 99% confidence interval to be no larger than $\hat v = 0.2[m/s]$? Assume $σ=0.4[m]$ and $∆t=2[s]$?

Model answer:

General formula for confidence interval: $\pm k\sigma_{\hat{v}}$

Approach for calculating: $\sigma_{\hat{v}} = \sqrt{A^T \Sigma_Y^{-1} A}$

$$\hat{\sigma}_v = \frac{\sigma}{\Delta t \sqrt{m}}$$$$2.576 \frac{ 0.4}{2 \sqrt{m}} < 0.2 \rightarrow m \geq 7$$

Another engineer assumes the initial position $x_0$ to be unknown, and that the movement may have an unknown acceleration $a$:

Model 2: $y_i = x_0 + vt_i + \frac{1}{2}at^2$

C. Which test should the engineer apply and what is the corresponding critical value used for significance level $α=0.05? Answer the questions without specifying the functional model of Model 2.

Model answer:

Generalized Likelihood Ratio Test.

Since model 2 has two more unknowns than model 1, the test statistic follows a central Chi-square distribution with $q=2$ degree of freedom, $\chi^2 (2, 0) $

The critical value is 5.99


Exercise 4: Numerical modelling¶

Use the Newton-Raphson method to solve for $x$ in the following equation:

$x5 + 3x = 100$

Recall that:

$x_j+1 = x_j - \frac{g(x_j)}{g′(x_j)}$

Where $g(x)=0$ and $j$ is the iteration number.

A. Write down the equation to be iterated in the Newton-Raphson method. Assume a first guess of $x=2$, then compute the second guess (i.e., one iteration).

Model answer:

This is the Newton-Raphson expression:

$$x^{j+1} = x^j - \frac{g(x^j)}{g'(x^j)}, \quad \text{where } g(x) = 0 $$

So, $$g(x) = x^5 + 3x - 100 \quad \text{and} \quad g'(x) = 5x^4 + 3. $$ The equation to be iterated is: $$x^1 = x^0 - \frac{g(x^0)}{g'(x^0)} = x - \frac{x^5 + 3x - 100}{5x^4 + 3}. $$ Substituting ( x = 2 ): $$x^1 = 2 - \frac{2^5 + 3 \cdot 2 - 100}{5 \cdot 2^4 + 3} = 2 - \frac{32 + 6 - 100}{80 + 3} = 2.747.$$


Given: $x2 − a$ and using the first 3 terms $[f(x_i) + ∆xf^′(x_i) +\cfrac{∆x^2}{2} * f^{′′}(x_i)]$, where $a$ is a constant.

B. What is the error order of the Taylor Series Expansion (TSE)?

  • There is no error
  • $O(∆x)$.
  • $O(∆x^2)$.
  • $O(∆x3)$.
  • $O(∆x^4)$.

Model answer:

  • There is no error

C. Use Forward Euler to approximate the solution of the following Initial Value Problem from $t = 0$ until $t=0.2$.

$$\frac{dy}{dt} = y + sin (t)$$$$y(t_0 = 0) = 1$$$$∆t = 0.1$$

Model answer: $$\frac{y_{i+1} - y_i}{\Delta t} = y_i + \sin(t_i) \Rightarrow y_{i+1} = y_i + \Delta t \cdot (y_i + \sin(t_i)) $$

Calculating for the first steps:

$$y_1 = 1 + 0.1 \cdot (1 + \sin(0)) = 1.1 $$

$$y_2 = 1.1 + 0.1 \cdot (1.1 + \sin(0.1)) = 1.2199 $$


D. Is Backward Euler more accurate than Forward Euler? (Y/N)

Model answer:

  • No, not inherently. It depends on what the graph looks like.

Consider the following convection equation:

$$\frac{dc}{dt} + v * \frac{dc}{dx}$$

E. Specify how many initial conditions and boundary conditions are required to solve the convection equation. Then, write the algebraic expression to approximate the first derivative of this PDE using: Central difference in space and central difference in time

Model answer: It requires 1 initial condition and 1 boundary condition. $$\frac{C_i^{j+1} - C_i^{j-1}}{2 \Delta t} = v \cdot \frac{C_{i+1}^j - C_{i-1}^j}{2 \Delta x} $$


Consider a grid with 5 nodes: $x_0, x_1,x_2,x_3, x_4$. The temperature profile on this grid is described by the discretized ODE:

$$\cfrac{1}{∆x^2}(T_{i−1} − 2T_i+ T_{i+1}) − α (T_i– T_0) = 0$$

where $α$, $∆x$ and $T_0$ are known constants. The boundary conditions are Dirichlet:

$ T (x_0) = T_0$ and $T(x_4) = T_4$

A fellow student proposes the following system to solve the problem:

$$ \begin{bmatrix} -(2 + \alpha \Delta x^2) & 1 & 0 \\ 1 & -(2 + \alpha \Delta x^2) & 1 \\ 0 & 1 & -(2 + \alpha \Delta x^2) \end{bmatrix} \begin{bmatrix}T_1\\ T_2\\ T_3\end{bmatrix} = \begin{bmatrix} \alpha T_{s} \Delta x^2\\ \alpha T_{s} \Delta x^2\\ \alpha T_{s} \Delta x^2 \end{bmatrix} $$

F. What is wrong with this proposed system. How would you correct it?

Model answer:

The BCs are not implemented and there is a minus sign missing in the vector $ \mathbf{b} $ $$\begin{bmatrix} -\alpha T_s \Delta x^2 - T_0 \\ -\alpha T_s \Delta x^2 \\ -\alpha T_s \Delta x^2 - T_4 \end{bmatrix}$$


Exercise 5: Probability and Reliability¶

A group of scientists are analyzing the influence of ice water content in the clouds [IC, kg/m2] on rainfall [RF, mm] as their theory is that it will improve the development of weather and climate models. Here you have a brief definition of the variables:

• Ice water content in the clouds [IC, kg/m2]. It is the amount of ice that you have in the clouds on the column of air above a given area. Thus, only positive values have a physical meaning.

• Rainfall [RF, mm]. Here, it is defined as the amount of rain that was accumulated during the hour when the measurement was conducted in the area used to compute IC. Again, only positive values have a physical meaning.

They have been able to conduct 10 field measurements that are shown in the table below:

[IC, kg/m2] [RF, mm]
999 10
242 8
76 7
666 4
1292 12
927 9
405 9.5
126 7
867 6
491 10

A. What do PDF and CDF stand for? Choose the right combination of words.

  • P-value Distribution Function, and Cumulative Distribution Function

  • Probability Density Function, and Cumulative Distribution Function

  • Probability Distribution Function, and Cumulative Density Function

  • Probability Density Function, and Cumulative Density Function

Model answer:

  • Probability Density Function, and Cumulative Distribution Function

B. Compute and draw the empirical CDF of the variable IC. Remember to label the axis and indicate their values. You may want to arrange your calculations in table format.

__Model answer:

[IC kg/m2] rank P[IC ≤ ic ]
999 9 0.82
242 3 0.27
76 1 0.09
666 6 0.55
1292 10 0.91
927 8 0.73
405 4 0.36
126 2 0.18
867 7 0.64
491 5 0.45

5b


C. Is the Uniform distribution a reasonable distribution for the random variable IC? Justify your answer with at least one reason.

Model answer:

Yes, it is. The above ECDF is approximately linear similar to a Uniform distribution. Also, we can compute the distance between different percentiles, such as $ d(min,P55) = 666 − 76 = 590 $ and $ d(P55,max) = 1262 − 666 = 596 $ and we can see that they are approximately the same so the empirical distribution seems to be symmetric. Also, the uniform distribution presents an upper and lower bound. In the case of the lower bound is desired although the upper bound does not represent reality.


D. Assuming that the scientists want to quantify the joint distribution function of IC and RF using a multivariate Gaussian distribution, what are the minimum parameters that they need to compute from the observations to quantify the distribution?

  • The mean, the variance and standard deviation of the each variable.

  • The mean of each variable and the correlation coefficient between them.

  • The covariance and correlation coefficient between the variables and the standard deviation of each variable.

  • The mean and variance of each variable and the covariance between them.

Model answer:

  • The mean and variance of each variable and the covariance between them.

E. Assuming that the scientists want to quantify the joint distribution function of IC and RF using a multivariate Gaussian distribution. Compute the covariance matrix of IC and RF . Do not use more than two decimal figures. You may want to make use of a table format to arrange your calculations.

Model answer:

The covariance matrix of two variables $X$ and $Y$, is given by: $$ \Sigma = \begin{pmatrix} \sigma_X^2 & \operatorname{Cov}(X, Y) \\ \operatorname{Cov}(X, Y) & \sigma_Y^2 \end{pmatrix} = \begin{pmatrix} 166,348.1 & 356 \\ 356 & 5.4 \end{pmatrix} $$ $$ \hat{\mu}_{IC} = \frac{1}{10} (999 + 242 + 76 + 666 + 1292 + 927 + 405 + 126 + 867 + 491) = 609.1 $$ $$ \hat{\mu}_{RF} = 8.25 $$ $$ \hat{\sigma}^2_{IC} = \frac{1}{9} ((999 - 609.1)^2 + (242 - 609.1)^2 + (76 - 609.1)^2 + (666 - 609.1)^2 + (1292 - 609.1)^2 + $$ $$ (927 - 609.1)^2 + (405 - 609.1)^2 + (126 - 609.1)^2 + (867 - 609.1)^2 + (491 - 609.1)^2) = 166,348.1 $$ $$ \hat{\sigma}^2_{RC} = 5.4 $$ $$ \operatorname{Cov}(X_1, X_2) = E\left[(X_1 - E(X_1))(X_2 - E(X_2))\right] = 356$$

[IC kg/m2] ICi − μIC RF RFi − μRF ICi − μIC × RFi − μRF
999 389.9 10 1.75 682.3
242 -367.1 8 -0.25 91.8
76 -533.1 7 -1.25 666.4
666 56.9 4 -4.25 -241.8
1292 682.9 12 3.75 2560.9
927 317.9 9 0.75 238.4
405 -204.1 9.5 1.25 -255.1
126 -483.1 7 -1.25 603.9
867 257.9 6 -2.25 -580.3
491 -118.1 10 1.75 -206.7

Assume that finally the scientists have changed their mind and do NOT want to quantify the joint distribution function of IC and RF using a multivariate Gaussian distribution. They have derived marginal distributions for IC, RF and the joint distribution of IC and RF as follows:

  • The marginal distribution of IC is given by the PDF $f_{IC} (ic)$ and the CDF $F_{IC} (ic)$.
  • The marginal distribution of RF is given by the PDF $f_{RF} (rf)$ and the CDF $F_{RF} (rf)$.
  • The joint probability distribution of IC and RF is given by the PDF $f_{IC,RF} (ic, rf )$ and the CDF $F_{IC,RF} (ic,rf)$

F. Using the information described ONLY in this subquestion, derive the expression for the joint exceedance probability $P [IC > ic, RF > rf ]$. You may want to draw a diagram to assist you.

Model answer:

$$P[IC > ic, RF > rf] = 1 - F_{IC}(ic) - F_{RF}(rf) + F_{IC,RF}(ic, rf) $$

You have a zero if: $$P[IC > ic, RF > rf] = 1 -P[IC < ic, RF < rf] $$