Object

com.twitter.scalding.mathematics

Combinatorics

Related Doc: package mathematics

Permalink

object Combinatorics

Serve as a repo for self-contained combinatorial functions with no dependencies such as combinations, aka n choose k, nCk permutations , aka nPk subset sum : numbers that add up to a finite sum weightedSum: For weights (a,b,c, ...), want integers (x,y,z,...) to satisfy constraint |ax + by + cz + ... - result | < error ...

Source
Combinatorics.scala
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Combinatorics
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. def combinations(n: Int, k: Int)(implicit flowDef: FlowDef, mode: Mode): Pipe

    Permalink

    Return a pipe with all nCk combinations, with k columns per row

  7. def combinations[T](input: IndexedSeq[T], k: Int)(implicit flowDef: FlowDef, mode: Mode): Pipe

    Permalink

    Given an int k, and an input of size n, return a pipe with nCk combinations, with k columns per row

    Given an int k, and an input of size n, return a pipe with nCk combinations, with k columns per row

    Computes nCk = n choose k, for large values of nCk

    Use-case: Say you have 100 hashtags sitting in an array You want a table with 5 hashtags per row, all possible combinations If the hashtags are sitting in a string array, then combinations[String]( hashtags, 5) will create the 100 chose 5 combinations.

    Algorithm: Use k pipes, cross pipes two at a time, filter out non-monotonic entries

    eg. 10C2 = 10 choose 2 Use 2 pipes. Pipe1 = (1,2,3,...10) Pipe2 = (2,3,4....10) Cross Pipe1 with Pipe2 for 10*9 = 90 tuples Filter out tuples that are non-monotonic For (t1,t2) we want t1<t2, otherwise reject. This brings down 90 tuples to the desired 45 tuples = 10C2

  8. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  13. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  17. def permutations(n: Int, k: Int)(implicit flowDef: FlowDef, mode: Mode): Pipe

    Permalink

    Return a pipe with all nPk permutations, with k columns per row

  18. def permutations[T](input: IndexedSeq[T], k: Int)(implicit flowDef: FlowDef, mode: Mode): Pipe

    Permalink

    Return a pipe with all nPk permutations, with k columns per row For details, see combinations(...) above

  19. def positiveWeightedSum(weights: IndexedSeq[Double], result: Double, error: Double)(implicit flowDef: FlowDef, mode: Mode): Pipe

    Permalink

    Does the exact same thing as weightedSum, but filters out tuples with a weight of 0 The returned pipe contain only positive non-zero weights.

  20. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  21. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  22. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  23. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  24. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  25. def weightedSum(weights: IndexedSeq[Double], result: Double, error: Double)(implicit flowDef: FlowDef, mode: Mode): Pipe

    Permalink

    Goal: Given weights (a,b,c, ...), we seek integers (x,y,z,...) to satisft the constraint |ax + by + cz + ...

    Goal: Given weights (a,b,c, ...), we seek integers (x,y,z,...) to satisft the constraint |ax + by + cz + ... - result | < error

    Parameters: The weights (a,b,c,...) must be non-negative doubles. Our search space is 0 to result/min(weights) The returned pipe will contain integer tuples (x,y,z,...) that satisfy ax+by+cz +... = result

    Note: This is NOT Simplex WE use a slughtly-improved brute-force algorithm that performs well on account of parallelization. Algorithm: Create as many pipes as the number of weights Each pipe copntains integral multiples of the weight w ie. (0,1w,2w,3w,4w,....) Iterate as below - Cross two pipes Create a temp column that stores intermediate results Apply progressive filtering on the temp column Discard the temp column Once all pipes are crossed, test for temp column within error bounds of result Discard duplicates at end of process

    Usecase: We'd like to generate all integer tuples for typical usecases like

    0. How many ways can you invest $1000 in facebook, microsoft, hp ? val cash = 1000.0 val error = 5.0 // max error $5, so its ok if we cannot invest the last $5 or less val (FB, MSFT, HP) = (23.3,27.4,51.2) // share prices val stocks = IndexedSeq( FB,MSFT,HP ) weightedSum( stocks, cash, error).write( Tsv("invest.txt"))

    1. find all (x,y,z) such that 2x+3y+5z = 23, with max error 1 weightedSum( IndexedSeq(2.0,3.0,5.0), 23.0, 1.0)

    2. find all (a,b,c,d) such that 2a+12b+12.5c+34.7d = 3490 with max error 3 weightedSum( IndexedSeq(2.0,12.0,2.5,34.7),3490.0,3.0)

    This is at the heart of portfolio mgmt( Markowitz optimization), subset-sum, operations-research LP problems.

Inherited from AnyRef

Inherited from Any

Ungrouped