diff --git a/joss.06880/10.21105.joss.06880.crossref.xml b/joss.06880/10.21105.joss.06880.crossref.xml new file mode 100644 index 0000000000..4b369a4dbc --- /dev/null +++ b/joss.06880/10.21105.joss.06880.crossref.xml @@ -0,0 +1,215 @@ + + + + 20240816180609-c1ee3674cbf230ecb9afad2524f198a14c3254bf + 20240816180609 + + JOSS Admin + admin@theoj.org + + The Open Journal + + + + + Journal of Open Source Software + JOSS + 2475-9066 + + 10.21105/joss + https://joss.theoj.org + + + + + 08 + 2024 + + + 9 + + 100 + + + + ChainoPy: A Python Library for Discrete Time Markov +Chain Based Stochastic Analysis + + + + Aadya A. + Chinubhai + + + + 08 + 16 + 2024 + + + 6880 + + + 10.21105/joss.06880 + + + http://creativecommons.org/licenses/by/4.0/ + http://creativecommons.org/licenses/by/4.0/ + http://creativecommons.org/licenses/by/4.0/ + + + + Software archive + 10.5281/zenodo.13305155 + + + GitHub review issue + https://github.com/openjournals/joss-reviews/issues/6880 + + + + 10.21105/joss.06880 + https://joss.theoj.org/papers/10.21105/joss.06880 + + + https://joss.theoj.org/papers/10.21105/joss.06880.pdf + + + + + + Markov chain neural networks + Awiszus + Proceedings of the IEEE conference on +computer vision and pattern recognition workshops + 10.1109/CVPRW.2018.00293 + 2018 + Awiszus, M., & Rosenhahn, B. +(2018). Markov chain neural networks. Proceedings of the IEEE Conference +on Computer Vision and Pattern Recognition Workshops, 2180–2187. +https://doi.org/10.1109/CVPRW.2018.00293 + + + PyDTMC + Belluzzo + 2024 + Belluzzo, T. (2024). PyDTMC. +https://github.com/TommasoBelluzzo/PyDTMC. +https://github.com/TommasoBelluzzo/PyDTMC + + + Simple-markov + Charisopoulos + 2016 + Charisopoulos, V., & +Andrikopoulos, K. (2016). Simple-markov. +https://github.com/Mandragorian/simple-markov. +https://github.com/Mandragorian/simple-markov + + + Mchmm + Terpilovskii + 2021 + Terpilovskii, M. (2021). Mchmm. +https://github.com/maximtrp/mchmm. +https://github.com/maximtrp/mchmm + + + Cython: The best of both +worlds + Behnel + Computing in Science & +Engineering + 2 + 13 + 10.1109/MCSE.2010.118 + 2010 + Behnel, S., Bradshaw, R., Citro, C., +Dalcin, L., Seljebotn, D. S., & Smith, K. (2010). Cython: The best +of both worlds. Computing in Science & Engineering, 13(2), 31–39. +https://doi.org/10.1109/MCSE.2010.118 + + + Array programming with NumPy + Harris + Nature + 7825 + 585 + 10.1038/s41586-020-2649-2 + 2020 + Harris, C. R., Millman, K. J., Van +Der Walt, S. J., Gommers, R., Virtanen, P., Cournapeau, D., Wieser, E., +Taylor, J., Berg, S., Smith, N. J., & others. (2020). Array +programming with NumPy. Nature, 585(7825), 357–362. +https://doi.org/10.1038/s41586-020-2649-2 + + + SciPy 1.0: Fundamental algorithms for +scientific computing in python + Virtanen + Nature methods + 3 + 17 + 10.1038/s41592-019-0686-2 + 2020 + Virtanen, P., Gommers, R., Oliphant, +T. E., Haberland, M., Reddy, T., Cournapeau, D., Burovski, E., Peterson, +P., Weckesser, W., Bright, J., & others. (2020). SciPy 1.0: +Fundamental algorithms for scientific computing in python. Nature +Methods, 17(3), 261–272. +https://doi.org/10.1038/s41592-019-0686-2 + + + PyTorch 2: Faster machine learning through +dynamic python bytecode transformation and graph +compilation + Ansel + Proceedings of the 29th ACM international +conference on architectural support for programming languages and +operating systems, volume 2 + 10.1145/3620665.3640366 + 2024 + Ansel, J., Yang, E., He, H., +Gimelshein, N., Jain, A., Voznesensky, M., Bao, B., Bell, P., Berard, +D., Burovski, E., & others. (2024). PyTorch 2: Faster machine +learning through dynamic python bytecode transformation and graph +compilation. Proceedings of the 29th ACM International Conference on +Architectural Support for Programming Languages and Operating Systems, +Volume 2, 929–947. +https://doi.org/10.1145/3620665.3640366 + + + Convergence rates for markov +chains + Rosenthal + SIAM Review + 3 + 37 + 10.1137/1037083 + 1995 + Rosenthal, J. S. (1995). Convergence +rates for markov chains. SIAM Review, 37(3), 387–405. +https://doi.org/10.1137/1037083 + + + Regime switching models + Hamilton + Macroeconometrics and time series +analysis + 10.1057/9780230280830_23 + 2010 + Hamilton, J. D. (2010). Regime +switching models. In Macroeconometrics and time series analysis (pp. +202–209). Springer. +https://doi.org/10.1057/9780230280830_23 + + + + + + diff --git a/joss.06880/10.21105.joss.06880.pdf b/joss.06880/10.21105.joss.06880.pdf new file mode 100644 index 0000000000..70b057ded6 Binary files /dev/null and b/joss.06880/10.21105.joss.06880.pdf differ diff --git a/joss.06880/paper.jats/10.21105.joss.06880.jats b/joss.06880/paper.jats/10.21105.joss.06880.jats new file mode 100644 index 0000000000..d4338d8971 --- /dev/null +++ b/joss.06880/paper.jats/10.21105.joss.06880.jats @@ -0,0 +1,705 @@ + + +
+ + + + +Journal of Open Source Software +JOSS + +2475-9066 + +Open Journals + + + +6880 +10.21105/joss.06880 + +ChainoPy: A Python Library for Discrete Time Markov Chain +Based Stochastic Analysis + + + + +Chinubhai +Aadya A. + + + + + +School of Engineering and Applied Sciences (SEAS), +Ahmedabad University, Ahmedabad, Gujarat, India + + + +9 +100 +6880 + +Authors of papers retain copyright and release the +work under a Creative Commons Attribution 4.0 International License (CC +BY 4.0) +2022 +The article authors + +Authors of papers retain copyright and release the work under +a Creative Commons Attribution 4.0 International License (CC BY +4.0) + + + +Markov Chains +Markov Chain Neural Networks +Stochastic Analysis +High Performance Computing + + + + + + Summmary +

Modeling time series data, such as stock prices and text sequences, + is effectively achieved using Markov Chains. ChainoPy facilitates the + modeling of time series data with Markov Chains and Markov Switching + Models, optimizing for computational efficiency in terms of speed and + memory usage. Additionally, ChainoPy enables the integration of + probabilistic models like Markov Chains with Neural Networks, + traditionally considered deterministic, through the + MarkovChainNeuralNetwork class. This hybrid approach leverages the + strengths of both probabilistic and neural network methodologies.

+
+ + Statement of Need +

There are limitations in current Markov Chain packages like PyDTMC + (Belluzzo, + 2024), simple-markov + (Charisopoulos + & Andrikopoulos, 2016), mchmm + (Terpilovskii, + 2021) that rely solely on NumPy + (Harris + et al., 2020) and Python for implementation. Markov Chains + often require iterative convergence-based algorithms + (Rosenthal, + 1995), where Python’s dynamic typing, Global Interpreter Lock + (GIL), and garbage collection can hinder potential performance + improvements like parallelism. To address these issues, we enhance our + library with extensions like Cython for efficient algorithm + implementation. Additionally, we introduce a Markov Chain Neural + Network + (Awiszus + & Rosenhahn, 2018) that simulates given Markov Chains while + preserving statistical properties from the training data. This + approach eliminates the need for post-processing steps such as + sampling from the outcome distribution while giving neural networks + stochastic properties rather than deterministic behavior. Finally, we + implement the famous Markov Switching Models + (Hamilton, + 2010) which are one of the fundamental and widely used models + in applications such as Stock Market price prediction. ChainoPy + enables new workflows through its advanced algorithms, such as Markov + Chain Neural Networks and Markov Switching Models, which are not + available in PyDTMC. These capabilities, combined with significant + performance improvements in both fast and slow functions, provide + added value for complex stochastic analysis tasks.

+
+ + Implementation +

We implement three public classes + MarkovChain, + MarkovChainNeuralNetwork and + MarkovSwitchingModel that contain core + functionalities of the package. Performance intensive functions for + the MarkovChain class are implemented in the + _backend directory where a custom Cython + (Behnel + et al., 2010) backend is implemented circumventing drawbacks of + Python like the GIL, dynamic typing, etc. The + MarkovChain class implements various + functionalities for discrete-time Markov chains. It provides methods + for fitting the transition matrix from data, simulating the chain, and + calculating properties. It also supports visualization for Markov + chains.

+

We do the following key optimizations:

+ + +

Efficient matrix power: If the matrix is diagonalizable, an + eigenvalue decomposition based matrix power is performed.

+
+ +

Parallel Execution: Some functions are parallelized.

+
+ +

__slots__ usage: + __slots__ is used instead of + __dict__ for storing object attributes, + reducing memory overhead.

+
+ +

Caching decorator: Class methods are decorated with caching to + avoid recomputation of unnecessary results.

+
+ +

Direct LAPACK use: LAPACK function dgeev + is directly used to calculate stationary-distribution via SciPy’s + (Virtanen + et al., 2020) cython_lapack API + instead of additional NumPy overhead.

+
+ +

Utility functions for visualization: Utility functions are + implemented for visualizing the Markov chain.

+
+ +

Sparse storage of transition matrix: The model is stored as a + JSON object, and if 40% or more elements of the transition matrix + are near zero, it is stored in a sparse format.

+
+
+

The MarkovChainNeuralNetwork implementation + defines a neural network model, using PyTorch + (Ansel + et al., 2024) for simulating Markov chain behavior. It takes a + Markov chain object and the number of layers as input, with each layer + being a linear layer. The model’s forward method computes the output + probabilities for the next state. The model is trained using + stochastic gradient descent (SGD) with a learning rate scheduler. + Finally, the model’s performance is evaluated using the + Kullback–Leibler divergence between the original Markov chain’s + transition probabilities and those estimated from the simulated + walks.

+
+ + Documentation, Testing and Benchmarking +

For documentation we use Sphinx. For testing and benchmarking we + use the Pytest and PyDTMC + (Belluzzo, + 2024) packages.

+

The results are as follows:

+ + +

is_absorbing Methods

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Transition-Matrix Size1050100
MeanSt. devMeanSt. devMeanSt. dev
Function
1. is_absorbing (ChainoPy)97.3ns2.46ns91.8ns0.329ns98ns0.4ns
1. is_absorbing (PyDTMC)386ns5.79ns402ns2.01ns417ns3ns
+
+ + +

stationary_dist vs + pi Methods

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Transition-Matrix Size1050100
MeanSt. devMeanSt. devMeanSt. dev
Function
1. stationary_dist (ChainoPy)1.47us1.36us93.4ns5.26ns96.6ns3.9ns
1. pi (PyDTMC)137us12.9us395ns15.4ns398ns10.5ns
+
+ + +

fit vs + fit_sequence Method:

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Number of Words1050100
MeanSt. devMeanSt. devMeanSt. dev
Function
1. fit (ChainoPy)116 µs5.28 µs266 µs15 µs496 µs47.3 µs
1. fit_sequence (PyDTMC)14 ms1.74 ms14.4 ms1.17 ms17.3 ms2.18 ms
+
+ + +

simulate Method

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Transition-Matrix SizeN-StepsChainoPy MeanChainoPy St. devPyDTMC MeanPyDTMC St. dev
10100022.8 ms2.32 ms28.2 ms933 µs
500086.8 ms2.76 ms155 ms5.25 ms
50100017.6 ms1.2 ms29.9 ms1.09 ms
500084.5 ms4.84 ms161 ms7.62 ms
100100021.6 ms901 µs37.4 ms3.99 ms
5000110 ms11.3 ms162 ms5.75 ms
500100024 ms3.73 ms39.6 ms6.07 ms
5000112 ms6.63 ms178 ms26.5 ms
1000100026.1 ms620 µs46.1 ms6.47 ms
5000136 ms2.49 ms188 ms2.43 ms
2500100042 ms3.77 ms59.6 ms2.29 ms
5000209 ms16.4 ms285 ms27.6ms
+
+

Apart from this, we test the + MarkovChainNeuralNetworks by training them and + comparing random walks between the original + MarkovChain object and those generated by + MarkovChainNeuralNetworks through a + histogram.

+
+ + Conclusion +

In conclusion, ChainoPy offers a Python library for discrete-time + Markov Chains and includes features for Markov Chain Neural Networks, + providing a useful tool for researchers and practitioners in + stochastic analysis with efficient performance.

+
+ + + + + + + + AwiszusMaren + RosenhahnBodo + + Markov chain neural networks + Proceedings of the IEEE conference on computer vision and pattern recognition workshops + 2018 + 10.1109/CVPRW.2018.00293 + 2180 + 2187 + + + + + + BelluzzoTommaso + + PyDTMC + https://github.com/TommasoBelluzzo/PyDTMC + 2024 + https://github.com/TommasoBelluzzo/PyDTMC + + + + + + CharisopoulosVasilis + AndrikopoulosKostis + + Simple-markov + https://github.com/Mandragorian/simple-markov + 2016 + https://github.com/Mandragorian/simple-markov + + + + + + TerpilovskiiMaksim + + Mchmm + https://github.com/maximtrp/mchmm + 2021 + https://github.com/maximtrp/mchmm + + + + + + BehnelStefan + BradshawRobert + CitroCraig + DalcinLisandro + SeljebotnDag Sverre + SmithKurt + + Cython: The best of both worlds + Computing in Science & Engineering + IEEE + 2010 + 13 + 2 + 10.1109/MCSE.2010.118 + 31 + 39 + + + + + + HarrisCharles R + MillmanK Jarrod + Van Der WaltStéfan J + GommersRalf + VirtanenPauli + CournapeauDavid + WieserEric + TaylorJulian + BergSebastian + SmithNathaniel J + others + + Array programming with NumPy + Nature + Nature Publishing Group UK London + 2020 + 585 + 7825 + 10.1038/s41586-020-2649-2 + 357 + 362 + + + + + + VirtanenPauli + GommersRalf + OliphantTravis E + HaberlandMatt + ReddyTyler + CournapeauDavid + BurovskiEvgeni + PetersonPearu + WeckesserWarren + BrightJonathan + others + + SciPy 1.0: Fundamental algorithms for scientific computing in python + Nature methods + Nature Publishing Group + 2020 + 17 + 3 + 10.1038/s41592-019-0686-2 + 261 + 272 + + + + + + AnselJason + YangEdward + HeHorace + GimelsheinNatalia + JainAnimesh + VoznesenskyMichael + BaoBin + BellPeter + BerardDavid + BurovskiEvgeni + others + + PyTorch 2: Faster machine learning through dynamic python bytecode transformation and graph compilation + Proceedings of the 29th ACM international conference on architectural support for programming languages and operating systems, volume 2 + 2024 + 10.1145/3620665.3640366 + 929 + 947 + + + + + + RosenthalJeffrey S + + Convergence rates for markov chains + SIAM Review + SIAM + 1995 + 37 + 3 + 10.1137/1037083 + 387 + 405 + + + + + + HamiltonJames D + + Regime switching models + Macroeconometrics and time series analysis + Springer + 2010 + 10.1057/9780230280830_23 + 202 + 209 + + + + +