Update a Sparse Context Count Environment
update_env.RdIncrement or decrement the count of a symbol within a given context stored in an environment-based sparse representation.
Arguments
- env
An environment mapping context IDs (`ctx`) to named integer vectors. Each vector represents symbol counts for that context.
- ctx
Character. Context identifier key (n-grams).
- sym
Character. Symbol/event that follows the context.
- sign
Integer. Update direction: - `+1` increments the count (default behavior during training) - `-1` decrements the count (used during detrain or reversal)
Details
This function is used as the core update primitive for PPM-style models, where each context (`ctx`) stores a named integer vector of symbol counts.
The update is *sparse*: only symbols with non-zero counts are stored.
Examples
env <- new.env()
# add observations
update_env(env, "A_B", "C", +1)
update_env(env, "A_B", "C", +1)
# decrement
update_env(env, "A_B", "C", -1)
env[["A_B"]]
#> C
#> 1