LIAM2 User Guide
  • 1. Introduction
    • 1.1. About LIAM2
    • 1.2. About microsimulation
    • 1.3. About this guide
  • 2. Installation
    • 2.1. Introduction
    • 2.2. Install using the Windows bundle
    • 2.3. Install from source
  • 3. Environment
    • 3.1. LIAM2 bundle
    • 3.2. Getting Started (using the Windows bundle)
    • 3.3. Getting Started (when running from source)
    • 3.4. Using your own data
  • 4. Model Definition
    • 4.1. Comments
    • 4.2. General structure
    • 4.3. import
    • 4.4. globals
    • 4.5. entities
    • 4.6. simulation
  • 5. Running a model/simulation
  • 6. Processes
    • 6.1. Expressions
    • 6.2. Assignments
    • 6.3. Functions
    • 6.4. globals
    • 6.5. Built-in Functions
    • 6.6. While loops
    • 6.7. Output
    • 6.8. Debugging and the interactive console
  • 7. Links
    • 7.1. many2one
    • 7.2. one2many
  • 8. Importing other models
  • 9. Importing data
    • 9.1. data files
    • 9.2. description file
    • 9.3. importing the data
  • Known issues
    • Contextual filter is inconsistent
    • 31 different variables per expression
  • Code architecture
    • Concepts
    • Other files
  • Technical choices
    • Python
    • HDF5
    • YAML
  • Change log
    • Version 0.12
    • Version 0.11
    • Version 0.10.5
    • Version 0.10.4
    • Version 0.10.3
    • Version 0.10.2
    • Version 0.10.1
    • Version 0.10
    • Version 0.9.1.1
    • Version 0.9.1
    • Version 0.9
    • Version 0.8.2
    • Version 0.8.1
    • Version 0.8
    • Version 0.7
    • Version 0.6.2
    • Version 0.6.1
    • Version 0.6
    • Version 0.5.1
    • Version 0.5
    • Version 0.4.1
    • Version 0.4
    • Version 0.3
    • Version 0.2.1
    • Version 0.2
    • Version 0.1
  • Credits
 
LIAM2 User Guide
  • Docs »
  • 7. Links

7. Links¶

Individuals can be linked with each other or with individuals of other entities, for example, mothers are linked to their children, partners are linked to each other and persons belong to households.

A typical link declaration has the following form:

name: {type: <type>, target: <entity>, field: <name of link field>}

LIAM2 uses integer fields to establish the link between entities. Those integer fields contain the id-number of the linked individual.

For link fields, -1 is a special value meaning the link points to nothing (eg. a person has no partner). Other negative values should never be used (whatever the reason) for link fields.

LIAM2 allows two types of links: many2one and one2many.

7.1. many2one¶

A many2one link links an individual of the entity to one other individual in the same (eg. a person to his/her mother) or another entity (eg. a person to its household).

This allows the modeller to use information stored in the linked entities.

entities:
    person:
        fields:
            - age: int
            - income: float
            - mother_id: int
            - father_id: int
            - partner_id: int

        links:
            mother: {type: many2one, target: person, field: mother_id}
            father: {type: many2one, target: person, field: father_id}
            partner: {type: many2one, target: person, field: partner_id}

        processes:
            test_link():
                - age: age + 1
                - mother_age: mother.age
                - parents_income: mother.income + father.income

To access a field of a linked individual (possibly of the same entity), you use:

link_name.field_name

For example, the mother_age process uses the ‘mother’ link to assign the age of the mother to the mother_age field. If an individual’s link does not point to anything (eg. a person has no known mother), trying to use the link would yield the missing value (eg. for orphans, mother.age is -1 and parents_income is nan).

As another example, the process below sets a variable to_separate to True if the variable separate is True for either the individual or his/her partner.

- to_separate: separate or partner.separate

Note that it is perfectly valid to chain links as, for example, in:

grand_parents_income: mother.mother.income + mother.father.income +
                      father.mother.income + father.father.income

Another option to get values in the linked individual is to use get:

link_name.get(expr)

this syntax is a bit more verbose in the simple case, but is much more powerful as it allows to evaluate (almost) any expression on the linked individual.

For example, if you want to get the average age of both parents of the mother of each individual, you can do it so:

mother.get((mother.age + father.age) / 2)

7.2. one2many¶

A one2many link links an individual of the entity to at least one other individual in the same (eg. a person to his/her children) or another entity (a household to its members).

entities:
    household:
        links:
            persons: {type: one2many, target: person, field: household_id}

    person:
        fields:
            - age: int
            - income: float
            - household_id: int

        links:
            household: {type: many2one, target: household, field: household_id}
  • persons is the link from the household to its members.
  • household is the link form a person to his/her household.

To access the information stored in the linked individuals through a one2many link, you have to use aggregate methods on the link:

link_name.method_name([arguments])

For example:

persons.avg(age)

one2many links support the following methods: count(), sum(), avg(), min() and max(). See link methods for details.

example

entities:
    household:
        fields:
            - num_children: int

        links:
            # link from a household to its members
            persons: {type: one2many, target: person, field: household_id}

        processes:
            composition():
                - num_children: persons.count(age <= 17)

    person:
        fields:
            - age: int
            - household_id: int

        links:
            # link form a person to his/her household
            household: {type: many2one, target: household,
                        field: household_id}

        processes:
            use_variable_computed_with_a_link():
                - num_kids_in_hh: household.num_children

The composition function, once called will compute the number of persons aged 17 or less in each household and store the result in the num_children field (of the household). Afterwards, that variable can be used like any other variable, for example through a many2one link, like in the num_kids_in_hh process. This process computes for each person, the number of children in the household of that person.

Note that the variable num_kids_in_hh could also have been simulated by just one process, on the “person” entity, by using:

- num_kids_in_hh: household.get(persons.count(age <= 17))
Next Previous

© Copyright 2011-2018, gb@plan.be, gd@plan.be, gdm@plan.be.

Built with Sphinx using a theme provided by Read the Docs.