A resampling statistic for voting systems

Suppose we have a population of voters, each of them casting a vote in some (not necessarily deterministic) voting system.

Let w be the winning candidate.

Consider the following experiment:

Fix some number N. Draw a sample of N voters with replacement (i.e. you may draw the same voter more than once). Run the election again. What is the probability of the result of the vote being different from the vote on the whole population (if the voting system is non-deterministic, rerun the system on the whole population too)?

Call this number S(N).

I suspect S(N) might reveal interesting behaviours of voting systems, but I haven’t yet analyzed this in any great detail or thought about it much.

It does however have the interesting property that it isn’t dependent on the type of voting system used – it works equally well for ranked, graded or scored votes, and for deterministic and non-deterministic systems, so it provides an interesting way of comparing potentially very different voting systems.

Some interesting questions one may reasonably ask:

  • What is the limiting behaviour of S(N) as \(N \to \infty\)?
  • Is S(N) monotonic increasing?
  • What is the behaviour like for very small N? (I don’t have a precise formulation of this yet)
  • Is there anything special about S(original number of voters)?

Two examples:

For first past the post voting, \(S(1)\) is the fraction of the population who voted for the candidate, and \(S(N) \to 1\) as \(N \to \infty\), because as \(N \to \infty\) the fractions of the samples who vote for a given candidate concentrate on the fractions in the original sample. I think \(S(N)\) is monotone increasing but have only proven it for the two candidate case. It’s intuitively plausible though.

For random ballot, \(S(N) = \sum_{i = 1}^m p_i^2 \), where we have candidates \(1 \ldots m\) and \(p_i\) is the fraction of the population who voted for candidate \(i\). Note that this is independent of N. This is because picking N candidates then picking a random one of them is exactly the same as just picking a random candidate in the first place, so \(S(N)\) is just the probability of running the election twice and getting the same result.

I haven’t worked out the answers for other, more complicated, systems. I would be interested to do so, and may well at some point, but if someone wants to do it for me or tell me if it’s already been done that’d be cool too.

This entry was posted in voting on by .

Twitter as the death of blogging

(Warning: Navel gazing post about my blogging habits)

I tweeted yesterday:

(The blog word count is from the database for this blog. The twitter account is just a guesstimate based on the number of tweets I have).

Alex Cruise replied:

I thought this idea was particularly funny given that my blog productivity has been really high recently, but I wondered if there was any truth to it over all. It’s certainly an idea I’ve heard from a number of people over the years. I decided to investigate.

Here’s my blog output by year since I’ve started this blog:

Year Post count Word count
2005 17 10681
2006 28 20496
2007 71 21741
2008 62 30203
2009 69 25858
2010 29 14507
2011 40 23163
2012 51 34670

Spot the bit where I start using twitter and my blog volume goes way down?

No, me neither.

2005 was particularly quiet because I only had the blog for the last 3 months of it. 2010 because I was under NDA about what I was doing and had recently quit Scala, so I’d lost two significant sources of blogging, but all told my blog output seems to have stayed at a pretty consistent 20-30k words per year for most of the time I’ve had it.

I don’t think this surprising in retrospect. Most of the thoughts I put on twitter are stuff which is too short to be worth blogging about, and most of the thoughts I put on here are things that would be painful to condense into 140c. Are we worrying unnecessarily about “Twitter making us dumb”?

This entry was posted in Admin on by .

Ignorance is bliss for whom?

My friend Pete was being a cruel and unfeeling excuse for a human being the other day and inflicting Christmas jingle ear worms on me. I needed a defence.

I’d recently been reminded of Jonathan Coulton, via RE: Your Brains (which is entertaining and I recommend). There was a brief period of time when I listened to him a lot, then I just stopped for some reason. However, I remembered he had some very effective and (mostly) less irritating ear worms (I recommend not listening to Dance, Soterios Johnson, Dance if you ever want it to leave your head), and a lot of his music is on Spotify recently so I thought I’d have a listen.

This didn’t go so well.

It was going ok, although some of his songs are way more depressing than I remember.

First I listened to Code Monkey. It’s… a little bit offensive. As well as being about nerd stereotypes (something you can probably imagine I’m a little touchy about), it’s also about a guy’s obsession with a fairly stereotypical secretary character (apparently) purely because she’s pretty. It’s not a big deal at all, but it annoyed me a little bit.

Then The Future Soon came on. I used to really like this song. It’s basically a kid fantasizing about what it’s going to be like in the future and how his life will be better then (nerd stereotype again, but one I can sympathize with enough that I’ll give it a pass).

And, err, about kidnapping his high school crush and forcing her to marry him.

So, huh, this is kinda rapey isn’t it?

Oh well. They can’t all be winners… Next one.

Hm. Skullcrusher Mountain you say? A light hearted humorous piece about a mad scientist trying to woo a lady… It’s not going so well given that she’s understandably rather terrified of him after his henchman kidnapped her for him.

This too, huh?

Ok. I’m done with this.

Well, this sucks. My increased awareness of social issues has ruined my enjoyment of something. No wonder they say ignorance is bliss, right?

This got me thinking.

A friend mentioned something recently, which resonated a lot: Heavily paraphrased, her point was that she found that getting better at social skills didn’t necessarily make social interactions easier and sometimes made them harder.

How does this make sense and why is it related?

The reason it happens is that increased social skills give you better social awareness. This means that you are more aware of how people are perceiving you and their reactions – if you’re making someone slightly uncomfortable, boring someone, etc. there’s now a much higher chance that you will realise this. You then have to figure out what to do about that, which can be difficult and potentially even painful.

The common point is that your increased awareness of the situation has made you more able to understand why there is a problem while not necessarily granting you the ability to do anything terribly effective about it – you can stop listening to the song (and maybe avoid Jonathan Coulton in future, although I’m not really sure I’d go that far. I have to think about it), or you can try to do better in the social situation (but “improved social skills” doesn’t necessarily translate to being smooth and suave and able to recover here, only that you know that you’re not doing so well. It’s a start, but not necessarily a finish). So by knowing more you have made your life worse.

This seems a bit sad: We’ve improved ourself and are being punished for it. No wonder they say ignorance is bliss.

Thinking some more, we get to the point of the title of the post: For whom, exactly, is your ignorance blissful?

Well, for you, obviously.

Thinking some more…

If you don’t know about them, the problems are still there, aren’t they?

It’s not like the Jonathan Coulton songs are not rapey just because you don’t notice. There’s a reason feminist theory has a term for things which normalize sexual assault, and it isn’t because they’re a rare event. They’re still there and still have an effect regardless of whether you explicitly notice that effect, but by noticing you at least register that it is not OK and start to push back on that.

It’s not like you weren’t (or, to make this less abstract, I wasn’t. There have definitely been periods in my life where in retrospect I was being a bit of a dick and didn’t realise) making people uncomfortable or otherwise causing social problems before you gained better social awareness. Your improved social awareness has come at a cost to you, which is that you are now more aware of others and have to take that into account. Well, you don’t have to, but it’s The Right Thing To Do.

So this is my thesis:

In many cases, knowing more comes with a cost to yourself. This might incline you to conclude that knowing more was bad and that you should avoid it in future. This can certainly be a good strategy in some cases.

But before you do that, you should ask yourself what cost other people were paying previously because you knew less.

This entry was posted in life on by .

So I accidentally wrote a monad tutorial

Put this up in a gist the other day. Recording it here for posterity. It’s probably not actually very helpful for understanding monads:

> module MonadTutorial where

Oh god it’s a monad tutorial.

(Why am I even writing this? It’s not like I use Haskell much at all these days)

I’m going to start by not talking about monads at all. Instead I’m going to give you a quick introduction to how to read Haskell and how Haskell type classes work. If you already understand that bit, feel free to skim until you start to
feel confused.

First off: This is literate Haskell. Every line starting with > is code. Everything else is plain text. You can run it through a Haskell compiler if you want.

Now. Here’s a type signature:

>blah :: Int -> Int -> Int

It says “There is a function called blah. It takes two Ints and returns an Int”.

Well, almost. What it actually says is “There is a function called blah. It takes an Int and returns a function which takes an Int and returns an Int”. Haskell tends to adopt a convention of using partial application for multiple
arguments. You mostly won’t need to worry about the difference here.

Here’s a definition for our function blah:

>blah x y = x + y

(not very exciting)

Type signatures (and data types) can also be polymorphic, that is they can work for a range of input types:

>reverse’ :: [a] -> [a]
>reverse’ [] = []
>reverse’ (x : xs) = reverse’ xs ++ [x]

(this is a stupid implementation. Don’t worry about that)

In Haskell type signatures any lower case symbol is in fact a type variable, which may be bound to any type. So you can have reverse applied to [Int] and it will produce an [Int], or [String] and it will produce a [String], etc.

The second thing to note in this code is pattern matching. If you don’t understand it, don’t worry about it too much, but this is basically how we define a function over lists.

So, we have polymorphic functions, which is all very well, but really the only thing you can do with a thing of an unknown type is pass it around. Sometimes you want to be able to say “I have a type and I don’t know what it is, but I can do these things to it”. That’s where type classes come in.

A type class is a way of writing an interface that a type must satisfy. Individual types can then be instances of this type class (note: Instances are types, not values) by implementing that interface.

Another important thing to note is that this is the only way Haskell supports overloading. There is no ad hoc overloading based on type or number of arguments – it’s type classes or bust.

Here’s a very simple type class:

>class Zeroed a where
> zero :: a

>instance Zeroed Int where
> zero = 0

This basically says “In order to be Zeroed a type ‘a’ must have a distinguished element called zero”. We’ve then made Int implement this in the obvious way.

There’s not much interesting we can do with this type class, but here’s an example using it:

>andZero :: (Zeroed a) => a -> (a, a)
>andZero x = (x, zero)

This is a polymorphic function where we’ve constrained the type variable a to implement the Zeroed class. All it does is return a pair of elements, the first being the element passed in, the second being the zero of the appropriate type.

One thing to note is that we’ve overloaded on the type of zero here. Because we’ve specified that the function returns an (a, a) it knows that the zero must belong to the same type the argument, so we get the right value of zero as defined in the type class.

We could also define a Zeroed implementation for lists. e.g.

>instance Zeroed [a] where
> zero = []

So we would have andZero 10 == (10, 0) and andZero “kittens” == (“kittens”, “”) (a string in Haskell is just a list of characters).

You can also extend type classes, and you can define functions in them as well as values:

>class (Zeroed a) => Group a where
> add :: a -> a -> a
> negate :: a -> a

>instance Group Int where
> add x y = x + y
> negate x = -x

So a Group (don’t worry about why it’s called that) is something with a zero in which you can add pairs of elements and negate individual elements.

It’s common to require implementations to satisfy additional invariants which we don’t enforce in code. These are just documentation things which you have to
prove and/or test for yourself, but your implementation is “wrong” if you don’t satisfy them.

Example rules for group:

add x (negate x) = zero add x (add y z) = add (add x y) z

It’s simply convention that these must hold, but we will often assume it for writing functions that depend on the class.

For example we could define the following generic function depending on the class:

>sum’ :: (Group a) => [a] -> a
>sum’ [] = zero
>sum’ (x : xs) = add x (sum’ xs)

(Yes, I know about fold. I don’t care to explain it here)

And somewhere later we might find ourselves wanting to assume that addList (x ++ y) == add (addList x) (addList y). We can do that because of the second of the rules we’ve imposed. (If this isn’t obvious, don’t worry about why).

There are two more things we need to talk about before we get on to monads.

Types in Haskell can depend on other types. For example you have lists. Lists of integers are one type ([Int]), lists of strings are another type ([String]. Which is [[Char]], but you don’t need to know that). List is a type constructor rather than a type – you feed it types, you get new types out.

You can have type classes for type constructors too.

>class Stacky s where
> push :: a -> s a -> s a
> pop :: s a -> (Maybe a, s a)

>instance Stacky [] where
> push x s = x : s
> pop [] = (Nothing, [])
> pop (y:ys) = (Just y, ys)

So a type constructor s is stacky if you can push an a to an s a and pop an a off an s a. So lists are stacky, but so are vectors, and so might other types be.

One final thing. This should be obvious, but just as a reminder… Functions are values in Haskell. So you can include higher order functions in type classes.

Here’s an example:

>class Mappable s where
> map’ :: (a -> b) -> s a -> s b

(Note: In Haskell this is normally called Functor and map’ is normally called fmap. Don’t worry about that for now)

What does this mean? It means that given a function which takes things of one type, you can lift it to some “container” type constructor. e.g.

>instance Mappable [] where
> map’ f [] = []
> map’ f (x : xs) = (f x) : (map’ f xs)

But there are also other examples where you can do this. The following one will require you to stop and think for a moment, so don’t worry if it’s not obvious:

>instance Mappable ((->) input) where
> map’ f g = \x -> f(g x)

What on earth is going on here?

The slightly obtuse type constructor notation might confuse you. (->) is a type constructor that produces the function type. It takes two arguments. What we’re doing is partially applying the constructor to some fixed type “input”. This gives us a type constructor which takes one argument – call it output – and produces functions input -> output.

We’re effectively treating functions as containers containing their output values. So in this case “mapping” is just composition – you map the values by applying another function to them. If this still doesn’t make sense, imagine how this works in the Indexed case if you treat a vector as a function from its indices to its elements.

Do try to understand this example. It will help later.

Ok. Take a deep breath, maybe go get a cup of tea. Basically, relax. I’m about to give you a type class definition for monads, and you need to believe that it’s not going to scare you that much.

>class (Mappable m) => Monad’ m where
> unit :: a -> m a
> join :: m (m a) -> m a

Yep, really. That’s it. It isn’t quite the one you may have seen, but it really is equivalent.

So every Monad is something Mappable. We can construct a monad over some type from values of that type. And we’ve got that slightly mysterious operation join. What’s it do?

Well, at the moment we’ve not constrained anything about how it behaves, so it could do whatever you want!

But let’s pick an example instance that might be helpful.

>instance Monad’ [] where
> unit x = [x]
> join = concat

So we build lists from elements by just taking the singleton list, and we join a list of lists to a list by concatenating it.

What are some reasonable ways for this to behave?

Well, here’s one:

join (unit x) == x

This is obvious with lists: concat [x] == x

But it’s also reasonable in the sense that it means that our monad building and collapsing rules are somehow inverses.

I’m not going to go into the other monad axioms right now (mainly because I can’t translate them off the top of my head to this notation, but I think they do all have sensible translations). It’s not that important to understand them just now.

What I am going to go into is how this relates to IO.

You remember how we constructed the mappable instance for ((->) a)? We can do something pretty similar to construct a Monad’ instance for it.

>instance Monad’ ((->) key) where
> unit x = \y -> x
> join f t = (f t) t

Ok. I’ll admit it. The join implementation looks pretty obtuse. What are we doing there?

What we’re actually doing is the most obvious thing that could possibly work.

We’ve got our value f which is an m (m b). That is, it is a key -> key -> b. Given an instance of key, which we’ve called t in the implementation, we want to produce a b. We have f, and all we can do is apply it to things of type key. So we do that, and we get a key -> b. All we can do with that is apply it to things of type key and, oh look, we have one right there. So we do that, we have a b, and the job is done.

Ok. So there’s a variety of monads.

Now, we’ll consider the IO monad. How is an IO like a list I hear you ask?

Well, it’s not very like a list. But it is quite a lot like ((->) ()).

“What on earth is that type?”, you ask? It’s the type construtor for functions which take an argument of type () (a type with only one value, also written (). I’m not sure what it’s called in Haskell, but I’ve seen it called Unit elsewhere).

So we’re talking about functions () -> a, for a variety of types a. In Haskell this isn’t very interesting, because all the functions are pure and all the values are lazy, so really there’s very little difference bettween a value of type a and a value of type () -> a.

But imagine we had some magic special annotation which said “And this function is allowed to perform side effects”. That is essentially what an IO a is. It is
a block of code that if run would maybe produce side effects and would then emit
a value a at the end.

We can’t define this type in Haskell, but if we could it would look something like this:

>data IO’ a

>instance Mappable IO’ where
> map’ f x = undefined

Run x with its side effects, then apply f to the result

>instance Monad’ IO’ where
> unit x = undefined

Just return the value x without performing any side effects

> join f = undefined

Run f. Get a new side effecting block which returns an a. Run that and return the result

When you are writing a Haskell program which does IO, has side effects, etc you are not “really” writing a program which has effects. Instead you are writing a purely functional program which evaluates to a result that when run by the Haskell runtime produces some effects. That value has type IO. (Yes, this sounds suspect to me too, but let’s go with it anyway). Those values thus obey the rules for a monad (I haven’t told you what they are yet, but lets forget that for now).

Ok. So I’ve talked you through some examples of a monad, and the operations it obeys. Why do we care again?

Partly it’s because we can write generic functions which take any monad and do useful things with it (this does happen, but I’m somewhat skeptical of how big a deal it is). Mostly though it’s because monads capture an important notion, that of sequencing.

Suppose we want to capture the notion of “A side effecting function u -> v”. You would write this as u -> IO v. So it’s a function from a which produces a side-effecting thing that would produce a v. Suppose can also produce a u with side-effects. How would we produce a v from these two things?

In fact. Let’s make this concrete. Suppose we had:

>getsLine’ :: IO’ String
>getsLine’ = undefined

>putsLine’ :: String -> IO’ ()
>putsLine’ = undefined

i.e. a function for reading a line from STDIN and a function for writing a line to STDOUT.

How do we combine these to write

>catLine :: IO’ ()

which reads one line from STDIN and writes it to STDOUT?

Really the types tell us the only thing we could possibly do. We start with an IO’ String. We then map putsLine’ across it to get an IO’ (IO’ ()). We then join to get an IO’ ().

>catLine = join((map’ putsLine’) getsLine’)

And because the types basically forced our hand here, this is good evidence of a more general sequencing operator:

>bind :: (Monad’ m) => m a -> (a -> m b) -> m b
>bind m f = join(map’ f m)

bind is more commonly introduced as one of the primitive monad operators, and it’s usually written >>= for improved readability (straight face).

In our IO interpretation what this means is “Run m. Get a value. Feed that value into f. Run the result. Return the value from that”

What does bind look like in our list interpretation?
Well, first of all we’re mapping each element to get a list. Then we’re concatenating it. This is sometimes calledFlatMap.

For example, bind [1, 2, 3] (\x -> [x, x+1]) == [1,2,2,3,3,4]

These behaviours don’t look like they have much in common. I have an intuitive way of looking at it that shows why they’re the same thing, but do bear in mind that it’s only a hand wavy intuition. This isn’t what a monad *is*. What a monad is is a set of operations and axioms they must obey. This is just a hand-wavy way of looking at it that might help:

Monads are things that may emit some number of values (which may be 0) when you “do something” to them. What that “do something” means is entirely dependent on the specific monad. In IO, it’s running the block of code. In a list it’s iteration. Further, they have enough structure to give you ways of sequencing monads together to create new monads of the same type.

So in this interpretation, the operations mean:

unit: Just emit this value map: Take the emissions from this monad, feed it through this function, emit the result of that.

bind: Take the emissions from this monad, each of those are themselves monads, take the emissions from those.

If you now think through the list and IO examples, hopefully you’ll see that these informal definitions line up with what we’re doing there.

There’s more stuff I could write about this, but I’m not going to. Instead I’d recommend digesting this, making sure you’ve understood it, and then head over to the wiki entry on the actual monad laws for your next dose of axiomatic monads and how this relates to the do notation you’ve probably seen.

This entry was posted in programming on by .

How do you interview people without a programming background?

As part of my current interest in hiring, I realised that I have very little idea of how to interview people who have no background in programming for software developer positions (with the intent of training them on the job).

The best I can do is think back to when I first interviewed for a programming job – there were some good interview questions, which largely consisted of idealized descriptions of algorithms (I don’t want to share them because as far as I know the companies in question are still using them and I don’t want to spoil their interview process). They seemed like a pretty good test at the time, but I have no idea how much I benefited from my background in mathematics.

Any tips?

If you’re willing to share specific interview tasks with me, that would be great. If you don’t want to do that in public my email is, as always, [email protected]. General advice is also appreciated.

Thanks.

This entry was posted in Hiring on by .