Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Haskell Online

mean :: [Double] -> Double
mean [a] = a
mean xs = sum xs / (fromIntegral (length xs))

varianceSummation :: [Double] -> Double -> Double
varianceSummation [a] mean = (a - mean)^2
varianceSummation (x:xs) mean = (x - mean)^2 + varianceSummation xs mean

myVariance :: [Double] -> Double
myVariance xs = (varianceSummation xs (mean xs)) / (fromIntegral (length xs))

coVarianceSummation :: [Double] -> [Double] -> Double -> Double -> Double
coVarianceSummation [a] [b] meanX meanY = (a - meanX) * (b - meanY)
coVarianceSummation (x:xs) (y:ys) meanX meanY = ((x - meanX) * (y - meanY) +
                           coVarianceSummation xs ys meanX meanY)
                           
myCovariance :: [Double] -> [Double] -> Double
myCovariance xs ys = (coVarianceSummation xs ys (mean xs) (mean ys))
                     / (fromIntegral ((length xs) - 1))
                     
dotprd :: [Double] -> [Double] -> Double
dotprd [a] [b] = a * b 
dotprd (x:xs) (y:ys) = (x * y) + (dotprd xs ys)

vectorlen :: [Double] -> Double
vectorlen xs = sqrt (dotprd xs xs)

getangle :: [Double] -> [Double] -> Double
getangle xs ys = acos ((dotprd xs ys) / ((vectorlen xs) * (vectorlen ys)))

main = do 
    print (myCovariance [1.0,2.0] [1.0,2.0])

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.