いろんな言語で宿題スレat TECH
いろんな言語で宿題スレ - 暇つぶし2ch576:デフォルトの名無しさん
09/07/28 01:03:39
>>570
-- Haskell

leastSquares :: (Fractional a) => [a] -> [a] -> (a,a)
leastSquares xs ys = ((n*sxy-sx*sy)/(n*sxx-sx*sx), (sxx*sy-sxy*sx)/(n*sxx-sx*sx))
where
n = fromIntegral $ length xs
sx = sum xs
sy = sum ys
sxy = sum $ zipWith (*) xs ys
sxx = sum $ map (^2) xs

pearson :: (Floating a) => [a] -> [a] -> a
pearson xs ys = a / (sqrt b) * (sqrt c)
where
mx = (sum xs) / (fromIntegral (length xs))
my = (sum ys) / (fromIntegral (length ys))
a = sum $ zipWith (\x y -> (x-mx)*(y-my)) xs ys
b = sum $ map (\x -> (x-mx)^2) xs
c = sum $ map (\y -> (y-my)^2) ys

main = do
let (a,b) = leastSquares is vs
putStrLn $ "a0=" ++ show b ++ ", a1=" ++ show a
print $ pearson is vs
where
is = [0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
vs = [11.6, 12.3, 12.8, 13.4, 13.7, 14.8]


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch