|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Median absolute deviation |
| 4 | +parent: Metric aggregations |
| 5 | +grand_parent: Aggregations |
| 6 | +nav_order: 65 |
| 7 | +redirect_from: |
| 8 | + - /query-dsl/aggregations/metric/median-absolute-deviation/ |
| 9 | +--- |
| 10 | + |
| 11 | +# Median absolute deviation aggregations |
| 12 | + |
| 13 | +The `median_absolute_deviation` metric is a single-value metric aggregation that returns a median absolute deviation field. Median absolute deviation is a statistical measure of data variability. Because the median absolute deviation measures dispersion from the median, it provides a more robust measure of variability that is less affected by outliers in a dataset. |
| 14 | + |
| 15 | +Median absolute deviation is calculated as follows:<br> |
| 16 | +median_absolute_deviation = median(|X<sub>i</sub> - Median(X<sub>i</sub>)|) |
| 17 | + |
| 18 | +The following example calculates the median absolute deviation of the `DistanceMiles` field in the sample dataset `opensearch_dashboards_sample_data_flights`: |
| 19 | + |
| 20 | + |
| 21 | +```json |
| 22 | +GET opensearch_dashboards_sample_data_flights/_search |
| 23 | +{ |
| 24 | + "size": 0, |
| 25 | + "aggs": { |
| 26 | + "median_absolute_deviation_DistanceMiles": { |
| 27 | + "median_absolute_deviation": { |
| 28 | + "field": "DistanceMiles" |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | +{% include copy-curl.html %} |
| 35 | + |
| 36 | +#### Example response |
| 37 | + |
| 38 | +```json |
| 39 | +{ |
| 40 | + "took": 35, |
| 41 | + "timed_out": false, |
| 42 | + "_shards": { |
| 43 | + "total": 1, |
| 44 | + "successful": 1, |
| 45 | + "skipped": 0, |
| 46 | + "failed": 0 |
| 47 | + }, |
| 48 | + "hits": { |
| 49 | + "total": { |
| 50 | + "value": 10000, |
| 51 | + "relation": "gte" |
| 52 | + }, |
| 53 | + "max_score": null, |
| 54 | + "hits": [] |
| 55 | + }, |
| 56 | + "aggregations": { |
| 57 | + "median_absolute_deviation_distanceMiles": { |
| 58 | + "value": 1829.8993624441966 |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### Missing |
| 65 | + |
| 66 | +By default, if a field is missing or has a null value in a document, it is ignored during computation. However, you can specify a value to be used for those missing or null fields by using the `missing` parameter, as shown in the following request: |
| 67 | + |
| 68 | +```json |
| 69 | +GET opensearch_dashboards_sample_data_flights/_search |
| 70 | +{ |
| 71 | + "size": 0, |
| 72 | + "aggs": { |
| 73 | + "median_absolute_deviation_distanceMiles": { |
| 74 | + "median_absolute_deviation": { |
| 75 | + "field": "DistanceMiles", |
| 76 | + "missing": 1000 |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | +{% include copy-curl.html %} |
| 83 | + |
| 84 | +#### Example response |
| 85 | + |
| 86 | +```json |
| 87 | +{ |
| 88 | + "took": 7, |
| 89 | + "timed_out": false, |
| 90 | + "_shards": { |
| 91 | + "total": 1, |
| 92 | + "successful": 1, |
| 93 | + "skipped": 0, |
| 94 | + "failed": 0 |
| 95 | + }, |
| 96 | + "hits": { |
| 97 | + "total": { |
| 98 | + "value": 10000, |
| 99 | + "relation": "gte" |
| 100 | + }, |
| 101 | + "max_score": null, |
| 102 | + "hits": [] |
| 103 | + }, |
| 104 | + "aggregations": { |
| 105 | + "median_absolute_deviation_distanceMiles": { |
| 106 | + "value": 1829.6443646143355 |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +### Compression |
| 113 | + |
| 114 | +The median absolute deviation is calculated using the [t-digest](https://github.com/tdunning/t-digest/tree/main) data structure, which balances between performance and estimation accuracy through the `compression` parameter (default value: `1000`). Adjusting the `compression` value affects the trade-off between computational efficiency and precision. Lower `compression` values improve performance but may reduce estimation accuracy, while higher values enhance accuracy at the cost of increased computational overhead, as shown in the following request: |
| 115 | + |
| 116 | +```json |
| 117 | +GET opensearch_dashboards_sample_data_flights/_search |
| 118 | +{ |
| 119 | + "size": 0, |
| 120 | + "aggs": { |
| 121 | + "median_absolute_deviation_DistanceMiles": { |
| 122 | + "median_absolute_deviation": { |
| 123 | + "field": "DistanceMiles", |
| 124 | + "compression": 10 |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | +{% include copy-curl.html %} |
| 131 | + |
| 132 | +#### Example response |
| 133 | + |
| 134 | +```json |
| 135 | +{ |
| 136 | + "took": 1, |
| 137 | + "timed_out": false, |
| 138 | + "_shards": { |
| 139 | + "total": 1, |
| 140 | + "successful": 1, |
| 141 | + "skipped": 0, |
| 142 | + "failed": 0 |
| 143 | + }, |
| 144 | + "hits": { |
| 145 | + "total": { |
| 146 | + "value": 10000, |
| 147 | + "relation": "gte" |
| 148 | + }, |
| 149 | + "max_score": null, |
| 150 | + "hits": [] |
| 151 | + }, |
| 152 | + "aggregations": { |
| 153 | + "median_absolute_deviation_DistanceMiles": { |
| 154 | + "value": 1836.265614211182 |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
0 commit comments