diff --git a/.gitignore b/.gitignore index 96855b7..d22c7d4 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,7 @@ temp[1-9].py *.mx # Sphinx auto-generated dirs -generated*/ \ No newline at end of file +generated*/ + +# Jupyter notebook +.ipynb_checkpoints \ No newline at end of file diff --git a/examples/ifrs17sim/plot_csm_amortization.py b/examples/ifrs17sim/plot_csm_amortization.py deleted file mode 100644 index f7d38bb..0000000 --- a/examples/ifrs17sim/plot_csm_amortization.py +++ /dev/null @@ -1,40 +0,0 @@ -""" -:mod:`ifrs17sim` CSM pattern -============================ - -Draw a graph of CSM amortization pattern. -""" -import modelx as mx - -try: - import ifrs17sim.ifrs17sim as ifrs17sim -except ImportError: - import ifrs17sim - - -def draw_bars(proj): - liab = [proj.InnerProjection[t].pv_NetLiabilityCashflow[t] for t in range(10)] - - proj.CSM_Unfloored(15) - data = [proj.CSM_Unfloored(i) for i in range(10)] - fg, ax = plt.subplots() - - draw_single_bar(liab, ax, -1) - draw_single_bar(data, ax, +1) - - -def draw_single_bar(data, ax, n): - width = 0.4 - ax.bar(np.arange(len(data)) + n * (width/2), data, width - 0.05) - -if __name__ == '__main__': - import numpy as np - import matplotlib.pyplot as plt - import seaborn as sns - sns.set() - - model = ifrs17sim.build() - proj = model.OuterProjection[1] - draw_bars(proj) - - diff --git a/examples/ifrs17sim/plot_csm_waterfall.py b/examples/ifrs17sim/plot_csm_waterfall.py deleted file mode 100644 index 5f5a724..0000000 --- a/examples/ifrs17sim/plot_csm_waterfall.py +++ /dev/null @@ -1,37 +0,0 @@ -""" -:mod:`ifrs17sim` CSM waterfall chart -==================================== - -Draw a graph of CSM amortization pattern. - -*Run this sample now!* |launch binder| - -.. |launch binder| image:: https://mybinder.org/badge.svg - :target: https://mybinder.org/v2/gh/fumitoh/ifrs17sim-demo/master?filepath=ifrs17sim-demo.ipynb - -""" -import pandas as pd -import collections -from draw_charts import draw_waterfall - -try: - import ifrs17sim.ifrs17sim as ifrs17sim -except ImportError: - import ifrs17sim - -model = ifrs17sim.build() -proj = model.OuterProjection[1] - -proj.CSM_Unfloored(15) -data = collections.OrderedDict() - -for cells in ['CSM_Unfloored', - 'IntAccrCSM', - 'AdjCSM_FulCashFlows', - 'TransServices']: - data[cells] = [proj.cells[cells](t) for t in range(15)] - -df = pd.DataFrame(data) -df['TransServices'] = -1 * df['TransServices'] - -draw_waterfall(df) diff --git a/examples/nestedlife/plot_actexpct.py b/examples/nestedlife/plot_actexpct.py deleted file mode 100644 index 350efcd..0000000 --- a/examples/nestedlife/plot_actexpct.py +++ /dev/null @@ -1,128 +0,0 @@ -""" -Actual vs expected -================== - -Lapse assumption changes based on previous year experience. -""" -import modelx as mx - -try: - import nestedlife.nestedlife as nestedlife -except ImportError: - import nestedlife - -model = nestedlife.build() - -# Policy point ID and aliases -polid = 171 -outer = model.OuterProjection[polid] -inner = outer.InnerProjection - -# %% Code block for overriding the default model - -def SurrRateMult(t): - if t == 0: - return 1 - else: - return SurrRateMult(t - 1) - - -def nop_Surrender(t): - """Number of policies: Surrender""" - return nop_BoP1(t) * asmp.SurrRate(t) * SurrRateMult(t) - - -def nop_EoP_inner(t): - """Number of policies: End of period""" - if t == t0: - return outer.nop_EoP(t) - else: - return nop_BoP1(t - 1) - nop_Death(t - 1) - nop_Surrender(t - 1) - - -model.BaseProjection.new_cells(formula=SurrRateMult) -model.BaseProjection.new_cells(formula=nop_Surrender) -inner.new_cells(name='nop_EoP', formula=nop_EoP_inner) - -outer.SurrRateMult[1] = 2 -outer.SurrRateMult[2] = 0.5 -outer.SurrRateMult[3] = 1 - -inner[1].SurrRateMult[1] = 2 -inner[2].SurrRateMult[2] = 0.5 -inner[3].SurrRateMult[3] = 1 - -# %% Code block for drawing graphs - -import numpy as np -import matplotlib.pyplot as plt -import seaborn as sns -sns.set() - -def get_nested(item): - cells = outer.cells[item] - - act = [cells[t] for t in range(50)] - expect = [] - - for t0 in range(0, 6): - expect_t0 = [np.nan] * 50 - for t in range(0, 50): - if t < t0: - expect_t0[t] = np.nan - else: - cells = outer.InnerProjection[t0].cells[item] - expect_t0[t] = cells[t] - - expect.append(expect_t0) - - return act, expect - - -def mask_act(act, t0): - masked_act = act.copy() - for t, val in enumerate(masked_act): - if t > t0: - masked_act[t] = np.nan - return masked_act - - -def draw_single_ncf(ncf, ax, ls): - ax.plot(ncf, marker='o', linestyle=ls) - ax.set_xlim(right=10, left=-1) - - -def draw_graph_column(item): - - act, expect = get_nested(item) - - nrows = len(expect) - fg, axs = plt.subplots(nrows=nrows, sharex=True, sharey=True) - for t0, ax in enumerate(axs): - draw_single_ncf(expect[t0], ax, ':') - draw_single_ncf(mask_act(act, t0), ax, '-') - - -def draw_graph_pair(*items): - - ncols = len(items) - pairs = [get_nested(item) for item in items] - nrows = len(pairs[0][1]) - - fg, axs = plt.subplots(nrows=nrows, ncols=ncols, sharex=True) - - for col in range(ncols): - axs[0][col].set_title(items[col]) - for t0 in range(nrows): - ax = axs[t0][col] - act, expect = pairs[col] - draw_single_ncf(expect[t0], ax, ':') - draw_single_ncf(mask_act(act, t0), ax, '-') - -# %% Draw graphs -if __name__ == '__main__': - draw_graph_pair('nop_Surrender', 'nop_EoP') - - - - diff --git a/examples/nestedlife/plot_pvnetcf.py b/examples/nestedlife/plot_pvnetcf.py deleted file mode 100644 index 76c1f1a..0000000 --- a/examples/nestedlife/plot_pvnetcf.py +++ /dev/null @@ -1,94 +0,0 @@ -""" -:mod:`nestedlife` cashflow -========================== - -Draw a graph of present value of cashflow -""" -import modelx as mx - -try: - import nestedlife.nestedlife as nestedlife -except ImportError: - import nestedlife - -model = nestedlife.build() - -# Policy point ID and aliases -polid = 171 -outer = model.OuterProjection[polid] -inner = outer.InnerProjection - -# %% Code block for overwiting the defaut model - -def SurrRateMult(t): - if t == 0: - return 1 - else: - return SurrRateMult(t - 1) - - -def nop_Surrender(t): - """Number of policies: Surrender""" - return nop_BoP1(t) * asmp.SurrRate(t) * SurrRateMult(t) - - -def nop_EoP_inner(t): - """Number of policies: End of period""" - if t == t0: - return outer.nop_EoP(t) - else: - return nop_BoP1(t - 1) - nop_Death(t - 1) - nop_Surrender(t - 1) - - -model.BaseProjection.new_cells(formula=SurrRateMult) -model.BaseProjection.new_cells(formula=nop_Surrender) -inner.new_cells(name='nop_EoP', formula=nop_EoP_inner) - -outer.SurrRateMult[1] = 2 -outer.SurrRateMult[2] = 0.5 -outer.SurrRateMult[3] = 1 - -inner[1].SurrRateMult[1] = 2 -inner[2].SurrRateMult[2] = 0.5 -inner[3].SurrRateMult[3] = 1 - -# %% Code block for PV graph - -import numpy as np -import matplotlib.pyplot as plt -import seaborn as sns -sns.set() - - -def draw_bars(item): - - term = 5 - - expect = [] - for t0 in range(term): - expect_t0 = [np.nan] * term - for t in range(t0, term): - cells = outer.InnerProjection[t0].cells[item] - expect_t0[t] = cells[t] - - expect.append(expect_t0) - - fg, ax = plt.subplots() - ax.set_xlim(left=-0.5, right=term + 1) - - for t0 in range(term): - draw_single_bar(expect[t0], ax, t0) - - -def draw_single_bar(data, ax, t0): - - size = len(data) - width = 1/ (size + 1) - ax.bar(np.arange(size) + t0 * (width + 0.05), data, width) - - - -# %% PV Test -if __name__ == '__main__': - draw_bars('pv_NetLiabilityCashflow') - diff --git a/examples/simplelife/plot_simplelife.py b/examples/simplelife/plot_simplelife.py deleted file mode 100644 index 86db11d..0000000 --- a/examples/simplelife/plot_simplelife.py +++ /dev/null @@ -1,52 +0,0 @@ -""" -:mod:`simplelife` cashflow -========================== - -Draw a graph of liability cashflows of a simple whole life policy - -*Run this sample now!* |launch binder| - -.. |launch binder| image:: https://mybinder.org/badge.svg - :target: https://mybinder.org/v2/gh/fumitoh/simplelife-demo/master?filepath=simplelife-demo.ipynb - -""" -import modelx as mx - -try: - import simplelife.simplelife as simplelife -except ImportError: - import simplelife - -polid = 171 -proj = simplelife.build().Projection[polid] - -vars = ['prj_incm_Premium', - 'prj_bnft_Surrender', - 'prj_bnft_Death', - 'prj_exps_Maint', - 'prj_exps_CommTotal', - 'prj_exps_Acq'] - -for cells in vars: - list(proj.cells[cells](t) for t in range(50)) - -list(proj.prj_NetLiabilityCashflow[t] for t in range(50)) - - -# %% Code block for drawing graph -import seaborn as sns -sns.set() - -def draw_cashflow(proj): - - cfs = proj.frame[vars].sort_index().dropna() - cfs[vars[1:]] = cfs[vars[1:]].mul(-1) # Change outflows to negatives - - ncf = proj.prj_NetLiabilityCashflow.frame.sort_index() - - axes = ncf.plot.line(marker='o', color='r') - cfs.plot(kind='bar', stacked=True, ax=axes) - -# %% Main -if __name__ == '__main__': - draw_cashflow(proj) diff --git a/examples/README.txt b/lifelib/projects/README.txt similarity index 100% rename from examples/README.txt rename to lifelib/projects/README.txt diff --git a/examples/ifrs17sim/README.txt b/lifelib/projects/ifrs17sim/README.txt similarity index 100% rename from examples/ifrs17sim/README.txt rename to lifelib/projects/ifrs17sim/README.txt diff --git a/examples/nestedlife/README.txt b/lifelib/projects/nestedlife/README.txt similarity index 100% rename from examples/nestedlife/README.txt rename to lifelib/projects/nestedlife/README.txt diff --git a/examples/simplelife/README.txt b/lifelib/projects/simplelife/README.txt similarity index 100% rename from examples/simplelife/README.txt rename to lifelib/projects/simplelife/README.txt diff --git a/makedocs/source/conf.py b/makedocs/source/conf.py index 3f7aac6..1712077 100644 --- a/makedocs/source/conf.py +++ b/makedocs/source/conf.py @@ -210,11 +210,12 @@ from sphinx_gallery.sorting import ExplicitOrder sphinx_gallery_conf = { # path to your examples scripts - 'examples_dirs': '../../examples', + 'ignore_pattern': '^(?!.*plot_)', + 'examples_dirs': '../../lifelib/projects', 'subsection_order': ExplicitOrder( - ['../../examples/simplelife', - '../../examples/nestedlife', - '../../examples/ifrs17sim']), + ['../../lifelib/projects/simplelife', + '../../lifelib/projects/nestedlife', + '../../lifelib/projects/ifrs17sim']), # path where to save gallery generated examples 'gallery_dirs': 'generated_examples', # Suppress warning: diff --git a/tests/filecomp/test_filecomp.py b/tests/filecomp/test_filecomp.py index 343e5a5..e3e7dd0 100644 --- a/tests/filecomp/test_filecomp.py +++ b/tests/filecomp/test_filecomp.py @@ -12,7 +12,6 @@ simplepath = simplelife.__path__[0] nestedpath = nestedlife.__path__[0] ifrs17simpath = ifrs17sim.__path__[0] -examplepath = str(pathlib.Path(lifelib.__path__[0]).parent.joinpath('examples')) common_files = [('build_input.py', [simplepath, nestedpath, ifrs17simpath]), ('lifetable.py', [simplepath, nestedpath, ifrs17simpath]), @@ -22,19 +21,6 @@ ('projection.py', [simplepath, nestedpath, ifrs17simpath]), ('input.xlsm', [simplepath, nestedpath, ifrs17simpath])] -sample_files = [('plot_simplelife.py', - [simplepath, os.path.join(examplepath, 'simplelife')]), - ('plot_pvnetcf.py', - [nestedpath, os.path.join(examplepath, 'nestedlife')]), - ('plot_actexpct.py', - [nestedpath, os.path.join(examplepath, 'nestedlife')]), - ('plot_csm_amortization.py', - [ifrs17simpath, os.path.join(examplepath, 'ifrs17sim')]), - ('plot_csm_waterfall.py', - [ifrs17simpath, os.path.join(examplepath, 'ifrs17sim')])] - -common_files += sample_files - @pytest.mark.parametrize('filename, filepaths', common_files) def test_filecomp(filename, filepaths):