A blog about math and programming
/
Recent content on A blog about math and programmingHugo -- gohugo.ioen-usTue, 19 Sep 2017 00:00:00 +0000Plotting 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.