math is good for something after all
By anders pearson 11 Aug 2005
when my coworker, Dan, woke up this morning, i’m pretty sure that “learning linear algebra” wasn’t on his todo list for the day. linear algebra certainly wasn’t on my mind either when he came to me for help on a problem.
Dan has to do basic faculty support and training for the university’s course management system (which we use, but don’t develop ourselves). when something’s broken, he usually has to be the one to file the bug report with the developers. today he was working on trying to understand a bug in the gradebook so he could give them a useful report of exactly what was going wrong.
the trouble seemed to be that the grades it was calculating weren’t quite right. for the class in question, there were only three grades per student that were supposed to be weighted 30%, 30%, and 40% respectively. Dan had imported the grades into a spreadsheet and created a formula to generate the correct grades and display them next to the incorrect ones that the gradebook was coming up with. we spent a couple minutes staring at it and noticed that there seemed to be a rough correlation between the first column and the average. when the grade in the first column was higher than the average, the gradebook’s result was also higher and vice versa. that gave us the suspicion that the gradebook was weighting the columns incorrectly and, in particular, weighting the first column too heavily.
Dan started plugging in different weights into his formula trying to guess the gradebook’s weights and verify that that was indeed what was going wrong. i watched him do that for a few minutes and realized that it could take him all day to stumble on the right set of weights. if we were right about the weights being off, then it’s just a system of linear equations with three unknowns (the three weights) which should be fairly straightforward to solve with Gaussian Elimination. So the spreadsheet looked like this:
|A |B | C|Gradebook|Correct|
|-----|----|--|--------:|------:|
|78.33|85.3|95| 85.07 | 87.1|
|68.33|80.3|89| 77.45 | 80.1|
|70.00|88.7|86| 79.24 | 82.0|
well, there were a bunch more rows, but three is enough to see what’s going on and actually just enough to solve the system. anyway, that just corresponds to these three equations:
(Wa * 78.33) + (Wb * 85.3) + (Wc * 95) = 85.07
(Wa * 68.33) + (Wb * 80.3) + (Wc * 89) = 77.45
(Wa * 70.00) + (Wb * 88.7) + (Wc * 86) = 79.24
where we need to solve for Wa, Wb, and Wc. in matrix form this is just:
|78.33 85.3 95| |Wa| |85.07|
|68.33 80.3 89| X |Wb| = |77.45|
|70.00 88.7 86| |Wc| |79.24|
Gaussian elimination is just a somewhat mechanical algorithm for taking a system like that and coming out with values for Wa, Wb, and Wc.
i started walking Dan through it on paper but we started getting some bogus looking numbers and i remembered why it was such a pain. the process is pretty mechanical, but it involves a lot of arithmetic and generally just has a lot of places where you can make a mistake and mess the whole thing up. if we’d had a copy of Matlab or even my trusty old TI-85, it would have been no problem.
luckily though, python has some nice linear algebra packages so i went and punched in this code:
from Numeric import *
from LinearAlgebra import solve_linear_equations
a = array([[ 78.33, 85.3 , 95. ],
[ 68.33, 80.3 , 89. ],
[ 70. , 88.7 , 86. ]])
b = array([ 85.07, 77.45, 79.24])
print solve_linear_equations(a,b)
and got the result [0.46242474, 0.23079146, 0.30696587]
, which we plugged back into the spreadsheet and saw that it indeed matched what the gradebook was calculating for all the rows. now, why the gradebook code was using those weights instead of [.30, .30, .40]
we have no idea; but that’s the developers’ problem not ours.
i guess my point is that math shows up in unexpected places so it pays to 1) know enough to recognize it and know how to approach solving a math problem and 2) it helps to know how to get a computer to solve the problem for you. so pay attention in calculus class, kids.
Tags: programming math python linear algebra gaussian elimination