JavaScript Choices for Haskell Programmers

haskellweb-programming

So, I’ve not done any blogging since the ill-fated Midwinter Polyphasic Sleep Experiment. I have a backlog of articles I want to write, but most of those involve some significant investment of time and effort. So you get this instead...

Context

I’ve been doing a lot of JavaScript programming recently. One of the two contracts I’m working on at the moment is for a UK university spin-off called OpenBrain, who are trying to bring Bayesian statistics to the masses by making tools that allow scientists to build complex (or simple!) Bayesian models and have them analysed using Markov chain Monte Carlo methods. All the really clever stuff is in the server-side MCMC inference code, but the user-facing side of things is a web app with a Yesod backend. My mission is to prettify this and make it ready for prime-time.

It’s a fairly complicated application that needs to manage literate Markdown documents describing statistical models, data sets for analysis and a bunch of other stuff. The first thing I looked at was a tool for building statistical models based on dynamical systems represented as systems of ordinary or stochastic differential equations. This tool allows you to give algebraic representations of ODEs or SDEs, which are then rendered as MathML (for prettiness and familiarity), you can do simulations of the systems and visualise the results, you can specify data sets that correspond to observations of system variables, you can set up prior distributions for system parameters, and you can then get some code (in a proprietary Haskell-like language specialised for statistical modelling) that can be used to drive MCMC inference for the parameter values.

All that stuff happens on the client side, except for the listing of the available data sets and the rendering of the model into code. This means that you need client side code for expression parsing, analysis of coupled systems of ODEs and SDEs (to turn them into a canonical form that you can simulate from), numerical integration of ODEs and SDEs, graphing, and a framework to tie it all together. That’s a lot of client side code!


Polyphasic Sleep: Day 2

sleep

Urgh. Well, that didn’t go quite as I was hoping. I discovered that I didn’t much enjoy being awake on my own through the 16 hours of darkness we currently have here. Not as a habit anyway. The trade-off between the vistas of potentially infinite extra time and feeling like a very slightly warmed-over turd in human form sent me off to bed for a proper sleep at about 4:00 this morning.


Polyphasic Sleep: Day 1

sleep

Right now, it’s early afternoon on “Day 2”, just about 24 hours after I started this experiment. Things haven’t gone completely smoothly, but I’ve learnt a few things:

  1. I need a louder alarm than the one I started with! I went to bed for a 30-minute nap at 1 a.m. and woke up at about 5:10. Not really to plan! I slept right through the alarm. I’ve turned the volume up now and it seems to work better.

  2. People weren’t joking when they said the adaptation period wasn’t much fun. I’m feeling a bit poo and not able to get much work or serious thinking done. Fortunately, Rita is away visiting her parents this week, so she won’t be exposed to an even more grumpy than usual me.

  3. It’s going to feel like pulling an all-nighter every night for a few days. Urgh.

I’ve decided to work with a 1-5-9 nap plan, i.e. a 20-30 minute nap at 1:00, 5:00, 9:00, 13:00, 17:00 and 21:00 (and no other sleep, unless I feel like crap and need to insert another nap somewhere).


Lilith’s Brood

book-reviews

by Octavia Butler

Dawn, Adulthood Rites & Imago

I think I remember reading part of one of these books when I was very much younger. I don’t remember getting a lot out of it, so I’m very glad that I reread them again now. There’s a lot to think about here.


Not Enough Hours in the Day?

sleep

Since I’ve found some contracting work, my only real problem in life has been lack of time. I spend three or four hours a day walking and caring for our dog Winnie (she’s very high energy and needs a lot of exercise to be happy), I sleep for eight hours or so, and I try to work for about eight hours per day. That doesn’t leave a lot of time for “everything else”.

So what to do? Winnie time isn’t really negotiable, and Rita’s schedule during the week doesn’t allow her to help too much, especially in the winter when it gets dark early. She does that stuff on the weekend, but then I also want to hang out with her and Winnie. Reducing work time isn’t really an option, both because it’s interesting and because I need to earn enough to support us.

That leaves sleep. It’s interesting that monophasic sleep, the idea of going to bed between ten and midnight and getting up between six and eight, with the whole intervening period ideally spent asleep, seems to be something of a modern invention, dating from the Industrial Revolution. In Europe in medieval times, a pattern called segmented sleep or biphasic sleep was more common–people who were tired after a long day’s work in the fields would come home and go to sleep immediately for “first sleep”, then would wake up some time in the night to do whatever (eat, have sex, visit neighbours, pray, etc.). Then they would go back to bed for “second sleep” before getting up for the next day’s work. And obviously, among nomadic or pastoralist people, sleep was often taken when and where it was possible, perhaps only as short naps.

It turns out that there are people who have experimented with a whole bunch of different sleep patterns. Most of this isn’t really validated scientific research, and there seems to be a very wide range of individual adaptability to different patterns, so it might be difficult to come up with any conclusions that would apply to everyone out there, but some of the anecdotal evidence is very interesting indeed.


The Contracting Life

day-job

Ah, another involuntary blogging hiatus. This time, for a good reason though. I have work. And it’s work that pays money, is interesting and has some future in it. It’s pretty cool. I’ve also had an offer of more work that I’ve had to defer until next year, which is encouraging. It seems as though this contracting thing might actually work out.

I have two clients I’m working with at the moment. The first is my old research group in Bristol. I’m doing some Fortran programming for them, extending a tool they use for generating input files for the main climate model that they use. It’s not a very complicated job, apart from the historical aspects–there are three main versions of the code for this tool, one of which comes in 269 distinct, slightly different versions... So sorting out which version is most “canonical” is an interesting problem. I’ve calculated Levenshtein distances between all the distinct versions and have used hierarchical clustering to get some idea of the historical relationships between all these versions. I think that this is probably going to be the trickiest part of the job! (There’s also a Tcl/Tk GUI that will need to be modified, which will be about as much fun as a root canal, but the changes needed should be pretty localised.)

The second client is a start-up that’s doing some really interesting stuff. They’re still in a pre-release development phase, so I’m not sure how much I’ll be able to blog about for now, but suffice it to say that there’s Haskell, Bayesian statistics, Markov chain Monte Carlo and data visualisation aspects to it. I’ve spent the last few weeks working on part of the web GUI front end, which has been an eye-opener, since it involved rather more JavaScript than is good for my mental health. I’ll write about some of that in general terms over the next few days.

I have another mad idea that I’m going to start on Monday. I’ll write about the details of that tomorrow, but it will either result in a 100% productivity increase or some sort of institutionalisation. We’ll see. More from the Department of Irresponsible Human Experimentation tomorrow...


Fun with Fay

haskellmathematics

This won’t sound like fun (or Fay!) to start with, but we’ll get there...


Lyapunov Exponents in Haskell: Part 2

haskellmathematics

In the previous article, we looked at some of the details of a scheme for calculating the Lyapunov exponents of a system of differential equations. In this post, we’ll see how to implement this scheme in Haskell. The code we describe is available as a Gist here.


Lyapunov Exponents in Haskell: Part 1

haskellmathematics

The archetypal chaotic dynamical system is the Lorentz system, defined by the differential equation system

$$\frac{dx}{dt} = \sigma (y - x)$$ $$\frac{dy}{dt} = x (\rho - z) - y$$ $$\frac{dz}{dt} = x y - \beta z$$

where $\sigma$, $\rho$ and $\beta$ are real constants. On the face of it, this looks like a fairly innocuous set of differential equations. However, as is well known, for a wide range of choices for the parameters $\sigma$, $\rho$ and $\beta$, this system of equations has chaotic orbits, with sensitive dependence on initial conditions and an attractor of fractal dimension.

Determining whether a given dynamical system will exhibit chaotic behaviour is, in general, difficultIn fact, the Lorenz system was only proven to be chaotic in 2002–see: W. Tucker (2002). A rigorous ODE solver and Smale’s 14th problem. Found. Comp. Math. 2(1), 53-117 (PDF).. The orbit structure of chaotic systems can be very complex, and it can be difficult to distinguish true chaos from a number of related phenomena.

One approach to characterising the orbit structure of dynamical systems is via Lyapunov exponents, a spectrum of values that measure the stretching and squashing of orbits–“normal” chaotic systems normally have at least one positive Lyapunov exponent and are dissipative, which means that the sum of all of their Lyapunov exponents is negative. While it is relatively straightforward to define Lyapunov exponents, in practice calculating them is tricky. We’ll see how to do this in some practical cases in this and the next couple of articles, following mostly the approach of Rangarajan et al. (1998).

The motivation for this series of articles was basically that I have recently been wondering about how practical it would be to use Haskell for more “traditional” mathematics: there is a big body of work and code relating to category theory and type theory in Haskell, but I’ve not seen so much about matrix algebra, differential equations, dynamical systems and so on. I implemented the Rangarajan method in Mathematica some years ago and thought it might be a good test case. I’m going to gloss over a lot of technical details about the computation of Lyapunov exponents since my main interest is in the Haskell implementation issues.


Winter is here!

Wintery scene

We woke up to snow in the garden, snow all around this morning. There had been some rather slushy sleety attempts at snow yesterday evening, but nothing that would have made us believe that we would wake up to the first white blanketing of the year in the morning.

It was pretty grey when I set out from the house with Winnie for our morning walk. After some crazy sniffing for mice in the fields below our house, we climbed up into the forest near Lanser Kopf, and the sun came out through the clouds. It was quite beautiful and very mysterious, mist rising from the ground, wisps of cloud around the mountains, beams of bright sunlight shining through the trees.

The sun came and went during the two hours of our walk, and we both got pretty wet from snow falling from the trees as it melted. Winnie also got dirty enough to need a rinse in the bathtub when we got back too!

Most of the snow has gone again this evening, at least down here in Igls, although the mountains all around are still coated. I guess we’ll be seeing a lot more of the white stuff soon!


Haskell on Arch Linux

haskell

So, GHC 7.6.1 was released last month. And there was a new release of Cabal too, bumping the version number to 1.16 from 0.14. The Arch Haskell people did a great job of quickly producing Arch packages for the new version, which meant that everyone pulled the new versions when they did an update via pacman -Su. Unfortunately, there seems to be a problem. The new cabal-install version can’t read its own default configuration file and just fails more or less immediately. There seems to be a fix, but it’s not yet percolated into the Arch packages.


Sliders for Yesod

haskellyesod

Michael Snoyman recently had a blog post about composability in the Yesod web framework for Haskell, where he responded to comments about it being difficult to build reusable components for Yesod by, well, building one. I’m pretty much a newbie with Yesod, but the composability aspect of things had never looked too difficult to me, so I thought I’d also have a go at the same kind of exercise.

I decided to implement a slider form field, using one of the nicest jQuery-based slider widgets I’ve found. All the code for this example is available as a Gist. I’ll follow the usual convention here of putting more or less all of the code in the blog post too.


Some recent(ish) papers

day-job

Some recent-ish papers of mine, that is. Although I didn’t enjoy my last post-doc in Montpellier a whole lot, there was a small amount of output from it, which probably deserves recording. So here goes.


New Life In Igls

So, we’ve embarked on a new life here in Igls. Rita has started school, and I have done most of the paperwork to set myself up as an independent software contractor. It’s all rather exciting!

There will be some changes here over the next couple of weeks, since I want to use www.skybluetrades.net as my “professional” website. I’m also going to start blogging again, after a break over the summer.


Holidays in Austria 2: Ravelsbach

After the two days we spent in Vorarlberg, we headed for Rita’s parents’ place in Lower Austria, in the village of Ravelsbach. The drive wasn’t too bad, going through the Vorarlberger tunnel (13 km long, the longest in Austria), then past Innsbruck (very nice), a bit through Germany (now with speed limits on the motorway, although they appear to be entirely optional), past Linz and towards Vienna, then a little “short cut” courtesy of Rita once we got closer to Ravelsbach...