|
1 | 1 | Covariance Matrix Estimation |
2 | 2 | ================================= |
3 | 3 |
|
4 | | -If the optional argument ``calc_cov=True`` is specified for :class:`~pyomo.contrib.parmest.parmest.Estimator.theta_est`, |
5 | | -parmest will calculate the covariance matrix :math:`V_{\theta}` as follows: |
6 | | - |
7 | | -.. math:: |
8 | | - V_{\theta} = 2 \sigma^2 H^{-1} |
9 | | -
|
10 | | -This formula assumes all measurement errors are independent and identically distributed with |
11 | | -variance :math:`\sigma^2`. :math:`H^{-1}` is the inverse of the Hessian matrix for an unweighted |
12 | | -sum of least squares problem. Currently, the covariance approximation is only valid if the |
13 | | -objective given to parmest is the sum of squared error. Moreover, parmest approximates the |
14 | | -variance of the measurement errors as :math:`\sigma^2 = \frac{1}{n-l} \sum e_i^2` where :math:`n` is |
15 | | -the number of data points, :math:`l` is the number of fitted parameters, and :math:`e_i` is the |
16 | | -residual for experiment :math:`i`. |
| 4 | +The uncertainty in the estimated parameters is quantified using the covariance matrix. |
| 5 | +The diagonal of the covariance matrix contains the variance of the estimated parameters. |
| 6 | +Assuming Gaussian independent and identically distributed measurement errors, the |
| 7 | +covariance matrix of the estimated parameters can be computed using the following |
| 8 | +methods which have been implemented in parmest. |
| 9 | + |
| 10 | +1. Reduced Hessian Method |
| 11 | + |
| 12 | + .. math:: |
| 13 | + V_{\boldsymbol{\theta}} = 2 \sigma^2 \left(\frac{\partial^2 \text{SSE}} |
| 14 | + {\partial \boldsymbol{\theta} \partial \boldsymbol{\theta}}\right)^{-1}_{\boldsymbol{\theta} |
| 15 | + = \boldsymbol{\theta}^*} |
| 16 | +
|
| 17 | + Where SSE is the sum of squared errors, WSSE is the weighted SSE, |
| 18 | + :math:`\boldsymbol{\theta}` are the unknown parameters, :math:`\boldsymbol{\theta^*}` |
| 19 | + are the estimate of the unknown parameters, and :math:`\sigma^2` is the variance of |
| 20 | + the measurement error. When the standard deviation of the measurement error is not |
| 21 | + supplied by the user, parmest approximates the variance of the measurement error as |
| 22 | + :math:`\sigma^2 = \frac{1}{n-l} \sum e_i^2` where :math:`n` is the number of data |
| 23 | + points, :math:`l` is the number of fitted parameters, and :math:`e_i` is the residual |
| 24 | + for experiment :math:`i`. |
| 25 | + |
| 26 | +2. Finite Difference Method |
| 27 | + |
| 28 | + .. math:: |
| 29 | + V_{\boldsymbol{\theta}} = \left( \sum_{r = 1}^n \mathbf{G}_{r}^{\mathrm{T}} \mathbf{W} |
| 30 | + \mathbf{G}_{r} \right)^{-1} |
| 31 | +
|
| 32 | + This method uses central finite difference to compute the Jacobian matrix, |
| 33 | + :math:`\mathbf{G}_{r}`, which is the sensitivity of the measured variables with |
| 34 | + respect to the parameters, `\boldsymbol{\theta}`. :math:`\mathbf{W}` is a diagonal |
| 35 | + matrix containing the inverse of the variance of the measurement errors, |
| 36 | + :math:`\sigma^2`. |
| 37 | + |
| 38 | +3. Automatic Differentiation Method |
| 39 | + |
| 40 | + .. math:: |
| 41 | + V_{\boldsymbol{\theta}} = \left( \sum_{r = 1}^n \mathbf{G}_{\text{kaug},\, r}^{\mathrm{T}} |
| 42 | + \mathbf{W} \mathbf{G}_{\text{kaug},\, r} \right)^{-1} |
| 43 | +
|
| 44 | + This method uses the model optimality (KKT) condition to compute the Jacobian matrix, |
| 45 | + :math:`\mathbf{G}_{\text{kaug},\, r}`. |
| 46 | + |
| 47 | +In parmest, the covariance matrix can be calculated after defining the |
| 48 | +:class:`~pyomo.contrib.parmest.parmest.Estimator` object and estimating the unknown |
| 49 | +parameters using :class:`~pyomo.contrib.parmest.parmest.Estimator.theta_est`. To |
| 50 | +estimate the covariance matrix, call |
| 51 | +:class:`~pyomo.contrib.parmest.parmest.Estimator.cov_est` and pass it the number |
| 52 | +of data points, e.g., |
| 53 | + |
| 54 | +.. testsetup:: * |
| 55 | + :skipif: not __import__('pyomo.contrib.parmest.parmest').contrib.parmest.parmest.parmest_available |
| 56 | + |
| 57 | + # Data |
| 58 | + import pandas as pd |
| 59 | + data = pd.DataFrame( |
| 60 | + data=[[1, 8.3], [2, 10.3], [3, 19.0], |
| 61 | + [4, 16.0], [5, 15.6], [7, 19.8]], |
| 62 | + columns=['hour', 'y'], |
| 63 | + ) |
| 64 | + num_data = len(data) |
| 65 | + |
| 66 | + # Create an experiment list |
| 67 | + from pyomo.contrib.parmest.examples.rooney_biegler.rooney_biegler import RooneyBieglerExperiment |
| 68 | + exp_list = [] |
| 69 | + for i in range(data.shape[0]): |
| 70 | + exp_list.append(RooneyBieglerExperiment(data.loc[i, :])) |
| 71 | + |
| 72 | +.. doctest:: |
| 73 | + :skipif: not __import__('pyomo.contrib.parmest.parmest').contrib.parmest.parmest.parmest_available |
| 74 | + |
| 75 | + >>> import pyomo.contrib.parmest.parmest as parmest |
| 76 | + >>> pest = parmest.Estimator(exp_list, obj_function="SSE") |
| 77 | + >>> obj_val, theta_val = pest.theta_est() |
| 78 | + >>> cov = pest.cov_est(cov_n=num_data) |
| 79 | + |
| 80 | +Optionally, one of the three methods; "reduced_hessian", "finite_difference", |
| 81 | +and "automatic_differenciation_kaug" can be supplied for the covariance calculation, |
| 82 | +e.g., |
| 83 | + |
| 84 | +.. doctest:: |
| 85 | + :skipif: not __import__('pyomo.contrib.parmest.parmest').contrib.parmest.parmest.parmest_available |
| 86 | + |
| 87 | + >>> cov_method = "reduced_hessian" |
| 88 | + >>> cov = pest.cov_est(cov_n=num_data, method=cov_method) |
0 commit comments