Thursday, March 29, 2012

Estimating Spanish budget deficit

This is one of my political/economical post (nothing related with computers) I write from time to time.

Past February, Spain published its estimation of whole year budget deficit.

Many well respected analysts suggested or explicitly wrote that maybe new Government was increasing 2011 deficit so that it could blame previous Government, and also in order to make it bigger so that it was easier to make it smaller. One for all, Xavier Xala i Martin:

Los números presentados hoy los ha construido el gobierno del Partido Popular y evalúan el año 2011, último año de gobierno del Partido Socialista. Bajo este punto de vista, hay que resaltar que al PP le interesa magnificar el déficit de 2011 (es decir, le interesa poder decir “hemos encontrado un déficit muy superior al esperado”) por varias razones: Primero, para “demostrar” la incompetencia del PSOE. Para los políticos de todos los colores, nunca está de más echar por tierra la reputación de tu enemigo político. Segundo, porque en la medida que puedan mover contablemente ingresos del 2011 al 2012 y puedan mover gastos en dirección contraria, el mayor déficit asignado al PSOE en 2011 se traduce en menor déficit para el PP de 2012. 
Even EU Commission was expected to open an investigation to clear if Spain had overestimated deficit for political purposes.

I couldn't believe it. If there had been any kind of manipulation in Budget deficit Spanish figures it has been downwards, not upwards.

I expected somebody could examine the data in a similar way, as I had been doing, and tell everybody 8.5% budget deficit in Spain for 2011 shouldn't have been a surprise. When I started to read this post of Edward Hugh, I thought "he finally got it", until I got to the part: "the deficit itself will not form the primary subject matter of this post."

OK. So maybe I've come a to a methodology to study monthly/periodic data so that we can anticipate its evolution before end of year. [laugh]

Usually for periodic data you get it compared with similar period of the previous year. For example, for a company you get quarterly data compared with same quarter previous year.

Sometimes you get the data for all the previous periods of the same year. For example, you get data for the 3Q2011, and accumulated with 1Q2011 and 2Q2011, compared with the first three quarters of 2010.

Both two methods (single period, natural accumulated) do not help estimating data series. I'm told econometricians try to remove variability using different techniques.

I use a simple method.

If you want to know some value at the end of the present year, but you don't have data of  the whole year, you should take data equivalent to a twelve month period.

For example: if you have accumulated data up to third quarter, and you want to estimate the data you'll have at the end of the fourth quarter, you should take the amount accumulated up to third quarter of this year, and add the amount of the the fourth quarter of last year.

Yes I know. It's not a great estimate, but for budget data shows a trend and it is very easy. On January 31st, 2012 we had available third quarter Spanish deficit figures. It was, in European terms (PDE,) 56719 million euros. So if we wanted to estimate fourth quarter 2011, we should form a twelve month period. We take fourth quarter 2010: 37042 million euros. Let's add up:56719+37042=93761 million euro.

Using a twelve month period for 2Q2011, you can get a total of 97956 million euro (in European terms).

So, with 2Q2011 data, estimation was 98 billion euro, with 3Q2011, estimation 94 billion euro. Is that a perfect estimate?. Of course not. But it shows a trend. And with that trend, it was obvious we were decreasing our deficit, but it was obvious we weren't decreasing fast enough.

February, 28th. Official figure: 91344(*). And everybody said the government was forging the data upwards.

Look the following data, and tell me somebody forged it (December 31st, 2011 data is official figure).



(*)The official figure is an estimation, and is not homogeneous with the quarterly data. The fourth quarter is still to be closed in accounting terms.

Friday, March 23, 2012

The synchronized silver bullet

I lead a team that is transitioning a 9-year-old internal framework from a somewhat Struts based to Spring IoC and Spring MVC based. However in the meantime we keep hunting old bugs, which needs to dive in the code. In those occasions, from time to time, I find non-thread-safe code. When talking with the team, one of the usual solutions (at least at the beginning), was something like "Let's put synchronized to the method". I explained why I thought it was a bad decision. But after some of these episodes, I used to think "how is it that people smart and competent in many aspects of Java, people from whom I learn lots day in day out, just don't get it".

Today I was reading a book. And I've found this :


You declare this method as synchronized to make it thread-safe.
package com.apress.springenterpriserecipes.sequence;
public class SequenceGenerator {
private String prefix;
private String suffix;
private int initial;
private int counter;
public SequenceGenerator() {}
public SequenceGenerator(String prefix, String suffix, int initial) {
this.prefix = prefix;
this.suffix = suffix;
this.initial = initial;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public void setInitial(int initial) {
this.initial = initial;
}
public synchronized String getSequence() {
StringBuffer buffer = new StringBuffer();
buffer.append(prefix);
buffer.append(initial + counter++);
buffer.append(suffix);
return buffer.toString();
}
}


(mine is a newer version, but you get the point).

Let me put it clear: Synchronized IS NOT A SILVER BULLET. Synchronized makes sure that code is executed only by one thread. But it doesn't make sure that fields(member-variables) used in that code are not modified in between.

In particular, in this case, you could get a prefix, then other thread modifies the initial, a different thread modifies the suffix, and you get a real mess in your synchronized method.

Many people could tell me "You're right, but the setters are not supposed to be invoked after the creation of the object". Then I have to tell two things:

  1. Delete the setters and the default constructor (Spring allows it).
  2. Don't synchronize a whole method if you only need to be thread-safe the counter++. There is plenty of methods for doing it.

And how would I do it?. Uhm. That's a whole post itselt, and it would depend on the class behavior.

By the way. A great book in the topic: Java Concurrency in Practice.