2017


Monty Carlo Pi Approximation

Topic: Maths
Date: 03/06/2017

What is PI?

To put it simply, PI is the ratio of a circles circumference to its diameter. We can find the irrational constant with the following calculation. Assuming we have a circle with a diameter $D$, we then measure the circumference $C$ of the circle. $\pi$ can then be calculated by $C/D$.

$$ \begin{aligned} let \quad D &= 5, \\ C &= 15.72 \\ \therefore \quad \pi &\approx \frac{C}{D} = \frac{15.72}{5} = 3.144 \end{aligned} $$

Its not particularly accurate, but neither was the measurement I got for the circumference. Now we know how to calculate PI by 2 measurable quantities, it doesn't take much to see why the circumference of a circle is found by the formula $C = 2 \pi r = \pi D$. So if we had a circle with a diameter of 1, it's circumference would be equal to $\pi$.

As a side note, we can find the equation for the area of a circle by integrating the circumference equation (integration gives the area under a curve).

$$ \begin{aligned} C &= \pi D = 2\pi r\\ A &= \int 2\pi r \, dr = \pi r^2 \end{aligned} $$

Approximating PI

While the above method will give us a value for $\pi$, it's not exactly easy/possible to calculate via a computer, i.e., how do you measure the circumference of a circle draw on a screen automatically? The answer is... you don't. There are many other ways of calculating the decimal places of $\pi$. The Gauss-Legendre algorithm is a particularly interesting example of how to calculate $\pi$ (more can be found here). It's an iterative algorithm, whereby each pass through doubles the precision for the value of $\pi$. Since I write code in Erlang for a living, I have also provided an example solution for the algorithm here. It is worth noting, that running the algorithm past 3 iterations will just return the same number. This is because of the limit to floating point numbers in Erlang.

Monty Carlo Method

For this post however, I am going to use a pretty basic Monty Carlo method. The idea is to place a unit circle inside a 1x1 square. We can then get a formula for $\pi$ based off the ratio of the circle's area and the square's area. $A_c$ is the area of the circle, $A_s$ is the area of the square.

$$ \begin{aligned} A_c &= \pi r^2 \\ A_s &= (2r)^2 = 4r^2 \\ \frac{A_c}{A_s} &= \frac{\pi r^2}{4r^2} = \frac{\pi}{4} \end{aligned} $$

Given the above, we can see that $\pi$ can be calculated as $4 \times \frac{A_c}{A_s}$. The simulation below uses random sampling to produce a value for this ratio thus letting us approximate $\pi$ (albeit very badly).

0

1000

$\pi \approx$ 4 * Inside / All

Accuracy = | Math.PI - PI Approximation |

Links:

Gauss-Legendre algorithm | Other algorithms | Monty Carlo Pi Approximation | Gauss-Legendre (Erlang Implentation)

Docker Notes

Topic: Docker
Date: 26/03/2017

What is Docker

Docker is a CI/CD tool that utilises containers for code deployment. Docker containers are instantiated from a composition of many docker images. Ranging from a simple OS level image to more advanced automatic build and deployment images. Docker images are generated from simple Dockerfiles which lay out the commands that the container runs either before or after its instantiation. Docker is useful when wanting to run the same piece of code on different machine architectures as it works much like a virtual machine would.

CLI tools

This section describes some of the command line tools that docker offers, as well as some useful tricks.

Login

Generally the first thing that you should do when running docker is to login. This is done with the docker login command. You will then be prompted for your docker username and password.

Running a container

Once logged in, the next step is to run an image. This is done via the docker run imagename command. If the image is not currently downloaded to your computer then it will be downloaded from the docker hub. You can update a previously downloaded image by using the docker pull imagename command. The run command is often run with several flags. A particularly useful flag is the --name flag, which lets you set a name for the container that you can use in other commands later on. -p port:externalport can map an exposed port in the container to a system port, i.e., running several of the same container and mapping different ports to the containers without having to change the code. -d lets you run the container as a background process.

Container management

Much like in a linux terminal, you can use ps to show the currently running containers. The flag -a shows all of the containers on the system.

You can remove a container with the docker rm container . If the container is currently running, you can either stop it with docker stop or use the -f flag to forcefully remove the container. You can remove all of the containers on the machine indescriminately by running the following docker rm $(docker ps -a -q) . Alternatively you can use the --rm flag when running the image so that the container is removed when it exits.

Images

To see a list of the currently downloaded images on the computer run the docker images command. To remove a specific image you can use the docker rmi imagename command.

Arguably the most important part of docker is the ability to create your own images with ease. This is done by using a dockerfile and then calling the build command. The easiest way of doing this is inside the same directory that the dockerfile is in. It is important to give the image a tag so that it can be easily referred to later on. docker build -t imagename . .

Once the image has been built, you can link it to a remote repo in the docker hub by using the docker tag localname remote/name . The once a link has been made, push the image to the hub docker push remote/name .

Dockerfiles

Dockerfiles are what docker uses to build the docker images. Each docker file contains a base docker image defined by the FROM image statement. This image is then built upon by using various other rules. The most common other rules being RUN and ADD . RUN will execute a system command for the OS defined by the base image. ADD will copy files from the build computer to the container.

Below is an example of how to create a golang dockerfile via a ubuntu image.

FROM ubuntu

MAINTAINER Mark Hillman

# Add the go path to the system
ENV GOPATH /go

# Copy over the files to the gopath dir
ADD . /go/src/github.com/imitablerabbit/go-project

# Update and install golang via apt
RUN apt-get update && \
	apt-get install -y golang && \
	go install github.com/imitablerabbit/go-project

# Set the entrypoint of the container.
ENTRYPOINT /go/bin/go-project

# Expose the port
EXPOSE 8080

There is a much better way though. You can just use the pre-existing golang images. The following example is based on the Go blog docker examples.

FROM golang:1.6-onbuild
MAINTAINER Mark Hillman
EXPOSE 80

Links:

Golang Docker blog