Posts Tagged ‘graph’

2008 – 2009 UK Tax Graphs

Monday, September 1st, 2008

I’ve produced some graphs using data about the 2008 – 2009 UK tax situation.

I’ve tried to make them accurate but beware that I’m not a tax expert so there could well be errors. They have been created for interest only, not for serious use.

The first graph is showing how much income tax you pay depending on how much you earn. This graph is based on the standard un-adjusted tax free allowance of £6305, a 20% band for the next £34800 and 40% after that.

income tax 08 09

Next is a similar graph but for national insurance contribution. I’ve used £105 per week as being free from NICs, 11% for £105-£770 per week and 1% after that.

national insurance 08 09

The third graph combines the total of the two to show the total taxation.

total tax 08 09

The final graph shows what percentage of your gross income you pay as tax. The interesting shape is caused by the National Insurance contributions changing to 1% before the 40% tax band kicks in.

percentage of income as tax 08 09

You may spot that when your salary reaches just over £40k the percentage of salary that you pay in tax actually goes down by a very small amount before going back up again.

Graphing the AXA Sun Life Guaranteed Over 50 Plan

Friday, August 22nd, 2008

On TV recently I’ve been bombarded by adverts about the AXA Sun Life Guaranteed Over 50 Plan. The current version of the advert is presented by Michael Parkinson. Previous versions have been presented by June Whitfield.

I am no way near the age of 50 and these plans have no relevance to me. I should also point out that I am not a financial advisor, and am not intending to offer any opinion on these plans. My interest is to look at them from a simple mathematical point of view.

In case you’ve missed the advert the basic idea is this. If you are over 50 you can pay AXA a fixed monthly sum for the rest of your life. When you die a fixed sum (fixed at the time you open the plan) is payable to your family. If you die within two years you don’t get the fixed sum, but your family do get 1.5x your premiums back. If you ever stop contributing you don’t get anything.

I went on their website and got a quote for a 60 year old male paying in £6 per month (the minimum a 60 year old male can pay in on the day I got the quote). This produces a cash lump sum of £760, payable on death after 2 years.

Here is a graph plotting how much you pay in, against how much you get back. You can see there is a cross-over point at which you end up paying in more money than you get back. In this case you end up having paid in more then you’d get out when you reach 71 years old.

axa sun life over 50 plan graph

The government publish data on life expectancy. I got the latest male life expectancy data from 2004 and 2006 and plotted this into another graph. Note how your life expectancy goes up as you get older. This is because you have already managed to avoid dying in the preceeding years.

uk life expectancy

You might not be able to make out the detail on the graph but the life expectance for a 60 year old male in the UK is 80.81 years.

This means that if you are an average person you are likely to be paying in 9 years of premiums beyond the lump sum value that you would get back.

However calculating the benefit of these plans isn’t quite as simple as this – they often provide extra benefits such as extra payouts in the event of dying in an accident or whilst travelling.

An obvious factor to look into is the effects of compound interest when adding the same amount (£6) into a bank savings account every month.

compound interest graph on a monthly saving of £6 at 4%

This graph is showing the effect of saving £6 a month based on a modest 4% gross interest rate. I based the calculations to produce this graph on the formula given on patrick schneider blog post. The final figure after 40 years matches the figure given by other compound interest monthly savings calculators I’ve seen on the internet so hopefully the graph is accurate!

Below I’ve put the compound interest curve on top of the previous graph comparing contributions against the cash lump sum.

compound interest graph on a monthly saving of £6 at 4% compared against AXA Sun Life Plan with £6 per month contribution

There are further complications to consider such as the effect of inflation. The real worth of any money your family would get back when using these plans is reduced every year due to inflation. On the other hand the real world cost to you goes down each year as your £6 per month will gradually constitute a smaller percentage of your income.

What are my conclusions? I’m not giving you any! Any decision on whether to use a financial product should be taken based on your personal financial circumstances and with the help of a trained financial advisor (which I’m not).

I would say that these are the kind of analyses you should be doing when investigating or comparing any kind of financial product, whether it be a savings account, loan or mortgage. Turning financial information into simple graphs is a very powerful tool that can save you a lot of money!

Dijkstra’s Algorithm code in C++

Tuesday, July 15th, 2008

Dijkstra’s algorithm is a famous algorithm that calculates the routes and distances from a start node to all other nodes in a connected graph where all the distances are positive.

Here is a version which I wrote in C++. My aim here was to try to create a version that was easy to read, rather than the most efficient version. This version is implemented using STL vectors to store the edges and nodes. I might later modify it to use adjacency lists and priority queues to produce a more efficient implementation.

First off is my Node and Edge class. You’ll note that the objects add themselves to the nodes and edges vectors as they are constructed.

vector<Node*> nodes;
vector<Edge*> edges;

class Node
{
public:
	Node(char id)
		: id(id), previous(NULL),
		distanceFromStart(INT_MAX)
	{
		nodes.push_back(this);
	}
public:
	char id;
	Node* previous;
	int distanceFromStart;
};

class Edge
{
public:
	Edge(Node* node1, Node* node2, int distance)
		: node1(node1), node2(node2), distance(distance)
	{
		edges.push_back(this);
	}
	bool Connects(Node* node1, Node* node2)
	{
		return (
			(node1 == this->node1 &&
			node2 == this->node2) ||
			(node1 == this->node2 &&
			node2 == this->node1));
	}
public:
	Node* node1;
	Node* node2;
	int distance;
};

Next up is my code to construct a simple test graph. The starting node is initialised with a distance of 0. Dijkstra’s algorithm is then called before the results are printed out. Underneath the code is a diagram of what this graph looks like.

void DijkstrasTest()
{
	Node* a = new Node('a');
	Node* b = new Node('b');
	Node* c = new Node('c');
	Node* d = new Node('d');
	Node* e = new Node('e');
	Node* f = new Node('f');
	Node* g = new Node('g');

	Edge* e1 = new Edge(a, c, 1);
	Edge* e2 = new Edge(a, d, 2);
	Edge* e3 = new Edge(b, c, 2);
	Edge* e4 = new Edge(c, d, 1);
	Edge* e5 = new Edge(b, f, 3);
	Edge* e6 = new Edge(c, e, 3);
	Edge* e7 = new Edge(e, f, 2);
	Edge* e8 = new Edge(d, g, 1);
	Edge* e9 = new Edge(g, f, 1);

	a->distanceFromStart = 0; // set start node
	Dijkstras();
	PrintShortestRouteTo(f);

	// Node / Edge memory cleanup snipped
}

Dijkstra's algorithm

Here is the actual algorithm implementation. We remove the node with the smallest distance from the list of nodes, and then calculate all the minimum distances. We repeat until there are no more nodes left.

void Dijkstras()
{
	while (nodes.size() > 0)
	{
		Node* smallest = ExtractSmallest(nodes);
		vector<Node*>* adjacentNodes =
			AdjacentRemainingNodes(smallest);

		const int size = adjacentNodes->size();
		for (int i=0; i<size; ++i)
		{
			Node* adjacent = adjacentNodes->at(i);
			int distance = Distance(smallest, adjacent) +
				smallest->distanceFromStart;

			if (distance < adjacent->distanceFromStart)
			{
				adjacent->distanceFromStart = distance;
				adjacent->previous = smallest;
			}
		}
		delete adjacentNodes;
	}
}

Here are the supporting functions. The first function removes and returns the node with the smallest distance from the list.

The next returns a new vector containing all the nodes which are adjacent to the node that we pass in.

The third returns the distance value of an edge which directly connects the two nodes.

The final one checks if a node is in the list of nodes.

// Find the node with the smallest distance,
// remove it, and return it.
Node* ExtractSmallest(vector<Node*>& nodes)
{
	int size = nodes.size();
	if (size == 0) return NULL;
	int smallestPosition = 0;
	Node* smallest = nodes.at(0);
	for (int i=1; i<size; ++i)
	{
		Node* current = nodes.at(i);
		if (current->distanceFromStart <
			smallest->distanceFromStart)
		{
			smallest = current;
			smallestPosition = i;
		}
	}
	nodes.erase(nodes.begin() + smallestPosition);
	return smallest;
}

// Return all nodes adjacent to 'node' which are still
// in the 'nodes' collection.
vector<Node*>* AdjacentRemainingNodes(Node* node)
{
	vector<Node*>* adjacentNodes = new vector<Node*>();
	const int size = edges.size();
	for(int i=0; i<size; ++i)
	{
		Edge* edge = edges.at(i);
		Node* adjacent = NULL;
		if (edge->node1 == node)
		{
			adjacent = edge->node2;
		}
		else if (edge->node2 == node)
		{
			adjacent = edge->node1;
		}
		if (adjacent && Contains(nodes, adjacent))
		{
			adjacentNodes->push_back(adjacent);
		}
	}
	return adjacentNodes;
}

// Return distance between two connected nodes
int Distance(Node* node1, Node* node2)
{
	const int size = edges.size();
	for(int i=0; i<size; ++i)
	{
		Edge* edge = edges.at(i);
		if (edge->Connects(node1, node2))
		{
			return edge->distance;
		}
	}
	return -1; // should never happen
}

// Does the 'nodes' vector contain 'node'
bool Contains(vector<Node*>& nodes, Node* node)
{
	const int size = nodes.size();
	for(int i=0; i<size; ++i)
	{
		if (node == nodes.at(i))
		{
			return true;
		}
	}
	return false;
}

And finally here is the code which prints out the route between our start node ‘a’ and the destination node ‘f’. It starts at the destination node and then works backwards using the previous pointer in each node.

void PrintShortestRouteTo(Node* destination)
{
	Node* previous = destination;
	cout << "Distance from start: "
		<< destination->distanceFromStart << endl;
	while (previous)
	{
		cout << previous->id << " ";
		previous = previous->previous;
	}
	cout << endl;
}

Update: 7th September 2008

I have now uploaded a zip file containing the Visual C++ Express Solution file and source code for this project – Dijkstra’s Source Code (8kb).