A blog about math and programming
/
Recent content on A blog about math and programmingHugo -- gohugo.ioen-usTue, 20 Aug 2019 00:00:00 +0000Solving a problem with a generalized outer product
/2019/08/20/solving-a-problem-with-a-generalized-outer-product/
Tue, 20 Aug 2019 00:00:00 +0000/2019/08/20/solving-a-problem-with-a-generalized-outer-product/Introduction I learned about this interesting problem from a recent blog post by @ryxcommar.
Imagine you have a time-series of the realized price of some stock:
library(ggplot2) set.seed(2) df <- data.frame(n = 1:100, y = cumsum(rnorm(100))) head(df) ## n y ## 1 1 -0.8969145 ## 2 2 -0.7120654 ## 3 3 0.8757800 ## 4 4 -0.2545957 ## 5 5 -0.3348475 ## 6 6 -0.2024272 p <- ggplot(df, aes(x = n, y = y)) + geom_line() p With the benefit of hindsight, when would’ve been the ideal period to buy and sell that stock?XKCD and the normal distribution
/2019/03/04/xkcd-and-the-normal-distribution/
Mon, 04 Mar 2019 00:00:00 +0000/2019/03/04/xkcd-and-the-normal-distribution/Thomas Lumley (@tslumley) has a nice blog post out today where he confirms the calculations of this XKCD picture:
In this post I do the same calculation, but take a somewhat different approach.
By exploiting the symmetry of the normal distribution, we can focus on calculating just the area in left part of the curve, and then double that to get the total area.
library(ggplot2) theme_set(theme_classic()) g <- ggplot() + stat_function(aes(-3:0), fun = dnorm) + geom_vline(xintercept = 0) + labs(x = NULL, y = NULL) g The XKCD picture tells us that the gray area is centered around the midpoint and constitute 52.Probability kata
/2017/12/20/probability-kata/
Wed, 20 Dec 2017 00:00:00 +0000/2017/12/20/probability-kata/Introduction While having dinner with a colleague last night, my colleague told me that he’d played a dice game with a risk-loving opponent (who also apparently made a lot of money on the Bitcoin bubble). My colleague said that they had each rolled a six-sided die ten times, and that his friend had (correctly!) bet that he would roll higher on all ten rolls. This seemed implausible to me, but exactly how implausible?Using a multivariate normal to draw a flower in ggplot2
/2017/11/24/using-a-multivariate-normal-to-draw-a-flower-in-ggplot2/
Fri, 24 Nov 2017 00:00:00 +0000/2017/11/24/using-a-multivariate-normal-to-draw-a-flower-in-ggplot2/Here’s a dumb thing I did over lunch; as I was playing around with the geom_density_2d and random draws from the standard multivariate normal distribution, I realized that the outcome looked a bit like the flower of a rose. So I figured I would try to see how difficult it would be to draw the full rose using ggplot2. So here goes.
knitr::opts_chunk$set(dev = "svg") library(tidyverse) We start with the stem.Trivial probability problem solved intuitively, rigorously, and by simulation
/2017/11/21/probability/
Tue, 21 Nov 2017 00:00:00 +0000/2017/11/21/probability/Introduction In this post we will consider a probability problem that is so simple we can find its solution after a moment’s thought. We will then over-engineer that solution in terms of mathematical rigour, and finally show how that mathematical model can be translated in a one-to-one fashion into R code. We will then use that code to simulate the problem and see if it matches our earlier solutions.
Problem (I found this problem on Brilliant, which is a fun app and website for doing math problems.Plotting consumer and producer surpluses in ggplot2
/2017/09/19/plotting-consumer-and-producer-surpluses-in-ggplot2/
Tue, 19 Sep 2017 00:00:00 +0000/2017/09/19/plotting-consumer-and-producer-surpluses-in-ggplot2/Andrew Heiss has an interesting recent post where he uses ggplot2 to plot a nicely annotated supply-demand diagram. In this post we will take a somewhat different approach to solve the same problem, with the added feature of filling in the areas representing consumer and producer surpluses.
We start by defining supply and demand functions.
library(tidyverse) demand <- function(q) (q - 10)^2 supply <- function(q) q^2 + 2*q + 8 We plot these over a specified domain using stat_function.Using purrr to refactor imperative code
/2017/08/24/using-purrr-to-refactor-imperative-code/
Thu, 24 Aug 2017 00:00:00 +0000/2017/08/24/using-purrr-to-refactor-imperative-code/Introduction In a recent blog post, Nathan Eastwood solved the so-called Twitter Waterfall Problem using R. In his post, Nathan provides two solutions; one imperative approach using a large for-loop, and one more substantive approach using R6.
This post uses Nathan’s first solution as a case study of how to refactor imperative code using a more functional approach.
Problem Consider this picture:
The blocks here represent walls, and we’re imagining what would happen if water were poured onto this structure.Fixed points
/2017/08/21/fixed-points/
Mon, 21 Aug 2017 00:00:00 +0000/2017/08/21/fixed-points/In mathematics, a fixed point of a function is an element that gets mapped to itself by that function. For example, the function
\[ f : \mathbb{R} \rightarrow \mathbb{R} \] \[ f(x) = x^2 \]
maps the elements 0 and 1 to themselves, since \(f(0) = 0^2 = 0\) and \(f(1) = 1^2 = 1\).
To illustrate the concept, we could define a function fixed_points which maps functions to the set of their fixed points.Most popular ggplot2 geoms
/2017/08/10/most-popular-ggplot2-geoms/
Thu, 10 Aug 2017 00:00:00 +0000/2017/08/10/most-popular-ggplot2-geoms/In this post we will use the gh package to search Github for uses of the different geoms used in ggplot2.
We load some necessary packages.
library(tidyverse) library(scales) library(stringr) library(gh) Next, we find all the functions exported by ggplot2, keep only those of the form geom_*, and put them in a data frame.
geoms <- getNamespaceExports("ggplot2") %>% keep(str_detect, pattern = "^geom") %>% data_frame(geom = .) geoms ## # A tibble: 44 x 1 ## geom ## <chr> ## 1 geom_text ## 2 geom_vline ## 3 geom_col ## 4 geom_tile ## 5 geom_qq ## 6 geom_label ## 7 geom_line ## 8 geom_smooth ## 9 geom_path ## 10 geom_spoke ## # .Taking powers of a matrix in R
/2017/08/08/taking-powers-of-a-matrix-in-r/
Tue, 08 Aug 2017 00:00:00 +0000/2017/08/08/taking-powers-of-a-matrix-in-r/Consider a number, say 4. If we want to multiply this number by itself 3 times we can write this as either \(4 \times 4 \times 4\), or, more compactly, as \(4^3\). This operation is known as exponentiation and follows some well-known algebraic laws and conventions. For example, for any non-zero number \(x\) we have:
\(x^k = \overbrace{x \times x \times \dots \times x}^{k \text{ times}}\) \(x^1 = x\) \(x^0 = 1\) \(x^{-1} = \frac{1}{x}\) \(x^{-k} = (\frac{1}{x})^{k}\) This is all surely familiar to the reader.Drawing an annotated unit circle with ggplot2
/2017/08/07/drawing-an-annotated-unit-circle-with-ggplot2/
Mon, 07 Aug 2017 00:00:00 +0000/2017/08/07/drawing-an-annotated-unit-circle-with-ggplot2/The unit circle: everybody’s favorite circle.
I recently needed to an annotated unit circle for some teaching material I was preparing. Rather than using one of the countless pictures already available, I thought it was a good excuse to play around a bit with using mathematical annotations in ggplot2. This post explains the process.
Here’s what we’ll be working towards:
We start by defining a function that, given a radius, generates a data frame of coordinates of a circle centered around the origin.Some notes on data cleaning
/2017/08/06/some-notes-on-data-cleaning/
Sun, 06 Aug 2017 00:00:00 +0000/2017/08/06/some-notes-on-data-cleaning/Maëlle Salmon has a new post up where she looks at a data set on guest hosts on a popular Swedish radio show. In the post Maëlle specifically asks for constructive criticism on her code, so here are some thoughts.
Original approach Maëlle starts by manually downloading an xlsx file and converting it to a csv, which she then reads with readr::read_csv.
# all summer guests sommargaester <- readr::read_csv("data/p1sommar.csv", col_names = FALSE, locale = readr::locale(encoding = "latin1")) To deal with the fact that the names are of the form “Last name, First name”, she then defines a function to split the names, creates a new data frame with those names, and then left joins that data frame to the original one.Implementing a perfect-playing Nim opponent in R
/2017/08/05/implementing-a-perfect-playing-nim-opponent-in-r/
Sat, 05 Aug 2017 00:00:00 +0000/2017/08/05/implementing-a-perfect-playing-nim-opponent-in-r/It’s time for some (combinatorial) game theory In a previous post we wrote a program that allows you to play a game of Nim against another human opponent. In this post we will show how we can modify our initial program to also allow for play against a computer-opponent. To understand how this is possible, however, we must first discuss a bit of algebra.
As mentioned in the previous post, the game of Nim gives rise to a special type of numbers called, naturally, “nimbers”.About
/about/
Fri, 04 Aug 2017 00:00:00 +0000/about/Eric is a lawyer, economist, central banker, and mathematician-in-training. This is his blog.Implementing Nim in R
/2017/08/04/implementing-nim-in-r/
Fri, 04 Aug 2017 00:00:00 +0000/2017/08/04/implementing-nim-in-r/Introduction The game Nim is a classic of computer science. In this post we will implement an interactive version of Nim in R using a functional style of programming. The implementation will allow the user to play a game against a human opponent in an R console.
In a second post, we will use a bit of object-oriented programming (using R’s S3 system) to implement a quirky algebra called Nimbers, and show how these can be used to “solve” Nim.