Sunday, 17 January 2010

Karl Pearson for Python Programmers- His Contribution and His Motto

Standard Deviation is an Application of Square Roots

STANDARD deviation in PYTHON is the same as standard deviation in STATISTICS, it is the SQUARE ROOT OF THE VARIANCE, or average squared deviations from the mean (intuitively, variability about the mean).

Standard Deviation is in the Same Units As the Data

What is Single, Most Cool Thing about STANDARD deviation?

Standard deviation is always in the SAME UNITS AS THE DATA, so if data is in DOLLARS, standard deviation is ALSO in DOLLARS, if data is in PERCENTAGE POINTS, my STANDARD deviation is ALSO in PERCENTAGE POINTS.

Standard Deviation is the Modern Name for Mean Error

Mean Error was the OLD SCHOOL way of saying STANDARD deviation

Gauss used the term "mean error" for standard deviation connoting the notion of variability about the mean and is probably more explicative than Karl Pearson's term, introduced in 1894, of standard deviation.

Just to Reiterate

Karl Pearson DID NOT INVENT STANDARD deviation. He merely invented the APPELLATION of STANDARD DEVIATION.

Respect to Pearson - Establishment of the Discipline

Karl PEARSON established the DISCIPLINE of mathematical statistics, and the numerous graduates of statistics from university programs are DISCIPLES of his incredible legacy.

Quote from KP: "Not one subject in the universe is unworthy of study".

Sunday, 10 January 2010

AJAX in Python? The Answer is Pyjamas.

First Concepts

First, INLINE FRAMES. An INLINE frame is "used to EMBED a document within the current HTML document" (quote: w3schools). The syntax is: "OPEN TAG iframe S R C equals U R L CLOSE TAG". The syntax is SIMPLE. That's inline frames 101.

Secret: It's all done with inline frames

Big assumption, but let's you have written your FIRST PYJAMAS PROGRAM. You have INVOKED the pyjs compiler using BUILD-DOT-ESS-AITCH (./build.sh). A new web page is BORN. How does it work? Simple - your fancy html page simply calls bootstrap, which has a function that calls injectWebModeFrame. This loads up a nocache.html file (a generic cache file) which then in turn location.replaces itself (Harry Houdini style) with the browser-specific cache file (named using navigator.useragent). To understand how your engine is working, you need to read the source of the browser-specific cache file.

Attacking the Mozilla
Open up the mozilla (firefox) file and you will see a sea of JS variables prefixed with $, such as var $wnd = parent. This is the style in which JS is now programmed. Have Venkman on hand to debug anything vaguely Gecko-related.

Aggrandising Your Intelligence
Read this book written by Luke.

Saturday, 24 October 2009

LOG-R using numpy (and why it's worth a code jam)

What

"In statistics, logistic regression (sometimes called the logistic model or logit model) is used for prediction of the probability of occurrence of an event by fitting data to a logistic curve". This is a pretty useless definition (from wikipedia) of a pretty useful concept. Logistic models are used extensively to model growth. Of what, you may well ask. Answer: anything. Business, market share, fish in a pond, if you have a growth process, and ESPECIALLY if the rates of growth are not constant through time (or space for that matter) logistic regression can potentially be applied to model the process.

How

Jeffrey Whitaker has written a module for logistic regression (back in 2006) that uses numpy. You might think the implementation is quite complex but at its heart is just the ubiquitous Newton-Raphson method used to find square roots.

Why

Why produce logistic regressions using this convoluted programming method. Are there not softwares that make this easier? The answer is yes and no. One can analyse data in OpenOffice and fit regression lines. However, some data sets that have "logistic shapes" such as learning curves cannot be modelled correctly in OpenOffice. The regressions supported include linear, logarithmic (function of lnx), exponential (function of e^x) and power regression (y=b*x^a) which will not model logistic data accurately.

Sunday, 27 September 2009

Lamenting the Deprecation of a Brilliantly, Simple Feature: String Exceptions in Python

Python used to allow string exceptions (raise "Exception thrown!") but now these are deprecated. You have to raise real exceptions. BaseException is used as base for built-in exceptions.

Friday, 4 September 2009

Solved by Bell Labs

What is R and why do I need it

R is a statistical computing language developed at Bell Laboratories. It can do classical hypothesis testing, time-series analysis and regression analysis.

It is based on the S language developed by ACM-award-winning-genius John Chambers (his initials are aptly JMC) and team, for which he received an honorary PhD in mathematics from University of Waterloo in Ontario. John Chambers has also been President of the International Association for Statistical Computing (IASC).

A very good elementary tutorial on R can be found on r-tutor.com. Also good is R by example. Also see the blog by Tim Moertel on R.

R can also do boxplots and other nice graphs.

Python and R Integration

Since R has its intellectual history rooted in Bell Labs, it seems most apt that solutions for Python integration should also emanate from Bell Labs. In this spirit, take a look at Duncan Temple Lang's website. There is also the rpy project on sourceforge.

Saturday, 11 July 2009

Background to Python functools module

To know the intellectual history of functools development read this (PEP309).
>>> import functools
>>> dir(functools)
['WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', '__builtins__', '__doc__', '__file__', '__name__', 'partial', 'update_wrapper', 'wraps']

Python: Old Style and New Style Classes and an Ode to Moses Schonfinkel

If I define a class C and function called static_method() and do C.static_method() the result is TypeError. static_method has become a special kind of method object, called an unbound-method object, not a plain function. When you call a method on an object, the first argument is self, and here self returns null. How does one avoid this problem?

The key difference is to understand the difference between function objects and the more general notion of "callables". "Mutation to unbound method objects" only happens to function objects not other "Callables". If we don't want mutation to occur, we must use a Callable that is not a function object, as follows:

class Callable:
def __init__(self, anycallable):
self.__call__ = anycallable


We create an instance of Callable which delegates calls to another Callable it holds.Class methods can then be implemented in the enclosing class as instances of Callable.

class ClassWithStaticMethod:
def staticMethod(name):
print "Hi there",name
staticMethod = Callable(Method)


Of course, an easier solution is to use the @staticmethod decorator to your function.

Lots of good stuff has been written by this man, Alex Martelli from Google, formerly of Open End AB, a Python-oriented software house in Gothenburg, Sweden. For example, check out this recipe on high-performance currying in Python. Haskell Curry (whose name the verb currying is derived) was a writer and teacher of mathematical logic and creator of the lambda calculus of combinatory logic. His intellectual predecessor was Moses Schonfinkel, a Russian logician, and the German mathematican David Hilbert.

Back to old style and new style classes in Python. For old style classes, type(instance variable x) returns "instance" as the type, whereas type(x) for new style classes returns __class__ attribute. To quote the Python documentation, "new-style classes were introduced in Python 2.2 to unify classes and types". Benefits of this approach include the ability to subclass most built-in types. Essentially, new style classes are old style classes with RTTI support.

Taken from the docs: "For compatibility reasons, classes are still old-style by default. New-style classes are created by specifying another new-style class (i.e. a type) as a parent class, or the “top-level type” object if no other parent is needed".

To understand more deeply what Guido alludes to when talking of type/class unification read his explanatory essay.