Loading TOC...

MarkLogic 9 Product Documentation
cts.covariance

cts.covariance(
   value1 as cts.reference,
   value2 as cts.reference,
   [options as String[]],
   [query as cts.query?],
   [forest-ids as (Number|String)[]]
) as Number?

Summary

Returns the frequency-weighted sample covariance given a 2-way co-occurrence. The co-occurrence is formed from the specified value lexicons. This function works like math.covariance except each pair in the co-occurrence is counted cts.frequency times. This function performs the calculation in parallel in all data nodes then aggregates the values.

Parameters
value1 Reference to a range index. The type of the range index must be numeric.
value2 Reference to a range index. The type of the range index must be numeric.
options Same as the "options" parameter in cts:aggregate.
query Same as the "query" parameter in cts:aggregate.
forest-ids Same as the "forest-ids" parameter in cts:aggregate.

Example

//   This query assumes range indexes with positions are
//   configured in the database for both xval and yval. It
//   generates some sample data and then performs the
//   aggregation in a separate transaction.

//  Generate data:
declareUpdate();

for (x=1; x<11; x++) {
  const j = 2 * x;
  const o = new Object();
  o.xval = x;
  o.yval = [];
  for (y=0; y<x; y++) {
    o.yval.push(j);
  };
  xdmp.documentInsert(("cov" + x + ".json"), o);
};

*******
// Run the query:
cts.covariance(
  cts.jsonPropertyReference("xval","type=int"),
  cts.jsonPropertyReference("yval","type=int"),
    ["item-frequency"]);
=>
12.2222222222222

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.