Friday, September 11, 2015

Aspect ratio calculation

Earlier today I was writing a migration script in F# where I had to calculate the aspect ratio based on the given screen dimensions. This is one of those problems where I don't even mind breaking my head over, but directly head over to Stackoverflow to find an accepted answer which I can just copy paste. Since I didn't find an F# snippet I could use, I ported some JavaScript, and embedded the result below for future snippet hunters.

The aspectRatio function does two things: 1. Recursively find the greatest common divisor between the width and height 2. Divide the width and height by the greatest common divisor

Monday, June 22, 2015

Basic casino math

In a previous series of posts, I went over the models used by casinos to spin a wheel (spinning, manipulating the odds, clustering and near misses). I did not yet expand on the basic mathematical models that ensure a casino makes money.

Let's pretend we are spinning the wheel again. The wheel has 5 pockets, and just one of those is the winning one. Given we will be using an unmodified wheel, you win 1 out of 5 spins. Each bet costs you 1 euro. Looking at the true odds (1/5), the casino should pay out 4 euro for you to break even.

Respecting the true odds would not make the casino any money, they pay out less to ensure that the house has an edge on you. So instead of paying out 4 euro, it will be a tad less.

The house edge can be cast into a fairly simple formula.

In this example, the house edge is a whopping 20%, meaning statistically 20% of each bet will go to the casino. So the higher the house edge, the better?

Not really, if players constantly go through their bankroll in a matter of minutes, it's not very likely they will keep returning to your casino. The inverse to the house edge, and maybe even a more important number, is the payout percentage. When the house edge is 20%, the player's payout percentage will be 80%. For each bet you make, you will statistically see a return of 80%. As a player to get maximum value for money - to play as long as possible - you should aim to play in a casino that has the highest payout percentages.

Often misunderstood is that this does not mean you will get to keep 80% of your bankroll by the end of the night. The payout percentage relates to a single bet. The casino's hold, or money eventually left on the table, is several times the house edge, since players tend to circulate through the same money more than once. So the longer you play, the more the house edge will nibble at your bankroll.

Knowing the house edge, it's pretty simple for a casino to predict a customer's worth; multiply the house edge, the average stake and the number of games per hour.

Given we spin the wheel 60 times an hour for a stake of 1 euro, we will make the casino 12 euro an hour on average. The higher this number, the bigger your potential, the harder a casino will try to make you a regular.

Understanding how casinos make a living, it's safe to say casinos aren't the place to play for money, but to play with money.

Sunday, May 24, 2015

Consumed: Queries and projections (F#)

This is the third post in my series on porting a node.js application to an F# application.

So far, I've looked at parsing command line arguments, handling commands and storing events. Today, I want to project those events into something useful that can be formatted and printed to the console.

In the original application, I only had a single query. The result of this query lists all items consumed grouped by category, sorted chronologically

Handling the query is done in a similar fashion to handling commands. The handle function matches each query and has a dependency on the event store.

Where C# requires a bit of plumbing to get declarative projections going, F#'s pattern matching and set of built-in functions give you this for free.

We can fold over the event stream, starting with an empty list, to append each item that was consumed, excluding the ones that were removed later. Those projected items can then be grouped by category, to be mapped into a category type that contains a sorted list of items.

The result can be printed to the console using a more imperative style.

And that's it, we've come full circle. We can now consume items, remove items and query for a list of consumed items.

Compared to the node.js implementation, the F# version required substantially less code (two to three times less). More importantly, although I wrote tests for both, I felt way more confident completing the F# version. A strong type system, discriminated unions, pattern matching, purity, composability and a smart compiler makes way for sensible and predictable code.

Source code is up on Github.