{ "info": { "author": "Danyang Su", "author_email": "fnosdy@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Science/Research", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Topic :: Scientific/Engineering :: Artificial Intelligence" ], "description": "# linkalman\n\n`linkalman` is a python package that solves linear structural time series models with Gaussian noises. Compared with some other popular Kalman filter packages written in python, linkalman has a combination of several advantages:\n\n - Account for partially and fully incomplete measurements \n - Flexible and convenient model structure\n - Robust and efficient implementation\n - Proper implementation for unknown priors\n - Built-in numerical and EM algorithm\n - Open-source with a comprehensive user manual \n - Modular design with intuitive model specification\n\n### Installation\n`linkalman` requires the following packages to run:\n\n - numpy\n - pandas\n - networkx\n - scipy\n \nTo install `linkalman`, simply use the standard `pip` command:\n\n```sh\n$ pip install linkalman\n```\n### Example\nHere I will provide a simple example using `linkalman`. See [here](https://github.com/DanyangSu/linkalman/tree/master/examples/jupyter_notebooks) for more examples, and [user's manual](https://github.com/DanyangSu/linkalman/blob/master/doc/manual.pdf) for technical details.\n\n```python\nimport pandas as pd\nimport numpy as np\nfrom scipy.optimize import minimize\nfrom linkalman.models import BaseConstantModel as BCM\nimport matplotlib.pyplot as plt\n\n\n# Get data\ndf = pd.read_csv('https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-total-female-births.csv')\ndf['x'] = 1\ndf.set_index('Date', inplace=True)\n```\n\nFirst we define the system dynamics of a Bayesian Structural Time Series (BSTS) model. Here I define a Stochastic linear trend model to extract the trend information from the time series (referring to the example section of [user's manual](https://github.com/DanyangSu/linkalman/blob/master/doc/manual.pdf) for details)\n```python\ndef my_f(theta):\n sig1 = np.exp(theta[0])\n sig2 = np.exp(theta[1])\n sig3 = np.exp(theta[2])\n\n F = np.array([[1, 1], [0, 1]])\n Q = np.array([[sig1, 0], [0, sig2]]) \n R = np.array([[sig3]])\n H = np.array([[1, 0]])\n # Collect system matrices\n M = {'F': F, 'Q': Q, 'H': H, 'R': R}\n\n return M \n```\nNext we define a solver or optimizer, you can choose any solver you prefer. Here I just use `scipy.optimize.minimize`.\n```python\ndef my_solver(param, obj_func, verbose=False, **kwargs):\n obj_ = lambda x: -obj_func(x)\n res = minimize(obj_, param, **kwargs)\n theta_opt = np.array(res.x)\n fval_opt = res.fun\n return theta_opt, fval_opt\n```\nNow we can fit the data. First we initialize the model and feed the system dynamics (`my_f`) and solver (`my_solver`). You may also pass the keyworded arguments to for `my_f` and `my_solver`.\n```python\nmodel = BCM()\nmodel.set_f(my_f)\nmodel.set_solver(my_solver, method='nelder-mead', \n options={'xatol': 1e-8, 'disp': True, 'maxiter': 10000})\ntheta_init = np.random.rand(3)\nmodel.fit(df, theta_init, y_col=['Births'], x_col=['x'], \n method='LLY')\ndf_LLY = model.predict(df)\n```\nThat is it! If you want to do additional work, you can do the following to plot a confidence interval around your predictions.\n```python\ndf_LLY['kf_ub'] = df_LLY.Births_filtered + 1.96 * np.sqrt(df_LLY.Births_fvar)\ndf_LLY['kf_lb'] = df_LLY.Births_filtered - 1.96 * np.sqrt(df_LLY.Births_fvar)\ndf_LLY = df_LLY[df_LLY.index > '1959-01-01']\ndf_LLY.index = pd.to_datetime(df_LLY.index)\n\n# Define plot function\ndef simple_plot(df, col_est, col_actual, col_ub, col_lb, label_est,\n label_actual, title, figsize=(12, 8)):\n ax = plt.figure(figsize=figsize)\n plt.plot(df.index, df[col_est], 'r', label=label_est)\n plt.scatter(df_LLY.index, df[col_actual], s=20, c='b', \n marker='o', label=label_actual)\n plt.fill_between(df.index, df[col_ub], df[col_lb], color='g', alpha=0.2)\n ax.legend(loc='right', fontsize=9)\n plt.title(title, fontsize=22)\n plt.show()\nsimple_plot(df_LLY, 'Births_filtered', 'Births', 'kf_ub', 'kf_lb', \n 'Prediction', 'Births', 'Filtered Births Data')\n```\n\n### License\n\n3-Clause BSD", "description_content_type": "text/markdown", "docs_url": null, "download_url": "https://github.com/DanyangSu/linkalman/archive/v0.11.2.tar.gz", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/DanyangSu/linkalman", "keywords": "kalman,time series,signal,filter", "license": "3-clause BSD", "maintainer": "", "maintainer_email": "", "name": "linkalman", "package_url": "https://pypi.org/project/linkalman/", "platform": "", "project_url": "https://pypi.org/project/linkalman/", "project_urls": { "Download": "https://github.com/DanyangSu/linkalman/archive/v0.11.2.tar.gz", "Homepage": "https://github.com/DanyangSu/linkalman" }, "release_url": "https://pypi.org/project/linkalman/0.11.5/", "requires_dist": null, "requires_python": "", "summary": "Flexible Linear Kalman Filter", "version": "0.11.5" }, "last_serial": 5910247, "releases": { "0.11.5": [ { "comment_text": "", "digests": { "md5": "d5dd376c7b3643f0b6744cc9ba6ea8b8", "sha256": "54e7d4e8082be1aedd7ab9d47f1d15b4932a85cf278de98bbf3df680beb15e02" }, "downloads": -1, "filename": "linkalman-0.11.5.tar.gz", "has_sig": false, "md5_digest": "d5dd376c7b3643f0b6744cc9ba6ea8b8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 25737, "upload_time": "2019-10-01T03:53:44", "url": "https://files.pythonhosted.org/packages/be/e5/77a7e03bd0e87486ef0f7846753efcf63512edf13896ce80a536eceeb073/linkalman-0.11.5.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "d5dd376c7b3643f0b6744cc9ba6ea8b8", "sha256": "54e7d4e8082be1aedd7ab9d47f1d15b4932a85cf278de98bbf3df680beb15e02" }, "downloads": -1, "filename": "linkalman-0.11.5.tar.gz", "has_sig": false, "md5_digest": "d5dd376c7b3643f0b6744cc9ba6ea8b8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 25737, "upload_time": "2019-10-01T03:53:44", "url": "https://files.pythonhosted.org/packages/be/e5/77a7e03bd0e87486ef0f7846753efcf63512edf13896ce80a536eceeb073/linkalman-0.11.5.tar.gz" } ] }