-- See: L. Allison. Types and classes of machine learning and data mining.
--      26th Australasian Computer Science Conference (ACSC) pp.207-215,
--      Adelaide, February 2003
--      L. Allison. Models for machine learning and data mining in
--      functional programming.      doi:10.1017/S0956796804005301
--      J. Functional Programming, 15(1), pp.15-32, Jan. 2005
-- Author: Lloyd ALLISON           lloyd at bruce cs monash edu au
--         http://www.csse.monash.edu.au/~lloyd/tildeFP/II/200309/
-- This program is free software; you can redistribute it and/or modify it
-- under the terms of the GNU General Public License (GPL) as published by
-- the Free Software Foundation; either version 2 of the License, or (at
-- your option) any later version.  This program is distributed in the hope
-- that it will be useful, but without any warranty, without even the implied
-- warranty of merchantability or fitness for a particular purpose.  See the
-- GNU General Public License for more details.  You should have received a
-- copy of the GNU General Public License with this program; if not, write to:
-- Free Software Foundation, Inc., Boston, MA 02111, USA.

module TSModels (module TSModels) where
import SM_Utilities
import SM_Classes
import Models
import FnModels


               -- estimate a Markov model of order k, a TimeSeries of dataSpace
estMarkov k dataSeries =
  --  scan makes list of input contexts for the predictor function
  let scan (d:ds) context = context : (scan ds (d:context))
      scan []     _       = []
      -- e.g. scan "abcdef" [] -> ["","a","ba","cba","dcba","edcba"]
      contexts = scan dataSeries []  -- NB. each context is "backwards"
  in functionModel2timeSeries (estFiniteListFunction k contexts dataSeries)

-- --------------------------------------9/2002--L.Allison--CSSE--Monash--.au--

test05 = let
 { seq = [H,T,H,T,H,T, T,H, H,T];    -- mostly HT alternating
   mm0 = estMarkov 0 seq;            -- order 0
   mm1 = estMarkov 1 seq;            -- order 1
   mm2 = estMarkov 2 seq;            -- order 2
   mmm = mixture(Mix fifty50 [mm0, mm1]);
   test n =
     let s = (take n . cycle) seq
         m k = let mm = timeSeries2model (estMarkov k s)
               in ", " ++ show k ++ ":" ++ show(msgBase 2 mm [s])
     in print( "length=" ++ show n ++ m 0 ++ m 1 ++ m 2 ++ " bits" )
 }
 in print "-- test05 --"
 >> print("prs (coinByPrH 0.4) [H,T,H,T] = "
     ++ show(prs (coinByPrH 0.4) [H,T,H,T] ))
 >> print("msg1 mm0 = " ++ show( msg1 mm0 ))
 >> print("msg1 mm1 = " ++ show( msg1 mm1 ))
 >> print("msg1 mm2 = " ++ show( msg1 mm2 ))
 >> print("prs mm0 seq = " ++ show( prs mm0 seq ))
 >> print("prs mm1 seq = " ++ show( prs mm1 seq ))
 >> print("prs mm2 seq = " ++ show( prs mm2 seq ))
 >> print("mix mm0 mm1 = " ++ show( prs mmm seq))
 >> print("mm1 = " ++ show mm1 )
 >> print("mmm = " ++ show mmm )
 >> test 10
 >> test 100
 >> test 1000

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

