probabilistic

Probabilistic sender implementation for the coding experiments library.

This module provides a sender implementation that generates messages randomly according to a specified probability distribution over the source alphabet. It’s useful for simulating realistic message sources and testing coding schemes under various statistical conditions.

class codinglab.senders.probabilistic.ProbabilisticSender(encoder, probabilities, message_length_range, logger=<codinglab.logger.NullLogger object>, seed=None)[source]

Bases: BaseSender[SourceChar, ChannelChar]

Sender that generates random messages with specified probabilities.

This sender generates messages by randomly selecting symbols from a source alphabet according to a given probability distribution. Message lengths are uniformly distributed within a specified range.

Parameters:
_probabilities

Probability distribution over source symbols

_alphabet

List of source symbols (keys from probabilities dict)

_weights

List of probabilities corresponding to alphabet symbols

_min_len

Minimum message length

_max_len

Maximum message length

_rng

Random number generator instance

_message_id

Counter for assigning unique message IDs

__init__(encoder, probabilities, message_length_range, logger=<codinglab.logger.NullLogger object>, seed=None)[source]

Initialize the probabilistic sender.

Parameters:
  • encoder (Encoder[TypeVar(SourceChar), TypeVar(ChannelChar)]) – Encoder instance for converting source to channel symbols

  • probabilities (Dict[TypeVar(SourceChar), float]) – Dictionary mapping source symbols to their probabilities (must sum to 1.0)

  • message_length_range (Tuple[int, int]) – Tuple of (min_length, max_length) for generated messages

  • logger (TransmissionLogger) – Logger for recording transmission events (defaults to NullLogger)

  • seed (Optional[int]) – Optional seed for the random number generator forreproducible experiments

Raises:

ValueError – If probabilities don’t sum to approximately 1.0, or if message length range is invalid

Return type:

None

property alphabet: Sequence[SourceChar]

Get the source alphabet from the probabilities dictionary.

Returns:

Sequence of source symbols that can appear in messages

message_stream(stream_len)[source]

Generate a stream of random encoded messages.

Each message is generated by randomly selecting symbols from the source alphabet according to the specified probability distribution. Message lengths are uniformly distributed within the configured range.

Parameters:

stream_len (int) – Number of messages to generate

Yields:

Encoded messages wrapped in Message containers with unique IDs

Raises:

ValueError – If stream_len <= 0

Return type:

Iterator[Message[TypeVar(ChannelChar)]]

reset()[source]

Reset the sender’s message ID counter.

This method resets the message ID counter to 0, allowing the sender to start fresh with a new sequence of message IDs.

Return type:

None