0

Using the graph of the gods example and adding the following 'amount' property:

rand = new Random()
g.withSack {rand.nextFloat()}.E().property('amount',sack())

The traversal below is based on https://neo4j.com/docs/graph-algorithms/current/algorithms/similarity-pearson/ with the goal of calculating the (Ai - mean(A)) and (Bi - mean(B)) terms:

g.V().match(
    __.as('v1').outE().valueMap().select('amount').fold().as('e1'),
    __.as('v1').V().as('v2'),
    __.as('v2').outE().valueMap().select('amount').fold().as('e2'),
    __.as('v1').outE().inV().dedup().fold().as('v1n'),
    __.as('v2').outE().inV().dedup().fold().as('v2n')
    ).
    where('v1',neq('v2').and(without('v1n'))).
    where('v2',without('v1n')).
    project('v1','v2','a1','a2','a1m','a2m').
        by(select('v1')).
        by(select('v2')).
        by(select('e1')).
        by(select('e2')).
        by(select('e1').unfold().mean()).
        by(select('e2').unfold().mean()).
    where(select('a1').unfold().count().is(gt(0))).
    where(select('a2').unfold().count().is(gt(0)))

Output of traversal:

==>[a1:[v1:v[4096],v2:v[4248],a1:[0.30577987,0.8416171,0.5247855,0.57484317,0.35161084],a2:[0.7349615,0.80212617,0.6879539],a1m:0.5197273015975952,a2m:0.7416805227597555]] ==>[a1:[v1:v[4096],v2:v[4264],a1:[0.30577987,0.8416171,0.5247855,0.57484317,0.35161084],a2:[0.37226892,0.8902944,0.4158439,0.9709829],a1m:0.5197273015975952,a2m:0.6623475253582001]] ==>[a1:[v1:v[8192],v2:v[4096],a1:[0.32524675],a2:[0.30577987,0.8416171,0.5247855,0.57484317,0.35161084],a1m:0.32524675130844116,a2m:0.5197273015975952]] ==>[a1:[v1:v[8192],v2:v[4184],a1:[0.32524675],a2:[0.53761715,0.9604127,0.87463444,0.7719325],a1m:0.32524675130844116,a2m:0.786149188876152]] ==>[a1:[v1:v[8192],v2:v[4248],a1:[0.32524675],a2:[0.7349615,0.80212617,0.6879539],a1m:0.32524675130844116,a2m:0.7416805227597555]] ==>[a1:[v1:v[8192],v2:v[4264],a1:[0.32524675],a2:[0.37226892,0.8902944,0.4158439,0.9709829],a1m:0.32524675130844116,a2m:0.6623475253582001]] ==>[a1:[v1:v[4184],v2:v[4096],a1:[0.53761715,0.9604127,0.87463444,0.7719325],a2:[0.30577987,0.8416171,0.5247855,0.57484317,0.35161084],a1m:0.786149188876152,a2m:0.5197273015975952]] ==>[a1:[v1:v[4184],v2:v[8192],a1:[0.53761715,0.9604127,0.87463444,0.7719325],a2:[0.32524675],a1m:0.786149188876152,a2m:0.32524675130844116]] ==>[a1:[v1:v[4248],v2:v[4096],a1:[0.7349615,0.80212617,0.6879539],a2:[0.30577987,0.8416171,0.5247855,0.57484317,0.35161084],a1m:0.7416805227597555,a2m:0.5197273015975952]] ==>[a1:[v1:v[4248],v2:v[8192],a1:[0.7349615,0.80212617,0.6879539],a2:[0.32524675],a1m:0.7416805227597555,a2m:0.32524675130844116]] ==>[a1:[v1:v[4264],v2:v[4096],a1:[0.37226892,0.8902944,0.4158439,0.9709829],a2:[0.30577987,0.8416171,0.5247855,0.57484317,0.35161084],a1m:0.6623475253582001,a2m:0.5197273015975952]]

How would someone calculate 'a1 - a1m' and 'a2 - a2m' from this point in the traversal? The issue here is subtracting a single value from every element in a list and then returning the list of differences, any help with an example would be great.

ebelogay
  • 11
  • 4

1 Answers1

1

Since you already have all values in map, let's start there.

gremlin> __.inject(['a1': [0.30577987,0.8416171,0.5247855,0.57484317,0.35161084],
......1>            'a2': [0.7349615,0.80212617,0.6879539],
......2>            'a1m': 0.5197273015975952,
......3>            'a2m': 0.7416805227597555])
==>[a1:[0.30577987,0.8416171,0.5247855,0.57484317,0.35161084],a2:[0.7349615,0.80212617,0.6879539],a1m:0.5197273015975952,a2m:0.7416805227597555]

Subtracting the mean value (am) from each single value (ai) is as simple as unfolding a, do the math (ai-am or (am-ai)*(-1)) and fold them back together:

gremlin> __.inject(['a1': [0.30577987,0.8416171,0.5247855,0.57484317,0.35161084],
......1>            'a2': [0.7349615,0.80212617,0.6879539],
......2>            'a1m': 0.5197273015975952,
......3>            'a2m': 0.7416805227597555]).
......4>    sack(assign).
......5>      by(select('a1m')).
......6>    select('a1').unfold().
......7>    sack(minus).
......8>    sack(mult).
......9>      by(constant(-1)).
.....10>    sack().fold()
==>[-0.2139474315975952,0.3218897984024048,0.0050581984024048,0.0551158684024048,-0.1681164615975952]

So, for both values it would simply be another projection:

gremlin> __.inject(['a1': [0.30577987,0.8416171,0.5247855,0.57484317,0.35161084],
......1>            'a2': [0.7349615,0.80212617,0.6879539],
......2>            'a1m': 0.5197273015975952,
......3>            'a2m': 0.7416805227597555]).
......4>    project('a','b').
......5>      by(sack(assign).
......6>           by(select('a1m')).
......7>         select('a1').unfold().
......8>         sack(minus).
......9>         sack(mult).
.....10>           by(constant(-1)).
.....11>         sack().fold()).
.....12>      by(sack(assign).
.....13>           by(select('a2m')).
.....14>         select('a2').unfold().
.....15>         sack(minus).
.....16>         sack(mult).
.....17>           by(constant(-1)).
.....18>         sack().fold())
==>[a:[-0.2139474315975952,0.3218897984024048,0.0050581984024048,0.0551158684024048,-0.1681164615975952],b:[-0.0067190227597555,0.0604456472402445,-0.0537266227597555]]

I guess there will be a few more steps to come up with the final value and I'm sure the final query can be simplified a lot, but that should better be handled in another thread.

Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34