# Combining multiple metrics into a likelihood

**URL:** https://discourse.numenta.org/t/combining-multiple-metrics-into-a-likelihood/4333
**Category:** Engineering
**Tags:** question
**Created:** [August 10, 2018, 7:32pm UTC](https://discourse.numenta.org/t/combining-multiple-metrics-into-a-likelihood/4333 "2018-08-10T19:32:28Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![efriis](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@efriis](https://discourse.numenta.org/u/efriis)
#### Post date: [August 10, 2018, 7:32pm UTC](https://discourse.numenta.org/t/combining-multiple-metrics-into-a-likelihood/4333/1 "2018-08-10T19:32:28Z")

</div>

Hey there,

I’m trying to implement anomaly detection along the lines of the discussion in supplementary section S4 of the “Unsupervised real-time anomaly detection for streaming data” paper (Pages 6-8 of [https://ars.els-cdn.com/content/image/1-s2.0-S0925231217309864-mmc1.pdf](https://ars.els-cdn.com/content/image/1-s2.0-S0925231217309864-mmc1.pdf))

Let’s say I have two models, which output prediction errors `s1_t` and `s2_t` at every time, `t`. The goal of the discussion is to be able to detect if the prediction error of the first model spikes at a different but close time to the second (i.e. `s1_4` and `s2_5` are spikes). I’m very confused why they propose including `G`, a Gaussian convolution kernel, which seems to use `x`, the input to HTM (the raw value).

Could someone walk me through the math of this section, and if possible, how one would implement it?

Thanks!

---

<div class="post-metadata">

### Author: ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)
#### Post date: [August 10, 2018, 8:03pm UTC](https://discourse.numenta.org/t/combining-multiple-metrics-into-a-likelihood/4333/2 "2018-08-10T20:03:43Z")

</div>

Are you talking about the “anomaly likelihood” process? If so, this is not a part of HTM theory, it is an optimization specifically added for anomaly detection. There is no link to any biological process here.

In any case, I don’t know the maths well enough to walk you though it.

---

<div class="post-metadata">

### Author: ![efriis](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@efriis](https://discourse.numenta.org/u/efriis)
#### Post date: [August 10, 2018, 8:08pm UTC](https://discourse.numenta.org/t/combining-multiple-metrics-into-a-likelihood/4333/3 "2018-08-10T20:08:40Z")

</div>

All good, and my bad! Misunderstood the categories.

I’m mostly curious if anyone has implemented a successful implementation of the ideas discussed in the paper’s S4 section. Or anything similar regarding running multiple models in parallel and trying to combine their output signals 😄

---

<div class="post-metadata">

### Author: ![subutai](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/subutai/32/106_2.png) [@subutai](https://discourse.numenta.org/u/subutai)
#### Post date: [August 10, 2018, 9:25pm UTC](https://discourse.numenta.org/t/combining-multiple-metrics-into-a-likelihood/4333/4 "2018-08-10T21:25:50Z")

</div>

I’ve only done a couple of small experiments using real data to satisfy myself it works and might help. Would be very curious to see if anyone has success (or not) with it!

---

<div class="post-metadata">

### Author: ![efriis](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@efriis](https://discourse.numenta.org/u/efriis)
#### Post date: [August 23, 2018, 1:15pm UTC](https://discourse.numenta.org/t/combining-multiple-metrics-into-a-likelihood/4333/5 "2018-08-23T13:15:13Z")

</div>

Do you think it would be possible to share some code for those small experiments? I’m a bit lost in the process of translating the math in the paper to code 😄

---

<div class="post-metadata">

### Author: ![sheiser1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/sheiser1/32/766_2.png) [@sheiser1](https://discourse.numenta.org/u/sheiser1)
#### Post date: [August 23, 2018, 1:56pm UTC](https://discourse.numenta.org/t/combining-multiple-metrics-into-a-likelihood/4333/6 "2018-08-23T13:56:40Z")

</div>

Hey @efriis,

So I had this same question about implementing the Anomaly Likelihood myself. First I’d say if you prioritize implementation over knowing the algorithm you can just go:

```python
from nupic.algorithms import anomaly_likelihood
anomLiklObj = anomaly_likelihood.AnomalyLikelihood()
...
for each raw_input to TM:
   ENC = encode( raw_input )
   TM_InputCols = spatialPooler( ENC )
   TM_PredCols = temporalMemory( SDR ) 
   A_Score = calcAnom(TM_PredCols_t-1,TM_InputCols)
   A_Likl = anomLiklObj.anomalyProbability(raw_input,A_Score)

```

---

<div class="post-metadata">

### Author: ![subutai](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/subutai/32/106_2.png) [@subutai](https://discourse.numenta.org/u/subutai)
#### Post date: [August 23, 2018, 7:53pm UTC](https://discourse.numenta.org/t/combining-multiple-metrics-into-a-likelihood/4333/7 "2018-08-23T19:53:51Z")

</div>

Unfortunately I don’t have any code for the multiple metrics equations in S4. The small experiments were done in Excel using proprietary data that I don’t think I am allowed to share. Here’s [a small version with random data](https://drive.google.com/file/d/1YMowp-XQ0cbCOz4rHRcAinb0ncLScvVF/view?usp=sharing). If you know Excel you can see how the computations were done.

The file assumes you already have the likelihood. You can use @sheiser1’s code to see how likelihood itself was computed.

This was done a couple of years ago, I did not spend much time on this, and it is not cleanly done. No guarantees - you’re mostly on your own here!

---

<div class="post-metadata">

### Author: ![steinroe](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@steinroe](https://discourse.numenta.org/u/steinroe)
#### Post date: [April 23, 2020, 5:06pm UTC](https://discourse.numenta.org/t/combining-multiple-metrics-into-a-likelihood/4333/8 "2020-04-23T17:06:56Z")

</div>

@efriis Did you achieve anything here? I am also struggling the the implementation of what is described in S4…
