~/devreads

#haskell

46 posts

13 Mar 2025

26 Apr 2020

Dominic Steinitz 1 min read

This is a short example of taking a stochastic system, using Fokker-Planck to convert it to a PDES and using SUNDIALS to solve a 2D partial differential equation in Haskell via the hmatrix-sundials library. The example is taken from the C examples that come with the SUNDIALS source. Here’s the full blog.

haskellnumerical methodsnumericspartial differential equations

18 Apr 2019

4 Apr 2019

19 May 2018

Dominic Steinitz 8 min read

Introduction This blog started off life as a blog post on how I use nix but somehow transformed itself into a “how I do data visualisation” blog post. The nix is still here though quietly doing its work in the background. Suppose you want to analyze your local election results and visualize them using a … Continue reading Cartography in…

haskellstatistics

4 Mar 2018

Dominic Steinitz 4 min read

Introduction These are some very hasty notes on Runge-Kutta methods and IRK2 in particular. I make no apologies for missing lots of details. I may try and put these in a more digestible form but not today. Some Uncomprehensive Theory In general, an implicit Runge-Kutta method is given by where and Traditionally this is written … Continue reading Implicit Runge…

haskellnumerical methods

25 Feb 2018

Dominic Steinitz 7 min read

Introduction For the blog post still being written on variatonal methods, I referred to the still excellent Bishop (2006) who uses as his example data, the data available in R for the geyser in Yellowstone National Park called “Old Faithful”. While explaining this to another statistician, they started to ask about the dataset. Since I … Continue reading Reproducibility and…

haskellstatistics

2 Jun 2017

Dominic Steinitz 35 min read

Introduction Summary Back in January, a colleague pointed out to me that GHC did not produce very efficient code for performing floating point abs. I have yet to produce a write-up of my notes about hacking on GHC: in summary it wasn’t as difficult as I had feared and the #ghc folks were extremely helpful. … Continue reading Haskell for…

haskellllvmnumerical methodsnumerics

18 Apr 2017

Dominic Steinitz 19 min read

Introduction Tribbles originate from the planet Iota Geminorum IV and, according to Dr. McCoy, are born pregnant. No further details are given but we can follow Gurtin and MacCamy (1974) and perhaps recover some of what happens on the Enterprise. Of course, age-dependent population models are of more than fictional use and can be applied, for … Continue reading Trouble…

haskellnumerical methodspartial differential equationsuncategorized

24 Nov 2016

Dominic Steinitz 8 min read

Introduction In most presentations of Riemannian geometry, e.g. O’Neill (1983) and Wikipedia, the fundamental theorem of Riemannian geometry (“the miracle of Riemannian geometry”) is given: that for any semi-Riemannian manifold there is a unique torsion-free metric connection. I assume partly because of this and partly because the major application of Riemannian geometry is General Relativity, … Continue reading Mercator: A…

haskellsemi-riemannian manifolds

26 Jun 2016

Dominic Steinitz 13 min read

Introduction In the 1920s, Lotka (1909) and Volterra (1926) developed a model of a very simple predator-prey ecosystem. Although simple, it turns out that the Canadian lynx and showshoe hare are well represented by such a model. Furthermore, the Hudson Bay Company kept records of how many pelts of each species were trapped for almost … Continue reading Ecology, Dynamical…

bayesianhaskellmachine learningnumerical methodsprobability

20 Jan 2016

Dominic Steinitz 19 min read

Introduction The equation of motion for a pendulum of unit length subject to Gaussian white noise is We can discretize this via the usual Euler method where and The explanation of the precise form of the covariance matrix will be the subject of another blog post; for the purpose of exposition of forward filtering / … Continue reading Particle Smoothing

bayesianhaskellstatistics

6 Dec 2015

Dominic Steinitz 15 min read

Introduction Let be a (hidden) Markov process. By hidden, we mean that we are not able to observe it. And let be an observable Markov process such that That is the observations are conditionally independent given the state of the hidden process. As an example let us take the one given in Särkkä (2013) where … Continue reading Naive Particle…

bayesianhaskellmachine learningprobabilitystatistics

12 Nov 2015

Dominic Steinitz 1 min read

Every so often, someone bitten by floating point arithmetic behaving in an unexpected way is tempted to suggest that a calculation should be done be precisely and rounding done at the end. With floating point rounding is done at every step. Here’s an example of why floating point might really be the best option for … Continue reading Floating Point:…

haskellnumerical methods

6 Jun 2015

Dominic Steinitz 4 min read

Introduction Let us see if we can estimate the parameter for population growth using MCMC in the example in which we used Kalman filtering. We recall the model. And we are allowed to sample at regular intervals In other words , where is known so the likelihood is Let us assume a prior of then … Continue reading Population Growth…

bayesianhaskellstatisticsmarkov chain monte carlo

31 May 2015

27 Apr 2015

Dominic Steinitz 3 min read

Introduction Suppose you want to sample from the truncated normal distribution. One way to do this is to use rejection sampling. But if you do this naïvely then you will run into performance problems. The excellent Devroye (1986) who references Marsaglia (1964) gives an efficient rejection sampling scheme using the Rayleigh distribution. The random-fu package … Continue reading Rejection Sampling

haskellprobabilitystatistics

11 Mar 2015

Dominic Steinitz 12 min read

Introduction Simple models for e.g. financial option pricing assume that the volatility of an index or a stock is constant, see here for example. However, simple observation of time series show that this is not the case; if it were then the log returns would be white noise One approach which addresses this, GARCH (Generalised AutoRegressive … Continue reading Stochastic…

bayesianfinancehaskellstatistics

9 Sept 2014

Dominic Steinitz 7 min read

Summary An extended Kalman filter in Haskell using type level literals and automatic differentiation to provide some guarantees of correctness. Population Growth Suppose we wish to model population growth of bees via the logistic equation We assume the growth rate is unknown and drawn from a normal distribution but the carrying capacity is known and … Continue reading Fun with…

bayesianhaskellmachine learningprobabilitystatistics

26 Aug 2014

Dominic Steinitz 2 min read

Introduction Suppose we have a vector of weights which sum to 1.0 and we wish to sample n samples randomly according to these weights. There is a well known trick in Matlab / Octave using sampling from a uniform distribution. num_particles = 2*10^7 likelihood = zeros(num_particles,1); likelihood(:,1) = 1/num_particles; [_,index] = histc(rand(num_particles,1),[0;cumsum(likelihood/sum(likelihood))]); s = sum(index); … Continue reading Haskell Vectors…

haskellprobability

23 Aug 2014

Dominic Steinitz 5 min read

Importance Sampling Suppose we have an random variable with pdf and we wish to find its second moment numerically. However, the random-fu package does not support sampling from such as distribution. We notice that So we can sample from and evaluate > {-# OPTIONS_GHC -Wall #-} > {-# OPTIONS_GHC -fno-warn-name-shadowing #-} > {-# OPTIONS_GHC -fno-warn-type-defaults … Continue reading Importance Sampling

bayesianhaskellstatistics

6 Aug 2014

Dominic Steinitz 6 min read

Introduction Suppose we have particle moving in at constant velocity in 1 dimension, where the velocity is sampled from a distribution. We can observe the position of the particle at fixed intervals and we wish to estimate its initial velocity. For generality, let us assume that the positions and the velocities can be perturbed at … Continue reading Fun with…

bayesianhaskellprobabilitystatistics

19 Jul 2014

Dominic Steinitz 3 min read

Suppose we wish to estimate the mean of a sample drawn from a normal distribution. In the Bayesian approach, we know the prior distribution for the mean (it could be a non-informative prior) and then we update this with our observations to create the posterior, the latter giving us improved information about the distribution of … Continue reading Fun with…

bayesianhaskellprobabilitystatistics

15 Jun 2014

Dominic Steinitz 10 min read

This is really intended as a draft chapter for our book. Given the diverse natures of the intended intended audiences, it is probably a bit light on explanation of the Haskell (use of monad transformers) for those with a background in numerical methods. It is hoped that the explanation of the mathematics is adequate for … Continue reading Gibbs Sampling…

bayesianhaskellstatistics

9 Apr 2014

Dominic Steinitz 8 min read

Introduction It’s possible to Gibbs sampling in most languages and since I am doing some work in R and some work in Haskell, I thought I’d present a simple example in both languages: estimating the mean from a normal distribution with unknown mean and variance. Although one can do Gibbs sampling directly in R, it … Continue reading Gibbs Sampling…

bayesianhaskellstatistics

2 Apr 2014

Dominic Steinitz 4 min read

Introduction The other speaker at the Machine Learning Meetup at which I gave my talk on automatic differentiation gave a very interesting talk on A/B testing. Apparently this is big business these days as attested by the fact I got 3 ads above the wikipedia entry when I googled for it. It seems that people … Continue reading Student’s T…

haskellstatistics

10 Feb 2014

Dominic Steinitz 19 min read

Introduction Suppose we have a square thin plate of metal and we hold each of edges at a temperature which may vary along the edge but is fixed for all time. After some period depending on the conductivity of the metal, the temperature at every point on the plate will have stabilised. What is the … Continue reading Laplace’s Equation…

haskellnumerical methodspartial differential equations

7 Dec 2013

23 Oct 2013

Dominic Steinitz 16 min read

I had a fun weekend analysing car parking data in Westminster at the Future Cities Hackathon along with Amit Nandi Bart Baddeley Jackie Steinitz Ian Ozsvald Mateusz Łapsa-Malawski Apparently in the world of car parking where Westminster leads the rest of UK follows. For example Westminster is rolling out individual parking bay monitors. Our analysis … Continue reading Parking in…

haskellstatistics

13 Oct 2013

Dominic Steinitz 12 min read

Preface The intended audience of this article is someone who knows something about Machine Learning and Artifical Neural Networks (ANNs) in particular and who recalls that fitting an ANN required a technique called backpropagation. The goal of this post is to refresh the reader’s knowledge of ANNs and backpropagation and to show that the latter … Continue reading Backpropogation is…

haskellmachine learningnumerical methodsstatistics

12 Sept 2013

Dominic Steinitz 7 min read

Introduction The planet Mercury has a highly elliptical orbit with a perihelion of about 0.31 AU and an aphelion of about 0.47 AU. This ellipse is not stationary but itself rotates about the Sun, a phenomenon known as the precession of the perihelion. A calculation carried out using Newtonian mechanics gives a value at variance … Continue reading The Precession…

haskellnumerical methods

6 Aug 2013

Dominic Steinitz 26 min read

This article attempts to show that Haskell [@Hudak:2007:HHL:1238844.1238856] performs reasonably well on numerical problems. When I started to do this, it seemed straightforward enough: pick a problem which admitted a numerical solution, find an algorithm and code it up. I chose the problem of orbital dynamics as I had always been fascinated by the precession … Continue reading Planetary Simulation…

haskellnumerical methodssymplectic manifolds

31 May 2013

Dominic Steinitz 18 min read

Introduction Neural networks are a method for classifying data based on a theory of how biological systems operate. They can also be viewed as a generalization of logistic regression. A method for determining the coefficients of a given model, backpropagation, was developed in the 1970’s and rediscovered in the 1980’s. The article “A Functional Approach … Continue reading Neural Networks…

haskellmachine learning

5 May 2013

Dominic Steinitz 9 min read

This is a very informal blog of the Cabal hacking team at OdHac: Sasha Manzyuk, Bram Schuur and me (Dominic Steinitz). I really enjoyed pair programming with both of them and I certainly wouldn’t have tracked down the bug I investigated without their help. Also a big thank you to Roman Cheplyaka for organising a … Continue reading Cabal Hacking…

haskell

30 Apr 2013

Dominic Steinitz 7 min read

Introduction Having shown how to use automated differentiation to estimate parameters in the case of linear regression let us now turn our attention to the problem of classification. For example, we might have some data about people’s social networking such as volume of twitter interactions and number of twitter followers together with a label which … Continue reading Logistic Regression…

haskellmachine learningprobabilitystatistics

26 Apr 2013

Dominic Steinitz 6 min read

Introduction Automated differentiation was developed in the 1960’s but even now does not seem to be that widely used. Even experienced and knowledgeable practitioners often assume it is either a finite difference method or symbolic computation when it is neither. This article gives a very simple application of it in a machine learning / statistics … Continue reading Regression and…

haskellmachine learningprobabilitystatisticsuncategorized

23 Feb 2013

Dominic Steinitz 6 min read

It’s part of Haskell folklore that the archetypal example for comonads is Conway’s game of life. Here’s an implementation using arrays. > {-# OPTIONS_GHC -Wall #-} > {-# OPTIONS_GHC -fno-warn-name-shadowing #-} > {-# OPTIONS_GHC -fno-warn-type-defaults #-} > import Diagrams.Prelude > import Diagrams.Backend.Cairo.CmdLine > import Data.Array > import Data.List The usual comonad class: > class Comonad … Continue reading Comonads, Life…

haskell

10 Feb 2013

5 Jan 2013

Dominic Steinitz 5 min read

Can we get better performance for pricing financial options in Haskell? First let us translate our pricer using the Explicit Euler Method to use repa. {-# LANGUAGE FlexibleContexts, TypeOperators #-} {-# OPTIONS_GHC -Wall -fno-warn-name-shadowing -fno-warn-type-defaults #-} import Data.Array.Repa as Repa import Data.Array.Repa.Eval import Control.Monad r, sigma, k, t, xMax, deltaX, deltaT :: Double m, n, … Continue reading Option Pricing…

haskellpartial differential equations

4 Nov 2012

Dominic Steinitz 2 min read

One way of visualizing a distribution is to take lots of samples for it and plot the resulting histogram. To do this we use the diagrams package. {-# LANGUAGE TupleSections #-} import Diagrams.Prelude import Diagrams.Backend.Cairo.CmdLine import Data.Colour (withOpacity) To generate the actual samples (in this case from the beta distribution) we use the random-fu and … Continue reading Plotting a…

haskellprobabilityuncategorized

15 Aug 2012

Dominic Steinitz 5 min read

A Simple Example In Section 7.1 of Doing Bayesian Data Analysis, John Kruschke gives a simple example of the Metropolis algorithm, in which we generate samples from a distribution without knowing the distribution itself. Of course the example is contrived as we really do know the distribtion. In the particular example, for i = 1…n where n … Continue reading…

haskellprobability

22 Apr 2012

Dominic Steinitz 4 min read

We can solve our differential equation using the Implicit Euler method which is unconditionally stable. We can also take this opportunity to use the Vector Package rather than Arrays as it has a richer set of combinators and to tidy up the code to make the payoff explicit (thanks to suggestions by Ben Moseley). First … Continue reading The Implicit…

category theoryhaskellpartial differential equations

1 Apr 2012

Dominic Steinitz 3 min read

Suppose we want to find the price of a European call option. Then we need to solve the Black-Scholes equation: Although this particular equation can be solved explicitly, under more realistic assumptions we have to rely on numerical methods. We can approximate the partial differential equation by a difference equation (the minus sign on the … Continue reading Solving a…

category theoryhaskellprobability

8 Jan 2012

Dominic Steinitz 4 min read

The title says it all. > {-# LANGUAGE > DeriveFunctor, > DeriveFoldable, > DeriveTraversable, > RankNTypes, > FlexibleContexts, > NoMonomorphismRestriction, > UndecidableInstances, > ScopedTypeVariables #-} > > import Prelude hiding (mapM) > import Data.Foldable > import Data.Traversable > import Data.Monoid > import Control.Monad.Writer hiding (mapM) > import Control.Monad.State hiding (mapM) > import qualified Data.Map as … Continue reading Catamorphisms are…

haskell

12 Nov 2011

Dominic Steinitz 1 min read

There don’t seem to be many examples of anamorphisms around. Here’s one to make up the lack. Let’s start off with our target language. > {-# LANGUAGE > DeriveFunctor, > DeriveFoldable, > DeriveTraversable #-} > > import Data.Foldable > import Data.Traversable > > data TermF a = PlusF a a > | MultF a a … Continue reading Anamorphism Example

category theoryhaskell

15 May 2011

Dominic Steinitz 4 min read

One of my colleagues (Roland Zumkeller) posted some nifty functions to count the number of expressions in an AST for the DSL we work on. This led to an email and chat discussion that I have summarised in this post. Any errors are entirely mine. Let’s start off with our target language. It’s easy to … Continue reading Monadic Caching…

category theoryhaskell