# [Haskell-cafe] Is there a module for multivariate linear regression?

Lihn, Steve horng_twu_lihn at merck.com
Mon Nov 5 14:14:09 EST 2007

```I found an first-order implementation in hstats (linreg, see below)
while inspecting its source code.
http://www.sftank.net/code/hstats/src/Math/Statistics.hs

But, for some reason, it does not show up in the documentation (not
released yet) !?
http://hackage.haskell.org/packages/archive/hstats/0.2/doc/html/Math-Sta
tistics.html

------------------------------------------------------------------------
--

-- |Least-squares linear regression of /y/ against /x/ for a
-- |collection of (/x/, /y/) data, in the form of (/b0/, /b1/, /r/)
-- |where the regression is /y/ = /b0/ + /b1/ * /x/ with Pearson
-- |coefficient /r/

linreg :: (Floating b) => [(b, b)] -> (b, b, b)
linreg xys = let !xs = map fst xys
!ys = map snd xys
!n = fromIntegral \$ length xys
!sX = sum xs
!sY = sum ys
!sXX = sum \$ map (^ 2) xs
!sXY = sum \$ map (uncurry (*)) xys
!sYY = sum \$ map (^ 2) ys
!alpha = (sY - beta * sX) / n
!beta = (n * sXY - sX * sY) / (n * sXX - sX * sX)
!r = (n * sXY - sX * sY) / (sqrt \$ (n * sXX - sX^2) *
(n * sYY - sY ^ 2))
in (alpha, beta, r)

-----Original Message-----

Don,
I checked most of them, but did not find anything close.
Hstats seems to be the right place, but no, it does not contain such
function.
It only contains predictive functions in statistics, but there is no
best-fit type of functions. I can calculate Sharpe ratio from it, but I
can not calculate the alpha and beta between two indexes/funds.

http://haskell.org/haskellwiki/Applications_and_libraries/Mathematics
FYI -- Under 2.8, HaskellMath links to a 404 page.

> this does sound like fairly easy to package up as a new module,
though, if you're keen..

This may be the only alternative if nothing obvious comes up...

Thanks,
Steve

-----Original Message-----
>    I am looking for a Haskell module that will do multivariate linear
>    regression. Does someone know which module will do it? That is, the
>    equivalent of Perl's Statistics::Regression.pm.
>
>
[1]http://search.cpan.org/~itub/PerlMol-0.35_00.ppm/lib/Statistics/Regre
ssion.pm
>
>    Thanks,
>    Steve
>

Always check hackage.haskell.org first, but I'm not sure
we have exactly what you're looking for:

http://hackage.haskell.org/packages/archive/pkg-list.html#cat:Math

in which case the backup is :

http://haskell.org/haskellwiki/Applications_and_libraries/Mathematics

this does sound like fairly easy to package up as a new module, though,
if you're keen..

-- Don

------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates (which may be known
outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD
and in Japan, as Banyu - direct contact information for affiliates is
available at http://www.merck.com/contact/contacts.html) that may be
confidential, proprietary copyrighted and/or legally privileged. It is
intended solely for the use of the individual or entity named on this
message. If you are not the intended recipient, and have received this
message in error, please notify us immediately by reply e-mail and then
delete it from your system.

------------------------------------------------------------------------------
```

More information about the Haskell-Cafe mailing list