Put the Python implementation module "pyDEM" of the Cross-mapping model that can estimate the non-linear time series causality in the Macbook

In the following article, for tabular data, __SAM model __ that can search for which variable pair in the __ table has a causal relationship expressed by a linear function __, I tried it with Google Colaboratory.

-I applied the "statistical causal search" model SAM (Structural Agnostic Modeling) using GAN to the Titanic dataset

The __ * LiNGAM * models __ and __SAM (* Structural Agnostic Modeling *) models __ were models that assumed __ linear functions as causal relationships between variables.

-@ YuyaOmori "[Causal search / causal reasoning] Execute causal search (SAM) using deep learning"

Therefore, LiNGAM and SAM have the constraint that if there is any causal relationship between variables, that relationship is always a "linear" relationship (modeled by a linear function).

This constraint can be removed by using a method called __ * Cross-mapping algorithm * that assumes the existence of a relationship that can be described by a nonlinear mechanical model between __ variables.

(Nonlinear mechanical model, so chaotic dynamics model will appear)

-@ Kumalpha-san Causal estimation in time series analysis (mainly Convergent Cross Mapping)

Convergent Cross Mapping (CCM)

A type of Empirical Dynamic Modeling (EDM). EDM is a method that regards time series data as a nonlinear dynamical system and executes actions between variables and predictions in the near future. Model-free analysis is performed by reconstructing the state space of the system from the observed values ​​without assuming mathematical formulas. It is a method that extends the idea of ​​Simplex projection and S-map to the alternate prediction of multiple variables. If it is a non-linear dynamical system with interaction, Takens' theorem is used to delay each other's time using the embedded information. It uses the feature that the embedding can be reconstructed (the time series of the other party can be predicted only from one's own time series).

-@ Nyanko-box learned about Empirical Dynamic Modeling

EDM is also used to clarify the causality between variables.

Causality test: Cross-mapping algorithm A method based on the idea that the manifold of a system reconstructed from a single variable obtains a mapping to the original system. For variables M1, M2M1, M2, if the result of Cross-mapping "converges", it is considered to belong to the same system (causally linked) (Cross-mapping Convergent: CCM). > "Convergence" here means that the cross-mapping ability (ρρ) increases as the library length increases.

As a typical method, if both of the following two tests are significant, it is judged that convergence is significant.

  1. __Kenddall's $ ττ $ test __
  2. __Fisher's $ ΔρZΔρZ $ test __

Try inserting the * pyEDM * module

Follow the instructions on GitHub and try inserting the * pyEDM * module with the following command:

OSX and Linux

Download pyEDM: git clone https://github.com/SugiharaLab/pyEDM
Build cppEDM library: cd pyEDM/cppEDM/src; make
Build and install package: cd ../..; python -m pip install . --user --trusted-host pypi.org

Run * git clone *

Terminal


ocean@AfoGuardMacBook-Pro Desktop % git clone https://github.com/SugiharaLab/pyEDM
Cloning into 'pyEDM'...
remote: Enumerating objects: 130, done.
remote: Counting objects: 100% (130/130), done.
remote: Compressing objects: 100% (84/84), done.
remote: Total 1524 (delta 72), reused 90 (delta 45), pack-reused 1394
Receiving objects: 100% (1524/1524), 22.60 MiB | 3.48 MiB/s, done.
Resolving deltas: 100% (911/911), done.
ocean@AfoGuardMacBook-Pro Desktop %

Run * make *

Terminal


ocean@AfoGuardMacBook-Pro Desktop % cd pyEDM/cppEDM/src; make
g++ -std=c++11 -Wpedantic -Wall -Wextra -Wreorder -O3 -DCCM_THREADED -fPIC -c API.cc
g++ -std=c++11 -Wpedantic -Wall -Wextra -Wreorder -O3 -DCCM_THREADED -fPIC -c CCM.cc
g++ -std=c++11 -Wpedantic -Wall -Wextra -Wreorder -O3 -DCCM_THREADED -fPIC -c Common.cc
g++ -std=c++11 -Wpedantic -Wall -Wextra -Wreorder -O3 -DCCM_THREADED -fPIC -c DateTime.cc
g++ -std=c++11 -Wpedantic -Wall -Wextra -Wreorder -O3 -DCCM_THREADED -fPIC -c EDM.cc
g++ -std=c++11 -Wpedantic -Wall -Wextra -Wreorder -O3 -DCCM_THREADED -fPIC -c EDM_Formatting.cc
g++ -std=c++11 -Wpedantic -Wall -Wextra -Wreorder -O3 -DCCM_THREADED -fPIC -c EDM_Neighbors.cc
g++ -std=c++11 -Wpedantic -Wall -Wextra -Wreorder -O3 -DCCM_THREADED -fPIC -c Eval.cc
g++ -std=c++11 -Wpedantic -Wall -Wextra -Wreorder -O3 -DCCM_THREADED -fPIC -c Multiview.cc
g++ -std=c++11 -Wpedantic -Wall -Wextra -Wreorder -O3 -DCCM_THREADED -fPIC -c Parameter.cc
g++ -std=c++11 -Wpedantic -Wall -Wextra -Wreorder -O3 -DCCM_THREADED -fPIC -c Simplex.cc
g++ -std=c++11 -Wpedantic -Wall -Wextra -Wreorder -O3 -DCCM_THREADED -fPIC -c SMap.cc
ar -rcs libEDM.a API.o CCM.o Common.o DateTime.o EDM.o EDM_Formatting.o EDM_Neighbors.o Eval.o Multiview.o Parameter.o Simplex.o SMap.o
cp libEDM.a ../lib/
ocean@AfoGuardMacBook-Pro src %

(Check resources with * ls *)

Terminal


ocean@AfoGuardMacBook-Pro src % ls
API.cc			Common.h		EDM.h			Eval.cc			Parameter.o		Version.h
API.h			Common.o		EDM.o			Eval.o			SMap.cc			libEDM.a
API.o			DataFrame.h		EDM_Formatting.cc	Multiview.cc		SMap.h			makefile
CCM.cc			DateTime.cc		EDM_Formatting.o	Multiview.h		SMap.o			makefile.windows
CCM.h			DateTime.h		EDM_Neighbors.cc	Multiview.o		Simplex.cc
CCM.o			DateTime.o		EDM_Neighbors.h		Parameter.cc		Simplex.h
Common.cc		EDM.cc			EDM_Neighbors.o		Parameter.h		Simplex.o
ocean@AfoGuardMacBook-Pro src %

Run * pip install * to install the * pyEDM * module

Terminal


ocean@AfoGuardMacBook-Pro src % cd ../..; python -m pip install . --user --trusted-host pypi.org
Processing /Users/ocean/Desktop/pyEDM
Requirement already satisfied: pandas>=0.20.3 in /Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages (from pyEDM==1.7.3.0) (1.1.5)
Requirement already satisfied: matplotlib>=2.2 in /Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages (from pyEDM==1.7.3.0) (3.3.3)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages (from matplotlib>=2.2->pyEDM==1.7.3.0) (2.4.7)
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages (from matplotlib>=2.2->pyEDM==1.7.3.0) (1.3.1)
Requirement already satisfied: numpy>=1.15 in /Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages (from matplotlib>=2.2->pyEDM==1.7.3.0) (1.19.4)
Requirement already satisfied: pillow>=6.2.0 in /Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages (from matplotlib>=2.2->pyEDM==1.7.3.0) (8.0.1)
Requirement already satisfied: python-dateutil>=2.1 in /Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages (from matplotlib>=2.2->pyEDM==1.7.3.0) (2.8.1)
Requirement already satisfied: cycler>=0.10 in /Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages (from matplotlib>=2.2->pyEDM==1.7.3.0) (0.10.0)
Requirement already satisfied: six in /Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages (from cycler>=0.10->matplotlib>=2.2->pyEDM==1.7.3.0) (1.15.0)
Requirement already satisfied: pytz>=2017.2 in /Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages (from pandas>=0.20.3->pyEDM==1.7.3.0) (2020.4)
Collecting pybind11>=2.3
  Using cached pybind11-2.6.1-py2.py3-none-any.whl (188 kB)
Building wheels for collected packages: pyEDM
  Building wheel for pyEDM (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /Users/ocean/.pyenv/versions/3.6.3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/pip-req-build-3lmwjyj2/setup.py'"'"'; __file__='"'"'/private/var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/pip-req-build-3lmwjyj2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/pip-wheel-nxydh55p
       cwd: /private/var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/pip-req-build-3lmwjyj2/
  Complete output (85 lines):
  cp libEDM.a ../lib/
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.15-x86_64-3.6
  creating build/lib.macosx-10.15-x86_64-3.6/pyEDM
  copying pyEDM/__init__.py -> build/lib.macosx-10.15-x86_64-3.6/pyEDM
  copying pyEDM/LoadData.py -> build/lib.macosx-10.15-x86_64-3.6/pyEDM
  copying pyEDM/CoreEDM.py -> build/lib.macosx-10.15-x86_64-3.6/pyEDM
  copying pyEDM/AuxFunc.py -> build/lib.macosx-10.15-x86_64-3.6/pyEDM
  creating build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/Smap_embd_block_3sp_pyEDM.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/sardine_anchovy_sst.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/TentMapNoise_rEDM.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/Multiview_combos_valid.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/circle_noise.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/Smap_circle_pyEDM.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/TentMap_rEDM.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/CCM_anch_sst_cppEDM_valid.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/circle.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/block_3sp.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/Multiview_pred_valid.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/Smplx_embd_block_3sp_pyEDM.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  copying pyEDM/data/Smplx_E3_block_3sp_pyEDM.csv -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/data
  creating build/lib.macosx-10.15-x86_64-3.6/pyEDM/tests
  copying pyEDM/tests/examples.py -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/tests
  copying pyEDM/tests/tests.py -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/tests
  copying pyEDM/tests/smapSolverTest.py -> build/lib.macosx-10.15-x86_64-3.6/pyEDM/tests
  running build_ext
  creating var
  creating var/folders
  creating var/folders/dw
  creating var/folders/dw/kq_8pwps5s771nklvc8krws40000gn
  creating var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include -I/Users/ocean/.pyenv/versions/3.6.3/include/python3.6m -c /var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/tmpta8u7un3.cpp -o var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/tmpta8u7un3.o -std=c++14
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include -I/Users/ocean/.pyenv/versions/3.6.3/include/python3.6m -c /var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/tmpq0p1f2cr.cpp -o var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/tmpq0p1f2cr.o -fvisibility=hidden
  building 'pyBindEDM' extension
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/private/var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/pip-req-build-3lmwjyj2/setup.py", line 212, in <module>
      zip_safe         = False,
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages/setuptools/__init__.py", line 153, in setup
      return distutils.core.setup(**attrs)
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/core.py", line 148, in setup
      dist.run_commands()
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/dist.py", line 955, in run_commands
      self.run_command(cmd)
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages/wheel/bdist_wheel.py", line 299, in run
      self.run_command('build')
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/command/build.py", line 135, in run
      self.run_command(cmd_name)
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages/setuptools/command/build_ext.py", line 79, in run
      _build_ext.run(self)
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/command/build_ext.py", line 339, in run
      self.build_extensions()
    File "/private/var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/pip-req-build-3lmwjyj2/setup.py", line 144, in build_extensions
      build_ext.build_extensions(self)
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/command/build_ext.py", line 448, in build_extensions
      self._build_extensions_serial()
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/command/build_ext.py", line 473, in _build_extensions_serial
      self.build_extension(ext)
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/site-packages/setuptools/command/build_ext.py", line 196, in build_extension
      _build_ext.build_extension(self, ext)
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/command/build_ext.py", line 533, in build_extension
      depends=ext.depends)
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/ccompiler.py", line 566, in compile
      depends, extra_postargs)
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/ccompiler.py", line 341, in _setup_compile
      pp_opts = gen_preprocess_options(macros, incdirs)
    File "/Users/ocean/.pyenv/versions/3.6.3/lib/python3.6/distutils/ccompiler.py", line 1075, in gen_preprocess_options
      pp_opts.append("-I%s" % dir)
    File "/private/var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/pip-req-build-3lmwjyj2/setup.py", line 73, in __str__
      import pybind11
  ModuleNotFoundError: No module named 'pybind11'
  ----------------------------------------
  ERROR: Failed building wheel for pyEDM
  Running setup.py clean for pyEDM
Failed to build pyEDM
Installing collected packages: pybind11, pyEDM
  WARNING: The script pybind11-config is installed in '/Users/ocean/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    Running setup.py install for pyEDM ... done
  DEPRECATION: pyEDM was installed using the legacy 'setup.py install' method, because a wheel could not be built for it. pip 21.0 will remove support for this functionality. A possible replacement is to fix the wheel build issue reported above. You can find discussion regarding this at https://github.com/pypa/pip/issues/8368.
Successfully installed pyEDM-1.7.3.0 pybind11-2.6.1
ocean@AfoGuardMacBook-Pro pyEDM %

Terminal


ocean@AfoGuardMacBook-Pro pyEDM % pwd
/Users/ocean/Desktop/pyEDM
ocean@AfoGuardMacBook-Pro pyEDM % 

Operation check

Usage

Example usage at the python prompt:

>>> import pyEDM
>>> pyEDM.Examples()

python:Python3.6.3


ocean@AfoGuardMacBook-Pro Desktop % cd pyEDM
ocean@AfoGuardMacBook-Pro pyEDM % python
Python 3.6.3 (default, Dec 10 2020, 22:43:16) 
[GCC Apple LLVM 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyEDM
>>> pyEDM.Examples()

(... Various graphs are displayed on a small screen ...)

>>> quit() 

In the above situation, * import pyDEM * can be executed only when the current directory is * pyDEM *.

Recommended Posts

Put the Python implementation module "pyDEM" of the Cross-mapping model that can estimate the non-linear time series causality in the Macbook
A function that measures the processing time of a method in python
[Python] The movement of the decorator that can be understood this time ② The decorator that receives the argument
Maximum likelihood estimation implementation of topic model in python
[python] Get the list of classes defined in the module
Variational Bayesian inference implementation of topic model in python
A reminder about the implementation of recommendations in Python
Put the process to sleep for a certain period of time (seconds) or more in Python
Implementation of quicksort in Python
I compared the calculation time of the moving average written in Python
Python standard module that can be used on the command line
The story of making a module that skips mail with python