Types API Reference

This page documents the core data types in REM.jl.

Events

Event

REM.Event — Type
Event{T}

Represents a single relational event (directed interaction between actors).

Fields

  • sender::Int: ID of the event sender/source
  • receiver::Int: ID of the event receiver/target
  • time::T: Timestamp of the event
  • eventtype::Symbol: Type/category of the event (default: :event)
  • weight::Float64: Weight/magnitude of the event (default: 1.0)

T is the clock: Float64/Int for an abstract or ordinal time, Date or DateTime for calendar time (elapsed time is then measured in seconds by the decay and window machinery). A self-loop (sender == receiver) is permitted at construction; exclude it from the risk set with CaseControlSampler(exclude_self_loops=true) (the default).

Example

using REM, Dates
e = Event(1, 2, 3.5)                                   # 1 → 2 at t = 3.5
e.sender, e.receiver, e.time                           # (1, 2, 3.5)
Event(1, 2, 3.5; eventtype=:email, weight=2.0).weight   # 2.0
Event(1, 2, DateTime(2024, 1, 15, 10, 30))              # calendar clock
source

EventSequence

REM.EventSequence — Type
EventSequence{T}

A sequence of relational events, sorted by time.

The actor universe should be declared, not inferred: relational-event likelihoods are conditional on the risk set, so an actor universe read off the observed event endpoints silently drops eligible nonparticipants (isolates, receiver-only actors) and changes the estimand. Pass actors to declare it:

using REM
events = [Event(1, 2, 1.0), Event(2, 1, 2.0), Event(1, 3, 3.0)]
seq = EventSequence(events; actors=ActorSet([1, 2, 3, 7]))  # 7 may be an isolate
length(seq), seq.n_actors, seq.actors_declared               # (3, 4, true)
seq[1], seq[end].time                                        # (Event(1 → 2 @ 1.0), 3.0)
[e.receiver for e in seq]                                    # [2, 1, 3]
push!(seq, Event(7, 1, 2.5))                                 # inserted in time order
[e.time for e in seq]                                        # [1.0, 2.0, 2.5, 3.0]
inferred = EventSequence(events)
inferred.actors_declared                                     # false — inferred
EventSequence(collect(inferred); actors=ActorSet(1:7)).actors_declared   # true

If actors is omitted the universe falls back to the observed participants only; fit_rem warns once when it is asked to fit against such a sequence without an explicit risk set.

Fields

  • events::Vector{Event{T}}: Vector of events sorted by time
  • actors::Set{Int}: Set of all actor IDs (the actor universe)
  • n_actors::Int: Number of actors in the universe
  • eventtypes::Set{Symbol}: Set of all event types
  • actors_declared::Bool: True if the universe was supplied by the caller rather than inferred from event endpoints

show(seq) is one line — EventSequence{Float64}(481 events, 37 actors, declared universe) — not the event vector.

source

Actor Data

ActorSet

REM.ActorSet — Type
ActorSet(ids::AbstractVector{<:Integer})
ActorSet(ids::AbstractSet{<:Integer})
ActorSet(names::Vector{String})

The actor universe: the set of actors eligible to send and receive, with an optional ID-to-name mapping. Pass it as actors= to EventSequence to declare the risk set the likelihood is conditional on — isolates and noncontiguous IDs included. Names are assigned the IDs 1:length(names).

Fields

  • ids::Vector{Int}: the actor IDs (unique)
  • id_to_name::Dict{Int,String}, name_to_id::Dict{String,Int}: the name mapping (empty when the set was built from IDs)

Example

using REM
actors = ActorSet(1:37)                      # a range: 37 eligible actors
length(actors), 40 in actors                 # (37, false)
named = ActorSet(["Alice", "Bob", "Carol"])
named.name_to_id["Carol"], named.id_to_name[1]   # (3, "Alice")
seq = EventSequence([Event(1, 2, 1.0)]; actors=actors)   # actors 3…37 are isolates
seq.n_actors                                 # 37
source

NodeAttribute

REM.NodeAttribute — Type
NodeAttribute{T}

Stores an attribute value for each actor.

NodeAttribute(name, values::Dict{Int,T})            # no default: a missing actor is an error
NodeAttribute(name, values::Dict{Int,T}, default)   # explicit default for missing actors
NodeAttribute(name, default)                        # empty, fill with `attr[id] = value`

Reading attr[id] for an actor the attribute has no value for throws an ArgumentError naming the actor unless a default was given explicitly. Silently filling a missing value (with 0.0, say) is the classic covariate trap — a zero that looks like data — so the fill is opt-in: pass default to say that missing actors should read as that value.

Fields

  • name::Symbol: Name of the attribute
  • values::Dict{Int, T}: Mapping from actor ID to attribute value
  • default::Union{_NoDefault,T}: Default value for actors not in the dict, or the _NoDefault sentinel when none was given (see has_default)

show prints one line — NodeAttribute{Float64}(:age, 2 actors, no default) — never the values Dict, so a vector of statistics wrapping an attribute stays readable in the REPL.

Example

using REM
age = NodeAttribute(:age, Dict(1 => 25.0, 2 => 30.0))          # no default
age[1]                                   # 25.0
try age[3] catch err; err isa ArgumentError end   # true — actor 3 has no age
filled = NodeAttribute(:age, Dict(1 => 25.0, 2 => 30.0), 0.0)  # explicit default
filled[3]                                # 0.0, deliberately
sprint(show, filled)                     # "NodeAttribute{Float64}(:age, 2 actors, default = 0.0)"
source
REM.has_default — Function
has_default(attr::NodeAttribute) -> Bool

Whether attr fills a missing actor with a default value (true only when one was passed to the constructor). Without one, reading an actor the attribute lacks is an ArgumentError — the deliberate alternative to a silent zero-fill.

Example

using REM
strict = NodeAttribute(:age, Dict(1 => 25.0, 2 => 30.0))
filled = NodeAttribute(:age, Dict(1 => 25.0, 2 => 30.0), 0.0)
has_default(strict), has_default(filled)     # (false, true)
source

RiskSet

REM.RiskSet — Type
RiskSet

Represents the risk set for a given event - the set of potential dyads that could have experienced an event at a given time.

Fields

  • event_index::Int: Index of the focal event in the sequence

  • potential_senders::Vector{Int}: Actors who could be senders

  • potential_receivers::Vector{Int}: Actors who could be receivers

  • exclude_self_loops::Bool: Whether to exclude self-loops from risk set

  • dyads::Union{Nothing, Vector{Tuple{Int,Int}}}: an explicit, sorted list of the dyads at risk (a dyad-level risk set), or nothing for the senders × receivers product

A RiskSet is what at_risk= accepts when senders and receivers differ, or per event; n_dyads counts its dyads. The likelihood is conditional on it, so every case must belong to its own risk set (checked before fitting).

RiskSet(event_index, dyads) builds a dyad-level risk set from a list of (sender, receiver) pairs — one-shot relations, dyads that are impossible or have been removed, a two-mode structure with holes. Exactly the listed dyads are at risk (a listed self-loop included). Passing the list itself as at_risk= does the same for every event.

Example

using REM
rs = RiskSet(5, [1, 2, 3], [1, 2, 3, 4])     # focal event 5; 4 may only receive
n_dyads(rs)                                  # 9 = 3·4 − 3 self-loops
RiskSet(5, [1, 2], [3, 4]; exclude_self_loops=false)   # disjoint sides: 4 dyads
only = RiskSet(5, [(1, 2), (2, 1), (1, 3)])  # dyad-level: exactly these three
n_dyads(only)                                # 3
source

Network State

The EventNetworkState type maintains the cumulative state of the network as events are processed. It tracks dyad counts, degrees, and neighbor sets, optionally with exponential decay.

EventNetworkState

REM.EventNetworkState — Type
EventNetworkState

Tracks the cumulative state of the network up to a given point in time. Used for efficient computation of statistics.

Memory is O(actors + active dyads) — independent of the number of events absorbed — unless the event log is kept: keep_history (default true for a hand-built state) retains one (sender, receiver, time, weight) tuple per absorbed event in event_history, the public, read-only log that history-reading statistics consume (Revel.jl's PShift, Inertia, OTP, ...). None of REM's own statistics reads it, so generate_observations and compute_statistics build their state with keep_history = any(needs_history, stats) (see needs_history): a fit on REM's statistics retains no per-event memory, and a statistic that needs the log gets it.

Two memory models: halflife decay or a sliding window

  • decay (eventnet's model, and the only one eventnet offers): every count is stored as a (value, last_update_time) pair and decayed lazily on read relative to current_time, so updating the state with an event is O(1) instead of O(number of nonzero counts). Adjacency ("ever had an event") never expires under decay — counts fade, structure does not.
  • window (the fixed-memory alternative found in other REM software, e.g. remstats' memory = "window"): an event older than current_time − window stops counting — in the dyad counts, the undirected counts, the degrees and the adjacency sets (a dyad whose events have all expired has no edge). Implemented with a FIFO of absorbed events and an expiry cursor advanced when the clock moves, amortized O(1) per event. window is in the units of the clock for numeric time and in seconds for Date/DateTime time — on a calendar clock pass a Dates.Period (window = Day(2), converted to seconds) rather than counting seconds by hand; an event exactly window old still counts. window = Inf (or nothing, the default) is no window.

The two are mutually exclusive: decay > 0 together with a finite window is an ArgumentError. last_event_time (what RecencyStatistic reads) and event_history are not windowed — the time since a dyad's last event is a fact about the clock, not a count.

Fields

  • n_actors::Int: Number of actors
  • dyad_counts::Dict{Tuple{Int,Int}, Tuple{Float64,T}}: (weighted count, last update) per directed dyad
  • undirected_counts::Dict{Tuple{Int,Int}, Tuple{Float64,T}}: (weighted count, last update) per undirected dyad (min,max sorted)
  • out_degree::Dict{Int, Tuple{Float64,T}}: (weighted out-degree, last update) per actor
  • in_degree::Dict{Int, Tuple{Float64,T}}: (weighted in-degree, last update) per actor
  • last_event_time::Dict{Tuple{Int,Int}, T}: Time of last event for each dyad
  • decay::Float64: Exponential decay rate (0 = no decay)
  • window::Float64: Sliding-window length (Inf = no window)
  • current_time::T: Current time in the event sequence
  • event_history::Vector{Tuple{Int,Int,T,Float64}}: The absorbed events as (sender, receiver, time, weight), in order — public and read-only; empty unless keep_history is set
  • keep_history::Bool: Whether update! appends to event_history
  • n_events::Int: Number of events absorbed by update! since the last reset! (what show reports; kept whether or not the log is)
  • by_type::Dict{Symbol, EventNetworkState{T}}: one sub-state per event type named in the types= keyword, absorbing the events of that type only — what an OfType statistic reads. Empty by default; generate_observations, compute_statistics and fit_rem create the sub-states their typed statistics need

show(state) is one line — EventNetworkState{Float64}(3 actors, 2 events absorbed, no memory decay, current_time = 2.0) — naming the memory model (halflife h for decay, window w for a window).

Example

using REM
events = [Event(1, 2, 1.0), Event(2, 1, 2.0)]
seq = EventSequence(events; actors=ActorSet(1:3))
state = EventNetworkState(seq)                    # keep_history = true
for e in seq; update!(state, e); end
length(state.event_history)                       # 2
get_dyad_count(state, 1, 2)                       # 1.0
lean = EventNetworkState(seq; keep_history=false) # O(actors + dyads) memory

win = EventNetworkState(seq; window=1.5)          # events older than 1.5 expire
for e in seq; update!(win, e); end
win.current_time = 3.0                            # event 1 (t = 1.0) is now 2.0 old
get_dyad_count(win, 1, 2)                         # 0.0 — expired
get_dyad_count(win, 2, 1)                         # 1.0 — 1.0 old, still counts
source

State Updates

REM.update! — Function
update!(state::EventNetworkState, event::Event) -> state

Absorb event into the network state: advance the clock to event.time, add event.weight to the sender→receiver dyad count, the undirected count and the two degrees, record the dyad's last-event time, add the adjacency, append to the event log (when keep_history) and to the window FIFO (when windowed). O(1) per event — counts decay lazily on read, so nothing else is touched — and 0 bytes on a warmed state whose dyad has been seen before (the log and the FIFO grow amortized when they are kept).

Statistics are meant to be read before the event is absorbed (that is what generate_observations and compute_statistics do), so the usual loop computes first and updates second.

Example

using REM
seq = EventSequence([Event(1, 2, 1.0), Event(2, 1, 2.0), Event(1, 2, 3.0)];
                    actors=ActorSet(1:3))
state = EventNetworkState(seq)
values = Float64[]
for e in seq
    push!(values, compute(Repetition(), state, e.sender, e.receiver))   # before
    update!(state, e)                                                    # after
end
values                           # [0.0, 0.0, 1.0]
get_dyad_count(state, 1, 2)      # 2.0
state.n_events                   # 3
source
REM.reset! — Function
reset!(state::EventNetworkState) -> state

Reset the network state to empty: every count, degree, last-event time, adjacency set, the event log and the window are cleared and the clock goes back to zero(T). The actor universe, decay, window and keep_history are kept, so the same state can replay another sequence.

Example

using REM
seq = EventSequence([Event(1, 2, 1.0), Event(2, 1, 2.0)]; actors=ActorSet(1:3))
state = EventNetworkState(seq)
for e in seq; update!(state, e); end
get_dyad_count(state, 1, 2)      # 1.0
reset!(state)
get_dyad_count(state, 1, 2)      # 0.0
state.n_events, state.current_time   # (0, 0.0)
source

State Query Functions

These functions query the current network state for various quantities.

Dyad Queries

REM.get_dyad_count — Function
get_dyad_count(state::EventNetworkState, sender::Int, receiver::Int) -> Float64

The (decayed, event-weighted) count of events from sender to receiver; 0.0 when there has been none, or when every one of them has left the window. This is what Repetition reads (and Reciprocity, with the roles swapped).

Example

using REM
seq = EventSequence([Event(1, 2, 0.0), Event(1, 2, 10.0)]; actors=ActorSet(1:2))
state = EventNetworkState(seq)
for e in seq; update!(state, e); end
get_dyad_count(state, 1, 2)      # 2.0
get_dyad_count(state, 2, 1)      # 0.0

decayed = EventNetworkState(seq; decay=halflife_to_decay(10.0))
for e in seq; update!(decayed, e); end
get_dyad_count(decayed, 1, 2)    # 1.5 — the event at t = 0 is one halflife old
source
REM.get_undirected_count — Function
get_undirected_count(state::EventNetworkState, actor1::Int, actor2::Int) -> Float64

The (decayed, event-weighted) count of events between the two actors in either direction — what Repetition(directed=false) and CommonNeighbors read. Symmetric in its arguments.

Example

using REM
seq = EventSequence([Event(1, 2, 1.0), Event(2, 1, 2.0), Event(1, 2, 3.0)];
                    actors=ActorSet(1:2))
state = EventNetworkState(seq)
for e in seq; update!(state, e); end
get_undirected_count(state, 1, 2)    # 3.0
get_undirected_count(state, 2, 1)    # 3.0
get_dyad_count(state, 2, 1)          # 1.0 — one direction only
source
Graphs.has_edge — Function
has_edge(state::EventNetworkState, sender::Int, receiver::Int) -> Bool

Check if there has been at least one event from sender to receiver.

A method of the SHARED Graphs.has_edge generic (re-exported by NetworkCore.jl), not a REM-local function of the same name — asking "is there a tie from i to j" of an accumulated event network is the same question Graphs asks of a graph. That is why it can be exported without colliding: using Graphs, REM (or using ERGM, REM) dispatches one generic on the state type.

Adjacency never expires under decay (a count fades, the edge stays); under a window it expires with the dyad's last live event.

Example

using REM
seq = EventSequence([Event(1, 2, 1.0), Event(2, 3, 2.0)]; actors=ActorSet(1:3))
state = EventNetworkState(seq)
for e in seq; update!(state, e); end
has_edge(state, 1, 2)    # true
has_edge(state, 2, 1)    # false — direction matters
has_edge(state, 1, 3)    # false
source

Degree Queries

REM.get_out_degree — Function
get_out_degree(state::EventNetworkState, actor::Int) -> Float64

The (decayed, event-weighted) number of events actor has sent — what SenderActivity reads. 0.0 for an actor that has sent nothing.

Example

using REM
seq = EventSequence([Event(1, 2, 1.0), Event(1, 3, 2.0), Event(2, 1, 3.0)];
                    actors=ActorSet(1:3))
state = EventNetworkState(seq)
for e in seq; update!(state, e); end
get_out_degree(state, 1)    # 2.0
get_out_degree(state, 3)    # 0.0
source
REM.get_in_degree — Function
get_in_degree(state::EventNetworkState, actor::Int) -> Float64

The (decayed, event-weighted) number of events actor has received — what ReceiverPopularity reads. 0.0 for an actor that has received nothing.

Example

using REM
seq = EventSequence([Event(1, 2, 1.0), Event(1, 3, 2.0), Event(2, 1, 3.0)];
                    actors=ActorSet(1:3))
state = EventNetworkState(seq)
for e in seq; update!(state, e); end
get_in_degree(state, 1)     # 1.0
get_in_degree(state, 2)     # 1.0
get_in_degree(state, 3)     # 1.0
source

Neighbor Queries

REM.get_out_neighbors — Function
get_out_neighbors(state::EventNetworkState, actor::Int) -> AbstractSet{Int}

Get the set of actors to whom the given actor has sent events (under a window, events still inside it). Returns the internal set — treat as read-only. An actor with no neighbours gets the shared immutable empty set (copy it to get a Set{Int}).

Example

using REM
seq = EventSequence([Event(1, 2, 1.0), Event(1, 3, 2.0), Event(2, 1, 3.0)];
                    actors=ActorSet(1:3))
state = EventNetworkState(seq)
for e in seq; update!(state, e); end
Set(get_out_neighbors(state, 1))    # Set([2, 3])
isempty(get_out_neighbors(state, 3))   # true — read-only shared empty set
mine = copy(get_out_neighbors(state, 3)); push!(mine, 9)   # a Set{Int} of your own
source
REM.get_in_neighbors — Function
get_in_neighbors(state::EventNetworkState, actor::Int) -> AbstractSet{Int}

Get the set of actors who have sent events to the given actor (under a window, events still inside it). Returns the internal set — treat as read-only. An actor with no neighbours gets the shared immutable empty set (copy it to get a Set{Int}).

Example

using REM
seq = EventSequence([Event(1, 2, 1.0), Event(1, 3, 2.0), Event(2, 1, 3.0)];
                    actors=ActorSet(1:3))
state = EventNetworkState(seq)
for e in seq; update!(state, e); end
Set(get_in_neighbors(state, 1))    # Set([2])
Set(get_in_neighbors(state, 3))    # Set([1])
source

Time Utilities

The decay helpers are documented with the other utilities on the Estimation page.