 # Aggregation by product

#1

Are there any tools for aggregating results by product rather than sum or avg?

(Michel Conrado) #2

Can you be more specific? Personally I’m not sure what you mean. Product?

#3

The same way that sum() or avg() are applied to a collection of variables, is there a way to evaluate the product of a collection of variables as well?
For example:
SUM([a b c]) = a+b+c
PRODUCT([a b c]) = abc

(Michel Conrado) #4

PS. I still feels I did not understood.

In aggregation itself we have only `min` `max` `sum` and `avg` there’s no other func.

If you mean other thing, It will depends.

Abstract: “You want to organize a result by the product of a calculation made in another query block”

You could use something like “`@filter(gt(val(PRODUCT), 60`”.

Check this example below (you can run in https://play.dgraph.io)

``````{
var(func:allofterms(name@en, "steven spielberg")) {
films as director.film {
p as count(starring)
q as count(genre)
date as initial_release_date
years as math(since(date)/(365*24*60*60))
score as math(cond(years > 10, 0, ln(p)+q-ln(years)))
}
}

TopMovies(func: uid(films), orderasc: val(years)) #@filter(gt(val(score), 2))
@filter(gt(val(years), 6) and le(val(years), 14))
{
name@en
val(score)
val(date)
val(years)
}
}
``````

#5

In my particular case I’m attempting a probability calculation using the weights (facets) of my graph with an unknown number of edges. The function I’m attempting to replicate is:

likelihood = PROD(probability)/(1-PROD(probability))

Where PROD is the math.prod function from Python and probability is the collection of probability values on the edges between nodes (facets).

So basically I’m looking how to replicate the functionality of Python’s math.prod() function.

PS Sorry I’m new here and don’t yet know how to format everything!

(Michel Conrado) #6

I did a google of it. From what I saw, I can’t see you’re doing that with Dgraph as far I understand Maths, aggregation and vars from Dgraph’s features. Maybe someone has some idea about it.

#7

Yeah I can’t figure it out either - I was hoping someone here had an idea of how to do it.

Thanks for the input!

#9

I think I have a work-around using logarithm rules:

``````log2(A) + log2(B) = log2(A*B)
2^log2(A) = A
``````

So I can get the product of the values using sum() aggregation and the logbase() and pow() math functions:

``````log_L = log2(L)
sum_log_L = sum(log_L)
prod_L = 2^sum_log_L
``````