Skip to content

Language

Language: the statistics of words from the economy of their use.

LanguageElement(label, exponent=1.0)

Bases: Element

Language with the exponent lambda of its word frequencies.

Parameters:

Name Type Description Default
label str

The label of the language.

required
exponent float

The initial guess of the exponent, from which it is solved.

1.0

Examples:

>>> from skcomplex.systems.social.language import LanguageElement
>>> LanguageElement('language').exponent
1.0
Source code in src/skcomplex/systems/social/language/_word_frequencies.py
39
40
41
def __init__(self, label: str, exponent: float = 1.0) -> None:
    super().__init__(label)
    self.exponent = exponent

LeastEffortInteraction(label, *, target, words)

Bases: Interaction

Probabilities of words that minimise the ratio of the mean cost to the entropy.

The mean cost is C and the entropy H. The probabilities that are stationary for C / H on the simplex are p_i = exp(-lambda c_i) / Z, for which H = lambda C + ln Z. The ratio is stationary in lambda where H = lambda C, that is where ln Z = 0, which solves the exponent. The sum reduces in log space.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the element that holds exponent.

required
words Role

The path of the group of words, which hold cost and probability.

required

Examples:

>>> from skcomplex.systems.social.language import LeastEffortInteraction
>>> LeastEffortInteraction('effort', target='language', words='words').words
'words'
Source code in src/skcomplex/systems/social/language/_word_frequencies.py
105
106
107
108
109
110
111
112
113
114
def __init__(
    self,
    label: str,
    *,
    target: Role,
    words: Role,
) -> None:
    super().__init__(label)
    self.target = target
    self.words = words

MandelbrotLeastEffortSystem(n_words=200, *, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The word frequencies that minimise the mean cost of a language over its entropy.

They are a power law of rank. A word of rank i costs log(i + 1), the length of its code, so the probabilities are (i + 1)^-lambda, where lambda makes them sum to 1 (Giordano 2024 §6, after Mandelbrot 1953). The system has the states language.exponent, words.cost, and words.probability.

Parameters:

Name Type Description Default
n_words int

The number of words.

200
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.social.language import MandelbrotLeastEffortSystem
>>> trajectory = MandelbrotLeastEffortSystem(50).prepare().simulate(until=1.0)
>>> round(float(trajectory.get_values('words.probability')[0].sum()), 6)
1.0
Source code in src/skcomplex/systems/social/language/_word_frequencies.py
163
164
165
166
167
168
169
170
171
172
def __init__(
    self,
    n_words: int = 200,
    *,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n_words = n_words

RandomTypewritingSystem(letters=5, *, space=0.2, keystrokes=1000, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

Miller's random typewriting.

The random keystrokes of monkeys give Zipf's law of word frequencies. A typewriter has letters letters and a space bar hit with the probability q = space, and every space ends a word. A word of L letters has the probability q ((1 - q) / letters)^L, so its frequency falls exponentially with its length, while the number of distinct words of length L grows exponentially as letters^L. The two exponentials give a power law of the frequency of the word of rank r, r^(-alpha) with alpha = 1 - log(1 - q) / log(letters) (Thurner et al. 2018 section 3.3.6.2). The system has the variable group words, which starts with the first word of the text.

Parameters:

Name Type Description Default
letters int

The number of letters of the typewriter.

5
space float

The probability that a keystroke hits the space bar.

0.2
keystrokes int

The number of keystrokes per step.

1000
random_state int | Generator | None

The seed or generator of the first word of the text.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.social.language import RandomTypewritingSystem
>>> RandomTypewritingSystem().prepare().system_.elements[0].size
1
Source code in src/skcomplex/systems/social/language/_typewriting.py
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
def __init__(
    self,
    letters: int = 5,
    *,
    space: float = 0.2,
    keystrokes: int = 1000,
    random_state: int | np.random.Generator | None = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.letters = letters
    self.space = space
    self.keystrokes = keystrokes
    self.random_state = random_state

TypedWordElement(label, *, count=1.0, length=1.0, code=1.0, size=None)

Bases: Element

Distinct word of a randomly typed text.

It holds its number of occurrences and its number of letters.

Parameters:

Name Type Description Default
label str

The label of the word or the group.

required
count Annotated[object, NN]

The number of occurrences of the word.

1.0
length Annotated[object, NN]

The number of letters of the word.

1.0
code Annotated[object, NN]

The word as a number, its letters k_1, ..., k_L from 0 read as the digits k_i + 1 of base letters.

1.0
size int | None

The number of words, or None for one.

None

Examples:

>>> from skcomplex.systems.social.language import TypedWordElement
>>> TypedWordElement('word', length=3.0).length
3.0
Source code in src/skcomplex/systems/social/language/_typewriting.py
66
67
68
69
70
71
72
73
74
75
76
77
78
def __init__(
    self,
    label: str,
    *,
    count: Annotated[object, NN] = 1.0,
    length: Annotated[object, NN] = 1.0,
    code: Annotated[object, NN] = 1.0,
    size: int | None = None,
) -> None:
    super().__init__(label, size=size)
    self.count = count
    self.length = length
    self.code = code

TypedWordView

Bases: AgentViewProtocol, Protocol

View of a typed word of the random typewriting system.

Its states are attributes.

TypingInteraction(label, *, target, letters=5, space_bar=0.2, keystrokes=1000)

Bases: Interaction

Typing of Miller's monkeys.

The keystrokes are random, on a typewriter with a number of letters and a space bar. Every keystroke hits the space bar with the probability space_bar, and otherwise one of the letters letters, uniformly. The space bar ends the word typed since the last space, a new distinct word or one more occurrence of an earlier one, and a space after a space types no word. A word is held as its code, its letters k_1, ..., k_L from 0 read as the digits k_i + 1 of base letters, so distinct words have distinct codes.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the variable group of distinct words.

required
letters Annotated[int, closed_open(1, oo)]

The number of letters of the typewriter.

5
space_bar Annotated[float, open(0, 1)]

The probability that a keystroke hits the space bar.

0.2
keystrokes Annotated[int, NN]

The number of keystrokes per step.

1000

Examples:

>>> from skcomplex.systems.social.language import TypingInteraction
>>> TypingInteraction('typing', target='words').letters
5
Source code in src/skcomplex/systems/social/language/_typewriting.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def __init__(
    self,
    label: str,
    *,
    target: Role,
    letters: Annotated[int, RealSet.closed_open(1, oo)] = 5,
    space_bar: Annotated[float, RealSet.open(0, 1)] = 0.2,
    keystrokes: Annotated[int, NN] = 1000,
) -> None:
    super().__init__(label)
    self.target = target
    self.letters = letters
    self.space_bar = space_bar
    self.keystrokes = keystrokes

type_keys(system)

Type the keystrokes of one step and count the words they end.

Parameters:

Name Type Description Default
system SystemView

The system.

required
Source code in src/skcomplex/systems/social/language/_typewriting.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def type_keys(self, system: SystemView) -> None:
    """Type the keystrokes of one step and count the words they end.

    Args:
        system:
            The system.
    """
    storage = system.storage
    if 'words' not in storage:
        storage['words'] = {int(word.code): word for word in cast(list[TypedWordView], system.get_agents('target'))}
    words, code, length = storage['words'], storage.get('code', 0), storage.get('length', 0)
    if not isinstance(words, dict) or not isinstance(code, int) or not isinstance(length, int):
        return
    spaces = system.rng.random(int(self.keystrokes)) < self.space_bar
    keys = system.rng.integers(int(self.letters), size=int(self.keystrokes))
    new: dict[tuple[int, int], int] = {}
    for is_space, key in zip(spaces.tolist(), keys.tolist(), strict=True):
        if not is_space:
            code, length = code * int(self.letters) + key + 1, length + 1
            continue
        if length:
            new[code, length] = new.get((code, length), 0) + 1
        code, length = 0, 0
    storage['code'], storage['length'] = code, length
    for (word_code, word_length), count in new.items():
        word = words.get(word_code)
        if word is not None:
            word.count += count
            continue
        created = system.create('target', count=float(count), length=float(word_length), code=float(word_code))
        words[word_code] = created

WordElement(label, cost=1.0, probability=0.0, size=None)

Bases: Element

Word with a quenched cost and the probability of its use.

Parameters:

Name Type Description Default
label str

The label of the word or the group.

required
cost object

The cost of the word.

1.0
probability object

The initial guess of the probability, which the equation replaces.

0.0
size int | None

The number of words, or None for one.

None

Examples:

>>> from skcomplex.systems.social.language import WordElement
>>> WordElement('word', cost=1.0).cost
1.0
Source code in src/skcomplex/systems/social/language/_word_frequencies.py
68
69
70
71
72
73
74
75
76
77
def __init__(
    self,
    label: str,
    cost: object = 1.0,
    probability: object = 0.0,
    size: int | None = None,
) -> None:
    super().__init__(label, size=size)
    self.cost = cost
    self.probability = probability