gremlin - how to compute standard deviation in a single query?

Viewed 107

On Kelvin Lawrence's excellent gremlin guide, there's a section on how to compute the standard deviation for a list of property values.

mean=g.V().hasLabel('airport').values('runways').mean().next()

count = g.V().hasLabel('airport').count().next()

g.withSideEffect("m",mean).
  withSideEffect("c",count).
  V().hasLabel('airport').values('runways').
  math('(_ - m)^2').sum().math('_ / c').math('sqrt(_)')

How would you condense this down into a single query?

1 Answers

I keep meaning to go add that to the book. It's actually Issue #174 on the repo. Here's the query:

gremlin> g.V().hasLabel('airport').
......1>       values('runways').fold().as('runways').
......2>       mean(local).as('mean').
......3>       select('runways').unfold().
......4>       math('(_-mean)^2').mean().math('sqrt(_)') 
==>0.7510927827902234
Related