> module Project5Calculation > (bestMatches) > where > import ColumnOperationsOnMatrices(standardDeviationUnbiasedColumns) > import NumericUtilities(correlation) > import SequenceUtilities(pam, quicksortWith) compute [stdDevJan, stdDevFeb, ... stdDevDec] from [(year1, rJan1, rFeb1, ... rDec1), (year2, rJan2, rFeb2, ... rDec2), ... (yearN, rJanN, rFebN, ... rDecN)] > normalizationFactors :: RealFloat r => [(String, [r])] -> [r] > normalizationFactors yearRainfallData = > standardDeviationUnbiasedColumns(map snd yearRainfallData) normalize rainfall data for a given year > normalizeYear:: RealFloat r=> [r] -> (String, [r]) -> (String, [r]) > normalizeYear factors (year, rainfall) = > (year, zipWith (*) factors rainfall) resemblance of rainfall pattern B to rainfall pattern A (both patterns normalized) > resemblance :: > RealFloat r => (String, [r]) -> (String, [r]) -> (String, r) > resemblance (yearA, rainfallA) (yearB, rainfallB) > = (yearB, correlation rainfallA rainfallB) find the three years whose rainfall patterns most closely resemble that of a given year; deliver closest years and their resemblances to the given year > threeClosest :: > RealFloat r => (String, [r]) -> [(String, [r])] -> [(String, r)] > threeClosest normalizedYear normalizedData = > (take 3 . quicksortWith betterResemblance) yearsAndResemblances > where > yearsAndResemblances = > map (resemblance normalizedYear) normalizedData deliver True iff operand A has greater resemblance than operand B > betterResemblance :: Ord r => (x, r) -> (x, r) -> Bool > betterResemblance (_, resemblanceA) (_, resemblanceB) = > resemblanceA > resemblanceB given a sequence of annual rainfall data figures and the pre-armistice data, deliver the pre-armistice years most closely resembling each of the annual rainfall data figures (along with the computed resemblances) > bestMatches :: > RealFloat r => > [(String, [r])] -> [(String, [r])] -> [(String, [(String, r)])] > bestMatches givenYears preArmisticeData = > zip (map fst givenYears) > (pam (map threeClosest normalizedYearData) > normalizedPreArmisticeData ) > where > normalizedPreArmisticeData = map normalize preArmisticeData > normalizedYearData = map normalize givenYears > normalize = normalizeYear factors > factors = normalizationFactors preArmisticeData