Last active
August 29, 2015 14:20
-
-
Save Sintrastes/b7441639d90d24a013a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Linear correlation stuff for MATH 211, yay? | |
-- An error, but a bit more condescending than usual. | |
(ಠ_ಠ) x = error ("ಠ_ಠ "++x++" ಠ_ಠ") | |
ccoeff :: [Double] -> [Double] -> Double | |
ccoeff xs ys = (n*xyΣ - xΣ*yΣ)/((n*x²Σ - xΣ**2.0)*(n*y²Σ-yΣ**2.0))**0.5 | |
where xΣ = sum xs | |
yΣ = sum ys | |
xyΣ = sum $ zipWith (*) xs ys | |
x²Σ = sum $ map (^2) xs | |
y²Σ = sum $ map (^2) ys | |
n = if (length xs == length ys) | |
then fromIntegral (length xs) :: Double | |
else ಠ_ಠ "Lists not the same size" | |
β₁ = (n*xyΣ-xΣ*yΣ)/(n*x²Σ-xΣ**2) | |
β₀ = yΣ/n - β₁*(xΣ/n) | |
linreg :: [Double] -> [Double] -> (Double,Double) | |
linreg xs ys = (β₁,β₀) | |
where xΣ = sum xs | |
yΣ = sum ys | |
xyΣ = sum $ zipWith (*) xs ys | |
x²Σ = sum $ map (^2) xs | |
y²Σ = sum $ map (^2) ys | |
n = if (length xs == length ys) | |
then fromIntegral (length xs) :: Double | |
else ಠ_ಠ "Lists not the same size" | |
β₁ = (n*xyΣ-xΣ*yΣ)/(n*x²Σ-xΣ**2) | |
β₀ = yΣ/n - β₁*(xΣ/n) | |
linreg' :: [Double] -> [Double] -> (Double -> Double) | |
-- ^ Return a function for the linear regression line | |
-- of the data set. | |
linreg' xs ys = \x -> β₁*x + β₀ | |
where (β₁,β₀) = linreg xs ys | |
stdErr :: [Double] -> [Double] -> Double | |
stdErr xs ys = ((y²Σ-β₀*yΣ-β₁*xyΣ)/(n-2))**0.5 | |
where xΣ = sum xs | |
yΣ = sum ys | |
xyΣ = sum $ zipWith (*) xs ys | |
x²Σ = sum $ map (^2) xs | |
y²Σ = sum $ map (^2) ys | |
n = if (length xs == length ys) | |
then fromIntegral (length xs) :: Double | |
else ಠ_ಠ "Lists not the same size" | |
β₁ = (n*xyΣ-xΣ*yΣ)/(n*x²Σ-xΣ**2) | |
β₀ = yΣ/n - β₁*(xΣ/n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment