# Should I 'swarm' some sample data?

**URL:** <https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366>\
**Category:** NuPIC\
**Created:** [May 30, 2017, 7:38pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366 "2017-05-30T19:38:12Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil\_d\_cat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/phil_d_cat/32/1289_2.png) [@phil\_d\_cat](https://discourse.numenta.org/u/phil_d_cat)\
**Post date:** [May 30, 2017, 7:38pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/1 "2017-05-30T19:38:12Z")

</div>

Im getting some v mixed results looking for anomalies using HTM.Java.

Here’s a chart after a fair bit of massage:

 ![](https://canada1.discourse-cdn.com/flex030/uploads/numenta/original/2X/f/f30e3ad027e80db1253ba64473124815b3c9127e.png)

The top chart is the data and the bottom is likelihood plotted as:

```
-100*(ln(1.0000000001-likelihood)/ln(1-0.9999999999))

```

Im wondering if it’s worthwhile to run the data through python / swarm to choose better parameters.

The results aren’t _bad_ but it seems like a great deal of effort to produce.

---

<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:** [May 30, 2017, 9:19pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/2 "2017-05-30T21:19:05Z")

</div>

> [@phil\_d\_cat](#):
>
> Im wondering if it’s worthwhile to run the data through python / swarm to choose better parameters.

By all means, it couldn’t hurt. What parameters are you currently using?

Also, is this data labelled? Can you tell when actual anomalies occurred?

---

<div class="post-metadata">

**Author:** ![phil\_d\_cat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/phil_d_cat/32/1289_2.png) [@phil\_d\_cat](https://discourse.numenta.org/u/phil_d_cat)\
**Post date:** [May 30, 2017, 9:45pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/3 "2017-05-30T21:45:08Z")

</div>

> Can you tell when actual anomalies occurred?

Those spikes that have risen above the background noise. For this sample anything above 6-800 is a good candidate.

---

<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:** [May 30, 2017, 10:04pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/4 "2017-05-30T22:04:36Z")

</div>

> [@phil\_d\_cat](#):
>
> Those spikes that have risen above the background noise. For this sample anything above 6-800 is a good candidate.

You can use an anomaly likelihood threshold to dial up/down the number of anomalies you’re getting. It looks like all the anomalies it found were over 600 like you said, so it is missing some of them. Lower the threshold until you get something more like what you want.

> [@phil\_d\_cat](#):
>
> The top chart is the data and the bottom is likelihood plotted as:
> 
> ```
> -100*(ln(1.0000000001-likelihood)/ln(1-0.9999999999))
> 
> ```

I don’t understand why you’re doing this? We usually just set a threshold like `0.99999` and say any anomaly likelihood value over that threshold should be considered an anomaly. Then adjust as needed.

---

<div class="post-metadata">

**Author:** ![phil\_d\_cat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/phil_d_cat/32/1289_2.png) [@phil\_d\_cat](https://discourse.numenta.org/u/phil_d_cat)\
**Post date:** [May 31, 2017, 11:10am UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/5 "2017-05-31T11:10:42Z")

</div>

I used that likelihood calculation to try to weed out some of the noise.

---

<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:** [May 31, 2017, 3:40pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/6 "2017-05-31T15:40:07Z")

</div>

The anomaly likelihood calculation itself is what is weeding out the noise, from the anomaly score (not shown).

---

<div class="post-metadata">

**Author:** ![phil\_d\_cat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/phil_d_cat/32/1289_2.png) [@phil\_d\_cat](https://discourse.numenta.org/u/phil_d_cat)\
**Post date:** [May 31, 2017, 4:01pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/7 "2017-05-31T16:01:34Z")

</div>

Here are the suggested params:

```
MODEL_PARAMS = {'aggregationInfo': {'days': 0,
                 'fields': [(u'stamp', 'first'), (u'result', 'sum')],
                 'hours': 0,
                 'microseconds': 0,
                 'milliseconds': 0,
                 'minutes': 0,
                 'months': 0,
                 'seconds': 1,
                 'weeks': 0,
                 'years': 0},
 'model': 'CLA',
 'modelParams': {'anomalyParams': {u'anomalyCacheRecords': None,
                               u'autoDetectThreshold': None,
                               u'autoDetectWaitRecords': None},
             'clParams': {'alpha': 0.003336578312138331,
                          'regionName': 'SDRClassifierRegion',
                          'steps': '1',
                          'verbosity': 0},
             'inferenceType': 'TemporalMultiStep',
             'sensorParams': {'encoders': {'_classifierInput': {'classifierOnly': True,
                                                                'clipInput': True,
                                                                'fieldname': 'result',
                                                                'n': 33,
                                                                'name': '_classifierInput',
                                                                'type': 'AdaptiveScalarEncoder',
                                                                'w': 21},
                                           u'result': None,
                                           u'stamp_dayOfWeek': {'dayOfWeek': (21,
                                                                              1.1076204575827224),
                                                                'fieldname': 'stamp',
                                                                'name': 'stamp',
                                                                'type': 'DateEncoder'},
                                           u'stamp_timeOfDay': None,
                                           u'stamp_weekend': None},
                              'sensorAutoReset': None,
                              'verbosity': 0},
             'spEnable': True,
             'spParams': {'boostStrength': 0.0,
                          'columnCount': 2048,
                          'globalInhibition': 1,
                          'inputWidth': 0,
                          'numActiveColumnsPerInhArea': 40,
                          'potentialPct': 0.8,
                          'seed': 1956,
                          'spVerbosity': 0,
                          'spatialImp': 'cpp',
                          'synPermActiveInc': 0.05,
                          'synPermConnected': 0.1,
                          'synPermInactiveDec': 0.1},
             'tpEnable': True,
             'tpParams': {'activationThreshold': 12,
                          'cellsPerColumn': 32,
                          'columnCount': 2048,
                          'globalDecay': 0.0,
                          'initialPerm': 0.21,
                          'inputWidth': 2048,
                          'maxAge': 0,
                          'maxSegmentsPerCell': 128,
                          'maxSynapsesPerSegment': 32,
                          'minThreshold': 9,
                          'newSynapseCount': 20,
                          'outputType': 'normal',
                          'pamLength': 1,
                          'permanenceDec': 0.1,
                          'permanenceInc': 0.1,
                          'seed': 1960,
                          'temporalImp': 'cpp',
                          'verbosity': 0},
             'trainSPNetOnlyIfRequested': False},
 'predictAheadTime': None,
 'version': 1}

```

Some of these have obvious analogs in the Java params but many do not. Working on copying these over now.

---

<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:** [May 31, 2017, 4:10pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/8 "2017-05-31T16:10:36Z")

</div>

Something strange in those params. In `sensorParams.encoders` it looks like it is only encoding “day of week” and not even the `result` value (which I assume is the predicted field?). I don’t think the swarm was set up properly. How are you doing it? Do you have a swarm definition to share?

---

<div class="post-metadata">

**Author:** ![phil\_d\_cat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/phil_d_cat/32/1289_2.png) [@phil\_d\_cat](https://discourse.numenta.org/u/phil_d_cat)\
**Post date:** [May 31, 2017, 4:14pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/9 "2017-05-31T16:14:11Z")

</div>

```
{
  "includedFields": [
    {
      "fieldName": "stamp", 
      "fieldType": "datetime"
    }, 
   {
      "fieldName": "result", 
      "fieldType": "int"
    }
  ], 
  "streamDef": {
  "info": "test", 
  "version": 1, 
  "streams": [
  {
    "info": "radar_anom_sample.csv", 
    "source": "file://data/radar_anom_sample.csv", 
    "columns": [
      "*"
    ] 
  }
  ], 
  "aggregation": {
  "hours": 0, 
  "microseconds": 0, 
  "seconds": 1, 
  "fields": [
      [
          "result", 
          "sum"
      ], 
      [
         "stamp", 
         "first"
      ]
  ], 
  "weeks": 0, 
  "months": 0, 
  "minutes": 0, 
  "days": 0, 
  "milliseconds": 0, 
  "years": 0
  }
  }, 
  "inferenceType": "MultiStep", 
  "inferenceArgs": {
  "predictionSteps": [
      1
   ], 
   "predictedField": "result"
 }, 
 "iterationCount": -1, 
 "swarmSize": "medium"
}
```

---

<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:** [May 31, 2017, 4:20pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/10 "2017-05-31T16:20:14Z")

</div>

Give `result` a max and min value like this (but integers):

> <https://github.com/numenta/nupic/blob/master/examples/opf/clients/hotgym/prediction/one_gym/swarm_description.py#L31-L32>

I’m not sure if you’re trying to use the aggregation functionality, but I would not suggest it. I find it much easier to deal with data manipulations outside of NuPIC. So in that case, you can remove the entire `aggregation` section.

---

<div class="post-metadata">

**Author:** ![phil\_d\_cat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/phil_d_cat/32/1289_2.png) [@phil\_d\_cat](https://discourse.numenta.org/u/phil_d_cat)\
**Post date:** [May 31, 2017, 4:25pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/11 "2017-05-31T16:25:26Z")

</div>

k. I’ll update and rerun.

---

<div class="post-metadata">

**Author:** ![phil\_d\_cat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/phil_d_cat/32/1289_2.png) [@phil\_d\_cat](https://discourse.numenta.org/u/phil_d_cat)\
**Post date:** [May 31, 2017, 6:52pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/12 "2017-05-31T18:52:38Z")

</div>

```
MODEL_PARAMS = {'aggregationInfo': {'days': 0,
                 'fields': [],
                 'hours': 0,
                 'microseconds': 0,
                 'milliseconds': 0,
                 'minutes': 0,
                 'months': 0,
                 'seconds': 0,
                 'weeks': 0,
                 'years': 0},
 'model': 'CLA',
 'modelParams': {'anomalyParams': {u'anomalyCacheRecords': None,
                               u'autoDetectThreshold': None,
                               u'autoDetectWaitRecords': None},
             'clParams': {'alpha': 0.0041745905756698015,
                          'regionName': 'SDRClassifierRegion',
                          'steps': '1',
                          'verbosity': 0},
             'inferenceType': 'NontemporalMultiStep',
             'sensorParams': {'encoders': {'_classifierInput': {'classifierOnly': True,
                                                                'clipInput': True,
                                                                'fieldname': 'result',
                                                                'maxval': 5000,
                                                                'minval': 0,
                                                                'n': 36,
                                                                'name': '_classifierInput',
                                                                'type': 'ScalarEncoder',
                                                                'w': 21},
                                           u'result': None,
                                           u'stamp_dayOfWeek': None,
                                           u'stamp_timeOfDay': None,
                                           u'stamp_weekend': {'fieldname': 'stamp',
                                                              'name': 'stamp',
                                                              'type': 'DateEncoder',
                                                              'weekend': (21,
                                                                          1)}},
                              'sensorAutoReset': None,
                              'verbosity': 0},
             'spEnable': True,
             'spParams': {'boostStrength': 0.0,
                          'columnCount': 2048,
                          'globalInhibition': 1,
                          'inputWidth': 0,
                          'numActiveColumnsPerInhArea': 40,
                          'potentialPct': 0.8,
                          'seed': 1956,
                          'spVerbosity': 0,
                          'spatialImp': 'cpp',
                          'synPermActiveInc': 0.05,
                          'synPermConnected': 0.1,
                          'synPermInactiveDec': 0.08290353055187707},
             'tpEnable': True,
             'tpParams': {'activationThreshold': 13,
                          'cellsPerColumn': 32,
                          'columnCount': 2048,
                          'globalDecay': 0.0,
                          'initialPerm': 0.21,
                          'inputWidth': 2048,
                          'maxAge': 0,
                          'maxSegmentsPerCell': 128,
                          'maxSynapsesPerSegment': 32,
                          'minThreshold': 10,
                          'newSynapseCount': 20,
                          'outputType': 'normal',
                          'pamLength': 2,
                          'permanenceDec': 0.1,
                          'permanenceInc': 0.1,
                          'seed': 1960,
                          'temporalImp': 'cpp',
                          'verbosity': 0},
             'trainSPNetOnlyIfRequested': False},
 'predictAheadTime': None,
 'version': 1}
```

---

<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:** [May 31, 2017, 7:30pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/13 "2017-05-31T19:30:22Z")

</div>

Interesting. The first model params you got back indicated that the value of `result` did not contribute to its own prediction, and that “day of week” did contribute. This tells me that there may not be discernible patterns in the data. Can a human look at this data and see patterns over different time periods?

Anyway, I wanted to make sure your params were right and when you ran the 2nd time, same thing (although oddly this time it “weekend/weekday” that contributes most to prediction.

My guess is that prediction confidence is really bad throughout because it’s not seeing any repeating sequences in the data.

---

<div class="post-metadata">

**Author:** ![phil\_d\_cat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/phil_d_cat/32/1289_2.png) [@phil\_d\_cat](https://discourse.numenta.org/u/phil_d_cat)\
**Post date:** [May 31, 2017, 8:53pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/14 "2017-05-31T20:53:40Z")

</div>

I was wondering if the randomness / noise would be an impediment to anomaly detection. While there _may_ be patterns at different levels there are no guarantees.

---

<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:** [May 31, 2017, 9:09pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/15 "2017-05-31T21:09:28Z")

</div>

If you are just interested in anomaly detection on a single scalar value, I would use the parameters returned by [`getScalarMetricWithTimeOfDayAnomalyParams`](http://nupic.docs.numenta.org/0.7.0.dev0/api/opf/utils.html#nupic.frameworks.opf.common_models.cluster_params.getScalarMetricWithTimeOfDayAnomalyParams), which would be something like this:

```json
{
  "inferenceArgs":{
    "predictionSteps":[
      1
    ],
    "predictedField":"c1",
    "inputPredictedField":"auto"
  },
  "modelConfig":{
    "aggregationInfo":{
      "seconds":0,
      "fields":[

      ],
      "months":0,
      "days":0,
      "years":0,
      "hours":0,
      "microseconds":0,
      "weeks":0,
      "minutes":0,
      "milliseconds":0
    },
    "model":"HTMPrediction",
    "version":1,
    "predictAheadTime":null,
    "modelParams":{
      "sensorParams":{
        "sensorAutoReset":null,
        "encoders":{
          "c0_dayOfWeek":null,
          "c0_timeOfDay":{
            "fieldname":"c0",
            "timeOfDay":[
              21,
              9.49
            ],
            "type":"DateEncoder",
            "name":"c0"
          },
          "c1":{
            "name":"c1",
            "resolution":0.7692307692307693,
            "seed":42,
            "fieldname":"c1",
            "type":"RandomDistributedScalarEncoder"
          },
          "c0_weekend":null
        },
        "verbosity":0
      },
      "anomalyParams":{
        "anomalyCacheRecords":null,
        "autoDetectThreshold":null,
        "autoDetectWaitRecords":5030
      },
      "spParams":{
        "columnCount":2048,
        "synPermInactiveDec":0.0005,
        "spatialImp":"cpp",
        "inputWidth":0,
        "spVerbosity":0,
        "synPermConnected":0.2,
        "synPermActiveInc":0.003,
        "potentialPct":0.8,
        "numActiveColumnsPerInhArea":40,
        "boostStrength":0.0,
        "globalInhibition":1,
        "seed":1956
      },
      "trainSPNetOnlyIfRequested":false,
      "clParams":{
        "alpha":0.035828933612158,
        "verbosity":0,
        "steps":"1",
        "regionName":"SDRClassifierRegion"
      },
      "tmParams":{
        "columnCount":2048,
        "activationThreshold":13,
        "pamLength":3,
        "cellsPerColumn":32,
        "permanenceDec":0.1,
        "minThreshold":10,
        "inputWidth":2048,
        "maxSynapsesPerSegment":32,
        "outputType":"normal",
        "initialPerm":0.21,
        "globalDecay":0.0,
        "maxAge":0,
        "newSynapseCount":20,
        "maxSegmentsPerCell":128,
        "permanenceInc":0.1,
        "temporalImp":"cpp",
        "seed":1960,
        "verbosity":0
      },
      "tmEnable":true,
      "clEnable":false,
      "spEnable":true,
      "inferenceType":"TemporalAnomaly"
    }
  }
}

```

These are tuned for anomaly detection on scalar data streams, specifically for min=0, max=5000 (I just ran this function). You’ll need to replace the `c0`, `c1` values with your field names. I hope that helps?

---

<div class="post-metadata">

**Author:** ![phil\_d\_cat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/phil_d_cat/32/1289_2.png) [@phil\_d\_cat](https://discourse.numenta.org/u/phil_d_cat)\
**Post date:** [June 5, 2017, 4:54pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/16 "2017-06-05T16:54:14Z")

</div>

Im not clear on how to use these params. Still looking for the ‘aha’ moment w/re HTM / nupic.

I tried running the params that swarming produced (with your mods). It didn’t output anything in the anomaly column and the predicted values were essentially random.

Is it possible that my data is just too noisy for HTM?

---

<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:** [June 5, 2017, 4:56pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/17 "2017-06-05T16:56:13Z")

</div>

> [@phil\_d\_cat](#):
>
> Is it possible that my data is just too noisy for HTM?

One big question I have that you have not answer is this: Can a human detect the anomalies in this data? If you say that any values above 600 are essentially anomalous, wouldn’t a threshold work? Also, since you are working in HTM.Java and there is no anomaly likelihood functionality (isn’t that right @cogmission?) , that may be the missing piece.

---

<div class="post-metadata">

**Author:** ![phil\_d\_cat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/phil_d_cat/32/1289_2.png) [@phil\_d\_cat](https://discourse.numenta.org/u/phil_d_cat)\
**Post date:** [June 5, 2017, 5:02pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/18 "2017-06-05T17:02:05Z")

</div>

> Can a human detect the anomalies in this data?

Yes. Definitely. Getting away from ‘eyes only’ is one of the goals of this project.

The 600 value is just for this data set. We have millions (another big issue with any proposed solution).

> and there is no anomaly likelihood functionality

Ive done my best to replicate the likelihood functionality from python in my code.

---

<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:** [June 5, 2017, 5:33pm UTC](https://discourse.numenta.org/t/should-i-swarm-some-sample-data/2366/19 "2017-06-05T17:33:59Z")

</div>

Split this topic on June 5, 2017, 5:33pm UTC

2 posts were split to a new topic: [Missing anomaly scores](https://discourse.numenta.org/t/missing-anomaly-scores/2386)
