{ "cells": [ { "cell_type": "markdown", "id": "94d6cef4", "metadata": {}, "source": [ "# 3. Memory-Optimized Mutiprocess Example \n", "\n", "\n", "This notebook demonstrates how to effectively run a heavy model with a large data set.\n", "The demonstration utilizes techniques such as:\n", "\n", "* Multiprocessing using [ipyparallel]\n", "* Memory-optimized runs introduced in [modelx v0.19.0](https://docs.modelx.io/en/latest/releases/relnotes_v0_19_0.html)\n", "\n", "**Multiprocessing by ipyparallel**\n", "\n", "Multiprocessing is a technique to make use of multiple CPU cores on a machine or on multiple machines\n", "by splitting a task into multiple processes.\n", "The multiple processes run in parallel on the multiple CPU cores, reducing the total run time required to finish the task.\n", "The [ipyparallel] package makes it easy and simple to perform multiprocessing.\n", "[ipyparallel] provides the following functions that are required for multiprocessing:\n", "\n", "\n", "* Handling both synchronous and asynchronous communications.\n", "* Sending and receiving numpy and pandas objects fast to and from engines.\n", "* Communicating with remote machines in the same way as with the localhost.\n", "\n", "**Memory-optimized run**\n", "\n", "Memory-optimized run is a feature that is introduced in modelx v0.19. \n", "Running a heaviy model with a large data set consumes a lot of memory. \n", "This feature makes it possible to run the model using less memory.\n", "For this feature, you need 2 runs. The first run is to get a list of *actions* and\n", "the model should be run with a small set of data.\n", "The second run is performed with the entire data set for getting the desired output.\n", "For more about memory-optimized run, see [modelx documentation](https://docs.modelx.io/en/latest/reference/generated/modelx.core.model.Model.execute_actions.html).\n", "\n", "\n", "**Steps in this notebook**\n", "\n", "The demonstration in this notebook involves the following steps:\n", "\n", "* A table of 100,000 model points is loaded.\n", "* A sample model is loaded in this Python process.\n", "* The model is run to generate *actions*. The actions are saved within the model.\n", "* 10 IPython engines are invoked, and a block of 10,000 model points is sent to each engine.\n", "* Each engine loads and runs the saved model with the 10,000 model by executing the actions saved in the model.\n", "* This process receives the results from all the engines and concatinate them.\n", "\n", "\n", "\n", "[ipyparallel]: https://ipyparallel.readthedocs.io/en/latest/\n", "[modelx]: https://docs.modelx.io\n", "\n", "
\n", " | spec_id | \n", "age_at_entry | \n", "sex | \n", "policy_term | \n", "policy_count | \n", "sum_assured | \n", "duration_mth | \n", "premium_pp | \n", "av_pp_init | \n", "
---|---|---|---|---|---|---|---|---|---|
policy_id | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
1 | \n", "A | \n", "47 | \n", "M | \n", "20 | \n", "22 | \n", "804000 | \n", "0 | \n", "804000 | \n", "0 | \n", "
2 | \n", "C | \n", "29 | \n", "F | \n", "9999 | \n", "75 | \n", "519000 | \n", "0 | \n", "900 | \n", "0 | \n", "
3 | \n", "A | \n", "51 | \n", "F | \n", "10 | \n", "5 | \n", "409000 | \n", "0 | \n", "409000 | \n", "0 | \n", "
4 | \n", "B | \n", "32 | \n", "M | \n", "15 | \n", "60 | \n", "128000 | \n", "0 | \n", "128000 | \n", "0 | \n", "
5 | \n", "D | \n", "28 | \n", "M | \n", "9999 | \n", "45 | \n", "698000 | \n", "0 | \n", "1200 | \n", "0 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
99996 | \n", "A | \n", "21 | \n", "M | \n", "10 | \n", "34 | \n", "152000 | \n", "0 | \n", "152000 | \n", "0 | \n", "
99997 | \n", "D | \n", "24 | \n", "F | \n", "9999 | \n", "53 | \n", "928000 | \n", "0 | \n", "1400 | \n", "0 | \n", "
99998 | \n", "B | \n", "46 | \n", "F | \n", "15 | \n", "72 | \n", "662000 | \n", "0 | \n", "662000 | \n", "0 | \n", "
99999 | \n", "A | \n", "46 | \n", "M | \n", "15 | \n", "36 | \n", "583000 | \n", "0 | \n", "583000 | \n", "0 | \n", "
100000 | \n", "B | \n", "35 | \n", "M | \n", "15 | \n", "3 | \n", "638000 | \n", "0 | \n", "638000 | \n", "0 | \n", "
100000 rows × 9 columns
\n", "\n", " | spec_id | \n", "age_at_entry | \n", "sex | \n", "policy_term | \n", "policy_count | \n", "sum_assured | \n", "duration_mth | \n", "premium_pp | \n", "av_pp_init | \n", "accum_prem_init_pp | \n", "
---|---|---|---|---|---|---|---|---|---|---|
poind_id | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
1 | \n", "A | \n", "20 | \n", "M | \n", "10 | \n", "100 | \n", "500000 | \n", "0 | \n", "500000 | \n", "0 | \n", "0 | \n", "
2 | \n", "B | \n", "50 | \n", "M | \n", "20 | \n", "100 | \n", "500000 | \n", "0 | \n", "500000 | \n", "0 | \n", "0 | \n", "
3 | \n", "C | \n", "20 | \n", "M | \n", "9999 | \n", "100 | \n", "500000 | \n", "0 | \n", "1000 | \n", "0 | \n", "0 | \n", "
4 | \n", "D | \n", "50 | \n", "M | \n", "9999 | \n", "100 | \n", "500000 | \n", "0 | \n", "1000 | \n", "0 | \n", "0 | \n", "
\n", " | Premiums | \n", "Death | \n", "Surrender | \n", "Maturity | \n", "Expenses | \n", "Commissions | \n", "Investment Income | \n", "Change in AV | \n", "Net Cashflow | \n", "
---|---|---|---|---|---|---|---|---|---|
policy_id | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
1 | \n", "1.768800e+07 | \n", "4.207421e+05 | \n", "7.167661e+06 | \n", "8.447648e+06 | \n", "2.549999e+05 | \n", "8.844000e+05 | \n", "5.002695e+06 | \n", "2.777545e+06 | \n", "2.737698e+06 | \n", "
2 | \n", "1.728737e+07 | \n", "5.399209e+06 | \n", "8.199806e+06 | \n", "0.000000e+00 | \n", "1.372393e+06 | \n", "8.643683e+05 | \n", "8.088528e+06 | \n", "6.038786e+06 | \n", "3.501331e+06 | \n", "
3 | \n", "2.045000e+06 | \n", "2.352243e+04 | \n", "6.103083e+05 | \n", "1.164191e+06 | \n", "4.394721e+04 | \n", "1.022500e+05 | \n", "2.460592e+05 | \n", "1.528495e+05 | \n", "1.939911e+05 | \n", "
4 | \n", "7.680000e+06 | \n", "5.362278e+04 | \n", "2.844819e+06 | \n", "4.554170e+06 | \n", "6.191257e+05 | \n", "3.840000e+05 | \n", "1.803562e+06 | \n", "1.027055e+06 | \n", "7.688294e+02 | \n", "
5 | \n", "1.388815e+07 | \n", "4.410248e+06 | \n", "7.023923e+06 | \n", "0.000000e+00 | \n", "8.270160e+05 | \n", "6.944077e+05 | \n", "6.925307e+06 | \n", "5.242075e+06 | \n", "2.615791e+06 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
99996 | \n", "5.168000e+06 | \n", "1.433198e+04 | \n", "1.546823e+06 | \n", "2.983362e+06 | \n", "2.994351e+05 | \n", "2.584000e+05 | \n", "6.263479e+05 | \n", "3.887630e+05 | \n", "3.032331e+05 | \n", "
99997 | \n", "1.936913e+07 | \n", "5.442479e+06 | \n", "1.005014e+07 | \n", "0.000000e+00 | \n", "9.893691e+05 | \n", "9.684563e+05 | \n", "9.870065e+06 | \n", "7.846641e+06 | \n", "3.942107e+06 | \n", "
99998 | \n", "4.766400e+07 | \n", "7.247140e+05 | \n", "1.760871e+07 | \n", "2.791406e+07 | \n", "7.413578e+05 | \n", "2.383200e+06 | \n", "1.113241e+07 | \n", "6.340720e+06 | \n", "3.083640e+06 | \n", "
99999 | \n", "2.098800e+07 | \n", "3.009029e+05 | \n", "7.454509e+06 | \n", "1.104981e+07 | \n", "3.706789e+05 | \n", "1.049400e+06 | \n", "4.407965e+06 | \n", "2.510836e+06 | \n", "2.659825e+06 | \n", "
100000 | \n", "1.914000e+06 | \n", "1.542257e+04 | \n", "7.087322e+05 | \n", "1.133148e+06 | \n", "3.094749e+04 | \n", "9.570000e+04 | \n", "4.491574e+05 | \n", "2.557838e+05 | \n", "1.234236e+05 | \n", "
100000 rows × 9 columns
\n", "