Skip to content

Add counters to see stdev of values per key #1068

Open

Description

@johnynek

If on every reduce operation or join we counted the number of values for the key (say N_v), and then incremented a counter by N_v^2, then since map-reduce gives the number of groups, and the number of input and output records, we could compute the standard deviation of the (values per key). If we compare that to the mean of (values per key) we get some idea of the skew. I don't think we would overflow a long counter with this, in most cases, but it is possible (probably we can't directly compute the third standard moment, which is more directly skew), but I think the stdev might be enough in most cases.

We probably want the incoming skew and the outgoing skew. For standard sums/reduces/folds, the outgoing stdev should be 0 since each key has one value.

Activity

  1. oeddyo commented on May 6, 2015

    @oeddyo
    Contributor

    Taking a look.

    May I have a bit more hints here?

    1. where is the "reduce operation or join" you mentioned located in the code? (I guess it's scalding/scalding-core/src/main/scala/com/twitter/scalding/ReduceOperations.scala) but not 100% sure

    2. why we want to add N_v^2? Is it for the convenience to do sqrt(E[x^2] - E[x]^2) and get std?

    3. where (in the code) can I get the number of input and output records? is the number of output records equal to number of keys, and thus we could get the mean?

  2. johnynek commented on May 6, 2015

    @johnynek
    ContributorAuthor

    Hadoop already keeps the number of key groups and the total number of pairs, so this gives you values/key. What we need to get (as you point out) is the \sum_k v_k^2 where v_k is the number of values for key k. With that, we can compute the Var[V] where V is the random variable of the number of values for a randomly selected key.

    To get the number of values for a key, we need to look at https://github.com/twitter/scalding/blob/develop/scalding-core/src/main/scala/com/twitter/scalding/Operations.scala#L480 TypedBufferOp. Not on 496, we iterate through the iterator, so we could increment a counter at the end with the size squared.

    For the case of joins, it is more complex (and probably should use a different stat key). That code is here: https://github.com/twitter/scalding/blob/develop/scalding-core/src/main/scala/com/twitter/scalding/typed/CoGrouped.scala#L306 - we might want a key for each of the join inputs, or maybe just the output. It's not clear. We can take it in two PRs.

  3. oeddyo commented on May 7, 2015

    @oeddyo
    Contributor

    Thanks @johnynek for the help.

    Currently I have the "proof of concept" PR.

    Basically what I'm doing is:

    Create a simple test, to do a reduce. The data I'm using is

    TypedPipe.from(List((1, 2), (1, 3), (2, 3), (2, 5), (3, 1000)))

    then I did: test-only com.twitter.scalding.ExperimentTest

    And I'm doing a print in

      val resIter = reduceFnSer.get(key, values)
      while (resIter.hasNext) {
        val tup = Tuple.size(1)
        val t2 = resIter.next
        println("t2 = " + t2)
        tup.set(0, t2)
        oc.add(tup)
      }
    

    However, the variable printed for t2 is unexpected.

    The output for above is:
    t2 = 5
    t2 = 8
    t2 = 1000
    RES = ArrayBuffer((1,5), (2,8), (3,1000))

    But I was expecting

    t2 = Iteraor[Int](2, 3) // For key 1
    t2 = Iterator[Int](3, 5) // For key 2

    It seems to be the case that "+" is associative and then reduce somehow combined them before going to the while loop?

    Because of this, the size of values (i.e. values.size) is always one. Is there any way I could get around this?

  4. oeddyo commented on May 12, 2015

    @oeddyo
    Contributor

    It seems to prove my theory that simply doing reduce on Int would trigger some sort of mechanism without calling the reduce function?

    Since if I do:
    TypedPipe.from(List((1, (1, 1)), (2, (2, 2)), (1, (3, 3)), (3, (3, 3))))
    .group
    .foldLeft((0, 0)){ (a, b) => (a._1 + b._1, a._2 + b._2) }
    .toTypedPipe
    .map{
    case (a: Int, (b: Int, c: Int)) =>
    (a, b, c)
    }
    .write(TypedTsv(Int, Int, Int))
    }

    I was able to get the size of the values

  5. oeddyo commented on May 12, 2015

    @oeddyo
    Contributor

    Have been thinking about this a bit for today (and also read a bit more related part by searching "Counter" in the core-package), and I'm wondering where shall I put this counter class?

    i.e. I'm not sure how to expose it for others to use. Seems like the only other counter in core is Stat. Shall I put this

    class KeyValueCounter(val nKey: Long, val valuesSquaredSum: Double, val valuesSum: Double) {

    into a Stat instead?

  6. self-assigned this
    on May 14, 2015
  7. johnynek commented on May 14, 2015

    @johnynek
    ContributorAuthor

    The cascading flowProcess gives access to the counter mechanism, so you should be able to increment that directly in the places where we put this statistic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions