# Math calculation in kibana to visualize

**URL:** <https://discuss.elastic.co/t/math-calculation-in-kibana-to-visualize/316970>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [October 19, 2022, 8:29am UTC](https://discuss.elastic.co/t/math-calculation-in-kibana-to-visualize/316970 "2022-10-19T08:29:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nanditha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nanditha/32/137350_2.png) [@Nanditha](https://discuss.elastic.co/u/Nanditha)\
**Post date:** [October 19, 2022, 8:29am UTC](https://discuss.elastic.co/t/math-calculation-in-kibana-to-visualize/316970/1 "2022-10-19T08:29:46Z")

</div>

I need to calculate below math calculations and visualize it in dashboard. When i push new data it should calculate the same and change. Can anyone please help on this.

1. precision = tp/ (tp + fp)
2. recall = tp/tp + fn
3. False alarm rate = fp/ (tp + fp)

Methods i have tried :

1. Using KQL filters i was not able to do any math operations.
2. I have tried Painless scripting but have so many exceptions and not able to resolve them. Here are few.

```auto
{"log":"607045d54125a1004057194c","algorithm_name":"Seatbelt","algorithm_version":0.1,"annotation_stream":"60811f2cec0687004291502c","timestamp":"2021-04-08 11:40:25","LabelID_d":0,"Label_d":"Seat belt on","LabelID_a":1,"Label_a":"Seat belt on","Seatclass":"Driver","confidence_d":100,"status":"TP","UUID_a":"3352970a-e3e4-4045-8d3b-5ecd2aad4f97","id":1}
{"log":"607045d54125a1004057194a","algorithm_name":"Seatbelt","algorithm_version":0.2,"annotation_stream":"60811f2cec0687004291502c","timestamp":"2021-04-09 11:40:25","LabelID_d":0,"Label_d":"Seat belt off","LabelID_a":1,"Label_a":"Seat belt off","Seatclass":"Driver","confidence_d":1,"status":"TN","UUID_a":"3352970a-e3e3-4045-8d3b-5ecd2aad4f97","id":2}

```

In kibana painless Lab:

```auto
int precision_cal() {
    int tpcount = 0;
    int fpcount = 0;
    if ((doc["status"].value) == "TP") {
        tpcount+=tpcount;
    } 
    if ((doc["status"].value) == "FP") {
        fpcount+=fpcount;
    }
    precision=tpcount/(tpcount + fpcount);
    return precision;
}

```

Error 1:

```auto
{
  "root_cause": [
    {
      "type": "x_content_parse_exception",
      "reason": "[14:2] Unexpected character ('{' (code 123)): was expecting comma to separate Object entries\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 14, column: 2]"
    }
  ],
  "type": "x_content_parse_exception",
  "reason": "[13:370] [painless_execute_request] failed to parse field [context_setup]",
  "caused_by": {
    "type": "x_content_parse_exception",
    "reason": "[14:2] Unexpected character ('{' (code 123)): was expecting comma to separate Object entries\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 14, column: 2]",
    "caused_by": {
      "type": "json_parse_exception",
      "reason": "Unexpected character ('{' (code 123)): was expecting comma to separate Object entries\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 14, column: 2]"
    }
  }
}

```

Error 2:

```auto
Unhandled Exception illegal_argument_exception

cannot resolve symbol [doc]

Stack:
[
  "... t fpcount = 0;\r\n if ((doc[\"status\"].value) == \" ...",
  " ^---- HERE"
]

```

i'm stuck on the logic for this math calculation from a week now. It would be of great help if anyone can help on this. Also let me know if any other info is required for debugging.

Thanks.

---

<div class="post-metadata">

**Author:** ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)\
**Post date:** [October 19, 2022, 9:37am UTC](https://discuss.elastic.co/t/math-calculation-in-kibana-to-visualize/316970/2 "2022-10-19T09:37:06Z")

</div>

Hi @Nanditha

welcome to the Kibana community.

From my understanding your math formula should be something like:

```auto
count(kql='status:"TP"') / (count(kql='status:"TP"') + count(kql='status:"FP"'))

```

Does it look right? If so, you can try Lens formula and use the one above here.

---

<div class="post-metadata">

**Author:** ![Nanditha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nanditha/32/137350_2.png) [@Nanditha](https://discuss.elastic.co/u/Nanditha)\
**Post date:** [October 19, 2022, 10:01am UTC](https://discuss.elastic.co/t/math-calculation-in-kibana-to-visualize/316970/3 "2022-10-19T10:01:15Z")

</div>

Hi @Marco_Liberati ,

Thank you so much for your response.  
yes, i want to calculate same as what you have mentioned. Can i make use of Metric as i want a number (Int or float, double) for my visualization.

Thanks.

---

<div class="post-metadata">

**Author:** ![Nanditha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nanditha/32/137350_2.png) [@Nanditha](https://discuss.elastic.co/u/Nanditha)\
**Post date:** [October 19, 2022, 10:07am UTC](https://discuss.elastic.co/t/math-calculation-in-kibana-to-visualize/316970/4 "2022-10-19T10:07:09Z")

</div>

Hi @Marco_Liberati ,

I used it in Metric aggregation and it works perfectly fine.  
Thank you so much for solving the doubt.

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 15, 2022, 11:15am UTC](https://discuss.elastic.co/t/math-calculation-in-kibana-to-visualize/316970/5 "2022-11-15T11:15:43Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 13, 2022, 11:15am UTC](https://discuss.elastic.co/t/math-calculation-in-kibana-to-visualize/316970/6 "2022-12-13T11:15:49Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
