Title: Speed of matrix calculation with Stata and R
アメリカでは、大学、リサーチ機関、政府の至る所で、統計・計量分析ソフトと言えばStataが最も広く使われている。デファクト・スタンダードといっていいかも知れない。日本ではあまり使われていないのが不思議なくらいだ。
In the United States, Stata is the application that is the most commonly used for statistical and econometric analyses, in academic institutions, research institutions and government agencies. It can safely be said that Stata is a de facto standard. For unknown reason, it is not widely used in Japan.
Stataの良いところの一つに、主要な機能ではないかも知れないが、行列計算が簡単にできるところがある。計量モデルを作る時には、地道に行列を計算したいこともあるかと思う。そこで、この道を専門にする人には分かりきったことかも知れないが、行列計算のスピードはどうなのかと気になり、フリーソフトのRと比べてみた。
One of the good things with Stata is that it is handy for matrix calculation, which might not be a major functionality of Stata. Yet you might want to calculate matrices by yourself when you would construct econometric models. In this light, the speed of matrix calculation arose as my concern, and I decided to compare it with that of R, a free statistical software. Experts in this area might know which is faster, though.
比較はまずMac OS X 10.4上で行った。使用したStataは、Intercooled Stata 9というバージョンである。これは通常版と比べて、扱えるデータの量に制限がつけられている。また、別途MPというバージョンがあるので分かるように、マルチプロセッサをパラレルに使った処理はできない。
The comparison was made on Mac OS X 10.4. The Stata I used is 'Intercooled Stata' version 9. This version has limited functionalities in terms of the size of data it can handle. Additionally, it can't compute using multiple processors in parallel, which is known from the fact that an MP version exists.
まず、確率密度関数を使い、正規標準分布N(0,1)に従う乱数を各要素に持つ、800x800の行列Xを作成する(800なのは、これがIntercooled Stataが扱える最大要素数であるため)。各要素は最大で小数点以下8桁の大きさを持つとする。次に、以下の計算に要する時間を計測した。
Firstly, using a probability density function, I prepared an 800 by 800 matrix whose elements were random numbers that conform to the standardized normal distribution N(0,1). (The matrix size is 800 because it is the maximum number of elements that Intercooled Stata is allow to handle.) Each element has at maximum eight digits for decimal fractions. Then, I timed the following calculations.
(1) Xの転置行列をYとしたとき、YX
YX, where Y is a transpose of X
(2) YXの逆行列
Inverse of YX
Stataでは、(1) に約30.7秒、(2) に 2.3秒を要した。(共に手動計測による)
Stata took approximately 30.7 seconds for (1) and 2.3 seconds for (2). Both are timed manually.
matrix X = matuniform(800, 800) matrix TX = X' matrix A = TX*X matrix B = inv(A) |
R 2.6.1(Stataに合わせてGUIを使用)では、(1) は約0.5秒、(2) は約0.9秒だった。
R 2.6.1 (GUI version, as is Stata) took approximately 0.5 seconds for (1) and 0.9 seconds for (2).
x <- matrix(rnorm(640000), nrow=800, ncol=800) system.time(z <- (t(x) %*% x)) system.time(solve(z)) |
R も Stata も、LAPACK および BLAS、あるいはそれを元にした計算ライブラリを使っているのだが、特に (1) において、何故これほど著しい差が出るのだろう。ライブラリを使っている箇所が違うのだろうか。(2) の逆行列の計算に関して言えば、差が2倍弱の開きであることから、マルチコアを使っているかどうかの違いだけであるかもしれない。
I wonder where these remarkable differences, especially in (1), come from, for both R and Stata use LAPACK and BLAS, or scientific libraries originating from them. Would it be a difference as to whether they use such libraries or not for specific computation models? As for the calculation of an inverse matrix with (2), the difference might have stemmed solely from whether they use multiple cores of CPU or not, because R run nearly two times faster.
補足: Windows用の Stata 10 MP をDual Core CPUで使える機会があったので、同じ計算を行った。(1)の計算に要した時間は 5.3秒で、随分Rに近くなった。Mac OS用の Intercooled Stata 9とは、なんと5倍以上の差がある。これはバージョンの違いというより、Mac OS版の出来が良くないからではないかと思われる。
PS: I had a chance to run Stata 10 MP for Windows on a dual core platform. The time required to calculate (1) was about 5.3 seconds and close to the speed of R. The performance was more than 5 times better than that of Intercooled Stata 9 for Mac OS X. From my speculation, the difference is in that the build for Mac OS is not designed well rather than in the difference in versions
2008年1月18日、Mac OS Xで行った計算を再計測し、この記事を改訂した。前回の計測に当っては、Macbook Proからバッテリーを外して使用していたため、Macbookの設計上、約半分の計算能力で稼働していた。
最近のコメント