%export "../MonteCarlo/random"
        random_walk
        statistics accumulate_statistics showstatistics
%include "Markov"
%include "../MonteCarlo/random"

random_walk :: net -> node -> sequence -> [node]
  random_walk n here s
    = here : random_walk n (select n here (hd s)) (tl s)

select :: net -> node -> real -> node
  select n here rnd = n here!(#n here*rnd)







count == int
int == num
frequency_table == [(node,count)]
statistics == (frequency_table,count) || giving total explicitly





accumulate_statistics :: [node] -> [statistics]
|| gives the endless accumulation of statistics from a given battery
|| of Monte Carlo simulation endings.
  accumulate_statistics run
    = map first_only (iterate update_statistics (([],0),run))
      where
      first_only (a,b) = a

update_statistics :: (statistics,[node]) -> (statistics,[node])
  update_statistics ((table,total),ending:rest)
    = ((insert_data_item ending table,total+1),rest)

insert_data_item :: node -> frequency_table -> frequency_table
  insert_data_item e [] = [(e,1)]
  insert_data_item e ((e',c):rest)
    = (e,c+1):rest, e=e'
    = (e,1):(e',c):rest, e<e'
    = (e',c) : insert_data_item e rest, e>e'







shownodecount :: count -> (node,count) -> [char]
  shownodecount total (here,c) = shownum here++"\t"++shownum c
                                 ++"\t"++shownum (100*c/total)++"%\n"

showstatistics :: statistics -> [char]
  showstatistics (table,total)
    = "RUNS SO FAR: " ++ shownum total
      ++ "\n\nDISTRIBUTION OF ENDINGS SO FAR:\n\n"
      ++ "NODE\tCOUNT\tPERCENTAGE\n----\t-----\t----------\n"
      ++ concat (map (shownodecount total) table)
      ++ "\n\n\n\n"
