Archive for the 'Template Method' Category

Page 2 of 2

Template Method: Taming Chi Square

x2Easy Order

Eeeeek! A statistic! Relax, it’s just a Chi Square. The secret to understanding complexity is decomposition and modularity. Decomposition refers to breaking down a whole into its elements. Ironically, the term is fundamental to parallel and concurrent programming where a basic process in programming, like a loop, is further decomposed into more than a single loop, but decomposition also applies to standard OOP programming as well. Any complex process broken down involves the decomposition process. Once broken down, you can attend to the individual modules—modularity.

The Template Method design pattern is a great way to modularize a complex problem, like working through a Chi Square statistic. Chapter 9 in Learning PHP Design Patterns, explains the Template Method in detail and all about “The Hollywood Principle,” but here, I want to look at the Template Method as a “modulaizer”—making complex problems manageable. To get started take a look at Figure 1:

Figure 1:  2 x 2 table with row and column totals

Figure 1: 2 x 2 table with row and column totals

Imagine that the data in the table are based on a survey you did at a PHP group, like the Boston group. Suppose that you want to find out the members language preferences based on whether the member owns a Macintosh or a Windows PC. (Sorry Linux.) You’re interested in PHP and jQuery and you record the responses in a two dimensional array. You find that the PC users favor PHP and the Mac users are evenly split between PHP and jQuery. Getting the table results in a 2 x 2 matrix like the one in Figure 1, you’re interested in whether there’s any real significance in the differences or the whole thing is random. You decide to use a Chi Square statistic to find out. Before going further you can run the application and download the files using the buttons below:
PlayDownload

It Started with a Multi-Dimentional Array

This post came out of a Boston PHP group Percolate session for advanced PHP and OOP programming. Working through Larry Ullman’s PHP Advanced and Object-Oriented Programming, 3rd Ed book, the first chapter covered multi-dimensional arrays. After creating a two-dimensional array, I then put it into a class and started playing with it. You can see it in the following listing:

< ?php
class TwoDim
{
	private $members;
	public function __construct()
	{
	$this->members= array(
		array(os => 'Mac', program => 'PHP'),
		array(os => 'Mac', program => 'jQuery'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'jQuery'),
		array(os => 'Mac', program => 'jQuery'),
		array(os => 'Mac', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'jQuery'),
		array(os => 'Mac', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'Mac', program => 'jQuery'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'Mac', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'jQuery'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'Mac', program => 'jQuery'),
		array(os => 'Mac', program => 'PHP'),
		array(os => 'Mac', program => 'jQuery'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'jQuery'),
		array(os => 'Mac', program => 'jQuery'),
		array(os => 'Mac', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'jQuery'),
		array(os => 'Mac', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'Mac', program => 'jQuery'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'Mac', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'jQuery'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'Mac', program => 'jQuery'),
		array(os => 'Mac', program => 'PHP'),
		array(os => 'Mac', program => 'jQuery'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'jQuery'),
		array(os => 'Mac', program => 'jQuery'),
		array(os => 'Mac', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'jQuery'),
		array(os => 'Mac', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'Mac', program => 'jQuery'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'Mac', program => 'PHP'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'PC', program => 'jQuery'),
		array(os => 'PC', program => 'PHP'),
		array(os => 'Mac', program => 'jQuery')
		);
	}
	public function getArray()
	{
		return $this->members;
	}
}
?>

I wasn’t thinking at all about the Template Method, but when I decided it’d be interesting to do a Chi Square test, I found that the Template Method was both helpful and appropriate. Breaking down the steps in a Chi Square test for a 2 x 2 table is pretty easy:

  1. Get the observed cell values (O)
  2. Get the column and row totals
  3. Determine the expected frequencies (E)
  4. Calculate the Chi Square value by adding up (O-E)2 / E
  5. Compare the Chi Square value to values in a probability table where df=1 to determine the p value

Continue reading ‘Template Method: Taming Chi Square’

Share