|
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: |
| 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. |
6 | 9 |
|
7 |
| -.. math:: |
8 |
| - V_{\theta} = 2 \sigma^2 H^{-1} |
| 10 | +1. Reduced Hessian Method |
9 | 11 |
|
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`. |
| 12 | + When the objective function is the sum of squared errors (SSE): |
| 13 | + :math:`\text{SSE} = \sum_{i = 1}^n \left(y_{i} - \hat{y}_{i}\right)^2`, |
| 14 | + the covariance matrix is: |
| 15 | + |
| 16 | + .. math:: |
| 17 | + V_{\boldsymbol{\theta}} = 2 \sigma^2 \left(\frac{\partial^2 \text{SSE}} |
| 18 | + {\partial \boldsymbol{\theta} \partial \boldsymbol{\theta}}\right)^{-1}_{\boldsymbol{\theta} |
| 19 | + = \boldsymbol{\theta}^*} |
| 20 | +
|
| 21 | + When the objective function is the weighted SSE (WSSE): |
| 22 | + :math:`\text{WSSE} = \frac{1}{2} \left(\mathbf{y} - f(\mathbf{x};\boldsymbol{\theta})\right)^\text{T} |
| 23 | + \mathbf{W} \left(\mathbf{y} - f(\mathbf{x};\boldsymbol{\theta})\right)`, |
| 24 | + the covariance matrix is: |
| 25 | + |
| 26 | + .. math:: |
| 27 | + V_{\boldsymbol{\theta}} = \left(\frac{\partial^2 \text{WSSE}} |
| 28 | + {\partial \boldsymbol{\theta} \partial \boldsymbol{\theta}}\right)^{-1}_{\boldsymbol{\theta} |
| 29 | + = \boldsymbol{\theta}^*} |
| 30 | +
|
| 31 | + Where :math:`V_{\boldsymbol{\theta}}` is the covariance matrix of the estimated |
| 32 | + parameters, :math:`y` are the observed measured variables, :math:`\hat{y}` are the |
| 33 | + predicted measured variables, :math:`n` is the number of data points, |
| 34 | + :math:`\boldsymbol{\theta}` are the unknown parameters, :math:`\boldsymbol{\theta^*}` |
| 35 | + are the estimates of the unknown parameters, :math:`\mathbf{x}` are the decision |
| 36 | + variables, and :math:`\mathbf{W}` is a diagonal matrix containing the inverse of the |
| 37 | + variance of the measurement error, :math:`\sigma^2`. When the standard |
| 38 | + deviation of the measurement error is not supplied by the user, parmest |
| 39 | + approximates the variance of the measurement error as |
| 40 | + :math:`\sigma^2 = \frac{1}{n-l} \sum e_i^2` where :math:`l` is the number of |
| 41 | + fitted parameters, and :math:`e_i` is the residual for experiment :math:`i`. |
| 42 | + |
| 43 | + In parmest, this method computes the inverse of the Hessian by scaling the |
| 44 | + objective function (SSE or WSSE) with a constant probability factor. |
| 45 | + |
| 46 | +2. Finite Difference Method |
| 47 | + |
| 48 | + In this method, the covariance matrix, :math:`V_{\boldsymbol{\theta}}`, is |
| 49 | + calculated by applying the Gauss-Newton approximation to the Hessian, |
| 50 | + :math:`\frac{\partial^2 \text{SSE}}{\partial \boldsymbol{\theta} \partial \boldsymbol{\theta}}` |
| 51 | + or |
| 52 | + :math:`\frac{\partial^2 \text{WSSE}}{\partial \boldsymbol{\theta} \partial \boldsymbol{\theta}}`, |
| 53 | + leading to: |
| 54 | + |
| 55 | + .. math:: |
| 56 | + V_{\boldsymbol{\theta}} = \left(\sum_{i = 1}^n \mathbf{G}_{i}^{\mathrm{T}} \mathbf{W} |
| 57 | + \mathbf{G}_{i} \right)^{-1} |
| 58 | +
|
| 59 | + This method uses central finite difference to compute the Jacobian matrix, |
| 60 | + :math:`\mathbf{G}_{i}`, for experiment :math:`i`, which is the sensitivity of |
| 61 | + the measured variables with respect to the parameters, :math:`\boldsymbol{\theta}`. |
| 62 | + :math:`\mathbf{W}` is a diagonal matrix containing the inverse of the variance |
| 63 | + of the measurement errors, :math:`\sigma^2`. |
| 64 | + |
| 65 | +3. Automatic Differentiation Method |
| 66 | + |
| 67 | + Similar to the finite difference method, the covariance matrix is calculated as: |
| 68 | + |
| 69 | + .. math:: |
| 70 | + V_{\boldsymbol{\theta}} = \left( \sum_{i = 1}^n \mathbf{G}_{\text{kaug},\, i}^{\mathrm{T}} |
| 71 | + \mathbf{W} \mathbf{G}_{\text{kaug},\, i} \right)^{-1} |
| 72 | +
|
| 73 | + However, this method uses the model optimality (KKT) condition to compute the |
| 74 | + Jacobian matrix, :math:`\mathbf{G}_{\text{kaug},\, i}`, for experiment :math:`i`. |
| 75 | + |
| 76 | +The covariance matrix calculation is only supported with the built-in objective |
| 77 | +functions "SSE" or "SSE_weighted". |
| 78 | + |
| 79 | +In parmest, the covariance matrix can be calculated after defining the |
| 80 | +:class:`~pyomo.contrib.parmest.parmest.Estimator` object and estimating the unknown |
| 81 | +parameters using :class:`~pyomo.contrib.parmest.parmest.Estimator.theta_est`. To |
| 82 | +estimate the covariance matrix, with the default method being "finite_difference", call |
| 83 | +the :class:`~pyomo.contrib.parmest.parmest.Estimator.cov_est` function, e.g., |
| 84 | + |
| 85 | +.. testsetup:: * |
| 86 | + :skipif: not __import__('pyomo.contrib.parmest.parmest').contrib.parmest.parmest.parmest_available |
| 87 | + |
| 88 | + # Data |
| 89 | + import pandas as pd |
| 90 | + data = pd.DataFrame( |
| 91 | + data=[[1, 8.3], [2, 10.3], [3, 19.0], |
| 92 | + [4, 16.0], [5, 15.6], [7, 19.8]], |
| 93 | + columns=['hour', 'y'], |
| 94 | + ) |
| 95 | + |
| 96 | + # Create the Experiment class |
| 97 | + from pyomo.contrib.parmest.examples.rooney_biegler.rooney_biegler import RooneyBieglerExperiment |
| 98 | + |
| 99 | + exp_list = [] |
| 100 | + for i in range(data.shape[0]): |
| 101 | + exp_list.append(RooneyBieglerExperiment(data.loc[i, :])) |
| 102 | + |
| 103 | +.. doctest:: |
| 104 | + :skipif: not __import__('pyomo.contrib.parmest.parmest').contrib.parmest.parmest.parmest_available |
| 105 | + |
| 106 | + >>> import pyomo.contrib.parmest.parmest as parmest |
| 107 | + >>> pest = parmest.Estimator(exp_list, obj_function="SSE") |
| 108 | + >>> obj_val, theta_val = pest.theta_est() |
| 109 | + >>> cov = pest.cov_est() |
| 110 | + |
| 111 | +Optionally, one of the three methods; "reduced_hessian", "finite_difference", |
| 112 | +and "automatic_differentiation_kaug" can be supplied for the covariance calculation, |
| 113 | +e.g., |
| 114 | + |
| 115 | +.. doctest:: |
| 116 | + :skipif: not __import__('pyomo.contrib.parmest.parmest').contrib.parmest.parmest.parmest_available |
| 117 | + |
| 118 | + >>> pest = parmest.Estimator(exp_list, obj_function="SSE") |
| 119 | + >>> obj_val, theta_val = pest.theta_est() |
| 120 | + >>> cov_method = "reduced_hessian" |
| 121 | + >>> cov = pest.cov_est(method=cov_method) |
0 commit comments