Shallow Thoughts : tags : math
Akkana's Musings on Open Source Computing and Technology, Science, and Nature.
Tue, 03 Apr 2012
How do you show equations on a web page? Every now and then, I write
an article that involves math, and I wrestle with that problem.
The obvious (but wrong) approach: MathML
It was nearly fifteen years ago that MathML was recommended as a
standard for embedding equations inside an HTML page. I remember being
excited about it back then. There were a few problems -- like the
availability of fonts including symbols for integrals, summations
and so forth -- but they seemed minor. That was 1998.
Now, in 2012, I found myself wanting to write an article involving an
integral, so I looked into the state of MathML. I found that even now,
all these years later, it wasn't widely supported.
In Firefox I could show some simple equations, like
and
But when I tried them in Chromium, I learned that webkit-based
browsers don't support MathML. At all. The exception is Safari:
apparently Apple has added some MathML support into their browser
but hasn't contributed that code back to webkit (yet?)
Besides that, MathML is ridiculously hard to use. Here's the code for
that little integral:
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<semantics>
<mrow>
<msubsup>
<mo>∫</mo>
<mn>x = 0</mn>
<mi>∞</mi>
</msubsup>
<mfrac>
<mrow>
<mo>ⅆ</mo>
<mi>x</mi>
</mrow>
<mi>x</mi>
</mfrac>
</mrow>
</semantics>
</mrow>
</math>
Ugh! You can't even specify infinity without using an HTML numeric
entity. And the code for the quadratic equation is even worse (use View
Source if you want to see it).
Good ol' tables
Several years ago, I wrote about the
Twelve
Days of Christmas and how to calculate the total number of gifts
represented in the song.
I needed summations, and I was rather proud of working out a way to
use HTML tables to display all the sums and line up everything correctly.
It wasn't exactly publication-quality graphics, but it was readable.
More recently, I worked out a way to do exponentials that way,
and found a hint about
how to do integrals:
| now | | |
| ∫
| P (t) |
dt |
P0 = | ———— |
| 1 + t |
| 0 | | |
Looks a little better than the tiny MathML version. But the code isn't
any easier to read:
<table border="0" cellpadding="0" cellspacing="0">
<tr><td><td align="center"><small><i>now</i></small></td><td></td><td></td></tr>
<tr>
<td>
<td rowspan="3" valign="middle"><font size="6" style="font-size:3em" class="bigsym">∫</font>
<td align="center"><i>P</i> (<i>t</i>)</td>
<td rowspan="3" valign="middle"> <i>dt</i></td></tr>
<tr><td>P<sub>0</sub> =<td align="center">————</td></tr>
<tr><td><td align="center">1 + <i>t</i></td></tr>
<tr><td><td valign="top"><small><i>0</i></small></td><td></td><td></td></tr>
</table>
The solution: MathJax
And then I discovered MathJAX.
It was added recently to the Udacity
forums, and I think it's also what MITx
is using for their courses.
MathJax is fantastic. It's an open-source library that lets you
specify equations in readable ways -- you can use MathML, but you
can also use LaTEX or even ASCII math like `x = (-b +- sqrt(b^2-4ac))/(2a) .`
It uses Javascript: you put your equations in the text of the page
with delimiters like $$ around them (you can control the delimiters),
then run a function that scans the page content and replaces any
equations it sees with pretty graphics. (Viewers using NoScript
or similar extensions will need to allow mathjax.org to see the
equations, unless you make a local copy of the mathjax.org libraries,
which you probably should anyway if you're using a lot of equations.)
For displaying those graphics,
MathJax might use MathML, HTML and CSS, or whatever, depending on the
user's browser ... but you don't have to worry about that.
(Alas,
even
in Firefox, MathML rendering isn't up to par so MathJax doesn't
use it by default, though you can
specify it as
an option if you know your equations render well.)
Here's that integral again, using LaTeX format:
$$ P_0 =\int_0^\infty \frac {P(t) dt}{1 + t} $$
and
$$ x = {-b \pm \sqrt{b^2-4ac} \over 2a} $$
It's beautiful! And although I don't know LaTex at all -- been wanting an
excuse to learn it -- I put together that integral with five minutes
of web searching. (The quadratic code came from a MathJax demo page.)
Here's what the code looks like:
$$ P_0 =\int_0^\infty \frac {P(t) dt}{1 + t} $$
$$ x = {-b \pm \sqrt{b^2-4ac} \over 2a} $$
MathJax is even smart enough to notice the code there is in a
<pre> tag, so I didn't have to find a way to escape it.
I'm sold! The MathJax team has really put together a nice package, and
I think we'll be seeing it on a lot more websites.
If you want to try it, start here:
Getting Started
with MathJAX.
Tags: math, science, html, web, mathml
[
16:45 Apr 03, 2012
More science |
permalink to this entry |
]
Tue, 13 Mar 2012
The MITx 6.002 "Circuits
and Electronics" class started a week ago Monday. Exciting -- I'm hoping
I'll be able to learn all those electronics concepts that baffle me
while I'm trying to design simple circuits. (Assuming I make it --
I'm struggling and it's only the first week.)
One of the early exercises required integrating a trig function.
No problem -- I used
Wikipedia's
tables of integrals. But subsequent discussion of that problem
in the forums reminded me that when you're after a numeric solution,
we do have computers to do that sort of thing for us.
In particular, someone linked to
Wolfram
Alpha: integral (120sqrt(2)cos(2pi60t))^2/110 from 0 to 1/60
A nifty tool that I should remember to use more often!
Not only does it give you the numeric answer, but it also gives you
a nice symbolic display (so you can make sure you typed in what
you thought you were typing in), and a graph.
There was one hitch, though. In this particular problem, there was some
debate over the integration limits -- should it be 0 to 1/60 or 0 to 1?
If you try the same thing with 0 to 1, you still get the numeric answer,
but you get "Computation timed out", with a link labeled "Try again with
more time" that leads to an exhortation to subscribe to Wolfram Alpha Pro.
Of course, that made me antsy and made me wonder ... aren't there local
solutions? What if I want to calculate an integral when I'm away from
a fast network? Is there some way to do this using Python, Octave or R?
And of course there is. I haven't found an easy way to get the pretty
graphics, but you can get the numerical results fairly easily.
In Octave, it's a bit roundabout: you have to
define
a function, then call quad. The easiest way is to use inline:
octave:2> f = inline("(120 * sqrt(2) * cos(120 * pi * x))^2 / 110");
octave:3> quad(f, 0, 1/60)
ans = 2.1818
though you can also define a function this way:
octave:4> function y = ff(t)
> y = (120 * sqrt(2) * cos(120 * pi * tx))^2 / 110;
> endfunction
octave:5> quad(f, 0, 1/60)
ans = 2.1818
Here's how to do the same thing in Python using
SciPy:
import scipy
import math
scipy.integrate.quad(lambda t: (120 * math.sqrt(2) * math.cos(120 * math.pi * t))**2 / 110., 0, 1./60)[0] * 60
Of course, you don't need to use lambda -- you can also define a
function and pass it to scipy.integrate.quad.
None of this gets the nice graphics of Wolfram Alpha, though of
course either Python or Octave can be programmed to generate them.
I saw a presentation at PyCon about a package called
Sage that can probably
do nice graphics. But it's about a 350Mb download, so trying it
wasn't an option during the conference, and now the site is down
due to an electrical problem. So for now, Wolfram Alpha
wins the graphics war.
Tags: math
[
20:44 Mar 13, 2012
More science |
permalink to this entry |
]
Sat, 31 Jul 2010
The
"Roadshow" column
in yesterday's Merc had some pretty ... odd ...
statistics involving marijuana and driving.
It quotes "an NHTSA report" as saying:
contrary to popular belief, marijuana has been found to play a
significant role in car accidents across the United States, with as
much as 33 percent of drivers arrested at the scene of the accident
being positive for marijuana and another 12 percent testing positive
for marijuana and cocaine. Every year, 28 percent of drivers in the
U.S. will attempt to drive within two hours after ingesting alcohol or
illicit drugs. Marijuana is the drug used most often — 70 percent — by
drivers who drove after drug use and is a major factor why crashes are
the leading cause of death for American young people.
Whoa. Let's play that back again:
45 percent of all drivers arrested at accident scenes (33 plus
another 12) test positive for marijuana? Nearly half?
Mr. Roadshow, you don't really believe that number, do you?
I didn't. So I did some searching, looking for the NTHSA source.
When I searched for large portions of the quoted phrase, I didn't
find anything from the NHTSA. The Roadshow quote appears to come
from an article on friendsdrivesober.org (I'm sure that's an unbiased
source). Here's their
MS Word file
or Google's
cached HTML version).
The same article is also available as a PDF at
prevnet.org
and there are lots of other pages making reference to it.
The friendsdrivesober.org article cites
"Brookoff, Cook & Mann, 1994; Sonderstrom, Dischinger, Kerns & Trillis, 1995."
for the 33% number.
There's no citation offered for the "28% will attempt to drive...".
They credit "NHTSA, 2000" for "Marijuana is the drug used most often
... by drivers who drove after drug use", but that one's not important
because it says nothing about prevalence in accidents, merely that
it's used more often than other drugs (no surprise there).
The NHTSA weighs in
Googling on a more general set of terms,
I found my way to a October 2000 NHTSA report,
Field Test of On-Site
Drug Detection Devices.
It's a roundup of many different studies, with drug use numbers all
over the map, though none larger than the 33% figure and certainly
nothing near 45%.
That 33% figure is near the bottom:
Brookoff et al. (1994) used on-site testing devices in a study that
found a 58% prevalence rate for drugs in subjects arrested for
reckless driving (who were not found to be impaired by alcohol). The
Brookoff team found that 33% of their sample tested positive for
marijuana, 13% for cocaine, or 12% for both. (Because of sampling
flaws in the study, these drug test rates should not be interpreted as
drug prevalence rates for reckless drivers.) Interestingly, the
on-site device (Microline) used by Brookoff and his colleagues
generated a significant false positive rate for marijuana when
compared to GC/MS results.
The horse's mouth
So what about the original study?
I wasn't able to find Dischinger, Kerns & Trillis, but
here's Brookoff et al. at the New England Journal of Medicine:
Testing
Reckless Drivers for Cocaine and Marijuana (cookies required).
A couple of important notes on the study: the figures represent
percentage of drivers arrested for "reckless driving that would
constitute probable cause to suspect intoxication by drugs", who
were not considered to be under the influence of alcohol, and
who were suspected of being under the influence of marijuana or
cocaine ("all patrol officers were told that they could summon [the
testing van] if they stopped a person suspected of driving recklessly
under the influence of cocaine or marijuana").
Morover, not all drivers consented to be tested, and the percentages
are only for those who were tested.
Seems like a perfectly valid study, as far as it goes (though there's been some
mild
criticism of the test they used).
It's mostly interesting as a study of how marijuana and cocaine use
correlate with visible intoxication and sobriety test results.
It's not a study of the prevalence of drugs on the road:
the NHTSA report is right about that. The numbers it reports are
useless in that context.
So the jump from that study to what friendsdrivesober.org
and Roadshow implied -- that 45% of people involved in car accidents
test positive for marijuana -- is quite a leap, and attributing
that leap to the NHTSA seems especially odd since they explicitly say
the study shouldn't be used for those purposes.
What really happened here?
So what happened here? Brookoff, Cook, Williams and Mann publish a
study on behavior of reckless drivers under the influence of drugs.
NHTSA makes a brief and dismissive reference to it in a
long survey paper.
Then friendsdrivesober.org writes an article that references the study
but entirely misinterprets the numbers. This study gets picked up and
referenced by other sites, out of context.
Then somehow the paragraph from friendsdrivesober.org shows up in
Roadshow, attributed to the NHTSA. How did that happen?
If you look at the friendsdrivesober.org article, the paragraph
cites Brookoff in its first sentence, then goes on to other unrelated
claims, citing an NHTSA study at the end of the paragraph. I suppose
it's possible (though hard to understand) that one could miss the
first reference, and take the NHTSA reference at the end of the
paragraph as the reference for the whole paragraph.
That's the best guess I can come up with.
Just another example of
the
game of telephone.
Nobody with any sense thinks it's a good idea to drive under the
influence of marijuana or other intoxicants. But bogus statistics
don't help make your point. They just cast doubt on everything else
you say.
Tags: math, statistics, headlines, science
[
13:33 Jul 31, 2010
More headlines |
permalink to this entry |
]
Tue, 23 Mar 2010
Overheard at a restaurant tonight:
Mother:
So, what did you learn in school today?
Son:
(Excited) We learned to do one takeaway one!
Mother:
Really? What is one takeaway one?
Son:
(with obvious pride) Zero!
Mother:
Are you sure?
Son:
(slightly flustered) Uh, yeah ... Zero ...?
Mother:
One takeaway one is ten. They're teaching you wrong, aren't they?
Tags: math, education
[
19:13 Mar 23, 2010
More education |
permalink to this entry |
]