illustrative abstractions

0%

# Discrete Random Variable

Given $X$, a discrete random variable,

Population Mean (Expectancy)

Population Variance

If two discrete random variables $X$ and $Y$ are independent,

# Bernoulli Distribution

If success, $X = 1$. If failure, $X = 0$.

# Binomial Distribution

$X =$ number of successes in $n$ independent repetitions of the experiment.

Define $X_i$ as the $i$th repetition’s success or not. $X_i$ follows a Bernoulli Distribution.

# Geometric Distribution

$X =$ number of tries up to and including first success

Let

Given $0 < q < 1, \quad n \xrightarrow{} \infty$

Such that $E(X) = \frac{p}{(1 - q)^2} = \frac{1}{p}$

Using the same method as above, $E(X^2) = p\frac{1 - q^2}{(1 - q)^4} = p\frac{1 + q}{(1 - q)^3} = \frac{1 + q}{(1 - q)^2}$

# Poisson Distribution (To be continued…)

Poisson Distribution is essentially a binomial distribution with very large $n$ (very small time interval).

## Description

I am developing a mobile application lately with Swift on iOS, with a provided and working back-end written in Python, with the Flask framework. The back-end project utilises a set of plugins named flask-security to handle login and user-related work.

This is an awesome plugin that works right out of the box. Almost little to no configuration is needed and it works like a charm on the webpage. It utilises a session-based authentication method that stores the key to sensitive information in browsers as cookies, which expires after a certain amount of time for security purposes.

Although session-based authentication is a viable way to set up a web application, this poses certain issues to a mobile client where we cannot store cookies like browsers and shall not record user password and send them out over and over again in every HTTP request.

## Initial Solution

Therefore, we will need a token-based authentication method, where a JSON Web Token (JWT) is issued the first time we log in. For all following requests, we package this JWT in the header as a way of verifying legit requests. For our particular plugin flask-security, this is done by sending an application-json request instead of x-www-form-urlencoded to the endpoint login.

## BUT…

But, the problem is not that easy. This is where Cross-Site Request Forgery (CSRF) comes into play. If we allow clients to simply authenticate by sending a JSON request with username and password, any other websites can fake that and make malicious requests. How to deal with this and have token-based authentication with CSRF protection at the same time?

## Description

Alter/Nate

Two friends, Alter and Nate, have a conversation:

Alter: Nate, let’s play a game. I’ll pick an integer between 1 and 10 (inclusive), then you’ll pick an integer between 1 and 10 (inclusive), and then I’ll go again, then you’ll go again, and so on and so forth. We’ll keep adding our numbers together to make a running total. And whoever makes the running total be greater than or equal to 100 loses. You go first.

Nate: That’s not fair! Whenever I pick a number X, you’ll just pick 11-X, and then I’ll always get stuck with 99 and I’ll make the total go greater than 100.

Alter: OK fine. New rule then, no one can pick a number that would make the sum of that number and the previous number equal to 11. You still go first. Now can we play?

Nate: Um… sure.

Who wins, and what is their strategy?

## Solution

The first player, Nate, is guaranteed to win in this case.

Our goal is to let Nate get the sum of all $12k + 3$, which would eventually let him get $12 \times 8 + 3 = 99$, a guaranteed win.

Therefore, our strategy for Nate is to let first pick number $3$, which contributes to the offset. Later, for him to increment by $12$ each round, if Alter picks any number $x > 1$, Nate can pick the number $2 \leq 12 - x \leq 10$. Otherwise if Alter picks $1$, Nate then picks $1$. Given the rule that two consecutive number cannot add up to the sum of $11$, Alter can only pick $y \leq 9$ in this case. Such that in these three rounds $1 + 1 + y \leq 11$ (Alter, Nate, Alter). Nate will then pick $1 \leq 12 - 2 - y \leq 9$ to reach $12k + 3$.

If Nate can guarantee he reaches a sum of $12k + 3 \quad \forall k \geq 0$, he wins the game by reaching $99$.

## More

For more puzzles, checkout

• Jane Street Puzzles

## Description

It is quite painful where you have a decent video API, but doesn’t provide you with a thumbnail. This makes it very hard to demonstrate to users what the video looks like a first glance.

Therefore, we have came up with a technique that buffers the first minute of a video and generate a thumbnail on the fly.

## Code

Using AVAsset, we can write an extension that dispatches a thread dedicated to generate a thumbnail (also duration of the video in the case below).