Reputation: 2139
When I want to combine my query and get collection from my "Match Query" and calculate some computations -> neo4j returns NULL
While when I make separation it seems ok and I have proper result , I meant when I run: 1. getting collection -> it works 2. computation -> also works but both of them can not work
create
(_0 {`name`:"Mahsa"}),
(_1 {`name`:"Frank"}),
(_2 {`name`:"Tag1"}),
(_3 {`name`:"Tag2"}),
(_4 {`name`:"Tag3"}),
_0-[:TAGGED { frequency: 1}]->_2,
_0-[:TAGGED { frequency: 2}]->_3,
_0-[:TAGGED { frequency: 3}]->_4,
_1-[:TAGGED { frequency: 4}]->_2,
_1-[:TAGGED { frequency: 5}]->_3,
_1-[:TAGGED { frequency: 6}]->_4
MATCH (p1:Person {username: 'Mahsa' })-[x:TAGGED]->(t:Tag)<-[y:TAGGED]-(p2:Person {username: 'Frank'})
WITH x.frequency AS aColl, y.frequency AS bColl
UNWIND aColl AS a
WITH collect(a) AS aColl, avg(a) AS aAvg, bColl
UNWIND bColl AS b
WITH aColl, aAvg, collect(b) AS bColl, avg(b) AS bAvg
Return REDUCE(x = 0.0, i IN range(0, size(aColl)- 1)| x +((aColl[i]- aAvg)*(bColl[i]- bAvg)))/(size(aColl) - 1) as cov
@Nicole White: That is intersting and I got it why x.frequencytotal is not a collection and we should try on collect it.
Upvotes: 0
Views: 448
Reputation: 7790
There are several things wrong here. x.frequency
and y.frequency
are not collections; UNWINDing them has no effect. You created data without any labels and yet you are querying for labels so you won't get any results. Your property on the Person node is name
and yet you are querying for username
, again leading to no results.
I've changed the data you provided to what I think you want it to be.
create
(_0:Person {`username`:"Mahsa"}),
(_1:Person {`username`:"Frank"}),
(_2:Tag {`name`:"Tag1"}),
(_3:Tag {`name`:"Tag2"}),
(_4:Tag {`name`:"Tag3"}),
_0-[:TAGGED { frequency: 1}]->_2,
_0-[:TAGGED { frequency: 2}]->_3,
_0-[:TAGGED { frequency: 3}]->_4,
_1-[:TAGGED { frequency: 4}]->_2,
_1-[:TAGGED { frequency: 5}]->_3,
_1-[:TAGGED { frequency: 6}]->_4
Then, the query to get covariance is:
MATCH (:Person {username:'Mahsa'})-[x:TAGGED]->(:Tag)<-[y:TAGGED]-(:Person {username:'Frank'})
WITH AVG(x.frequency) AS avg_x,
AVG(y.frequency) AS avg_y,
COLLECT(x.frequency) AS x_col,
COLLECT(y.frequency) AS y_col
RETURN REDUCE(s = 0.0, i IN RANGE(0, SIZE(x_col) - 1) | s + ((x_col[i] - avg_x) * (y_col[i] - avg_y))) / (SIZE(x_col) - 1) AS cov;
You can take a closer look here: http://console.neo4j.org/r/8vdp81
In this case the covariance is 1 since your observations are [1,2,3] and [4,5,6]:
> cov(c(1,2,3), c(4,5,6))
[1] 1
Upvotes: 1