Thursday, July 25, 2013

Scala Traits decompilation and the rest: Scala version 2.10.2

package demo

trait Trait1 {
  def abstractMethod: Int
}
The above Scala Trait generates a Java Interface:

public interface demo.Trait1 {
  public abstract int abstractMethod();
}
Now let us find out what happens when a Scala Trait has got an implemented method along with an abstract one.
package demo

trait Trait3 {
  def abstractMethod: Int

  def implMethod: Int = 7
}
This one generates two Java entities:
public interface demo.Trait3 {
  public abstract int abstractMethod();

  public abstract int implMethod();
}

public abstract class demo.Trait3$class {
  public static int implMethod(demo.Trait3);
    Code:
       0: bipush        7
       2: ireturn      

  public static void $init$(demo.Trait3);
    Code:
       0: return       
}
So along with a sensible Java Interface, it also generated an abstract class with two static methods! Let us have a closer look at the first static method: it's name is same as the implemented method on the Trait and the default implementation logic is reflected too. But what's the use of this abstract class?

To understand how this abstract class is used let us use it, of course. :)

package demo

class SomeClass extends Trait3 {
  def abstractMethod = 1
}
And this is how it was used:
public class demo.SomeClass implements demo.Trait3 {
  public int implMethod();
    Code:
       0: aload_0      
       1: invokestatic  #17                 // Method demo/Trait3$class.implMethod:(Ldemo/Trait3;)I
       4: ireturn      

  public int abstractMethod();
    Code:
       0: iconst_1     
       1: ireturn      

  public demo.SomeClass();
    Code:
       0: aload_0      
       1: invokespecial #24                 // Method java/lang/Object."<init>":()V
       4: aload_0      
       5: invokestatic  #28                 // Method demo/Trait3$class.$init$:(Ldemo/Trait3;)V
       8: return       
}
 The SomeClass implements the generated Java Interface, demo.Trait3. makes sense.
But this class has also magically got the default implemented Scala Trait method, implMethod!
And how is this auto-generated method implemented? It simply delegates to the namesake static method in abstract class Trait3$class. No magic. Isn't it?

Wait a minute, if the generated Trait3$class has all static methods then what could have been the reason behind declaring it "abstract"? Please let me know when you find it. No, I am serious.

Friday, July 05, 2013

Scala Objects as Modules

You must have noticed the word "module" when you define an object in scala REPL.

scala> object Foo{
     | }
defined module Foo
If you are curious to know why that object is being called a module then read this:
http://www.artima.com/pins1ed/modular-programming-using-objects.html


How to load a specific version of scala repl with "sbt console"

If you want to run a specific version of scala REPL with sbt then run the following on commandline:

$   sbt "++ 2.10.2" console

It will load scala 2.10.2 interpreter/repl.


Friday, June 21, 2013

The difficulty of doing nothing

Doing nothing is not easy. I have to get rid of my fears and desires to really do nothing. Isn't it?

Saturday, May 11, 2013

How to setup Haskell Platform (haskell-platform-2012.4.0.0) on Ubuntu 13.04

As per the Haskell Platform website you need ghc 7.4.2 to use haskell-platform-2012.4.0.0.

On Ubuntu 13.04 you can't install ghc 7.4.2 using apt-get as the repository only allows you to install ghc 7.6*.

Start with installing some dependencies using apt-get:
$> sudo apt-get install libgmp3-dev freeglut3 freeglut3-dev

And download ghc 7.4.2 bundle from here.
Extract it and run:

$> cd <extracted folder of ghc 7.4.2>

$> ./configure

It will fail and the solution is discussed here.

$> sudo ln -s /usr/lib/x86_64-linux-gnu/libgmp.so.10 /usr/lib/libgmp.so.3

$> ./configure && make install

That should lead to successful installation of ghc 7.4.2.

Now Haskell Platform:
Download Haskell Platform from here.

Extract it and run:
$> cd <extracted folder of haskell-platform 2012.4.0.0>

$> ./configure

$> make

$> make install

All set. Now get back to hacking! :)

Thursday, March 07, 2013

Generative models vs Conditional models

When to choose one model strategy against the other:

  • Computational speed: Conditional or Discriminative models need to observe all data to be optimally trained. So they fit poorly as far as computational efficiency is concerned. Usually Generative models learn each data instance in isolation, so they run faster.
  • Complex business domain where Classes are inter-dependent: Since Conditional models observe all data they learn inter-dependency of Classes. Here Conditional models fit better.
  • Evolving business domain where new Classes appear: Since each model is trained in isolation, Generative models fit better. In Discriminative models the whole system needs to get retrained.
  • Missing data: Generative models consider relationship among variables equally, so in the case of missing data they degrade gracefully. Conditional models must be trained for the missing data.
  • Corrupt data: Generative models can detect corrupt data and user can accordingly take corrective measures. Conditional models perform poorly.
  • Quality of Inference: Better and quality algorithms like EM of Generative models outperform likes of Gradient descent of Conditional models.
  • Feature selection: Conditional models ignore the features which don't contribute to the end result. Generative models can't distinguish between useful and useless features.
  • Computational resources: As Conditional models are highly specialized and focus on specific features, they are better at resource utilization.
  • Dealing with Test data: Since Testing is all about evaluating conditional likelihood, it is intuitive to realize that Conditional models fit better as they would have already optimized better on Training data.
However, these two approaches can be mixed based on various needs.

Sunday, February 03, 2013

Family of density functions in R

There is a family of density functions in R. Their names follows this pattern:

d<distribution name>

For example: dnorm, dbinom etc.

Now when do we need such functions?

Whenever

  • you know your target distribution and 
  • you know its distribution parameters and 
  • you want to know the probability of occurrence of an input.
For example
  • you know that it is Normal distribution and
  • you know that its Mean is 0 and SD is 1 and
  • you want to know the probability of occurrence of -0.5555556 then
  • invoke dnorm(c(-0.5555556),mean=0,sd=1)
Nice.

Wednesday, January 30, 2013

Science a short cut.

Science provides short cuts to otherwise human activities. These human activities are in general slow processes. They give you plenty of time to recourse the strategies, correct your mistakes. The damage is limited.

Science on the other hand is all about achieving short cut alternatives to these otherwise human activities. The results and reactions are fast. You need to be prepared with another scientific tool to handle and control the reactions. Study of pros and cons of a scientific activity becomes so so critical. A myopic attitude with science in hand is a sure way to commit suicide.

Friday, January 25, 2013

Computer programming is an art?

Not again. Computer programming is not an art!

The output of a computer program may happen to be a piece of art. But the input to a computer which is a program SHOULD NOT be a piece of art! The input to a computer needs to be very discrete and should not be vague/subjective like an art. These programs may be very ugly to look at too! Computers are not humans and they don't mind if instructions to them look ugly to humans.

Moreover, computer Languages are not supposed to be art too! Programmers need to understand very accurately(not subjectively) what other programmers have written. It is not about art, it is all about clarity and unambiguity.

And I think these computer Languages should NOT be called "languages". They are not languages, they are simply abstracted instructions to the computer. Of course few of them realized that early direct instructions to the micro-processors are not going to fly in long run. So they started abstracting these low level instructions. But even after these revolutionary computer "languages" set in, they are not like the languages we humans use. And actually these "languages" should never be like regular human languages. Some computer scientist must have taken things easy and declared that these mechanical instructions are also language in a philosophical way.

We should never write poetry in computer "languages", they are not meant for that.

Thursday, January 24, 2013

Static typing and program generation

A lot of people dislike static typing for "human" reasons. They feel constrained.

I like static typing because it is mechanical. It is constrained. It is discrete. You know where I am going?

The more people work/improve on static typing the more powerful the compilers become. The more a type checker understands contraints the easier it become to generate programs from programs.

If I failed to convey through this article then it means my thought process needs static typing. :)

Monday, January 21, 2013

Journey from individual to mob

There is a pattern.

All so called "great" humans in history started their journey to "greatness" with personal and individual ideas. Those ideas were not products of groups. They were personal observations/realisations.

Then comes the urge to "spread" the ideas! This strong conviction that they are correct is so overwhelming!

Then begin the rituals of marketing. Organizations are launched, monuments are built, words of mouth are sponsored. Starry eyes join the march for personal gains or for the gain of "humanity".

Soon the organizations turn into mobs. The bright ideas which launched the all this melee take back seats.

Why do organizations become more powerful than individuals? Why haven't the bright race of humans invented a structure where individuals are valued at least as importantly as the organizations? Why do these entities which are created by humans become more important than the creators?

The desire to control others? May be.. :)




Friday, January 18, 2013

The "regular" program is just a subset of overfitted hypothesis

In our "regular" programming scene the programmer knows all combinations of input/output and the program is "deterministic". If the programmer forgets about a combination, it leads to a bug. :)

If the input/output dataset is known exhaustively and is free of noise then this learner overfits/memorizes all instances. This can be seen as "deterministic" use-cases, which I mentioned above. In other words if you think like a tester and are aware of all input/output instances then you don't need to write code explicitly.

Of course, I am not talking about the efficiency of such an outcome here. ;)

Friday, January 11, 2013

Hail the rationals! Die emotions!

"Rational" humans always tend to demean emotions. They take pride in their "superior" ability to seal emotions away when they are reasoning. And I always hear it everywhere that we should not decide in "emotional" state of mind.

So I started observing myself, my "rational" and "emotional" myself. The "observation" is still going on. But I have already discovered something very disturbing on my way:

Each and every thought and action of mine is rooted/directed from an emotion. The so called "rational" actions and thoughts all point back to some emotion! When I am angry and hurl abuses at others I am emotional in the classical way. But while I am reasoning out my steps after getting angry at somebody (like a classical "rational" person),  I discover that I am being driven by the "desire of peace and desire of saving myself from harm".

I always find myself giving "reasons" to my deep seated emotions. Damn the hypocrite human mind! ;)


Opinions and Judgement

In machine learning terminology,
opinions = {hypotheses}.
judgement = Hypothesis which overfits the given dataset.

Before you accept the above hypothesis please grind it in your personal Bayes machine.

;)

Thursday, January 10, 2013

My importance

The other day I thought I was so important and must be taken seriously! But today I came to know about some homeless who died unattended.