Thursday, September 16, 2010

Single exponential smoothing and foldl

Recently I looked at simple techniques for time series smoothing and forecasting. One such primitive method is the single exponentially weighted moving average (SEWMA), defined as

\begin{align*}<br />S_{t+1} & = \alpha y_t + \left(1-\alpha \right ) S_t \quad \quad 0 \leq \alpha \leq 1 \quad \quad t \geq 2 \\<br />S_2 & = y_1<br />\end{align*}

where the sequences are indexed from 1. y_i denotes an observed value of the time series and S_i a SEWMA value. In words, this recursive definition expresses that the next SEWMA value is a weighted average of the curent observed and SEWMA values.

If I pose the question "What is the predicted value for the next time series observation?" we just apply the formula.


A typical imperative implementation

sewma <- function (alp, y) {
S <- y[1]
for (t in 2:length(y)) { S <- alp * y[t] + (1-alp) * S }
S
}
This is R code, but it should look familiar enough to anyone who has written Java, C#, etc.


Haskell implementation
In Haskell, a fold is a way of recursively iterating a data structure, accumulating a return value. The left fold has type
foldl :: (a -> b -> a) -> a -> [b] -> a
foldl f s y = ...
Denote one of the values from the conceptual sequence of accumulator values as S_i. The function f maps from the current accumulator and list values to the next accumulator value

S_{t+1} = f\left(S_t, y_t \right)

which is the form of the SEWMA equation above. The foldl1 function is a specialisation of foldl that uses the first value of the list as the initial accumulator value, yielding the implementation
sewma alp = foldl1 (\s y -> alp * y + (1-alp) * s)


Revisiting R
R has the Reduce function for performing folds.
sewma <- function (alp, ys) {
Reduce(function (S, y) { S <- alp * y + (1-alp) * S }, ys[2:length(ys)], ys[1])
}


Summary
If accumulating a value over a data structure can be represented with a step function of the form

S_{t+1} = f\left(S_t, y_t \right)

then a left fold captures the recursive implementation.

Monday, September 6, 2010

Haskell mode in MacVim

I set up Haskell mode (haskellmode-20100622.vba) in MacVim 7.3 (53). As a Vim newbie I found this page useful, even though it says pretty much the same thing as the Haskell mode project page.

I followed the instructions and created $HOME/.vimrc with these contents.

" use ghc functionality for haskell files
au Bufenter *.hs compiler ghc

" switch on syntax highlighting
set syntax on

" enable filetype detection, plus loading of filetype plugins
set filetype plugin on

" Configure browser for haskell_doc.vim
let g:haddock_browser = "open"
let g:haddock_browser_callformat = "%s %s"
Unfortunately when I edited a Haskell file none of the Haddock integration was working. I assume the haskell_doc.vim filetype plugin wasn't being loaded because the :DocSettings command failed with E492: Not an editor command.

After some hours I changed the line set filetype plugin on to :filetype plugin on and Haddock integration now works (don't know why). I am using the Haskell Platform which includes the documentation.

After reading A minimal Vim configuration I have switched from $HOME/.vimrc to $HOME/.gvimrc with these contents.
" line numbers
set number

" show the cursor line and column number
set ruler

" turn off blinking cursor in normal mode
set gcr=n:blinkon0

" hide the toolbar
set go-=T

" switch on syntax highlighting
set syntax

" highlight matches in a search (hls)
" show the current matching pattern as you search (is),
" ignore case (ic) unless you are searching for both upper and lowercase letters (scs)
set hls is ic scs

" use ghc functionality for haskell files
au Bufenter *.hs compiler ghc

" enable filetype detection, plus loading of filetype plugins
:filetype plugin indent on

" Configure browser for haskell_doc.vim
let g:haddock_browser = "open"
let g:haddock_browser_callformat = "%s %s"

Wednesday, July 28, 2010

Monday, July 12, 2010

Charles Leadbeater: Education innovation in the slums (TED talk)

  • education that works by pull not push

  • learning has to be productive to make sense

  • learning that starts from questions not from knowledge in curriculum

  • Chinese restaurant model instead of McDonalds model

  • technology that makes learning fun and accessible

In these models, it seems the "teacher" plays more of a mentor type role of guiding and encouraging.

Wednesday, July 7, 2010

Managing PDF books and papers

I have a small collection of mathematical and computer science books and papers as PDF files. Some are stashed in Google Docs, others in Read it Later and others as links in email, etc. It would be nice if:

  • all documents were organised together

  • I could access the documents from all my devices - work and home macs and iPad

  • adding / removing documents automatically synchronised across all devices

  • there is a record of the url each document came from

  • document contents are searchable

A cloud solution with device specific apps would be a good start. I would be happy to pay an annual price similar to flickr and Remember the Milk for a good product. From memory these are about USD$25 / year.

All the solutions I have come across are intended for academic bibliographic management. Most of my documents were collected adhoc from the web, without access to restricted channels such as the ACM Digital Library. The tools are geared towards searching the popular open and paid publication repositories and extracting bibliographic details automatically. I can see how this would be very useful if you have access, but I found it problematic in matching my documents when I don't have access. Given my requirements this became more hassle then it was worth.

Some solutions integrate social networking. Being able to share your reading and observe what others are reading might be quite useful. Warrants future investigation.

I don't have any need to annotate, highlight or add notes to the PDF files at present.

Papers
A Mac application (USD$42) featuring document synchronisation with the corresponding iPhone and iPad apps (USD$17.99). Storing the database and documents on Dropbox should also enable sharing between Macs. Metadata and content are searchable and the full screen reading mode is great. You can add notes to documents, but not annotate or highlight portions. At work I am behind a strict corporate firewall and proxy. Papers gave a spurious error about internet connectivity and disabled all online actions. Following this forum post fixed things up.

I had high hopes for Papers. The iPad synchronisation is very compelling, but otherwise it currently doesn't do enough for me to warrant paying money for (and putting my data in) Mac only software. If I end up regularly using the supported repositories then I will probably reconsider.

Mendeley
A web application with a companion Mac/Windows/Linux desktop app. There is no iPad app, although it is apparently in the works. The desktop UI looks like it is Java and is a bit clunky on the Mac.

The desktop app is orientated around managing your documents and associated files. You can add your files, set their metadata and sync with your website account. Synchronisation is initiated manually via a UI button. There is a full screen viewer, although I couldn't figure out the zoom and navigation keys. Alternatively you can open documents with the default external application. Document annotations, highlighting and notes are all supported, as well as full text searching. Unfortunately the network proxy settings aren't automatically detected from System Preferences and I need to manually edit them when taking the MacBook Pro between work and home.

On the website you can manage your references and notes and view your files, but I couldn't figure out how to upload files or see annotations and highlighting. Otherwise, the web site is all about social networking. You get 500MB of personal storage space and 500MB of shared space. The first upgrade is USD$4.99/month for 3.5GB + 3.5GB.

To make it attractive for me, Mendeley needs to tidy up the desktop app, automatically sync in the background, make the full screen view useful, release a synchronised iPad app and lower the price.

BibDesk + Dropbox + Skim + GoodReader
BibDesk is a Mac BibTex editor and reference manager. I can record as much metadata as is convenient, associate url's and PDF files, organise and search. By storing the BibDesk database file and PDF files on Dropbox, both content and metadata is automatically synchronised between all Macs. Dropbox provides 2GB of free storage, which is a great to get started. Skim is a PDF reader with a decent full screen mode on the Mac. GoodReader (USD$1.19) is a PDF viewer with Dropbox integration on the iPad. Although it is much quicker to transfer documents to GoodReader via iTunes.

The main downside with this setup is the lack of BibDesk support on the iPad and that all documents aren't automatically synchronised locally to the iPad. It is sufficient for me now though, as I am currently only adding new documents periodically.

CiteULike
Web site sponsored by Springer. I haven't investigated it properly, but it looks like you can add references from the major publication repositories or add your own, upload the associated documents and setup social connections. There are no Mac or iPad apps, so you need to manage your local files and how to view them yourself.

Friday, July 2, 2010

QOTD

Do not have the mind set that the day you release version 1.0 is the finish line, it’s the starting line, so hurry up and get there.

-- Sam Howley

From: Lessons learned from 13 failed software products via @ericries

Tuesday, June 29, 2010

QOTD

It's not that people cannot think mathematically. It's that they have enormous trouble doing it in a de-contextualized, abstract setting.

-- Keith Devlin

From In Math You Have to Remember, In Other Subjects You Can Think About It