Search code examples
matlabmultidimensional-arraystatisticsconfidence-intervaldata-fitting

MATLAB: build confidence region


I am trying to perform anomaly detection of images in a patch-wise manner in MATLAB. For each patch in the image, I extract a 6x1 feature vector g, where each component is an indicator.

I have to use the confidence region cr, defined by the snippet below (I couldn't post the image), built on all the feature vectors of the normal patches and use it for testing new patches.

<a href="https://www.codecogs.com/eqnedit.php?latex=\dpi{100}&space;\mathcal{R}_{\gamma}=\lbrace\phi\in\mathbb{R}^6:&space;\sqrt{(\phi&space;-&space;\overline{\bf{g}})'\Sigma^{-1}(\phi-\overline{\bf{g}})}\leq\gamma\rbrace&space;\\\\&space;\text{where&space;$\overline{\bf{g}},$\Sigma$&space;are&space;the&space;average&space;and&space;the&space;sample&space;covariance&space;of&space;g}" target="_blank"><img src="https://latex.codecogs.com/gif.latex?\dpi{100}&space;\mathcal{R}_{\gamma}=\lbrace\phi\in\mathbb{R}^6:&space;\sqrt{(\phi&space;-&space;\overline{\bf{g}})'\Sigma^{-1}(\phi-\overline{\bf{g}})}\leq\gamma\rbrace&space;\\\\&space;\text{where&space;$\overline{\bf{g}},$\Sigma$&space;are&space;the&space;average&space;and&space;the&space;sample&space;covariance&space;of&space;g}" title="\mathcal{R}_{\gamma}=\lbrace\phi\in\mathbb{R}^6: \sqrt{(\phi - \overline{\bf{g}})'\Sigma^{-1}(\phi-\overline{\bf{g}})}\leq\gamma\rbrace \\\\ \text{where $\overline{\bf{g}},$\Sigma$ are the average and the sample covariance of g}" /></a>

Informally, I want to check if a test feature vector falls inside the confidence region and so label the patch as normal, otherwise anomalous.

I am struggling to understand how to build a confidence region in R6 using MATLAB. I have tried using bootci, but doing so cr becomes a 2x6x6 matrix and I don't understand the meaning of the third dimension. Any help or suggestion is appreciated! Thanks.


Solution

  • If all you want is to classify a 6-D vector φ, just apply the formula in your snippet. Assuming that sigmaInv is the inverse of the sample covariance and that φ and g_bar are column vectors i.e. size(phi) = size(g_bar) = (6,1) then

    s = (phi-g_bar)'*sigmaInv*(phi-g_bar) % note the ' after the first () = transpose
    

    is a scalar and sqrt(s) <= gamma means normal, the opposite means anomalous. (Taking the square root assumes that the sample covariance is positive-definite). If phi and g_bar are row vectors, then the formula should have the transpose after the second parenthesis:

    s = (phi-g_bar)*sigmaInv*(phi-g_bar)' %  apostrophe now after second ()
    

    Hope this helps