package Ġeg ger . software . h mm Ċ Ċ import Ġjava . util .* ĊĊ Ċ fun Ġ< T State > ĠIterable < State With Probability < T State >> . select State At Offset ( offset : ĠDouble ): ĠT State Ġ{ ĊĠĠĠ Ġvar Ġsum Ġ= Ġ 0 . 0 ĊĊĠĠĠ Ġrequire ( offset Ġ>= Ġ 0 . 0 , Ġ{ Ġ" Offset Ġhas Ġto Ġbe Ġ>= Ġ 0 . 0 " Ġ}) ĊĠĠĠ Ġrequire ( offset Ġ< Ġ 1 . 0 , Ġ{ Ġ" Offset Ġhas Ġto Ġbe Ġ< Ġ 1 . 0 " Ġ}) ĊĊĠĠĠ Ġval Ġstates Sorted With Acc umulated Probability Ġ= Ġmutable ListOf < State With Probability < T State >>() ĊĠĠĠ Ġthis . sorted By Ġ{ Ġstate With Probability Ġ-> Ġstate With Probability . probability Ġ}. forEach Ġ{ ĊĠĠĠĠĠĠĠ Ġsum Ġ+= Ġit . probability ĊĠĠĠĠĠĠĠ Ġstates Sorted With Acc umulated Probability . add ( State With Probability ( it . state , Ġsum )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfor Ġ( state With Probability Ġin Ġstates Sorted With Acc umulated Probability ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( offset Ġ< Ġstate With Probability . probability ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġstate With Probability . state ĊĠĠĠ Ġ} ĊĠĠĠ Ġthrow ĠIllegalStateException (" Could Ġnot Ġfind Ġ$ offset Ġin Ġ$ states Sorted With Acc umulated Probability Ġ[$ this ]") Ċ Ċ } Ċ Ċ fun Ġ< T State , ĠT Observation > ĠHidden Mark ov Model < T State , ĠT Observation >. generate Observation Sequence Acc ording To Model ( random : ĠRandom , ĠnumberOf Ob servations : ĠInt ): ĠList < TO b servation > Ġ{ ĊĊĠĠĠ Ġval Ġobservation List Ġ= Ġmutable ListOf < TO b servation >() ĊĠĠĠ Ġvar Ġstate Ġ= Ġthis . initialState Prob abilities . select State At Offset ( random . next Double ()) ĊĊĠĠĠ Ġfor Ġ( count Ġin Ġ 0 Ġuntil ĠnumberOf Ob servations ) Ġ{ ĊĠĠĠĠĠĠĠ Ġobservation List . add ( this . observation Prob abilities . transitions Away From ( state ). select State At Offset ( random . next Double ())) ĊĠĠĠĠĠĠĠ Ġstate Ġ= Ġthis . state Transitions . transitions Away From ( state ). select State At Offset ( random . next Double ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġreturn Ġobservation List Ċ } ĊĊ Ċ fun Ġ< T State > Ġgenerate State Sequence Acc ording To Model ( ĊĠĠĠĠĠĠĠ ĠinitialState Prob abilities : ĠList < State With Probability < T State >>, ĊĠĠĠĠĠĠĠ Ġstate Transition Table : ĠState Transition Table < T State , ĠT State >, Ġrandom : ĠRandom , ĠnumberOf States : ĠInt ): ĠList < T State > Ġ{ ĊĊĠĠĠ Ġval Ġstate List Ġ= Ġmutable ListOf < T State >() ĊĠĠĠ Ġvar Ġstate Ġ= ĠinitialState Prob abilities . select State At Offset ( random . next Double ()) ĊĠĠĠ Ġstate List . add ( state ) ĊĊĠĠĠ Ġfor Ġ( count Ġin Ġ 1 Ġuntil ĠnumberOf States ) Ġ{ ĊĠĠĠĠĠĠĠ Ġstate Ġ= Ġstate Transition Table . transitions Away From ( state ). select State At Offset ( random . next Double ()) ĊĠĠĠĠĠĠĠ Ġstate List . add ( state ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġreturn Ġstate List Ċ } Ċ Ċ val Ġ< T State > ĠList < State With Probability < T State >> . as Normalized : ĠList < State With Probability < T State >> ĊĠĠĠ Ġget () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġsum Ġ= Ġthis . sum By Double Ġ{ Ġit . probability Ġ} ĊĠĠĠĠĠĠĠ Ġreturn Ġthis . map Ġ{ ĠState With Probability ( it . state , Ġit . probability Ġ/ Ġsum ) Ġ} ĊĠĠĠ Ġ} Ċ Ċ val Ġ< T State > ĠMap < T State , ĠDouble >. as Normalized : ĠMap < T State , ĠDouble > ĊĠĠĠ Ġget () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġsum Ġ= Ġthis . values . sum () ĊĠĠĠĠĠĠĠ Ġreturn Ġthis . map Values Ġ{ Ġentry Ġ-> Ġentry . value Ġ/ Ġsum Ġ} ĊĠĠĠ Ġ} ĊĊ Ċ fun Ġ< TKey , ĠTValue > ĠMutable Map < TKey , ĠTValue >. init Using ( keys : ĠSet < TKey >, Ġvalue : ĠTValue ) Ġ= Ġthis . apply Ġ{ Ġkeys . forEach Ġ{ Ġset ( it , Ġvalue ) Ġ} Ġ}