Hey you!

Postcards are cool! Go send some ->

postme.me

This Haskell is wrong. Why?

Jan 20 2012

English: 5D virtual 3^5 sequential move puzzle...

Image via Wikipedia

The problem I’m trying to solve is the simple but lovely euler 62.

The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.

Find the smallest cube for which exactly five permutations of its digits are cube.

A bit of fun coding after a statistics midterm last night and the solution should be in the bag. Except it isn’t, I am doing something wrong somehow and I can’t figure out what! Hopefully someone a bit better than me can shed some light whether my proposed solution is wrong or I just suck at Haskell.

Algorithm

  1. Generate cubes up to 10,000
  2. Every cube is a pair of a digit-ordered string n^3 and n, for instance (“279″,9)
  3. Order cubes by the string number presentation
  4. Group together all cubes with the same n^3
  5. Pick out groups with the size of 5
  6. Sort by n
  7. Pick the smallest number

Should work in principle right? So why doesn’t the website accept my answer (5027)? My guess is I’m doing something wrong in the sorting and grouping department and I was hoping someone with a bit more knowledge of Haskell could point out where I’m being stupid.

Code

import Data.List
 
cubes::(Num a) => a -> [(String, a)]
cubes 1 = [(show 1, 1)]
cubes n = (sort$show(n^3), n):(cubes $ n-1)
 
sortStrNum (s1, n1) (s2, n2)
  | length s1 == length s2 = compare s1 s2
  | otherwise = compare (length s1) (length s2)
 
permuted_cubes n =
  groupBy (\a b -> fst a == fst b) $ sortBy sortStrNum $ cubes n
 
fives n =
  filter (\xs -> length xs == 5) $ permuted_cubes n
 
comparing p x y = compare (p x) (p y)
 
smallest =
  head $ sortBy (comparing snd) $ head $ fives 100000

The whole thing looks kind of alright to me, no matter how much I poke around it doesn’t seem like something is misbehaving … but it still is.

Ideas?

Enhanced by Zemanta

4 responses so far

Blogging, hats, stuff

Jan 19 2012

Presenting

Presenting

Yesterday I gave a talk at Kiberpipa on the awesome #wwwh weekly event. The talk was about this blog and how after that one insanely popular posteveryone suddenly decided I know what I’m doing and should tell others how its done.

Video at bottom

It’s funny how difficult coming up with a talk is when somebody tweets you Hey, you should totally come give a talk about blogging. The problem with these kinds of talk is that you don’t really know what you’ll be trying to say. Every time you’re up there on stage you should have a message – something to convince the audience of.

Just giving a general talk sucks for that. It invariably turns into something a bit like this post – a rambling conglomerate of sentences that sort of go together. Always reminds me of that one line in a movie: You talk a lot, but you don’t say much.

I guess the overall message of my talk was this:

Patience! It takes a lot of patience and sticking-to-it-ness, don’t do if it isn’t inherently fun for you.

Despite all of that I think the talk was a smashing success. Sure I forgot to even mention hats – was supposed to mention changing the blog’s name from Cthulhu and Other Crazies to A Geek With a Hat … oops? In general the talk ended up a bit rambley, even finished with “Wait, there was something else I wanted to say … oh well. Questions?”

That’s not a very strong ending. The originally planned ending was: But hey, at least I’m no longer The Author on hackernews, but Swizec

All in all, rhetoric sucked, body language was attrocious, hands found their way into pockets several times, but people laughed a few times, asked a bunch of questions and I think everyone had fun. This one girl even asked for actual advice and I thank her for thinking I know enough to give advice about this stuff.

The slides for Blogging, hats, stuff are over at Speakerdeck whose embeds don’t work with WordPress … there is also a video.

Enhanced by Zemanta

2 responses so far

Are you a boy scout coder?

Jan 17 2012

The Boy Scouts have a rule: “Always leave the campground cleaner than you found it.” If you find a mess on the ground, you clean it up regardless of who might have made the mess. /../ the original form by Robert Stephenson Smyth Baden-Powell, was “Try and leave this world a little better than you found it.”

What if we followed a similar rule in our code: “Always check a module in cleaner than when you checked it out.” No matter who the original author was, what if we always made some effort, no matter how small, to improve the module. What would be the result?

by Uncle Bob at O’Reilly commons

Lieut. Gen. Robert Stephenson Smyth Baden-Powe...

Image via Wikipedia

When I first saw this rule in Clean Code I loved it! It’s just such an awesome rule. You come into a file, you clean it up a little bit. Remove a stupid comment, indent something better … anything.

It makes the world a better place and everyone a happy camper right?

Well, this might be great in theory and work well when you are employed by a company where you will spend the next few years of your life. The software you’re working on will live and grow with you, with your team. You are the guy shouting “Fuck this! Who the fuck made this code! This is bloody impossible to maintain!” a year from now.

For a freelancer the situation is a bit different.

Here you are, plomped into the middle of an ongoing project. Decisions have been made, rabbit holes have been followed. The deadline is in a month and as a crack team of one specialist on a tight deadline, you’re making nice gold per hour.

Right there in front of you. A mess. Code so ugly, so horrible, it would make a grown man cry. You’re just supposed to add a feature. Figure out the mess, add two or three lines of code, cross your fingers and hope for the best.

Or should you rewrite the whole function?

Rewriting would be the Right Thing ™ to do. The code will be more maintainable, easier to test, it will save your client a bunch of money down the line. You won’t be maintaining this so you have a responsible towards everyone coming after you to fix something.

But, right now, right this very instance, you are strong-arming the poor client to pay more. Sure, you’re making the code better, but they care about that one feature. Should you really spend three hours rewriting the code instead of one hour adding something and hoping for the best?

On the other hand: When the messy code breaks, and it will break, it will be your fault. You’re the last guy who touched it. Not rewriting will come back to haunt you. The guy who maintains your code will curse you in their sleep and dream of delicious murder. And it’s not even your code!

So what do you do?

Personally I always try to rewrite crappy code. Add testing suites. Anything I can do to make the codebase better. But I always carefully explain the situation to my client. Why am I doing this, how is it benefiting the client. It’s important to make them understand I’m not just inventing work to rake in more gold.

Clients are surprisingly permissive most of the time and I can sleep better at night. win-win!

Enhanced by Zemanta

22 responses so far

Shoes

Jan 16 2012

Shoes are wonderful technology, they protect our feet, they make us look good and there’s a whole culture associated with wearing those things. A culture I, as a man, will never understand.

Shoes on their deathbed

Shoes on their deathbed

Apparently there is a million and one way to wear the wrong kind of shoes to the wrong kind of outfit. For instance, my scruffy shoes … don’t go with any outfit. Or so I’ve been told. I like to think they go with every outfit exactly because they’re scruffy and falling apart. Gives them a bit of character.

But, they are about to die, so I have to find a replacement this week. The thing I don’t understand is: Why do women need so many? A good pair of black leather sneakers covers pretty much every occasion.

  1. They’re comfy, so you can use them for walking to places
  2. Because they’re black leather, they look nice when clean and can be used for business
  3. Because they look kinda nice, they can be used for semi-formal stuff too. Anything that isn’t black- or white- tie really.
  4. If you aren’t a sissy most sport can be done in them as well

The only other pair of shoes you might need is a pair of sandals, if you’re too sissy to go barefoot at the beach in the summer. And while some advocate using a pair of water-proof shoes for special weather, you don’t actually need that. Shoes dry out over night and pretty much all types of shoe I’ve seen gets soaking wet the moment you start thinking of them as being water-proof.

Rubber boots old people use for gardening might be an exception, but those get soaking wet from the inside anyway, so what’s the point?

Last night I asked an internet about the need for multiple shoes and got a rather interesting answer from some girl:

For me I think I have

3 pairs of heeled sandals (to wear with skirts, dresses, or summer clothes)
2 pairs of flip flops
4 pairs of black boots
1 pair of grey boots (for when black doesn’t work with what I’m wearing)
2 pairs of black heels (for dress or black dress pants, ones more business then the other)
2 pairs of black low heeled shoes (business or going somewhere that might include walking)
1 pair of tennis shoes
1 pair of pink heels
1 pair of cobalt blue heels
1 pair of teal heels
2 pairs of low heeled dance shoes
2 pair of latin heeled dance shoes
1 pair of jazz
1 pair of highland
1 pair of low heeled sandals

Those are the ones I can think of off hand.

That’s just too many shoes! Where does she even keep them all? And who has time to buy all those things anyway? And the mental energy it takes to go out and look for shoes … women are truly insane.

An answer from the most well-dressed guy I know was far better in my view:

In general, men don’t need too many shoes short of shoes needed for hobbies. I have two pairs of black shoes, two pairs of brown shoes, and a pair of evening shoes; the rest are activity-specific. Riding boots, hiking boots, dance shoes, boat shoes, cold/wet-climate hiking boots, and so on are only necessary for specific purposes.

That makes sense, having shoes for specific purposes doesn’t really count as having another pair of shoes does it? I certainly wouldn’t think of listing my diving fins or rollerblades under “pairs of shoes” even though they are technically footwear …

Now excuse me while I go meditate on my misery of having to buy a new pair of shoes this week.

Enhanced by Zemanta

5 responses so far

Why you don’t exercise every day

Jan 15 2012

Because you are an idiot.

Can you save a life?

Can you save a life?

But that’s harsh, so let me explain why this makes you an idiot. There was an article posted to HN yesterdayabout why people don’t go to the gym, it included a simple motivational technique of paying yourself to go.

After commenting that the only thing it takes to go to the gym every day is to go to the gym every day, I was downvoted to oblivion. But there’s really nothing more to it than that. You can sugar coat it whichever way you want, you can come up with dozens of motivational techniques – in the end, all it takes is going.

Even if you don’t go to the gym every day, you should still make sure to exercise daily. Here’s how you do it:

  1. Day 1 – exercise when you wake up
  2. Day 2 – exercise when you wake up
  3. Day 3 – exercise when you wake up
  4. Day 4 – eh you’ve exercised three days in a row, might as well do it again
  5. Day N – do the default thing [of exercising]

After a couple of days you have to make a conscius effort not to exercise. The default action is simply to exercise and as humans we love nothing more than not having to make a decision. This is also known as the Seinfeld motivational technique by the way.

Exercise also does a better job of waking you up than coffee, thought I’d mention that.

BUT!

I don’t have time

Richard Branson is one of the busiest humans alive, this is what he has to say about exercising daily:

Tim Ferriss tells this story about how Richard Branson was once asked the single biggest thing most people could do to increase productivity. Due to the fact that he’s one of the busiest men on the planet, every single person in the audience leaned forward with bated breath. His answer? Exercise daily. It improves the quality of your sleep, so you need less. It makes you more emotionally stable, so you’re more motivated. And most importantly, it increases mental clarity, so you’re more focused through-out the day. Branson said that it gives him multiple hours more productivity every day. It’s bull to say you don’t have enough time every day to exercise; if you’re that busy then in fact you don’t have enough time to NOT exercise.

And if you’re anything like the average person who spends a bunch of time commuting, watching television and browsing the online … you have no right to complain about lack of time.

It cuts into family time

A popular complaint in the HN discussion was that exercise cuts into quality time with their families.

Sure … but one day your building will catch fire, or you’ll have a car crash, or _something_. Can you be there for your family when family time involves saving the people you love from death?

No, your flabby office worker muscles won’t cut it and that beer+pizza belly won’t help either. Every man should be able to save his life, and the lives of those he loves! Since this is the 21st century, women should too.

Exercise is hard!

Nobody cares. You’re fat, you’re flabby and you are useless in an emergency. Spending 30 minutes every day doing some basic exercises is nothing compared to the dividends it pays in pretty much all areas of your life.

And if you don’t care about any of that, then do it to make the world a prettier place, one flabby human at a time.

Enhanced by Zemanta

5 responses so far

Geeks of america, please start fighting SOPA

Jan 12 2012

The Stop Online Piracy Act is a bill in the US aimed at protecting intellectual property online. As originally proposed the bill would (skip to Something Useful if you know this stuff)

An episode of the lynching of the Italians in ...

Image via Wikipedia

/../ allow the U.S. Department of Justice, as well as copyright holders, to seek court orders against websites accused of enabling or facilitating copyright infringement. Depending on who requests the court orders, the actions could include barring online advertising networks and payment facilitators such as PayPal from doing business with the allegedly infringing website, barring search engines from linking to such sites, and requiring Internet service providers to block access to such sites. The bill would make unauthorized streaming of copyrighted content a crime, with a maximum penalty of five years in prison for 10 pieces of music or movies within six months. The bill also gives immunity to Internet services that voluntarily take action against websites dedicated to infringement, while making liable for damages any copyright holder who knowingly misrepresents that a website is dedicated to infringement

Not too bad, if you have a site dedicated to copyright infringement you pretty much get taken offline and put out of business. However, according to The Internet, it is extremely easy to fall under that definition. All it takes is a single piece of user generated content to be infringing and BAM! the whole website goes down.

You know all those videos of cute kittens with a copyrighted song in the background that you see on YouTube? Yeah, that means youtube.com is put out of business.

Oops.

As a European there isn’t really anything I can do about this. But americans can and should!

The lynch mob

The crowd at the lynching of Will James in Cai...

Image via Wikipedia

The internet lynch mob has been mobilized. The angle of attack? Boycott supporters of SOPA. A month ago GoDaddy suffered a mass exoddus of users and Namecheap turned it into one of the most brilliant marketing stunts I have ever seen.

Also GoDaddy has made surprising strides in their UI/UX design over the last month, more changes for the better and awesomer than I have seen in the last … five? Six? years.

On January 18th reddit.com is going into a blackout, briefly even Wikipedia considered blacking itself out. There is talk google.com and facebook.com will/should be doing the same. Isn’t that awesome? All these huge sites are against SOPA and are doing something.

Fighting against rich 50-somethings who don’t care about the internet beyond what their grandchildren tell them about cute kitten memes by disabling various parts of it? … yeah, very effective, you can only imagine!

So a few sites are blacked out, and on the next family gathering the congressperson will hear that their granddaughter was kind of upset for a day because she couldn’t feed an electronic sheep or something.

Even if traditional media manages to throw a big hullabaloo about it … what do you think a mexican standof between the government and the internet will look like? All these companies bleeding millions of dollars every single day, while the congresspeople play golf and chat about how awesome winter break was and plotting how to backstab all the big players next time ’round.

By the time they have to be re-elected again the whole thing will have blown over and no damage will have been done. Well except for the big sites, who now have a bunch of influental people angry at them.

Do something useful instead

Instead of being a tempest in a teacup, how about all you american geeks start actually fighting this thing eh? You should!

Direct lobbying statistics in the United State...

Image via Wikipedia

Here’s a small tip I got from a guy who was old enough to pay attention when geeks of Europe fought against software patents and won

  1. Go to Washington
  2. Find names of congresspeople
  3. Find out their habits
  4. Crash their lunches, crash their breakfast, crash their golf game, crash every social event they have
  5. Tell them SOPA sucks (perhaps in more eloquent words)

The RIAA and MPAA are doing this. They are taking people out to lunches, they are telling them how awesome SOPA is, they are telling them how this will save the economy, their precious sportscar and how it will make everyone happy to give them even more money.

Geeks aren’t, at least not enough that anyone would know about it.

We can all agree the internet is an awesome and great communication tool. But real change, real influence … that happens face-to-face. So go out there and be face-to-face with the people who think the internet is a novelty.

Please, for everyone. Do it.

Enhanced by Zemanta

No responses yet

Minimum substring cover problem

Jan 11 2012

A major part of my thesisinvolves finding an algorithm to discover a good substring cover of text in order to properly syllabify said text. But what is the substring cover problem anyway and what does it entail?

algorithms doodle

Image by Shreyans Bhansali via Flickr

The Minimum Substring Cover Problem paper from Hermelin, Rawitz, Rizzi and Vialette dating back to 2007 (judging by the filename) serves as a good entry point into this topic.

There are actually a lot of cover problems, the most famous being Minimum Set Cover and Minimum Vertex Cover problems. In this type of problems we are faced with two sets of elements and we want to cover one of the sets with another, by using the “least” elements from the covering set. I put “least” in quotes because the definition depends on what we want – maybe we want to use the least number of elements, perhaps we want the shortest elements … whatever.

For an example consider this:

S = ['a', 'aab', 'aba']
C(S) = ['a', 'b', 'aa', 'ab', 'ba', 'aab', 'aba']

We can easily see that C(S) is a set of all the possible coverings of S – using a combination of strings from C we can construct every string in S. This part isn’t very difficult to calculate.

Everything gets slightly hairier when you look for minimum covers:

C_1 = ['a', 'b']  # 3-cover (need 3 strings to cover the longest string in S)
C_2 = ['a', 'ab'] # 2-cover (need 2 strings to cover the longest string in S)

Depending on how you choose the weight, both C_1 and C_2 are minimum substring covers of S. Considering “least” to mean  least amount of strings then both are of weight 2, but if you consider “least” to mean the total length of strings then C_1 is better.

You could easily argue C_2 is better, because it uses the least amount of elements to cover the whole set S. 1+3+3 = 7 for C_1 and 1+2+2 = 5 for C_2.

Ok, so now we know that finding the minimum substring cover of a set of strings depends a whole lot on what you actually want. Always a good sign, having a well-known problem where people can’t even agree on what the best solution looks like.

The paper goes on to explain in great theoretical detail that, because this problem is similar to minimum vertex cover, minimum set cover and similar problems, it is NP-hard to approximate. This means that the problem is at least as hard as the hardest problems in NP, but it doesn’t necessarily mean that there is no polynomial solution – it just hasn’t been found yet.

Luckily, if we constrain some parameters of the problem, it becomes/remains APX-hardproblems in this class have efficient algorithms that can find an answer within some fixed percentage of the optimal answer. 

The article then proposes two approximation algorithms for finding minimum substring covers of S.

Local-Ratio Algorithms

This algorithm follows from the local-ratio lemma, which in the case of substring cover means

Let C be a cover for S, and let w_1 and w_2 be weight functions for C(S). If C is an alpha-approximate, both with respect to w_1 and with respect to w_2, then C is also alpha-approximate with respect to w_1+w_2.

Data: A set of strings S, a weight function w:C(S) -> Q+, an integer l >= 2
Result: An l-cover C for S (l is the number of substrings covering the longest s in S)
begin
 
C <- {c in C(S) : w(c) = 0}.
if C is an l-cover of S then return C.
Let s in S be a string not l-covered by C of maximum length.
C_s <- {c in C(S)\C : c is a substring of s}.
Set eps = min{w)(c_  c in C_s}.
Define w_1(c) = eps if c in C_s, 0 otherwise.
C <- LR(S, w_2, l).
if C\{s} is an l-cover for S then C <- C\{s}.
return C.
 
end

This algorithm is guaranteed to terminate after a polynomial amount of recursive calls and it returns a (((m+1) binomial 2) – 1)-approximate l-cover of S.

In sensible terms the algorithm basically does this: Add everything with zero weight to a partial solution, if this isn’t the solution, it selects an uncovered substring in S and tries to cover it by examining all substrings in C_s.

Linear Programming Rounding

Originally the linear programming rounding algorithm was developed by Hajiaghayi et all. for the Minimum Multicolored Subgraph problem when l=2. It has now been expanded for any constant value of l.

This section is extremely light on practical results and just shows a bunch of mathematics that supposedly prove how the algorithm can be extended and that the final result is an O(log^(1/l) n * m^((l/1)^2/l))-approximate algorithm.

From what I can understand this algorithm approaches the problem with the idea that they are basically looking for l-factorizations of strings.

According to this section, the minimum substring cover can be formulated using the following integer linear program:

min    SUM_(c in C(s)) w(c)x_c
s.t.   SUM_(f in F_l(s)) y_f >= 1          every s in S
       SUM_(c in f in F_l(s)) y_f <= x_c   every s in S, every c substring of s
       x_c, y_f in {0,1}                   every c in C(S), every f in F_l(S)

# F_l(S) is the set of all factorizations of S

Then there are a bunch of proofs that this algorithm works and is indeed very awesome ... but by this time my eyes started glazing over and the September deadline for my thesis started looking very near.

Enhanced by Zemanta

No responses yet

The No brown M&M’s rule

Jan 10 2012

There once was a band, let’s call them Van Halen, who had a very long and complex contract for venues. Partly because they were famous and venues would do anything to get them, partly because people could literally die.

Van Halen performs their song "Jump"...

Image via Wikipedia

The contract was full of useful things like “The floor should support such and such weight” and “We need power outlets there and there and there [or our guitars won't work and you'll have a shitty show]”

In the middle of nowhere was a demand for a bowl of M&M’s backstage, without any brown pieces.

An outlandish request, by flamboyant rockstars stretching their decadence? Not really, just a very good way to make sure the contract was followed to the letter and, you know, they’d survive the show. Find a brown piece – go check over the whole production. You will find something wrong.

With the current startup climate developers are the modern rockstars. We may not get all the groupies and we may be quite well behaved for the most part – but it’s time we started making fun outlandish requests don’t you think?

Designers are usually seen as the extravagant bit of the startup world, getting all the cool toys, working from rooms filled with inspiration and mojo … developers concern themselves much less with these things – give us a good set of monitors, free reign on our computer, some peace and quiet and we’ll be happy.

But when there are so many opportunities out there, you need a brown M&M so you don’t end up wasting even a day at a company that doesn’t quite live up to expectations.

For me, the brown M&M is version control.

If a company isn’t using git or mercurial, I can be pretty certain there will be other problems as well. Everything from a shoddy codebase, to expecting my physical presence before my brain has had a chance to boot in the morning.

Usually the use of old-ish tools also correlates with a corporate feel to the company, which goes directly against my rule of only working with [small] startups. Plus it usually means I won’t be given freedom in choosing the best technology stack for the job, but will have something mandated from above.

I could probably go on, but you can imagine the rest. Use of SVN or, god forbid, nothing, is a deal breaker for me and it’s the symptom I can discover very early in the process … haven’t gone so far as putting it in the contract yet.

Do you use a brown M&M technique to assess potential clients?

Enhanced by Zemanta

2 responses so far

Collatz, Haskell and Memoization

Jan 09 2012

xkcd collatz conjecture

xkcd collatz conjecture

After an awesome longboarding session yesterday afternoon I decided to play around with infinite sequences in Haskell – it’s supposed to be one of the more (most?) powerful features of Haskell – because it’s a lazy language apparently.

My first impulse of creating a primes generator was nipped in the bud by a long page of prime number generators in Haskell. Scary, complex, mindboggling.

Project Euler posed a much better challenge: Which starting number, under one million, produces the longest collatz chain?

The solution I came up with was a simple brute force generator of infinitely many collatz sequences. Then I would take the first 1,000,000 find the maximum and that’s that.

collatz :: Integer -> [Integer]
collatz 1 = []
collatz n
    | odd n = 3*n+1:collatz(3*n+1)
    | even n = div n 2:collatz(div n 2)
 
chains = [collatz x | x <- [1..]]

Didn’t help.

So I started looking for the maximum a bit differently – take all the sequences, sort them by length and take the last one.

longest max =
  last $ sortBy (comparing snd) $ zip [1..] $ map length $ take max chains

Great! It worked! But it takes ~26 seconds!

Well sorting maybe isn’t the best idea ever, so let’s try creating a list of sequences where the list’s tail only contains those sequences that are longer than the head. A sprinkle of dropWhile and it was done.

longest' (max_i, max_l) =
  let l = head $ dropWhile (\(i,l) -> l <= max_l) $ zip [max_i..] $ map length $ chains' max_i
  in l:longest' l

~25 seconds!

That’s odd … even odder still is running both algorithms one after another only takes 33 seconds. Huh?

It would seem I’m using memoization incorrectly. I’ve heard it performs funny in recursive functions. The theory I formulated last night was that because haskell was lazy each execution chain was constructed to its end and the intermittent memoized values never got used until the whole function was called again.

Looking at the code samples this morning, though, I discovered this:

collatz :: Integer -> [Integer]
collatz = memoize col where
  col 1 = []
  col n
    | odd n = 3*n+1:collatz(3*n+1)
    | even n = div n 2:collatz(div n 2)

As you can see, I don’t call the memoized function internally. Just goes to show what a night’s sleep can do to one’s coding abilities. I bashed my head against this problem for four hours yesterday and I never noticed I was recursing to the wrong function!

Interestingly enough, fixing that makes the algorithm spaz out and die after 16 seconds. The only output I get is “Killed”. Curious.

Enhanced by Zemanta

5 responses so far

A message from your future self

Jan 06 2012

About two weeks ago I watched a TED talk on the battle between one’s present and future self. The idea being that a lot of our problems can be summed up to this: Your present self is here, he wants cool things, your future self is greatly affected by these decisions, but he can’t do anything about it.

Over the holidays I gave Future Swizec a voice. Every three days he sends me an email to tell me what sort of financial situation he will find himself in after two weeks of Present Swizec doing stupid shit. That’s not very far into the future, but it’s just enough to do something about it, while not being too far away to think Meh, that’s in ten years, I’ll deal with it nine years from now.

If you want to give Future You this kind of voice have a look at my Personal Runway project on github. If you think this is a cool idea, but don’t want to set it up yourself – email me! I might just make this proof of concept into a minimum viable product if there is any interest :)

I first ran it when I had just finished paying all my bills, which was a huge expense, so Future Swizec sent me this email:

Future Swizec

Future Swizec

Maybe he should be less happy about only having 5euro, but right now he’s super every time his balance isn’t negative. Perhaps Future Swizec needs a gray area.

How it works

Right now the predictions are somewhat rudimentary, for instance I don’t take into account the fact money doesn’t magically show up in my account after every billable hour. Another thing not taken into account are periodic expenses, super large spikes also throw it off somewhat.

The basic idea is this:

  1. Smooth out the data with a rolling average to get a curve
  2. Calculate an expected value for the next day after known data
  3. Expand the window for expected value by two (one into the future, one into the past) and calculate the next expected value

Weights are calculated according to the simplest infinite series I could think of that always sums to one no matter how many elements are needed. But I also split it in half and shuffle it about at the pivot, so the strongest weight is moved back in the data when calculating days. So in a sense, when calculating the 1st unknown value, the last known has highest weight. Then the penultimate known and so forth.

The algorithm is pretty simple in Haskell

 
-- ((n-i)*2)/((n-1)*n)
weights :: (Num b, Fractional b, Enum b) => b -> [b]
weights n =
  map (\i -> ((n-i)*2)/((n-1)*n)) [1.0..n]
 
rotate :: Int -> [a] -> [a]
rotate n l = t++h
  where (h,t) = splitAt ((length l) - n) l
 
expected :: (Fractional a, Enum a) => Int -> [a] -> a
expected pivot xs = sum $ map (\(w,v) -> w*v) $ zip (rotate pivot (weights $ fromIntegral $ length xs)) xs

Of course someone who actually knows Haskell would write it much better.

And there you have it, a simple way to talk with Future Swizec. There is still some tweaking to do and hopefully I can find more people to run this on and see how it works out for them as well.

Enhanced by Zemanta

One response so far

« Newer - Older »

« Programmers are born not made Collatz, Haskell and Memoization »