View a markdown version of this page

$covarianceSamp - Amazon DocumentDB

$covarianceSamp

New from version 8.0.2.

The $covarianceSamp operator in Amazon DocumentDB returns the sample covariance of two numeric expressions. It is a window operator that is used only in the $setWindowFields stage; it is not valid in the $group or $bucket stages. You specify the operator under the stage's output field, and optionally define the window boundaries with a window document.

Parameters

  • $covarianceSamp takes a two-element array [ <expression1>, <expression2> ], where each element is a numeric expression (a field path such as "$x", an expression, or a constant). It returns the sample covariance of the two expressions across the documents in the window.

Example (MongoDB Shell)

The following example partitions the documents by series, sorts each partition by t, and computes the sample covariance of x and y over the whole partition.

Create sample documents

db.measurements.insertMany([ { _id: 1, series: "A", t: 1, x: 1, y: 2 }, { _id: 2, series: "A", t: 2, x: 2, y: 5 }, { _id: 3, series: "A", t: 3, x: 3, y: 8 }, { _id: 4, series: "B", t: 1, x: 1, y: 8 }, { _id: 5, series: "B", t: 2, x: 2, y: 5 }, { _id: 6, series: "B", t: 3, x: 3, y: 2 } ]);

Query example

db.measurements.aggregate([ { $setWindowFields: { partitionBy: "$series", sortBy: { t: 1 }, output: { covariance: { $covarianceSamp: [ "$x", "$y" ], window: { documents: ["unbounded", "unbounded"] } } } } } ]);

Output

[ { "_id": 1, "series": "A", "t": 1, "x": 1, "y": 2, "covariance": 3 }, { "_id": 2, "series": "A", "t": 2, "x": 2, "y": 5, "covariance": 3 }, { "_id": 3, "series": "A", "t": 3, "x": 3, "y": 8, "covariance": 3 }, { "_id": 4, "series": "B", "t": 1, "x": 1, "y": 8, "covariance": -3 }, { "_id": 5, "series": "B", "t": 2, "x": 2, "y": 5, "covariance": -3 }, { "_id": 6, "series": "B", "t": 3, "x": 3, "y": 2, "covariance": -3 } ]

Every document in a partition receives the same covariance, the sample covariance of x and y across all documents in that partition: 3 for series A (x and y increase together) and -3 for series B (y decreases as x increases). The sample covariance divides by n - 1 rather than n, so it is larger in magnitude than the population covariance for the same data.

Code examples

To view a code example for using the $covarianceSamp operator in the $setWindowFields stage, choose the tab for the language that you want to use:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); try { await client.connect(); const db = client.db('test'); const measurements = db.collection('measurements'); await measurements.insertMany([ { _id: 1, series: "A", t: 1, x: 1, y: 2 }, { _id: 2, series: "A", t: 2, x: 2, y: 5 }, { _id: 3, series: "A", t: 3, x: 3, y: 8 }, { _id: 4, series: "B", t: 1, x: 1, y: 8 }, { _id: 5, series: "B", t: 2, x: 2, y: 5 }, { _id: 6, series: "B", t: 3, x: 3, y: 2 } ]); const result = await measurements.aggregate([ { $setWindowFields: { partitionBy: "$series", sortBy: { t: 1 }, output: { covariance: { $covarianceSamp: [ "$x", "$y" ], window: { documents: ["unbounded", "unbounded"] } } } } } ]).toArray(); console.log(result); } finally { await client.close(); } } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') try: db = client['test'] measurements = db['measurements'] measurements.insert_many([ { '_id': 1, 'series': 'A', 't': 1, 'x': 1, 'y': 2 }, { '_id': 2, 'series': 'A', 't': 2, 'x': 2, 'y': 5 }, { '_id': 3, 'series': 'A', 't': 3, 'x': 3, 'y': 8 }, { '_id': 4, 'series': 'B', 't': 1, 'x': 1, 'y': 8 }, { '_id': 5, 'series': 'B', 't': 2, 'x': 2, 'y': 5 }, { '_id': 6, 'series': 'B', 't': 3, 'x': 3, 'y': 2 } ]) result = list(measurements.aggregate([ { '$setWindowFields': { 'partitionBy': '$series', 'sortBy': { 't': 1 }, 'output': { 'covariance': { '$covarianceSamp': [ '$x', '$y' ], 'window': { 'documents': ['unbounded', 'unbounded'] } } } } } ])) print(result) finally: client.close() example()