Example #1
0
def main(force_name, start_year, start_month, end_year, end_month):

    # construct and run the model
    microsim = model.CrimeMicrosim(force_name, (start_year, start_month),
                                   (end_year, end_month))
    no.run(microsim)

    plt = visualisation.density_map(microsim.crimes, force_name)

    plt.show()
Example #2
0
def test_linear_timeline() -> None:
    # 40 years annual steps
    m = _TestModel2(2011, 2051, 40)
    assert m.timeline.time() == 2011
    assert m.timeline.dt() == 1.0
    assert m.timeline.index() == 0
    assert m.timeline.end() == 2051

    no.run(m)
    assert m.timeline.index() == 40
    assert m.timeline.time() == 2051
Example #3
0
def test_resume() -> None:
    t0 = 0.1
    n = 10
    m = _TestResume(t0, n)  # unit timesteps

    t = t0
    while not m.timeline.at_end():
        no.run(m)
        t += 1
        assert m.timeline.time() == t

    assert m.timeline.time() == t0 + n
Example #4
0
def get_crimes(loading):
    global model

    no.log("Setting loading factor to %f" % loading)
    no.log("Sampling crimes in %s for month beginning %s" %
           (model.force_area(), model.timeline().time()))
    model.set_loading(loading)
    no.run(model)

    buf = StringIO()
    model.crimes.to_csv(buf)
    return buf.getvalue()
Example #5
0
def test_null_timeline() -> None:
    t0 = no.NoTimeline()
    assert t0.nsteps() == 1
    assert t0.dt() == 0.0
    assert not t0.at_end()
    assert t0.index() == 0
    assert no.time.isnever(t0.time())
    assert no.time.isnever(t0.end())

    m = _TestModel2(0, 1, 1)
    no.run(m)
    assert m.timeline.at_end()
    assert m.timeline.index() == 1
    assert m.timeline.time() == 1.0
Example #6
0
def test_halt_finalise() -> None:
    class HCModel(no.Model):
        def __init__(self, timeline: no.Timeline, halt: bool = False) -> None:
            super().__init__(timeline,
                             no.MonteCarlo.deterministic_identical_stream)
            self.do_halt = halt
            self.finalise_called = False

        def step(self) -> None:
            if self.do_halt:
                self.halt()

        def finalise(self) -> None:
            self.finalise_called = True

    m = HCModel(no.LinearTimeline(0, 3, 3))
    no.run(m)
    assert m.finalise_called

    m = HCModel(no.LinearTimeline(0, 3, 3), True)
    no.run(m)
    assert not m.finalise_called
    assert not m.timeline.at_end()
    assert m.timeline.index() == 1
    no.run(m)
    assert not m.finalise_called
    assert not m.timeline.at_end()
    assert m.timeline.index() == 2
    no.run(m)
    assert m.finalise_called
    assert m.timeline.at_end()
    assert m.timeline.index() == 3
Example #7
0
def main(force_name, start_year, start_month, end_year, end_month):

    from crims import model
    #from crims import geography
    #from crims import utils
    from crims import visualisation

    # construct and run the model
    microsim = model.CrimeMicrosim(force_name, (start_year, start_month),
                                   (end_year, end_month))
    no.run(microsim)

    plt = visualisation.density_map(microsim.crimes, force_name)

    plt.show()
Example #8
0
def pop_crimes():
    global model, time, timestep

    # TODO this is inefficient
    if time >= model.crimes.time.max():
        no.log("Sampling crimes in %s for month beginning %s" %
               (model.force_area(), model.timeline().time()))
        no.run(model)

    end = time + timestep

    buf = StringIO()
    model.crimes[(model.crimes.time >= time)
                 & (model.crimes.time < end)].to_csv(buf)
    time = end
    return buf.getvalue()
Example #9
0
def get_crimes(start, end):
  global model, timestep

  ts = datetime.strptime(start, TIME_FORMAT)
  te = datetime.strptime(end, TIME_FORMAT)

  # NB model time is the start of the *next* (as yet unsampled) timestep
  if ts >= model.timeline().time():
    no.log("%s Sampling crimes in %s for month beginning %s..." % (datetime.now(), model.force_area(), model.timeline().time()))
    no.run(model)
    no.log("%s sampling complete" % datetime.now())

  #no.log("%s -> %s: %d" % (ts, te, len(model.crimes[(model.crimes.time >= ts) & (model.crimes.time < te)])))

  buf = StringIO()
  model.crimes[(model.crimes.time >= ts) & (model.crimes.time < te)].to_csv(buf)
  return buf.getvalue()
Example #10
0
def test_check_flag() -> None:
    class FailingModel(no.Model):
        def __init__(self) -> None:
            super().__init__(no.NoTimeline(),
                             no.MonteCarlo.deterministic_identical_stream)

        def step(self) -> None:
            pass

        def check(self) -> bool:
            return False

    # fails
    assert not no.run(FailingModel())

    no.checked(False)
    # succeeds
    assert no.run(FailingModel())
Example #11
0
def test_numeric_timeline() -> None:
    class NumericTimelineModel(no.Model):
        def __init__(self, numerictimeline: no.Timeline) -> None:
            super().__init__(numerictimeline,
                             no.MonteCarlo.deterministic_identical_stream)

        def step(self) -> None:
            assert self.timeline.dt() == 1 / 16
            assert self.timeline.time() == self.timeline.index() / 16

        def finalise(self) -> None:
            assert self.timeline.time() == 1.0
            assert self.timeline.time() == self.timeline.end()
            assert self.timeline.index() == 16

    # 16 steps to avoid rounding errors
    m = NumericTimelineModel(no.NumericTimeline(np.linspace(0.0, 1.0, 17)))
    assert m.timeline.time() == 0.0
    assert m.timeline.index() == 0
    no.run(m)
Example #12
0
def test_open_ended_timeline() -> None:
    class OpenEndedModel(no.Model):
        def __init__(self, timeline: no.Timeline) -> None:
            super().__init__(timeline,
                             no.MonteCarlo.deterministic_identical_stream)
            self.i = 0

        def step(self) -> None:
            assert self.i == self.timeline.index()
            self.i += 1
            if self.i > 10: self.halt()

    m = OpenEndedModel(no.LinearTimeline(0, 1))
    assert m.timeline.end() == no.time.far_future()
    assert m.timeline.nsteps() == -1
    assert m.timeline.dt() == 1.0
    no.run(m)
    assert m.i == 11

    m = OpenEndedModel(no.CalendarTimeline(date(2020, 12, 17), 1, "d"))
    assert m.timeline.end() == no.time.far_future()
    assert m.timeline.nsteps() == -1
    assert np.fabs(m.timeline.dt() - 1.0 / 365.2475) < 1e-8
    no.run(m)
    assert m.i == 11

    m = OpenEndedModel(no.CalendarTimeline(date(2020, 12, 17), 1, "m"))
    assert m.timeline.end() == no.time.far_future()
    assert m.timeline.nsteps() == -1
    assert np.fabs(m.timeline.dt() - 31.0 / 365.2475) < 1e-8
    no.run(m)
    assert m.i == 11
Example #13
0
def test_dummy_model() -> None:
    class DummyModel(no.Model):
        def __init__(self) -> None:
            super().__init__(no.NoTimeline(),
                             no.MonteCarlo.deterministic_identical_stream)

        def step(self) -> None:
            pass

        def finalise(self) -> None:
            pass

    assert no.run(DummyModel())
Example #14
0
def test_calendar_timeline() -> None:
    # monthly timesteps checking we don't overshoot in shorter months
    dim = [31, 29, 31, 30, 31, 30]

    class CalendarModel(no.Model):
        def __init__(self, calendartimeline: no.Timeline) -> None:
            super().__init__(calendartimeline,
                             no.MonteCarlo.deterministic_identical_stream)

        def step(self) -> None:
            assert self.timeline.time().day == min(dim[self.timeline.index()],
                                                   d)

        def finalise(self) -> None:
            assert self.timeline.dt() == 0.0
            assert self.timeline.time() == self.timeline.end()
            assert self.timeline.index() == 6

    for d in range(1, 32):
        t = no.CalendarTimeline(date(2020, 1, d), date(2020, 7, d), 1, "m")

        m = CalendarModel(t)
        no.run(m)
Example #15
0
def init_model(run_no, force_area, year, month, initial_loading, burn_in):
  global model

  # this adjustment needs to happen on netlogo side to keep dates in sync
  # assert burn_in > 0 
  # # adjust year/month so that the supplied values correspond to the *end* of the burn-in period
  # month -= burn_in
  # while month < 1:
  #   year -= 1
  #   month += 12


  # use canned data if requested
  if force_area == "TEST":
    model = CannedCrimeData((year, month))
    return

  # monthly open-ended timeline (run_no is used to seed the mc)
  model = CrimeMicrosim(run_no, force_area, (year, month), burn_in=burn_in)
  no.log("Initialised crime model in %s at %s" % (force_area, model.timeline().time()))
  no.log("MC seed=%d" % model.mc().seed())
  # simulate the first month
  model.set_loading(initial_loading)
  no.run(model)
Example #16
0
def test_multimodel() -> None:
    class TestModel(no.Model):
        def __init__(self) -> None:
            super().__init__(no.LinearTimeline(0, 10, 10),
                             no.MonteCarlo.deterministic_identical_stream)

            self.x = 0.0

        def step(self) -> None:
            #no.log(self.mc.ustream(1))
            self.x += self.mc.ustream(1)

        def finalise(self) -> None:
            no.log(self.x)

    models = [TestModel(), TestModel()]

    [no.run(m) for m in models]

    assert models[0].x == models[1].x
Example #17
0
def test_consistency() -> None:

    # need to wrap timeline in a model to do the stepping, which isnt directly accessible from python
    class ConsistencyTest(no.Model):
        def __init__(self, timeline: no.Timeline) -> None:
            super().__init__(timeline,
                             no.MonteCarlo.deterministic_identical_stream)

        def step(self) -> None:
            pass

    m = ConsistencyTest(no.NoTimeline())
    assert m.timeline.nsteps() == 1
    no.run(m)
    assert m.timeline.index() == 1

    m = ConsistencyTest(no.LinearTimeline(2020, 2021, 12))

    assert m.timeline.nsteps() == 12
    no.run(m)
    assert m.timeline.index() == 12
    assert m.timeline.time() == 2021

    m = ConsistencyTest(no.NumericTimeline([2020 + i / 12 for i in range(13)]))
    assert m.timeline.nsteps() == 12
    no.run(m)
    assert m.timeline.index() == 12
    assert m.timeline.time() == 2021

    s = date(2019, 10, 31)
    e = date(2020, 10, 31)

    m = ConsistencyTest(no.CalendarTimeline(s, e, 1, "m"))
    assert m.timeline.time().date() == s
    assert m.timeline.nsteps() == 12
    no.run(m)
    assert m.timeline.time().date() == e
    assert m.timeline.index() == 12
Example #18
0
"""
Competing risks - fertility & mortality
"""
import neworder
# model implementation
from people import People
# separate visualisation code
from visualise import plot

# neworder.verbose()

# create model
# data are for white British women in a London Borough at 1 year time resolution
dt = 1.0 # years
fertility_hazard_file = "examples/competing/fertility-wbi.csv"
mortality_hazard_file = "examples/competing/mortality-wbi.csv"
population_size = 100000
pop = People(dt, fertility_hazard_file, mortality_hazard_file, population_size)

# run model
neworder.run(pop)

# visualise results
plot(pop)

Example #19
0
# m = RedisDemoModel("West Yorkshire", 2020, 1, 2021, 1)

# no.run(m)

#def run_model(force, start_year, start_month, duration_months):

# list for model requests
for m in pubsub.listen():
    no.log(m)
    if m["type"] == "message" and m["channel"] == b"crime_model_init":
        params = json.loads(m["data"])
        m = RedisDemoModel(params["force"], params["start_year"],
                           params["start_month"], params["end_year"],
                           params["end_month"])
        no.run(m)

# # # or json? "unpickling can execute code"
# # cache.set("crimes", pickle.dumps(df))

# for m in range(3):
#   print(m)
#   # send crime data
#   cache.publish("crime_data", pickle.dumps(df))
#   # wait for response
#   m = next(pubsub.listen())
#   if m["type"] == "message":
#     print(m["data"])
#   else:
#     print(m)
Example #20
0
# neworder.verbose()
# checks disabled to emphasise performance differences
neworder.checked(False)

# the max value in the timeline
max_age = 100.0
# The mortality rate data
mortality_hazard_file = "examples/mortality/mortality-wbi.csv"
population_size = 100000

neworder.log("Population = %d" % population_size)

# run the discrete model
mortality_discrete = PeopleDiscrete(mortality_hazard_file, population_size, max_age)
start = time.perf_counter()
neworder.run(mortality_discrete)
end = time.perf_counter()
neworder.log("Discrete model life expectancy = %f, exec time = %f" % (mortality_discrete.life_expectancy, end - start))

# run the continuous model
mortality_continuous = PeopleContinuous(mortality_hazard_file, population_size, 1.0)
start = time.perf_counter()
neworder.run(mortality_continuous)
end = time.perf_counter()
neworder.log("Continuous model life expectancy = %f, exec time = %f" % (mortality_continuous.life_expectancy, end - start))

# visualise some results
# hist_file = "docs/examples/img/mortality_%dk.png" % (population_size//1000)
# anim_file = "docs/examples/img/mortality_hist_%dk.gif" % (population_size//1000)
plot(mortality_discrete.population, mortality_continuous.population)
Example #21
0
"""
Chapter 1
This is a direct neworder cover version of the Basic Cohort Model from the Belanger & Sabourin book
See https://www.microsimulationandpopulationdynamics.com/
"""
import numpy as np
import neworder
from person import People

# neworder.verbose() # uncomment for detailed output

# "An arbitrarily selected value, chosen to produce a life expectancy of about 70 years."
# (see the "mortality" example for a more realistic model)
mortality_hazard = 0.014
population_size = 100000

model = People(mortality_hazard, population_size)

neworder.run(model)

# now we can sample the population generated by the model to see the proportion of deaths at (arbitrarily) 10 year intervals
for age in np.linspace(10.0, 100.0, 10):
  neworder.log("Age %.0f survival rate = %.1f%%" % (age, model.alive(age) * 100.0))
Example #22
0
def test_model() -> None:
    model = _TestModel()
    no.run(model)
    assert model.step_count == 10
Example #23
0
import numpy as np
import neworder
from schelling import Schelling

#neworder.verbose()

# category 0 is empty
gridsize = (480, 360)
categories = np.array([0.36, 0.12, 0.12, 0.4])
# normalise if necessary
# categories = categories / sum(categories)
similarity = 0.6

# open-ended timeline with arbitrary timestep
# the model halts when all agents are satisfied, rather than at a specific time
timeline = neworder.LinearTimeline(0, 1.0)

schelling = Schelling(timeline, gridsize, categories, similarity)

neworder.run(schelling)
Example #24
0
        for i, r in self.population.iterrows():
            if r.talkative:
                neworder.log("Hello from %d" % i)

    # !finalise!

    # def check(self) -> bool:
    #   """
    #   Custom checks can be made after every timestep during the simulation.
    #   This method is optional
    #   Arguments: self
    #   Returns: bool
    #   """
    #   return True


# !script!
# uncomment for verbose output
# neworder.verbose()
# uncomment to disable checks entirely
# neworder.checked(False)

# construct the model with the population size and the probability of becoming talkative
hello_world = HelloWorld(10, 0.5)

# run the model and check it worked
ok = neworder.run(hello_world)
if not ok:
    neworder.log("model failed!")
# !script!
Example #25
0
    # infection rate amongst those previously uninfected
    neworder.log("effective infection rate %.2f%% new : %d" % (100.0 * len(new_infections) / len(uninfected), len(new_infections)))
    self.infection_rate[self.timeline().index()] = len(new_infections) / len(uninfected)
    self.mortality_rate[self.timeline().index()] = len(new_deaths) / len(self.pop[self.pop.State != State.DECEASED])

  def finalise(self):

    deaths = sum(~neworder.time.isnever(self.pop.tDeceased.values))
    # simple measure of test coverage 100% or severe and above, 25% of mild
    observed_cases = sum(~neworder.time.isnever(self.pop.tSevere.values)) + 0.25 * sum(~neworder.time.isnever(self.pop.tMild.values))

    neworder.log("Mortality: observed = %.2f%%, actual = %.f%%" % (100.0 * deaths / observed_cases, 100.0 * deaths / self.npeople))

    self.summary = self.summary.fillna(0)
    self.summary.index = range(1,len(self.summary)+1)
    # use the string representations of thobserved_casese int enums
    self.summary.rename(columns={s: State(s).name for s in self.summary.columns.values}, inplace=True)

    Graphics().plot(self)


# initialise the model
days = 180
dt = 1
npeople = 10000

disease_model = DiseaseModel(days, dt, npeople)

neworder.run(disease_model)
Example #26
0
            if m["type"] == "message" and m["channel"] == b"crime_model_stop":
                self.halt()

    def checkpoint(self):
        # send done signal (NB (int)0 gets serialised as b"0" i.e. a string (?))
        cache.publish("crime_model_result", json.dumps({"status": 0}))
        pubsub.unsubscribe("crime_rate")


cache = redis.StrictRedis(host='localhost', port=6379)
pubsub = cache.pubsub()
pubsub.subscribe("crime_model_init")

# m = RedisDemoModel("West Yorkshire", 2020, 1, 2021, 1)

# no.run(m)

#def run_model(force, start_year, start_month, duration_months):

while True:
    # list for model init requests
    no.log("waiting for init request")
    for msg in pubsub.listen():
        no.log(msg)
        if msg["type"] == "message" and msg["channel"] == b"crime_model_init":
            params = json.loads(msg["data"])
            model = RedisDemoModel(params["force"], params["start_year"],
                                   params["start_month"], params["end_year"],
                                   params["end_month"])
            no.run(model)
Example #27
0
import time
from datetime import date

import neworder
from population import Population

#neworder.verbose()

# input data
initial_population = "examples/people/E08000021_MSOA11_2011.csv"
# age, gender and ethnicity-specific rates
fertility_rate_data = "examples/people/fertility.csv"
mortality_rate_data = "examples/people/mortality.csv"
in_migration_rate_data = "examples/people/migration-in.csv"
out_migration_rate_data = "examples/people/migration-out.csv"

# define the evolution timeline
timeline = neworder.CalendarTimeline(date(2011, 1, 1), date(2051, 1, 1), 1,
                                     "y")

# create the model
population = Population(timeline, initial_population, fertility_rate_data,
                        mortality_rate_data, in_migration_rate_data,
                        out_migration_rate_data)

# run the model
start = time.time()
ok = neworder.run(population)
neworder.log("run time = %.2fs" % (time.time() - start))
assert ok
Example #28
0
See:
https://www.statcan.gc.ca/eng/microsimulation/modgen/new/chap3/chap3
https://www.statcan.gc.ca/eng/microsimulation/modgen/new/chap4/chap4

  'RiskPaths is a simple, competing risk, case-based continuous time microsimulation model. Its
  main use is as a teaching tool, introducing microsimulation to social scientists and demonstrating
  how dynamic microsimulation models can be efficiently programmed using the language
  Modgen.
  Modgen is a generic microsimulation programming language developed and maintained at
  Statistics Canada.
  RiskPaths as well as the Modgen programming language and other related documents are
  available at www.statcan.gc.ca/microsimulation/modgen/modgen-eng.htm'

"""
import neworder
from data import max_age
from riskpaths import RiskPaths
from visualisation import plot

# serial mode
#neworder.verbose()

population_size = 100000

# single step (continuous model)
riskpaths = RiskPaths(population_size)

neworder.run(riskpaths)

plot(riskpaths)
Example #29
0
# neworder.checked(False) # uncomment to disable checks

# requires 4 identical sims with perturbations to compute market sensitivities 
# (a.k.a. Greeks)
assert neworder.mpi.size() == 4, "This example requires 4 processes"

# initialisation

# market data
market = {
  "spot": 100.0, # underlying spot price
  "rate": 0.02,  # risk-free interest rate
  "divy": 0.01,  # (continuous) dividend yield
  "vol": 0.2    # stock volatility
}
# (European) option instrument data
option = {
  "callput": "CALL",
  "strike": 100.0,
  "expiry": 0.75 # years
}

# model parameters
nsims = 1000000 # number of underlyings to simulate

# instantiate model
bs_mc = BlackScholes(option, market, nsims)

# run model
neworder.run(bs_mc)