# Quiz statistics calculations in practice

Here I'm going to document what the code actually does. Documenting it here principally because I can't use TEX notation in php comments, well I guess I can but only real TEX geeks will be able to read it :-)

## Notation used in the calculations

If you want to see things nicely formatted, you will have to copy them to a Moodle with a working TeX filter, or copy and paste the following magic into your browser's Address bar and hit enter (or get the [http://www.mathtran.org/wiki/index.php/MathTran_bookmarklet MathTran bookmarklet):

javascript:(function(){function searchWithinNode(node,re){var pos,imgnnode,middlebit,endbitskip=0;if(node.nodeType==3){pos=node.data.search(re);if(pos>=0){middlebit=node.splitText(pos);endbit=middlebit.splitText(RegExp.lastMatch.length);imgnode=document.createElement(%22img%22);imgnode.src=%22http://www.mathtran.org/cgi-bin/mathtran?tex=%22 + encodeURI(middlebit.data).replace(%22+%22,%22%252B%22);middlebit.parentNode.replaceChild(imgnode,middlebit);}}else if(node.nodeType==1&& node.childNodes){for (var child=0; child < node.childNodes.length; ++child){searchWithinNode(node.childNodes[child], re);}}}searchWithinNode(document.body, /\$\$(.*?)\$\$/);})();


## Calculating MEAN of grades for all attempts by students

$$\displaystyle = \bar{T} = \frac{1}{S} \sum_{s \in S} T_s$$

We calculate the MEAN of first and all attempts by :

• fetching two totals and counts of all grades for attempts from the db the total grades for first attempts and the rest of the attempts.
• then to get the mean of first attempts :
• we divide the total grade for all first attempts.
• for the mean for all attempts :
• we add the total of all first attempts to the total for the rest of the attempts (not the first attempt) and divide by the total count of first attempts plus the total count of the rest of the attempts.

Depending on whether we are calculating the rest of the statistics for all attempts or for just the first attempts we select the appropriate mean to use in the rest of the calculations and save a empty string or a string of sql that selects first attempts only.

## Calculating Standard Deviation, Skewness and Kurtosis of grades for all attempts by students

In order not to have to load potentially large datasets into memory we get the DB to do the bulk of the work doing these calculations.

We can get sql to do the following calculations :

power2 = $$\sum_{s \in S} (T_s - \bar{T})^2$$

power3 = $$\sum_{s \in S} (T_s - \bar{T})^3$$

power4 = $$\sum_{s \in S} (T_s - \bar{T})^4$$

### Standard Deviation

Test standard deviation $$\displaystyle = SD = \sqrt{V(t)} = \sqrt{\frac{1}{S - 1} \sum_{s \in S} (T_s - \bar{T})^2}$$

$$= \sqrt{\frac{power2}{S-1}$$

### Skewness and Kurtosis

So then :

$$\displaystyle m_2 = \frac{1}{S} \sum_{s \in S} (T_s - \bar{T})^2 = \frac{power2}{S}$$

$$\displaystyle m_3 = \frac{1}{S} \sum_{s \in S} (T_s - \bar{T})^3 = \frac{power3}{S}$$

$$\displaystyle m_4 = \frac{1}{S} \sum_{s \in S} (T_s - \bar{T})^4 = \frac{power4}{S}$$

Then Skewness $$\displaystyle = \frac{k_3}{k_2^{2/3}}$$

$$\displaystyle k_2 = \frac{S}{S - 1} m_2 = \frac{S m_2}{S - 1}$$

$$\displaystyle k_3 = \frac{S^2}{(S - 1)(S - 2)} m_3 = \frac{S^2 m_3}{(S - 1)(S - 2)}$$

$$\displaystyle k_4 = \frac{S^3}{(S - 1)(S - 2)(S - 3)} \left((S + 1)m_4 - 3(S - 1)m_2^2\right)$$

$$= \frac{S^3 ((S + 1)m_4 - 3(S - 1)m_2^2)}{(S - 1)(S - 2)(S - 3)}$$