Watson IoT Platform - Node-RED - 直近10件の平均や標準偏差を求める

#平均、分散、標準偏差
- 最近10件のNode-REDのフローで平均、分散、標準偏差を求めます。
- [Node-REDのフローをコピーする方法](http://qiita.com/egplnt/items/84b244a50d8bdaced324)を参照して、下記のフローをコピペします。

```lang:日付・時刻の追加
[{"id":"c860e2a.ebc43a","type":"inject","z":"ad2e7b14.5fbf","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":110,"y":120,"wires":[["c581603e.fdb54"]]},{"id":"c581603e.fdb54","type":"function","z":"ad2e7b14.5fbf","name":"配列に格納","func":"if ( !context.global.array ) {\n    context.global.array = new Array (10);\n}\nvar shifted = context.global.array.shift();\n\ncontext.global.array.push(msg.payload);\n\nmsg.array = context.global.array;\nmsg.length = context.global.array.length;\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":120,"wires":[["2b11b89f.d89058","65ff3cb4.d32ef4"]]},{"id":"2b11b89f.d89058","type":"debug","z":"ad2e7b14.5fbf","name":"","active":true,"console":"false","complete":"array","x":520,"y":120,"wires":[]},{"id":"28ccafb5.6cec48","type":"inject","z":"ad2e7b14.5fbf","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"x":110,"y":160,"wires":[["c581603e.fdb54"]]},{"id":"65ff3cb4.d32ef4","type":"function","z":"ad2e7b14.5fbf","name":"平均","func":"var sum = 0, average = 0;\n    for (i=0; i<msg.length; i++) {\n        sum = sum + msg.array[i];\n}\nmsg.sum = sum;\naverage = sum / msg.length;\nmsg.average = average;\nreturn msg;\n","outputs":1,"noerr":0,"x":330,"y":160,"wires":[["bff2a271.a589f","31088a48.039a26","b406d373.2a658"]]},{"id":"bff2a271.a589f","type":"debug","z":"ad2e7b14.5fbf","name":"","active":false,"console":"false","complete":"sum","x":520,"y":160,"wires":[]},{"id":"31088a48.039a26","type":"debug","z":"ad2e7b14.5fbf","name":"","active":true,"console":"false","complete":"average","x":530,"y":200,"wires":[]},{"id":"b82682f3.04c8a","type":"inject","z":"ad2e7b14.5fbf","name":"","topic":"","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"x":110,"y":200,"wires":[["c581603e.fdb54"]]},{"id":"b406d373.2a658","type":"function","z":"ad2e7b14.5fbf","name":"分散","func":"var variance = 0;\nfor (i=0; i<msg.length; i++) {\n    variance = variance + Math.pow(msg.array[i] - msg.average, 2);\n}\nvariance = variance / msg.length;\nmsg.variance = variance;\nreturn msg;\n","outputs":1,"noerr":0,"x":330,"y":240,"wires":[["aa7acb18.aa6f4","33ef2550.71dde2"]]},{"id":"aa7acb18.aa6f4","type":"debug","z":"ad2e7b14.5fbf","name":"","active":true,"console":"false","complete":"variance","x":530,"y":240,"wires":[]},{"id":"33ef2550.71dde2","type":"function","z":"ad2e7b14.5fbf","name":"標準偏差","func":"var standard_deviation = 0;\nstandard_deviation = Math.sqrt(msg.variance);\nmsg.std = standard_deviation;\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":280,"wires":[["944c5173.658728"]]},{"id":"944c5173.658728","type":"debug","z":"ad2e7b14.5fbf","name":"","active":true,"console":"false","complete":"std","x":520,"y":280,"wires":[]},{"id":"d29c9b32.125fb8","type":"inject","z":"ad2e7b14.5fbf","name":"","topic":"","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"x":110,"y":240,"wires":[["c581603e.fdb54"]]},{"id":"be760ca7.8eab08","type":"inject","z":"ad2e7b14.5fbf","name":"","topic":"","payload":"20","payloadType":"num","repeat":"","crontab":"","once":false,"x":110,"y":280,"wires":[["c581603e.fdb54"]]},{"id":"57c47b99.9b8d94","type":"inject","z":"ad2e7b14.5fbf","name":"","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"x":110,"y":320,"wires":[["c581603e.fdb54"]]}]

```

- injectノードを適当に操作して直近１０件分のメッセージを投入し平均や分散、標準偏差を確認します。
![スクリーンショット 2016-05-22 15.55.18.png](https://qiita-image-store.s3.amazonaws.com/0/125842/82eedcbc-2ada-b3dc-d292-1ecbc811d201.png)
![スクリーンショット 2016-05-22 15.55.23.png](https://qiita-image-store.s3.amazonaws.com/0/125842/65f8bf99-da42-5b65-ed24-d3cf295c3ab1.png)
![スクリーンショット 2016-05-22 15.55.30.png](https://qiita-image-store.s3.amazonaws.com/0/125842/d13695e4-1315-52fc-29d7-d12eac7caeed.png)
![スクリーンショット 2016-05-22 15.55.50.png](https://qiita-image-store.s3.amazonaws.com/0/125842/b0728f31-0fa5-da97-d1e4-1121adf02154.png)

- IoTアプリに接続して使用します。
- 直近何件のデータを対象とするかによって、配列の要素数を変更します。

```js:配列に格納
if ( !context.global.array ) {
    context.global.array = new Array (10);
}
var shifted = context.global.array.shift();

context.global.array.push(msg.payload);

msg.array = context.global.array;
msg.length = context.global.array.length;
return msg;
```

- 平均(average)を算出するjsは以下の通りです。

```js:平均を算出
var sum = 0, average = 0;
    for (i=0; i<msg.length; i++) {
        sum = sum + msg.array[i];
}
msg.sum = sum;
average = sum / msg.length;
msg.average = average;
return msg;
```

- 分散(variance)を算出するjsは以下の通りです。

```js:分散を算出
var variance = 0;
for (i=0; i<msg.length; i++) {
    variance = variance + Math.pow(msg.array[i] - msg.average, 2);
}
variance = variance / msg.length;
msg.variance = variance;
return msg;
```

- 標準偏差(standard deviation)を算出するjsは以下の通りです。

```js:標準偏差を算出
var standard_deviation = 0;
standard_deviation = Math.sqrt(msg.variance);
msg.std = standard_deviation;
return msg;
```