Monday, August 26, 2013

Firefox OS ZTE phone

The other day I received one of the limited number of ZTE Open phones running the relatively new Firefox OS. The specifications of the hardware are limited, especially next to my Nexus 4. But, at £59 compared to the Nexus 4's £290 (and that's cheap for a top-end smart phone), I think it is excellent value. Yes, Firefox OS is a little rough round the edges from a user's point of view, but my first impressions are that it's perfectly usable. And, I have more trust in Mozilla than Google, especially after the NSA/Snowden revelations of recent weeks.

To me, the main attraction of Firefox OS is the fact it is based on HTML5 and CSS and javascript. To those who don't know what that means: those are standards that keep the web open, i.e. you can rely on being able to view and interact with content on a web page on almost any type of PC, tablet or phone, made by any manufacturer, or under an operating system from any vendor. In contrast, mobile phone apps are not like this: android apps work only on devices with Google's android; iOS apps only work on Apple's iThings. But, if you write an app in Firefox OS, it's a simple matter to deploy it as a web app which may be used from any device with a web browser. There are of courses cons against that pro, but that's a discusson for another blog post.

About the time I received my phone, I noticed a message from a well-respected-techy-geeky-sort-of-a-chap, called Jezra, who had also recently acquired the same phone, referring to it as "shitty". Upon asking why, he told me it was because he thought it was hard to get his software on to the phone. Despite my dewy-eyed enthusiasm for this phone driven by open standards, I was soon to discover he had a point.

To start my adventure, I read this article and set about trying to install the Firefox OS Simulator. It's an Add-on to Firefox and should've been a simple matter to install, but I discovered that it's not available for Firefox 17 ESR, which is the default version in slackware 14.0, which I have on my laptop (Patrick Volkerding did provide prompt upgrades to recent Firefoxes, but at 22 he couldn't work around a troubling bug, and decided to go back to 17 ESR for slackware stable). Not wishing to mess with a very stable operating system, I downloaded the linux binary tar ball of Firefox 23.0.1, closed Firefox 17 and ran the newer Firefox binary without installing it - it just sits under my home directory. I was then able to install the Firefox OS simulator version 4.0 as I would any other Add-on, and it seemed to work fine. Soon I had this Hello World app and the Boiler-plate app working without issue.

Next up was the potential "shitty" bit - getting the app onto my phone. Now there are numerous ways of doing this, including via a website, but I wanted to get it done via USB, because that, in my experience, mainly with android, is probably best when developing and debugging an app.

According to the aforementioned article, connecting my phone to my laptop involved creating the file /etc/udev/rules.d/51-android.rules containing the following:

SUBSYSTEM=="usb", ATTR{idVendor}=="19d2", MODE="0666", GROUP="plugdev"



The bit that will vary from phone to phone is idVendor; 19d2 means ZTE. You can look these up online (there's a table somewhere in the android adb documentation), or, you can do what I did under linux, and hunt around in the output of dmesg | tail immediately after plugging the phone in. A restart of udev is recommended after you create the rules file, and the udev folk recommend restarting your system.


Luckily, I happened to have the android adb tool already installed, and from the output of running adb devices, I could see that "roamer2" was listed, which, despite the misleading name, was indeed my ZTE phone.

Now when I fired up the Firefox OS simulator it said "Device connected" and "Push" buttons appeared to the right of my apps. This all looked very encouraging. But, clicking the "Push" button did nothing. Nothing happened in the simulator, not even an error message, and the phone didn't display the connection prompt depicted on the page of instructions (mentioned above). Further down that page, a command line method of pushing apps on to the device was described. OK, I thought, if I try that, even if it doesn't work, at least I'll get some clueful error messages, such is the advantage of the CLI.

The CLI tool is called make-fxos-install, and it needs both the android adb, which I had installed, and xpcshell from Mozilla's xulrunner, which I did not. Luckily, I found xulrunner listed on slackbuilds.org and so within a few seconds I had sbopkg downloading and then building xulrunner.

The source downloaded was 80MB, so I was expecting the compilation to take a while on my rather slow laptop. To pass the time, I decided to play around editing the various demo apps and testing the results out in the Firefox OS simulator. I immediately hit problems. After even the tiniest change to an app's manifest, the simulator kept complaining it couldn't update the manifest cache and something about an NS_ERROR was mentioned amongst other gobbledegook. Frustrated, I decided to launch VirtualBox and try out the simulator under Windows to see if the trouble was caused by some linux-related issue (which I thought very odd and rare). To my astonishment, Virtual box failed to start and also gave an NS_ERROR. I thought for a moment that my dual use of Firefox had screwed up the system. I rebooted the system and found X Windows wouldn't start.

I was just about to hurl myself out the nearest window (OK, I was on the ground floor), when I noticed a little snippet that mentioned a write to /tmp/something had failed. I quickly realised that the disk had filled because of the xulrunner compilation.

I then set xulrunner building on my more powerful desktop machine which has a much larger and less full hard disk. Unfortunately, since I had done this whilst sat at my laptop downstairs, my wife inadvertently shut-down the desktop machine upstairs once our son had gone to bed. Grumble. Once my third attempt at building xulrunner had finished without incident, I scp'd the .txz slackware package over to my laptop and installed it.

I then needed to edit the Makefile that comes with make-fxos-install to specify the path to the xpcshell executable. But it didn't work. After a fair bit of pulling of what little hair I had left, I noticed that the xulrunner version I had built was 15. Guessing this was rather old, on the basis that version numbers track firefox, and unable to face any more compiling, I went to mozilla ftp site and found linux 64bit binaries for xulrunner version 23, which matched the firefox I was using for the simulator. I downloaded the xulrunner-sdk (which is where xpcshell is to be found these days) and stuck it in a directory underneath the Makefile.

But still xpcshell refused to work, complaining about a missing library, but this line in the Makefile got it to play ball:

LD_LIBRARY_PATH=xulrunner-sdk/lib xulrunner-sdk/bin/xpcshell

After a little RTFM I found the command to push the helloworld app to my phone was:

make FOLDER=/home/MyUserName/dev/firefoxos/examples/firefoxos-helloworld packaged install

Did I finally get to rejoice and dance about the room at the sight of this little app installing on my phone? No. Grrrrrr! It complained that it couldn't bind to port 6000. After looking at the Makefile I understood that the android adb tool was trying to forward port 6000 on my laptop to 6000 on my phone. Right. So if local port 6000 was unavailable, for reasons unknown, perhaps I could just tell the Makefile to use 6001 and forward that to port 6000 on the phone, which simply involved a tiny tweak to the thoughtfully constructed Makefile, namely:

PORT_LOCAL = 6001

And now executing the make command above worked! The phone prompted me to accept the connection and the app installed and ran. Phew. But, I wasn't quite satisfied; I had to know what port 6000 was being used for on my system. Executing the command

lsof -i tcp:6000

gave me no output, from which I concluded that no process was actually using the port. But this command

nmap localhost

told me that port 6000 was in some way being claimed by X windows. A bit of web research told me that it was needed for remote sessions, which I never use. To stop it using this port, I shut down X windows and started it again with

startx -- -nolisten tcp

I put PORT_LOCAL back to 6000 in the Makefile and found I could now push to the phone. More than that, I found that the "Push" button in the Firefox OS simulator now worked too.

In summary, to get apps to push to a phone from the FireFox OS simulator, in linux:
  • use a recent version of Firefox (23.0.1 worked for me)
  • create the udev rule file
  • make sure port 6000 is open
and to get it to work from the command line, in addition you need to:
  • get a recent version of xulrunner sdk which includes xpcshell (23.0.1 worked for me)
  • get the android SDK which includes the adb tool
  • edit the Makefile to specify paths to xpcshell and adb, and libraries they need
So, is FireFox OS "shitty"? No, I'd say not. Is FireFox OS currently difficult to push apps to via USB? Yes, definitely. But to be fair, the FireFox OS simulator is clearly marked alpha and some of my difficulties were freak (wife turning off desktop) and others were linux distro specific (port 6000). That said, without drivers for the ZTE Open phone, it's currently impossible to do it on Windows.

I hope the details above prove useful to someone, and if not, I hope you had a geeky chuckle or two at my FireFox OS misadventure. Hopefully I'll  soon write some posts about how easy it is (stifles laugh) to write web apps.

Thursday, June 13, 2013

Writing

I've enjoyed writing since I was a small boy. At school I remember writing Dr Who stories on old computer paper from my Dad's work. I also wrote a series of Goon Show-like parodies of Star Trek which seemed to go down well with folk at my school. Later on writing became a central part of my job when I worked at the Open University, writing science courses. I learned a huge amount doing that, especially from the diligent and patient science editors that would pore over my work, and sit with me, talking me through their comments sentence by sentence.

These editors made sure the writing was clear, correct in grammar and free of typos whilst also taking care not to disturb the style of writing. This surprised me because the text books we wrote involved several authors and I expected part of an editor's job was to pull it together and bring more consistency of style between chapters (writing style, not font sizes etc which was strictly controlled). I distinctly remember how one of them explained his role to me: "We do not want to make the book homogenized. Students generally like a change of tone and change of pace. Besides, you can't edit those things anyway."

It was gruelling at times. I remember being sat in one editor's office as she patiently explained when I should use 'which' rather than 'that', e.g. "This is the train which takes you to Newcastle." versus "This is the train that takes you to Newcastle." Which one is correct, dear reader?*

But, there's no other way to learn other than to write and rewrite and seek reactions from readers, then write something else and revise, and so on. I find it very hard to progress a piece of writing working by myself. Very often it would start well enough but then I would get stuck during the revisions, often abandoning the piece. So I consciously decided to write my first drafts with more care, rationalising that if I could do that, then I'd have less revising to do. But this made matters worse. My writing now slowed to a snail's pace crawl, was thoroughly unenjoyable and the turgid result could not be rectified by any amount of disciplined editing because the style and tone were compromised. Had I stopped to think about it back then, I might have realised that that's exactly what the editor at the OU had meant.

So, for many years I hardly wrote at all. It was even a welcome relief when the rise of mobile phones and twitter meant that emails became shorter and more sloppy: I could spend less time trying to write them well, or so I thought for a while.

But I missed writing. I wanted to return to it, even though it seemed like a futile waste of my time. I had ideas that needed expressed, but repressed them because I couldn't think how to get them out. After writing nothing for years, the urge strengthened sufficiently for me to bang out a few short stories, but I didn't feel happy enough with them to let others read them. And I was slow. One story took me more than two years to complete.

I remember sitting in my car outside a supermarket listening to BBC Radio 4. The author Frederick Forsyth was being questioned about how he wrote. To my surprise he typically wrote six pages a day, but stressed that it was very important to get into the discipline of writing every day. That didn't seem like a lot to me. What did he do for the rest of the day? Presumably gathering the experiences and knowledge that gave him something to write about.

I investigated the habits of other authors and I found many did something similar. One author, I forget who, had a strict rule of writing only two pages per day and never any more. But, as Forsyth had also emphasised, even at this rate you can yield a novel-length work in a few months.

The details of how authors work varies tremendously, but I'm not aware of one that advocates binge writing. Little and often yet disciplined and regular was the recurring advice. Approaches to revising vary greatly, but most recommended that you should not aim for perfection on a first draft.

Then I happened across NanoWriMo.org, which encourages people to produce 50,000 words from scratch in the month of November (continuing an existing work is not allowed). At first I was skeptical. Isn't this just going to produce 50,000 words of drivel and probably verbose drivel at that, so as to produce the requisite 1667 words per day?

The answer is sometimes, perhaps even in most cases. But that's missing the point. What it achieves is to encourage a person to write with discipline and work towards a goal. In my case it helped prove to myself that even with two children, a day job and many other calls on my time, I could get those 1667 words out almost every day.

The quality of my writing varied tremendously of course, with some days being awful, most days being OK and a few being good. But I realised it didn't matter. As long as I was relaxed when I wrote so that my writing was natural rather than forced, what I produced was generally acceptable in that it expressed my ideas well enough so that someone else might understand them. It took me another year to get the word count to 80,000 and since then seven months have elapsed during which I have performed two editing passes over the whole thing. It's still not finished. On the second editing pass I decided to add several new sections and remove a few that weren't working. Once I've done that, I hope, it'll be ready to be read by someone else.

It's certainly the case that my writing has improved over this time and with it I'm probably being more economical with words, though I still struggle with repetition (She did that... she did this) and banishing superfluous words, especially adverbs (He just stood there, impassively). I'm also somewhat blind to typos and rely on others to help me find them. But I'm realising that staying relaxed and writing a little too much and editing out is better, for me at least, than trying to write precisely under conscious invigiliation. Also, most readers, at least ones who are not authors, will skate over pointless adverbs and other minor flaws as long they want to know what happens next.

If I had to summarise it in one pithy phrase, then I'd settle for "be as simple as possible, but no simpler".

You can read some of my writing here at http://www.mcnalu.net/writing

*That one. :)


Thursday, January 3, 2013

Faster USB sticks

I've been trying to set up my Linutop 2 box as a small server for a while now, but keep hitting a variety of annoying problems. The latest was that an intenso 32GB USB stick seemed to be the cause of achingly slow performance.

I read in Linux Format recently that it was wise to make sure partitions and filesystems were aligned with the structures found on the flash drives. Flash devices come set up in this way, but invariably with archaic FAT32 filesystems and I wanted to use a more up-to-date linux filesystem. I didn't know how to ensure alignment, so I embarked on a course of reading across the internet.

Unfortunately the information I found was generally very confusing, some articles told me what to do, but not why, and others went into bamboozling levels of gritty detail, and one or two even told me aligning wasn't worth the effort on USB2.0 drives. I'll provide links of the sites I found most helpful at the end, but here's my account of what I did, starting with the USB stick formatted in a completely misaligned way with three partitions - the result of my fettling before I knew any better.

First I plugged the drive into my laptop, then, as root:
mount /dev/sdb3 /mnt/hd
I tested the read speed with:
hdparm -t /dev/sdb
and this told me the speed was 23.8 MB/s - I did a few tests and found this varied by no more that +/- 0.05 MB/s. I then mounted the third partition, which had an ext2 filesystem on it, with:
mount -o sync /dev/sdb3 /mnt/hd
The sync option means that the writing isn't buffered - if it is buffered then the test will give you a misleadingly high speed because it will only include the time take to write to the buffer in memory. I then tested the write speed with:
dd count=100 bs=1M if=/dev/zero of=/mnt/hd/test
This copies one hundred 1 megabyte blocks of zeroes to a file called test. The result was a paltry 893 kB/s.

I then set about deciding how I could reformat the drive to get better performance, particularly for writing. I wish I'd kept a note of the original partitioning of the stick as that would give me vital clues about how to align partitions and filesystems. After searching the web, the best I could do was guess that the USB stick was likely to have a 4kB or 8kB page size and a 512kB erase block size.

To be honest, I haven't really had time to get to grips with the details, so please excuse all the uncertainty, but I'll try and set out my patchy understanding. The erase block size is significant because if the filesystem requires, say, 100kB to change, then the flash drive electronics will have to wipe at least a 512kB block and rewrite it all. I say "at least" because if that 100kB happens to cross the boundary between two adjacent 512kB blocks then both blocks will have to be erased. These erase blocks are grouped together to form erase block groups which, I understand, are typically 4 MB in size. This means that partitions boundaries should be on multiples of 4MB on the device.

The page size represents the smallest unit that can be addressed on the flash drive. I'm not sure what impact this has, except that clearly you don't want the partition or filesystem constantly straddling the "pages". But, if you're using a block size of 4kB in an ext filesystem and you've aligned the partition to 4MB there should not be a problem. With modern multi-GB drives 4kB will usually be the default block size used by the mkfs command and it's also the maximum permissible block size on typical systems.

From this you can get some idea of how aligning data with these blocks can help with performance. Also, because it reduces the number of writes to the disk, alignment can also help increase the life of a flash drive. One big problem in aligning is that there is no easy way to find out the page or erase block size for a particular stick because such information is often not published by manufacturers. The best you can do is look at the original factory formatting of the stick or search around on the internet to see if someone has measured it. (There are tools to do it yourself, and I tried flashbench which was intriguing but unfortunately it gave inconclusive results for my USB stick.)

I side stepped the issue of partition alignment by not creating any partitions at all (zero is a multiple of all numbers!) In linux it is quite permissible to create a file system directly on the device. I did this with the command:
mkfs.ext4 /dev/sdb -b 4096 -E stride=128,stripe-width=128
This creates an ext4 filesystem on device sdb (sda is the hard drive of my laptop). The -b 4096 specifies the use of a 4kB block size (I had tried 8192 but that is possible only on a few specialised systems. Next, I tried to make sure the filesystem was likely to be aligned with 512kB blocks by setting the stride and stripe-width both to 128 because 128 times 4kB is 512kB. These two parameters are more often used to make sure filesystems work efficiently with RAID arrays, but as we're only dealing with one drive here I understand that they should be set to be equal to each other (though I confess I don't know why).


Finally, to save on disk writes, I turned the journal off:
tune2fs -O ^has_journal /dev/sdb

The downside of this is that the disk will be more likely to suffer from data loss if powered down or removed incorrectly. (This option can also be set when creating the filesystem I believe so it should be possible to combine the above two commands.)

With this done, I performed the above tests again and found that the read speed was almost the same at 22.8 MB/s. The write speed was now 4.7 MB/s - a five-fold increase. Job well done! That wasn't all, if I repeated the test, the write speed seemed to go up reaching a little over 10 MB/s after 4 runs of the test. I'm not sure why this might be, but it could be to do with some optimisation in the onboard electronics in the flash drive combined with the artifice of the test in writing long stretches of zeroes.

But, I'm not quite comparing like with like here. In the "before" tests I was writing to an ext2 partition, so is it possible that some of the speed-up is due to inefficiencies of ext2 compared to ext4? To test this I created an ext2 partition directly on the disk with this command:
mkfs.ext2 /dev/sdb -b 4096
(The stripe and stride width options are not available for ext2.) The first thing that struck was how slow this was compared to the ext4 creation - 30 minutes compared to a few minutes. The tests revealed that performance was back to almost exactly what I found on the original ext2 partition with its completely misaligned partitions and filesystems.

Finally, I wanted to see if it was ext4 that was behind the speed gain, or if it was the setting of the stripe and stride option that brought about the improvement. To test for this I issued this command:
mkfs.ext4 /dev/sdb -b 4096
and did not disable the journal. The write speed was now 3.6 MB/s which seems to suggest that ext4 is responsible for most of the speed gain. The continued flashing of the stick's LED after the test had ended suggested that the journal was keeping the stick busy at least to some extent and this was further confirmed when I umounted the stick and the flashing ceased (this ruled out the activity as the flash doing wear-leveling maintenance. Interestingly, there was a slightly improvement in read speed, but no more than a few percent.

So, in conclusion, the most important improvement seemed to be just to use ext4 instead of ext2. Disabling the journal helped a little too, as did setting the stripe and stride options on ext4. But, I didn't find any evidence that alignment helped improve the performance of this drive.

Here are some links:

Wednesday, December 26, 2012

Reading Keynes part 3

In my previous post in this series I described some thoughts that came from reading book III of Keynes's General Theory. Before moving on to discussing subsequent books, I'd like to make some notes regarding the simple model I discussed in that post.

The first note regards how a lack of consumer spending (in the absence of saving and investment) could drive an economy to disaster. A step in the argument was that employees who feared for their jobs will cut their consumer spending. This is plausible, but there are really two types of such spending. The one that can be most readily cut is the spending on luxuries, but spending on essentials, such as food, clothing, rent and repairs cannot be cut below a certain level. This enforced minimal level of consumption can stop a descent into complete economic collapse, but the word 'disaster' is still applicable as this bottomed-out economy will doubtless have high unemployment and a class of the employed that are living on the breadline.

My second note on the last post concerns the statement at the end on how effectively the 1% can syphon away the wealth of the 99%. This jars with the fact that the 1% own the companies that pay the wage bill of the 99%, and that they are therefore reliant on the consumers (the 99%) recycling the money back to them by spending and/or saving. I think the answer here is two-fold. A minority of companies do successfully syphon money to themselves (e.g. Apple) but it's at the expense of their competitors (e.g. Microsoft) not their customers. But I was really meaning sums of money summed up across the whole economy, and I believe that aggregate syphoning from consumers isn't significant, and that it can actually occur towards the poorer 99%. However, it is clear that the 1% start off with disproportionate ownership of wealth, including the capital to generate and control it, and they use that control to make sure they continue to own their unfair share.

Book IV of The General Theory is entitled The inducement to invest and its first of eight chapters is called The marginal efficiency of capital. In very loose terms, the marginal efficiency of capital is to capital what interest is to money (despite the popular misconception, capital is not synonymous with money). If we give money to another party, we can expect them to pay us a sum of money over time  - the interest - for allowing them to hold and use the money. Likewise, if we invest in purchasing some capital equipment, say machines for making mobile phones in a factory, then we will expect our ownership of those machines to bring us a net income over time from the sale of produced goods. Keynes makes a rigorous definition of the marginal efficiency of capital so he can go on to compare such income with interest rates. The main conceptual difference between the two is that for the latter the owner of the money is not directly concerned with how the asset (i.e. the money) is used to generate a return.

Much of the subsequent chapters are concerned with discussing the interplay between the marginal efficiency of capital and interest rates. If interest rates are low then there is a greater incentive to invest in purchasing capital assets and expect a return from them, and vice versa for high interest rates. But, of course, it is not so simple. There are psychological factors at work. How do people perceive the future? If there is optimism that consumer spending will remain strong, then investment in capital will be more attractive. But if not, people may have a preference for liquidity and be less inclined to lock up money in more difficult to release capital equipment, even if interest rates are not very high.

Keynes discusses what sets money apart from other repositories of wealth and makes the point that interest is not actually unique to money. One example that occurred to me was carrots. A crop of carrots is perishable, unlike money, but a given number of carrots can be planted and grown - that is "invested" - so that year-on-year, assuming you didn't sell them, you would add to the total number of carrots in your possession (even though these wouldn't be the same carrots). You could quite legitimately call the extra number of carrots each year your interest. If you instead, as most farmers do, sold those carrots to receive a return in terms of money then that return would feed into your calculation of your marginal efficiency of capital (along with the initial stock of carrots, land, machinery etc).

The carrot example illustrates a couple of points, most obviously that money is preferential to carrots for holding wealth because it doesn't necessarily incur land, labour and storage costs. But it also demonstrates what Keynes calls "own-interest", i.e. you can define the interest rate of carrots in terms of carrots. This helps explain what he means in this key quote from Chapter 17 Section III:
No further increase in the rate of investment is possible when the greatest amongst the own-rates of own-interest of all available assets is equal to the greatest amongst the marginal efficiencies of all assets, measured in terms of the asset whose own-rate of own-interest is greatest.

He argues that the thing with the greatest own-rate of own-interest is money. So, in plainer English, he is saying that people will put their wealth into a form that they see as most likely to increase their wealth. The convoluted digression into interest defined in terms of other assets (such as carrots) strikes me as possibly a distracting abstraction, but it shows the rigour that Keynes wished to apply to his arguments.

To return to the key point, the halting of investment caused by a high money rate of interest can (and usually does) stop an economy proceeding to full employment. To put it more starkly: even if companies are wanting to find a way to increase profits, and even if willing and able unemployed workers are wanting employment, the economy might not be able to respond because of a blockage caused by a shortage of the very thing that is supposed to facilitate the economy: money. Keynes highlights this absurdity in his delightfully idiosyncratic style:
Unemployment develops...because people want the moon;- men cannot be employed when the object of desire (i.e. money) is something which cannot be produced and the demand for which cannot readily be choked off. There is no remedy but to persuade the public that green cheese is practically the same thing and to have a green cheese factory (i.e. a central bank) under public control.
He goes on to highlight the folly of attempting to anchor the value of money against a rare substance extracted from the Earth:
It is interesting to notice that the characteristic which has been traditionally supposed to render gold especially suitable for use as the standard of value, namely, its inelasticity of supply, turns out to be precisely the characteristic which is at the bottom of the trouble.
In other words, if you tie money to something that appears to have tangible value, then you actually make the situation worse because you lose all control over the supply of money to the economy and are then completely helpless in dealing with a money shortage.

But, putting gold standards, interest rates and capital aside, the problem is very simple: we have become too preoccupied with money itself, to the extent it distracts us from the essentials of the production it is supposed to facilitate, namely, securing food, shelter, employment and health.

Sunday, November 18, 2012

Fesenjan

Fesenjun - pestle & mortar and saffron to the left.
When I was a wee boy, probably about the age my son is now (7), I remember being confronted by a dish that my aunt had made at her home in London. "That's mud, isn't it?" I said, without meaning to be funny, but everyone laughed and they still laugh about it. The mud was an ancient dish from Iran called Fesenjan. It was by far the most delicious mud I had ever tasted.

I don't recall eating Fesenjan again until a few years ago when I spotted it on the menu of an excellent restaurant called Paradise in Glasgow. That was very different from the one my aunt had made, much more sweet and sour, so much so that I imagine that it would over-challenge too many people's palettes. Sadly it probably did, as it doesn't appear on their menu any more.

Some months back I started cooking recipes I found in a Persian cookbook my wife had bought on a trip to California (where there are many Iranians). At first I didn't spot the recipe for Fesenjan, but when I did I had a go at cooking it. The first try was good, but not quite as dramatic and intense as I had wanted, so I tried again and ended up with the recipe I described below. I've cooked it for quite a few friends now and it seems to go down very well and it really is quite different from anything they've tried before.

Fesenjan is really made distinctive by the combination of walnuts and pomegranate. It is most commonly served with chicken, but duck, beef or vegetables can all be substituted. I don't recall what my aunt put in it, but there were no large lumps of meat, so I wonder if she used minced beef or even lamb. I intend to experiment with that for the true muddy consistency.

Fesejan - serves 6
2 finely sliced medium onions
1kg of chicken, legs and thighs preferred
1 litre of pomegranate juice
300g of walnuts, ground in food processor
1 tsp saffron, ground in a pestle and mortar
1 butternut squash cut into 1 inch cubes
1 tbsp of muscavado sugar
juice of one lime or lemon
some pomegranate seeds and some unground walnuts for garnish

1. In a large deep-sided pot, fry the onion in a few tablespoons of olive oil, and add the chicken and butternut squash after a few minutes. After a few more minutes of stir frying, turn the heat down and cover it while you prepare the sauce.
2. Put the ground walnuts in a bowl with the pomegranate juice and lime or lemon juice and sugar.
3. Dissolve the saffron in a tablespoon or two of hot water (as much as it will dissolve) and pour into the bowl and mix it.
4. Pour the contents of the bowl into the pan with the onion, squash and chicken.
5. Cook on a low heat, uncovered for 2 hours. Stir every 15 minutes or so to avoid the walnuts burning. The sauce should reduce and thicken quite a bit, but if you like it a bit more runny and less intense in flavour, you can put a lid on it for some of the time.
6. Garnish with pomegranate seeds and unground walnuts and serve with rice.

Wednesday, November 14, 2012

Reading Keynes part 2

I've now completed reading book III of The General Theory by John Maynard Keynes. I previously wrote about my thoughts on books I and II. The title of book III is "The propensity to consume" and it is composed of three chapters.

Probably the most important statement made in this part is in Chapter 3, section II, paragraph 2:
 

Thus, to justify any given amount of employment there must be an amount of current investment sufficient to absorb the excess of total output over what the community chooses to consume when employment is at a given level.

Out of context the meaning may not be clear, so I'll try and explain in my own words, using a very simple model of my own devising.

Consider a simple, closed economy which comprises a number of companies and a community of people - the consumers - who will buy products from those companies. The economy is closed in the sense that it does no trade with any other companies or consumers elsewhere, i.e. there are no imports or exports. Let's also suppose there is no taxation or spending by a government.

So each one of the consumers are either employed by a company or else they are unemployed and have zero income (the government doesn't spend, so there is no welfare state). The ones who are employed receive a monthly wage from their company. Let's add up all the wages across this community and call it W. Also, each consumer will spend a certain amount each month. Let's add up all that is spent and call it C.

Each company will have an income, and if we add up the income from selling to consumers across all companies we must find that it comes to C - the total the consumers have spent. One company can sell to another company and so we can add up the business-to-business income across all companies and call it B.

The costs of each company will be the amount it pays out in wages plus the amount it spends on buying from other companies. Adding up wages across all companies must give us W. Because this is a closed economy, the total spent on companies buying products must be B.

So the total income of all companies is C+B and their total costs are W+B. The total profit across all companies is therefore C-W (the Bs cancel out).

And you may have spotted the problem with this simple economy. People will generally not spend more than they earn (and even if they do, they can not do so indefinitely). This means that the total amount spent on buying products from companies C will have to be less than the total wage bill W, or, in other words, the companies in this economy must make an overall loss of W-C. Of course, there will be winners and losers - profitable companies and loss-making companies - but overall companies are loss making in this economy.

A company that finds it is making a loss will try to drive down its costs. It can do this by reducing what it pays to other companies, and it will probably also reduce its wage bill, either by cutting wages or by firing employees. The overall result will of course be to drive down B and W, though B has no impact on the overall profitability of companies. But, as we argued above, C cannot be greater than W, so if W is decreased, C will decrease too. In fact, employees who are starting to fear for their jobs are likely to start saving and so the total amount they spend C will probably fall by even more than W and so the overall loss W-C may even increase. In any event though, W will remain greater than C.

This effect is an example of positive feedback in that the fact that W is greater than C will cause companies to act in a way that will cause W-C to increase. In this way, without any other stabilising factors, the economy will collapse: all companies will go bust and everyone will become unemployed.

And this is where the quote from Keynes above becomes relevant. To stop this happening, the companies, as a whole, need to receive monies from another source to offset the loss they collectively make, i.e. W-C. This extra source of monies Keynes calls "investment". And where does it come from? Well, in this simple economy it can only come from what the consumers don't spend on goods. Instead they either use their savings to buy shares in a company or save the money by putting it in banks that then invest or lend the money to the companies. And, of course, this excess of consumers income over spending is exactly equal to W-C, which means this economy can be made stable in the sense that companies can break even overall and so the level of employment can be sustained.

The essential point that Keynes is making here is that investments and savings in an economy are necessarily equal. I had previously only thought of investment as being necessary for companies that want to grow or establish themselves as start-ups. Although that's true, I can now see that continued investment is a necessity to sustain a stable economy because of the gap between C and W.

The other point which Keynes makes is that an economy that has reached a stable equilibrium may have done so at less than full employment. It may well be, and in fact generally is the case, that the willingness to invest and the propensity to consume are not sufficient to motivate companies to increase their production and take on more employees. The unemployed who have zero income (there is no welfare state in my model) would clearly prefer to work, but the companies will not employ them because they cannot see how this would maintain or increase their profits.  This conclusion, that an economy can be stable at less than full employment, distinguishes Keynes's theory from that of classical economics (at least of his own time, but I bet there are still many who do not get this point today). 

There are a number of other interesting implications that can be drawn from this simple model. One is to consider what happens if hoarding rather than saving occurs in the economy. For example, an old miser who distrusts banks, stuffs all excess money under a mattress. Likewise, a bank that takes deposits but refuses to lend is doing much the same thing. The result in both cases is that money enters a stagnant pool and the economy as a whole will suffer because it is deprived of investment. If money could be released from that stagnant pool, then employment could rise because companies could access a new potential source of revenue. Of course, in my simple model there is no government to spend, but in reality, government spending can achieve the same result in raising employment.

But, if there's one lesson to take away from this simple model, it is that "one person's spending is another's income", or more specifically, "consumer spending is company revenue". Companies as a whole are completely beholden to their consumers. If consumers do not part with their money to buy products, then it is their savings transformed into investments which keeps the companies in business. This raises an interesting question: how does this fit with the current situation in which the richest 1% of the population, who presumably own companies, are often supposed to be syphoning wealth from the 99%? I'm still pondering this question, but will save my attempts at an answer for a future blog post.

Tuesday, October 16, 2012

Reading Keynes part 1

It was my fortieth birthday recently and I was glad to be presented with many books. Amongst them, courtesy of my father, were The Wealth of Nations by Adam Smith and The General Theory of Employment, Interest and Money by John Maynard Keynes. I decided to start reading the latter book first. This is the first in a series of posts in which I'll try and explain what I've got out of the experience.

First off, I'm not an economist by training, but the problems with economies around the world in recent years have kindled my interest, especially when I began to appreciate that so-called "Keynesian" solutions, as described by the likes of Paul Krugman, seemed to not only stand up to skeptical, rational scrutiny but also had a very solid basis in historical precedent. Before going further, the very first thing I learned is that Keynes is pronounced canes, not keens.

Not being an economist of the early 20th century makes reading Keynes's classic book a bit of a challenge. My first stumble was when I encountered the word "disutility". I tried and failed to understand it from the context and a search of the web kept leading back to extracts from the book itself. It turned out that "utility" is a term in economics used to quantify human satisfaction and that the word "disutility" was either peculiar to Keynes himself, or else is now arcane. The term appears in the context of Keynes disputing the received economist wisdom of his time that workers settle for a real-wage (i.e. in real terms, not in terms of an amount of money) that is just sufficient to make their employed state of greater utility (or lesser disutility) than their unemployed state. If you are struggling with that last sentence, then you have a sense of what it feels like to read Keynes's book.

The point is valid though. An employee will immediately seek redress with their employer if forced to take a pay cut, i.e. the money-wage is reduced, but may not even notice if the real-wage is reduced, say, because prices of everyday items have increased. Even if noticed, the employer is unlikely to see rebargaining the wage as their problem; they too may be faced with increased business costs due to increased prices. This situation is playing out just now across Europe: in Spain and Greece people are understandably furious at having to take money-wage cuts (or else be made unemployed) to solve economic problems that were not of their creation. Meanwhile, outside the land of the Euro, in Iceland, most people implicitly accepted real-wage cuts when the Icelandic currency was devalued.

The next piece of received wisdom at which Keynes takes aim is the notion that "Supply creates its own demand". That strikes me as immediately strange, but nevertheless there are still people who believe it today. One implication of it is that there should be no involuntary unemployment, on the basis that everyone who wishes to work can do, because when they produce something, there will be a demand from someone, somewhere to buy it. This seems so obviously absurd that you may wonder why it would need refuted, either in 1936 by Keynes, or by anyone today. Of course, the absurdity of it is not often as plainly stated as this, but it is implicit in much economic wrong-thinking. In fact, as Keynes sets out, if you believe in "supply creates its own demand" or one of many equivalent variants of that statement then you are actually adopting an economic model that assumes there is no real barrier to full employment. In boom times that might not be so bad a model, but during a depression, like the current one, it's simply the wrong model to adopt. To use an analogy: if a car fails to start, no amount of tinkering with the engine will help if you've incorrectly assumed that it has sufficient fuel in the tank.

But, that said, after only having read books I and II of Keynes' classic text, I have not yet encountered anything else that you would recognise as "Keynesian", e.g. that governments should spend their way out of a recession. Instead, Keynes has merely sketched his ideas in broad brush and then painstakingly set out a series of definitions of net income, investment, saving and something called "user cost" (akin to depreciation, except that no cost is incurred if equipment is idle) so that he can go on to describe his theory in subsequent books. In other words, he found the economic descriptions of his own time too vague and imprecise to formulate his theory and so invented his own.

I am not one who is prone to hero-worship and I certainly do not worship Keynes nor accept his ideas without question. But it is clear Keynes was a very intelligent person and, more importantly, an independent-minded individual. Don't take my word for it - have read (or listen) to what philospher John Gray had to say about him.