My first GM experience

Grand Meetup is an event here at Automattic where the whole company gathers once a year to do classes, projects, or just hack around ideas in general.

This year it was held in Whistler, BC, Canada.

Working remotely with strangers and meeting them in person was a bit weird and overwhelming in the beginning.

However, I had already met my team during a team meetup in Vienna, so hanging out with them during the GM was more relaxing 🙂

I also met Matt!

Then, besides meeting all of these nice folks, I also played my first D&D game ever! My character was a Human Wizard (named Matt :D) and we slayed the dragon!

The food at the hotels was really good!

The village had some really nice views! We went for some souvenir shopping.

Then, for the closing party we had some arcades!

Also, my team Woo rocked the stage 😀

Some stats: before the GM I had met 1% of all Automatticians. Now that has moved up to around 20%. With that, my objective of meeting all the folks that I have interacted or worked with was completed!

So a few words about the company: Automattic is about transparency, passion, freedom, impact, people, differences. If you care about these things, Automattic is hiring and you can apply.

Thank you A8c for this great experience!

Recursive and iterative processes and procedures

During my js ⊂ lisp talk at beerjs, I noticed there were a few questions about recursion and tail call recursion, so with this post I hope to address that.

To start with, let’s define the following methods for creating a pair and getting its values:

function cons( $a, $b ) {
return function( $second ) use ( $a, $b ) {
return $second ? $b : $a;
};
}

function car( $pair ) {
return $pair( false );
}

function cdr( $pair ) {
return $pair( true );
}

An iterative process is characterized by a process whose state is captured with variables and rules how to transform them to the next state.

A recursive process is characterized by a chain of deferred evaluations. Note that a recursive procedure need not always generate a recursive process.

An iterative procedure is a procedure that does not have any references to itself.

A recursive procedure is a procedure that has references to itself.

With this, we see that there are recursive processes, and there are recursive procedures. Combining those, we get the following table:

Process / Procedure Iterative Recursive
Iterative 1 2
Recursive 3 4

Now that we’ve classified the combinations, we can discuss their properties and characteristics by examples:

  1. An iterative process with iterative procedure. No recursion is involved, and there is no need for deferred evaluations (since the process is iterative). It can be further optimized using other looping constructs than goto, but we will write it this way to show the similarity with 3.
    // Iterative process, iteration (for languages that don't support tail call optimization)
    function lengthiter_i( $pair, $acc = 0 ) {
    begin:
    if ( null === $pair ) {
    return $acc;
    }        $pair = cdr( $pair );
    $acc  = 1 + $acc;
    goto begin;
    }
    
  2. A recursive process with iterative procedure. Recursion is involved, but since the procedure is iterative, we will implement it with the help of a list (stack), e.g. to allow for deferred evaluations in some cases:
    // Recursive process, iteration
    function lengthrec_i( $pair ) {
    $stack = [];
    $acc = 0;        while ( null !== $pair ) {
    array_push( $stack, 1 );
    $pair = cdr( $pair );
    }while ( ! empty( $stack ) ) {
    $acc += array_pop( $stack );
    }
    
    return $acc;
    }
    
  3. An iterative process with recursive procedure. Similar to the iterative process with iterative procedure, although differently implemented. May fail on languages that don’t have tail call optimization. Compare this code to 1 to see the similarity (additionally we map the arguments $pair => cdr( $pair ) and $acc => 1 + $acc).
    // Iterative process, recursion
    function lengthiter_r( $pair, $acc = 0 ) {
    if ( null === $pair ) {
    return $acc;
    }        return lengthiter_r( cdr( $pair ), 1 + $acc );
    }
    
  4. A recursive process with recursive procedure. Similar to the recursive process with iterative procedure, although differently implemented in the way that the stack is implicit by calling the function multiple times, so we don’t need to handle it ourselves. For example:
    // Recursive process, recursion
    function lengthrec_r( $pair ) {
    if ( null === $pair ) {
    return 0;
    }        return 1 + lengthrec_r( cdr( $pair ) );
    }
    

Deriving derivative and integral

To reach to here, we first had to work out the point slope formula and then figure out limits. Derivatives are very powerful. This post was inspired by doing gradient descent on artificial neural networks, but I won’t cover that here. Instead we will focus on the very own definition of a derivative.

So let’s get started. A secant is a line that goes through 2 points. In the graph below, the points are A = (x, f(x)) and A' = (x + dx, f(x + dx)).

To derive a formula for this, we can use the point-slope form of a equation of a line: y - y_0 = \frac {y_1 - y_0} {x_1 - x_0} (x - x_0).

Plugging in the values, we get: f(x) - f(x + dx) = - \frac {f(x + dx) - f(x)} {dx} (dx).

What is interesting about this formula using the secant is that, as we will see, it provides us with a neat approximation at f(x).
Let’s define f_{new}(x, dx) = \frac {f(x + dx) - f(x)} {dx}. So now we have: f(x + dx) = f(x) + f_{new}(x, dx) (dx).

The limit as dx approaches 0 for f_{new} will give us the actual slope (according to the definition of an equation of a line) at x.

So, let’s define \lim_{dx \to 0} f_{new}(x, dx) = f'(x). This slope is actually our definition of a derivative. This definition lies at the heart of calculus.

The image below (taken from Wikipedia) demonstrates this for h = dx.

Back to the secant approximation, we now have: f(x + dx) \approx f(x) + f'(x) (dx). This is an approximation rather than an equivalence because we already calculated the limit for one term but not the rest. As dx -> 0, the approximation -> equivalence.

For example, to calculate the square of 1.5, we let x = 1 and dx = 0.5. Additionally, if f(x) = x^2 then f'(x) = x*2. So f(1 + 0.5) = f(x + dx) \approx f(1) + f'(1) 0.5 = 1 + 2 * 1 * 0.5 = 2. That’s an error of just 0.25 for dx = 0.5. Algebra shows for this particular case the error to be dx^2. For dx = 0.1, the error is just 0.01.

Pretty cool, right?

Here are some of the many applications to understand why derivatives are useful:

  • We can use the value of the slope to find min/max using gradient descent
  • We can determine the rate of change given the slope
  • We can find ranges of monotonicity
  • We can do neat approximations, as shown

Integrals allow us to calculate the area under a function’s curve. As an example, we’ll calculate the area of the function f(x) = x in the interval [0, 2]. Recall that the area of a rectangle with size w by h is w \cdot h. Our approach will be to construct many smaller rectangles and sum their area.

We start with the case n = 2 – two rectangles. We have the data points x = (1, 2), which give us two rectangles with width and height (1, f(1)) and (1, f(2)) respectively – note the width is constant because the spaced interval is distributed evenly. To sum the area of this spaced interval, we just sum 1 \cdot f(1) + 1 \cdot f(2) = 3. But note that there’s an error, since the rectangles do not cover the whole area. The main idea is the more rectangles, the less the error and the closer we get to the actual value.

Proceed with case n = 4. We have the data points x = (0.5, 1, 1.5, 2). Since we have four elements, in the range [0, 2] each element has width of \frac{2 - 0}{4} = 0.5. The result is 0.5 [ f(0.5) + f(1) + f(1.5) + f(2) ] = 2.5.

Having looked at these cases gives an idea to generalize. First, note the differences of the points in x – when n = 2, the difference between any consecutive points in x is 1, and when n = 4, the difference is 0.5. Generalizing for n, the difference between x_i and x_{i+1} will be \frac{b-a}{n}. Also, generalizing the summation gives \sum_{i=0}^n f(x_i) \cdot \Delta x_i, and since we only consider evenly spaced intervals we have \Delta x_i = \Delta x, for all i. This is called a Riemann sum and defines the integral \int _{a}^{b}f(x)\,\Delta x=\lim_{\Delta x \to 0}\sum_{i=0}^{n}f(x_{i})\Delta x, where \Delta x = \frac{b - a}{n}. Also, since a is the starting point, that gives x_i = a + i \cdot \frac{b-a}{n}.

Going back to the example, to find the interval for f(x) = x, we need to calculate \sum_{i=0}^{n}f(x_{i}) \frac{b - a}{n} = \frac{b - a}{n} \sum_{i=0}^{n}(a + i \cdot \frac{b-a}{n}). From here, we evaluate the inner sum \sum_{i=0}^{n}(a + i \cdot \frac{b-a}{n}) = (n+1)a + \frac{(b - a)(n+1)}{2}. Plugging back in gives (b - a + \frac{b}{n} - \frac{a}{n}) (a + \frac{b - a}{2}).

Now we can take the limit of this as n \to \infty. Note that \lim_{n \to \infty} \frac{a}{n} = 0 so we have (b - a)(a + \frac{b-a}{2}), which finally gives \frac{b^2 - a^2}{2}. This represents the sum of the area [a, b] under the curve of the function f(x) = x.

Definition of the mathematical limit

This post assumes knowledge in mathematical logic and algebra.
We will stick to sequences for simplicity, but the same reasoning can be extended to functions. For sequences we have one direction: infinity.

Informally, to take the limit of something as it approaches infinity is to determine its eventual value at infinity (even though it may not ever reach it).

As an example, consider the sequence a_n = \frac {1} {n}. The first few elements are: 1, 0.5, 0.(3), 0.25, and so on.

Note that \frac {1} {\infty} is undefined as infinity is not a real number. So here come limits to the rescue.

If we look at its graph, it might look something like this:

We can clearly see a trend that as x -> infinity, 1/x tries to “touch” the horizontal axis, i.e. is equal to 0. We write this as such: \lim_{n \to \infty} \frac {1} {n} = 0.

Formally, to say that \lim_{n \to \infty} a_n = a, we denote: (\forall \epsilon > 0) (\exists N \in \mathbb {N}) (\forall n \geq N) (|a_n - a| < \epsilon). Woot.

It looks scary but it’s actually quite simple. Epsilon is a range, N is usually a member that starts to belong in that range, and the absolute value part says that all values after that N belong in this range.

So for all ranges we can find a number such that all elements after that number belong in this range.

Why does this definition work? It’s because when the range is too small, all elements after N belong in it, i.e. the values of the sequence converge to it endlessly.

As an example, let’s prove that \lim_{n \to \infty} \frac {1} {n} = 0. All we need to do is find a way to determine N w.r.t. Epsilon and we are done.

Suppose Epsilon is arbitrary. Let’s try to pick N s.t. N = \frac {1} {\epsilon}.

Let’s see how it looks for \epsilon = \frac {1} {2}: for n > \frac {1} {\epsilon} = 2, we have: a_n < \epsilon. This is obviously true since 1/4 < 1/3 < 1/2. So there’s our proof.

This bit combined with the slope point formula form the derivative of a function, which will be covered in the next post.

Deriving point-slope from slope-intercept form

In this post we’ll derive the form of a linear equation between two points by simply knowing one thing:

Given y = cx + d, this line passes through the point A = (x, y).

The inspiration of this post is deriving the derivative.

So, let’s get started. We want to find an equation of a line that passes through A = (a, f(a)), B = (b, f(b)).

So we plug the points into the equation of a line to obtain the system:

\begin{cases} f(a) = ma + d \\ f(b) = mb + d \end{cases}

Solving for m, d:

\begin{cases} f(a) - ma = d \\ (f(b) - f(a))/(b - a) = m \end{cases}

To eventually conclude y - f(a) = m(x - a).

In some of the next posts, we will derive the formula of a derivative of a function.