Class: Sooth::Predictor

Inherits:
Object
  • Object
show all
Defined in:
ext/sooth_native/native.c,
ext/sooth_native/native.c

Overview

A minimal stochastic predictive model, implemented in C for efficiency. No assumptions about PRNG or real-world significance of context/event.

Instance Method Summary collapse

Constructor Details

#initialize(error_event) ⇒ Object

Returns a new Sooth::Predictor instance.

Parameters:

  • error_event (Fixnum)

    The event to be returned by #select when no observations have been made for the context.



60
# File 'ext/sooth_native/native.c', line 60

VALUE method_sooth_native_initialize(VALUE self, VALUE error_event);

Instance Method Details

#clearObject

Clear the predictor to a blank slate.



65
66
# File 'ext/sooth_native/native.c', line 65

def clear
end

#count(context) ⇒ Fixnum

Return the number of times the context has been observed.

Parameters:

  • context (Fixnum)

    A number that provides a context for observations.

Returns:

  • (Fixnum)

    A count of the number of times the context has been observed. This is guaranteed to be equal to the sum of the counts of observations of all events observed in the context.



16
17
18
# File 'ext/sooth_native/native.c', line 16

def count(context)
  # (native code)
end

#distribution(context) ⇒ Array

Return an Enumerator that yields each observed event within the context together with its probability.

Parameters:

  • context (Fixnum)

    A number that provides a context for observations.

Returns:

  • (Array)

    A list of event-probability pairs.



25
26
27
# File 'ext/sooth_native/native.c', line 25

def distribution(context)
  # (native code)
end

#frequency(context, event) ⇒ Float

Return a number indicating the frequency that the event has been observed within the given context.

Parameters:

  • context (Fixnum)

    A number that provides a context for observations.

  • event (Fixnum)

    A number representing the observed event.

Returns:

  • (Float)

    The frequency, as a number between 0.0 and 1.0



34
35
36
# File 'ext/sooth_native/native.c', line 34

def frequency(context, event)
  # (native code)
end

#load(filename) ⇒ Object

Load the predictor from the specified filename. The predictor will be cleared before the file is loaded.

Parameters:

  • filename (String)

    The path of the file to be loaded.



73
74
# File 'ext/sooth_native/native.c', line 73

def load(filename)
end

#observe(context, event) ⇒ Fixnum

Register an observation of the given event within the given context.

Parameters:

  • context (Fixnum)

    A number that provides a context for the event, allowing the predictor to maintain observation statistics for different contexts.

  • event (Fixnum)

    A number representing the observed event.

Returns:

  • (Fixnum)

    A count of the number of times the event has been observed in the given context.



19
20
21
# File 'ext/sooth_native/native.c', line 19

def observe(context, event)
  # (native code)
end

#save(filename) ⇒ Object

Save the predictor to a file that can be loaded later.

Parameters:

  • filename (String)

    The path of the file to be merge.



80
81
# File 'ext/sooth_native/native.c', line 80

def save(filename)
end

#select(context, limit) ⇒ Fixnum

Return an event that may occur in the given context, based on the limit, which should be between 1 and #count. The event is selected by iterating through all observed events for the context, subtracting the observation count of each event from the limit until it is zero or less.

Parameters:

  • context (Fixnum)

    A number that provides a context for observations.

  • limit (Fixnum)

    The total number of event observations to be analysed before returning a event.

Returns:

  • (Fixnum)

    An event that has been previously observed in the given context, or the error_event if the #count of the context is zero, or if limit exceeds the #count of the context



22
23
24
# File 'ext/sooth_native/native.c', line 22

def select(context, limit)
  # (native code)
end

#size(context) ⇒ Fixnum

Return the number of different events that have been observed within the given context.

Parameters:

  • context (Fixnum)

    A number that provides a context for observations.

Returns:

  • (Fixnum)

    The number of distinct events that have been observed within the given context. This is guaranteed to be equal to the length of the #distribution for the context.



92
93
94
# File 'ext/sooth_native/native.c', line 92

def size(context)
  # (native code)
end

#surprise(context, event) ⇒ Float

Return a number indicating the surprise received by the predictor when it observed the given event within the given context. Note that nil will be returned if the event has never been observed within the context.

Parameters:

  • context (Fixnum)

    A number that provides a context for observations.

  • event (Fixnum)

    A number representing the observed event.

Returns:

  • (Float)

    The surprise, which is calculated to be the Shannon pointwise mutual information of the event according to the #distribution over the context.



31
32
33
# File 'ext/sooth_native/native.c', line 31

def surprise(context, event)
  # (native code)
end

#uncertainty(context) ⇒ Float

Return a number indicating how uncertain the predictor is about which event is likely to be observed after the given context. Note that nil will be returned if the context has never been observed.

Parameters:

  • context (Fixnum)

    A number that provides a context for observations.

Returns:

  • (Float)

    The uncertainty, which is calculated to be the Shannon entropy of the #distribution over the context.



28
29
30
# File 'ext/sooth_native/native.c', line 28

def uncertainty(context)
  # (native code)
end