#import "@preview/cetz:0.4.2"
Section 6.4

#title[Binomial coefficients, formula and identities]
= Binomial coefficient
From last week:

$C(n, k) = $ the number of k-combinations from an n-set. Or the number of ways to select k elements from an n-set. Or the number of k-subsets of an n-set. Formula: $n!/(k!-(n-k)!)=mat(n;k)$ for $0<=k<=n$ 


== Pascal's triangle
#align(center, grid(columns: 2, column-gutter: 1em, cetz.canvas({
  import cetz.draw: *

  let n = 6

  // calculate the triangle
  let pascal = ()
  for row in range(n + 1) {
    let row-data = ()
    for col in range(row + 1) {
      let value = if col == 0 or col == row {
        1
      } else {
        let prev = pascal.at(row - 1)
        prev.at(col - 1) + prev.at(col)
      }
      row-data.push(value)
    }
    pascal.push(row-data)
  }

  // draw lines
  for (row-idx, row) in pascal.enumerate() {
    if row-idx < n {
      let row-len = row.len()
      let y = n - row-idx
      for (col-idx, val) in row.enumerate() {
        let x = col-idx - row-len / 2 + 0.5

        let next-row-len = row-len + 1
        let left-child-x = col-idx - next-row-len / 2 + 0.5
        let right-child-x = (col-idx + 1) - next-row-len / 2 + 0.5
        let child-y = y - 1

        line((x, y / 1.5), (left-child-x, child-y / 1.5), stroke: gray)
        line((x, y / 1.5), (right-child-x, child-y / 1.5), stroke: gray)
      }
    }
  }

  // draw values
  for (row-idx, row) in pascal.enumerate() {
    let row-len = row.len()
    let y = (n - row-idx) / 1.5
    for (col-idx, val) in row.enumerate() {
      let x = col-idx - row-len / 2 + 0.5
      circle((x, y), radius: 0.25, fill: white, stroke: none, name: "c-" + str(row-idx) + "-" + str(col-idx))
      content((x, y), $binom(#str(row-idx), #str(col-idx))$)
    }
  }

  // draw n
  for i in range(n + 1) {
    content((rel: (-2, 0), to: ("c-" + str(i) + "-0", "-|", "c-" + str(n - 1) + "-0")), [n=#i])
  }
}), cetz.canvas({
  import cetz.draw: *

  let n = 6

  // calculate the triangle
  let pascal = ()
  for row in range(n + 1) {
    let row-data = ()
    for col in range(row + 1) {
      let value = if col == 0 or col == row {
        1
      } else {
        let prev = pascal.at(row - 1)
        prev.at(col - 1) + prev.at(col)
      }
      row-data.push(value)
    }
    pascal.push(row-data)
  }

  // draw lines
  for (row-idx, row) in pascal.enumerate() {
    if row-idx < n {
      let row-len = row.len()
      let y = n - row-idx
      for (col-idx, val) in row.enumerate() {
        let x = col-idx - row-len / 2 + 0.5

        let next-row-len = row-len + 1
        let left-child-x = col-idx - next-row-len / 2 + 0.5
        let right-child-x = (col-idx + 1) - next-row-len / 2 + 0.5
        let child-y = y - 1

        line((x, y / 1.5), (left-child-x, child-y / 1.5), stroke: gray)
        line((x, y / 1.5), (right-child-x, child-y / 1.5), stroke: gray)
      }
    }
  }

  // draw values
  for (row-idx, row) in pascal.enumerate() {
    let row-len = row.len()
    let y = (n - row-idx) / 1.5
    for (col-idx, val) in row.enumerate() {
      let x = col-idx - row-len / 2 + 0.5
      circle((x, y), radius: 0.25, fill: white, stroke: none, name: "c-" + str(row-idx) + "-" + str(col-idx))
      content((x, y), str(val))
    }
  }
})))

#figure(image("Pascals triangle-2.png"),caption: [Another form of Pascals triangle. It is mirrored])

Binomial identity:

$ mat(n;k) = mat(n;n-k) $
You can do analytic proof (mathematic proof using the formulas) or the combinatorial proof (count it in one way to get first result, then in a different way to get the other result).

*Combinatorial proof for $mat(n;k)=mat(n;n-k)$:* You could say that for n people, you ask k people to walk out the door, or you could ask `n-k` people to stay in the room.

In Pascal's triangle, adding the rows: 
$ mat(n;0)+mat(n;1)+mat(n;2)+dots+(n;n)=sum^n_(k=0)mat(n;k)=2^k $

= Binomial identity
For $mat(n;k)$ you can also write $mat(n-1;k)+mat(n-1;k-1)$. It is called Pascal's identity.
#image("pascals identity.png")

= Binomial formula
$ (x+y)¹=x¹+y¹\
(x+y)²=x²+2x y+y²\
(x+y)³=x³ + 3x²y + 3x y³ + y³\
(x+y)⁴=x⁴+4x³y+6x²y²+4x y³+y⁴
$
Note for each $x^(a)y^(b)$, you have $(x+y)^(a+b)$

You also have that the coefficients (for $(x+y)⁴$ you have 1,4,6,4,1) they follow Pascal's triangle

== The formula
$ (x+y)^n&=mat(n;0)x^n+mat(n;1)x^(n-1) y + mat(n;2)x^(n-2)y²+dots+mat(n;n-1)x y^(n-1)+mat(n;n)y^n\
&=sum^n_(k=0) mat(n;k)x^(n-k)y^k $

$ (x+y)^n=sum^n_(k=0)mat(n;k)x^k y^(n-k)$

== Proof by induction with respect to `n`
*Basis step:* Put $n=1$ and prove that the formular is true:
$ (x+y)¹&=mat(1;0)dot x⁰ dot y¹ + mat(1;1)dot x¹ dot y⁰\
 &=y+x $
 *Induction step:* Assume true for `n-1`, then prove that it's then true for `n`:
 $ (x+y)^(n-1)=sum^(n-1)_(k=0)mat(n-1;k)x^k y^(n-1-k) $
 $  
 (x+y)^n&=(x+y)dot (x+y)^(n-1) \
 &= (x+y)dot sum^(n-1)_(k=0)mat(n-1;k)x^k y^(n-1-k)\
 &=sum^(n-1)_(k=0)mat(n-1;k)x^(k+1)y^(n-1-k)+sum^(n-1)_(k=0)mat(n-1;k)x^k y^(n-k)\
 &= x^n+ sum^(n-2)_(k=0)mat(n-1;k)x^(k+1)y^(n-1-k) + sum^(n-1)_(k=1)mat(n-1;k)x^k y^(n-k)+y^n\
 &"Replace k+1 with k"\
 &= x^n+ sum^(n-1)_(k=1)mat(n-1;k-1)x^(k)y^(n-k) + sum^(n-1)_(k=1)mat(n-1;k)x^k y^(n-k)+y^n\
 & "Note from pascals identity:" mat(n-1;k-1) + mat(n-1;k) = mat(n;k)\
 $

 *Combinatorial Proof:*
  
  Consider expanding $(x+y)^n = (x+y)(x+y)dots.c(x+y)$ (n factors).
  
  To get a term $x^(n-k)y^k$, we must:
  - Choose $k$ of the $n$ factors to contribute a $y$ (the rest contribute $x$)
  - There are $binom(n,k)$ ways to do this
  
  Therefore, the coefficient of $x^(n-k)y^k$ is $binom(n,k)$.

= Van der Mande
 $mat(m+n;r)=sum^r_(k=0)mat(m;r-k) mat(n;k)$