Statistical Measures


  • Queries can calculate the percentile, slope, and standard deviation of a time series, or of a range of entries within a time series.

  • For time series that have more than one value per entry, these methods return one measure for the first values in each entry, another measure for the second values in each entry, and so on.

  • In this page:


Syntax

Percentile

A percentile of a time series is the value that divides the time series values by some ratio, when they are arranged from smallest to largest.

For example, a 90th percentile is greater than 90% of the values in the series, and less than the remaining 10%.

  • RQL method: percentile()
  • LINQ method: Percentile()

The percentile method can be used to calculate any percentile in a time series or range of time series entries. It takes one double value that is greater than 0 and less than or equal to 100. This represents the percent of the time series values that should be smaller than the result.

See examples below.


Slope

  • RQL method: slope()
  • LINQ method: slope()

The slope of a time series or range of time series entries is the difference between the first and last values of the range (disregarding the values in between) divided by the difference in time.

Queries that use this method must also aggregate the time series, grouping the entries into whole numbers of time units.

The difference in time is measured in milliseconds. Use scaling to adjust the results to your preferred units.

See examples below.


Standard Deviation

  • RQL method: stddev()
  • LINQ method: StandardDeviation()

These methods return the standard deviation of time series values.

See examples below.


Result Format

Queries with these methods return results with the following format:

{
    "From": <first entry's timestamp>,
    "To": <last entry's timestamp>,
    "Count": [
        <number of first values from each entry>,
        <number of second values from each entry>,
        ...
    ],
    <"Percentile"/"Slope"/"Standard Deviation">: [
        <double - measure for first values from each entry>,
        <double - measure for second values from each entry>,
        ...
    ]
}

If the query uses group by aggregation, there will be one of these results for each of the aggregates.

Examples

Percentile

var query = session.Query<Employee>()
    .Select(p => RavenQuery.TimeSeries(p, "HeartRates")
        .Select(x => new
        {
            P = x.Percentile(90)
        }
    )
);
var query = session.Advanced.RawQuery<TimeSeriesAggregationResult>(@"
    from Employees as e
    select timeseries(
        from e.HeartRate 
        select percentile(90)
    )
");

Slope

var query = session.Query<Employee>()
    .Select(p => RavenQuery.TimeSeries(p, "HeartRates")
        .GroupBy(g => g.Hours(1))
        .Select(x => new
        {
            Slope = x.Slope()
        }
    )
);
var query = session.Advanced.RawQuery<TimeSeriesAggregationResult>(@"
    from Employees as e 
    select timeseries(
        from e.HeartRates
        group by 1 hour
        select slope()
    )
");

Standard Deviation

//Example query with defined range
var date = DateTime.Today;

var query = session.Query<Employee>()
    .Select(p => RavenQuery.TimeSeries(p, "HeartRates", date, date.AddDays(1))
        .Select(x => new
        {
            StdDev = x.StandardDeviation()
        }
    )
);
//Example query with defined range
var date = DateTime.Today;

var query = session.Advanced.RawQuery<TimeSeriesAggregationResult>(@"
    from Employees as e
    select timeseries(
        from e.HeartRates 
        between $start and $end
        select stddev()
    )
")
.AddParameter("start", date)
.AddParameter("end", date.AddDays(1));