{"id":18016269,"url":"https://github.com/egarpor/sdetorus","last_synced_at":"2025-03-26T18:32:06.027Z","repository":{"id":47665557,"uuid":"123150356","full_name":"egarpor/sdetorus","owner":"egarpor","description":"Statistical tools for toroidal diffusions. Software companion for \"Langevin diffusions on the torus: estimation and applications\"","archived":false,"fork":false,"pushed_at":"2024-03-01T11:08:24.000Z","size":47399,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-30T06:12:11.292Z","etag":null,"topics":["circular-statistics","inference","maximum-likelihood","r","reproducible-research","sde","statistics","toroidal-data"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/egarpor.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-02-27T15:40:53.000Z","updated_at":"2024-05-30T21:40:19.000Z","dependencies_parsed_at":"2024-03-01T12:25:37.182Z","dependency_job_id":"9dd90413-5395-4eff-b6a7-32e42cd00209","html_url":"https://github.com/egarpor/sdetorus","commit_stats":{"total_commits":33,"total_committers":3,"mean_commits":11.0,"dds":"0.48484848484848486","last_synced_commit":"3b0be93c4ea452bb664df797a0f4f3c5d1b1d30e"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egarpor%2Fsdetorus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egarpor%2Fsdetorus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egarpor%2Fsdetorus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egarpor%2Fsdetorus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/egarpor","download_url":"https://codeload.github.com/egarpor/sdetorus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245712958,"owners_count":20660325,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["circular-statistics","inference","maximum-likelihood","r","reproducible-research","sde","statistics","toroidal-data"],"created_at":"2024-10-30T04:16:54.785Z","updated_at":"2025-03-26T18:32:05.348Z","avatar_url":"https://github.com/egarpor.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput:\n  md_document:\n    variant: markdown_github\n---\n\n```{r, echo = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE, comment = \"#\u003e\", fig.path = \"README/README-\",\n  message = FALSE, warning = FALSE, fig.asp = 1, fig.align = 'center'\n)\n```\n\nsdetorus\n========\n\n```{r, echo = FALSE, results = 'asis'}\ncat(\n  badger::badge_license(license = \"GPLv3\", color = \"blue\",\n                        url = \"https://www.gnu.org/licenses/gpl-3.0\"),\n  badger::badge_github_actions(action = \"R-CMD-check\"),\n  badger::badge_cran_release(color = \"green\"),\n  badger::badge_cran_download(pkg = NULL, type = \"grand-total\"),\n  badger::badge_cran_download(pkg = NULL, type = \"last-month\")\n)\n```\n\n\u003c!-- \u003cimg src=\"\" alt=\"sdetorus  hexlogo\" align=\"right\" width=\"200\" style=\"padding: 0 15px; float: right;\"/\u003e --\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg style=\"width:90%;\" id=\"sdetorus\" src=\"https://raw.githubusercontent.com/egarpor/sdetorus/master/logo/sdetorus-small.gif\"\u003e\n\u003cbr\u003e\n\u003ci\u003eTransition probability density of the Langevin diffusion guided by the \"sdetorus\" density\u003c/i\u003e\n\u003c/p\u003e\n\n## Overview\n\nThis library provides statistical tools for estimation of toroidal diffusions. It is the package companion for the paper *Langevin diffusions on the torus: estimation and applications* (García-Portugués et al., 2019).\n\n## Install\n\nGet the released version from CRAN:\n\n```{r, install-CRAN, eval = FALSE}\n# Install the package\ninstall.packages(\"sdetorus\")\n\n# Load package\nlibrary(sdetorus)\n```\n\nAlternatively, get the latest version from GitHub:\n\n```{r, install-GitHub, eval = FALSE}\n# Install the package\nlibrary(devtools)\nSys.setenv(\"PKG_CXXFLAGS\" = \"-std=c++11\")\nSys.setenv(\"PKG_LIBS\" = \"-llapack\")\ninstall_github(\"egarpor/sdetorus\")\n\n# Load package\nlibrary(sdetorus)\n```\n\n```{r, load, echo = FALSE}\n# Load package\nlibrary(sdetorus)\n```\n\n## Usage\n\n### Example 1: simulation of diffusion trajectories\n\n```{r, example1}\n# Load package\nlibrary(sdetorus)\n\n## vM diffusion in 1D\n\n# Initial points\nnx0 \u003c- 50\nx0 \u003c- seq(-pi, pi, l = nx0 + 1)[-(nx0 + 1)]\nplot(rep(0, nx0), x0, pch = 16, col = rainbow(nx0), xlim = c(0, 1),\n     xlab = expression(t), ylab = expression(Theta[t]), axes = FALSE)\ntorusAxis(2)\naxis(1)\n\n# Trajectories of the vM diffusion\nN \u003c- 100\nmu \u003c- 0\nset.seed(12345678)\nsamp \u003c- euler1D(x0 = x0, mu = mu, alpha = 3, sigma = 1, N = N,\n                delta = 0.01, type = 2)\nabline(h = mu)\ntt \u003c- seq(0, 1, l = N + 1)\nfor (i in 1:nx0) linesCirc(tt, samp[i, ], col = rainbow(nx0)[i])\npoints(rep(1, nx0), samp[, N + 1], pch = 16, col = rainbow(nx0))\n\n## WN diffusion in 2D\n\n# Initial points\nnx0 \u003c- 10\nx0 \u003c- seq(-pi, pi, l = nx0)\nx0 \u003c- as.matrix(expand.grid(x0, x0))\nnx0 \u003c- nx0^2\nplot(x0[, 1], x0[, 2], xlim = c(-pi, pi), ylim = c(-pi, pi), pch = 16,\n     col = rainbow(nx0), xlab = expression(Theta[list(1,t)]),\n     ylab = expression(Theta[list(2,t)]), axes = FALSE)\ntorusAxis()\n\n# Trajectories of the WN diffusion\nN \u003c- 100\nset.seed(12345678)\nsamp \u003c- euler2D(x0 = x0, mu = c(0, 0), A = rbind(2:1, 1:2),\n                sigma = c(1, 1), N = N, delta = 0.01, type = 1)\nfor (i in 1:nx0) linesTorus(samp[i, 1, ], samp[i, 2, ],\n                            col = rainbow(nx0, alpha = 0.5)[i])\n```\n\n### Example 2: computation of transition probability densities\n\n```{r, example2}\n# Load package\nlibrary(sdetorus)\n\n## Cauchy diffusion in 1D\n\n# Drift\nMx \u003c- 1e3\nx \u003c- seq(-pi, pi, l = Mx + 1)[-c(Mx + 1)]\nmu \u003c- 0\nb \u003c- driftJp(x = x, alpha = 1, mu = mu, psi = -1)\n\n# Squared diffusion coefficient\nsigma2 \u003c- rep(1, Mx)\n\n# Initial condition concentrated at x0\nx0 \u003c- pi - 0.25\nsdInitial \u003c- 0.01\nu0 \u003c- cbind(dWn1D(x = x, mu = x0, sigma = sdInitial))\nperiodicTrapRule1D(u0)\n\n# Tpd for times t = 0.0, 0.1, 0.2, ..., 5.0\nN \u003c- 5e3\nn \u003c- seq(0, N, by = 100)\ntpd \u003c- crankNicolson1D(u0 = u0, b = b, sigma2 = sigma2, N = n, deltat = 2 / N,\n                       Mx = Mx, deltax = 2 * pi / Mx)\nmatplot(x, tpd, type = \"l\", col = matlab.like.colorRamps(length(n), two = TRUE),\n        lty = 1, ylim = c(0, 2), axes = FALSE)\nabline(v = c(x0, mu), col = c(4, 2), lwd = 2)\ntorusAxis(1)\naxis(2)\n\n## WN diffusion in 2D\n\n# Function for computing and plotting a tpd\nplotTpd \u003c- function(t) {\n\n  Mx \u003c- 150\n  tpd \u003c- dTpdPde2D(Mx = Mx, My = Mx, x0 = c(pi / 2, -pi), t = t,\n                   alpha = c(1, 1, 0), mu = c(0, 0), sigma = c(2, 1),\n                   type = \"WN\", Mt = ceiling(1e2 * t), sdInitial = 0.1)\n  x \u003c- seq(-pi, pi, l = Mx)\n  plotSurface2D(x = x, y = x, z = pmin(tpd, 0.25), axes = FALSE,\n                xlab = expression(theta[1]), ylab = expression(theta[2]),\n                main = paste(\"t =\", t), levels = seq(0, 0.25, l = 20))\n  torusAxis()\n  points(c(0, pi / 2, pi / 2), c(0, -pi, pi), pch = 16, cex = 1)\n\n}\n\n# Tpd at different times\npar(mfrow = c(2, 2))\nplotTpd(t = 0.2)\nplotTpd(t = 0.5)\nplotTpd(t = 1.0)\nplotTpd(t = 3.0)\n```\n\n### Example 3: approximate maximum likelihood estimation in 1D\n\n```{r, example3}\n# Load package\nlibrary(sdetorus)\n\n## WN diffusion in 1D\n\n# Sample\nset.seed(12345678)\nN \u003c- 500\ndelta \u003c- 0.1\nsamp \u003c- rTrajWn1D(x0 = 0, alpha = 0.5, mu = pi, sigma = 1, N = N, delta = delta)\ntt \u003c- seq(0, N * delta, by = delta)\nplot(tt, samp, type = \"n\", ylim = c(-pi, pi), xlab = expression(t),\n     ylab = expression(Theta[t]), axes = FALSE)\nlinesCirc(x = tt, y = samp)\naxis(1)\ntorusAxis(2)\n\n# Drift and diffusion\nb \u003c- function(x, pars) driftWn1D(x = x, alpha = pars[1], mu = pars[2],\n                                 sigma = pars[3])\nb1 \u003c- function(x, pars, h = 1e-4) {\n  l \u003c- length(x)\n  res \u003c- driftWn1D(x = c(x + h, x, x - h), alpha = pars[1], mu = pars[2],\n                   sigma = pars[3])\n  drop(res[1:l] - 2 * res[(l + 1):(2 * l)] + res[(2 * l + 1):(3 * l)])/(h^2)\n}\nsigma2 \u003c- function(x, pars) rep(pars[3]^2, length(x))\n\n# Common optimization parameters\nstart \u003c- 1:3\nlow \u003c- c(0, -pi, 0)\nup \u003c- c(100, pi, 100)\n\n# MLE by numerical solution of PDE\nest1 \u003c- mlePde1D(data = samp, delta = delta, b = b, sigma2 = sigma2, Mx = 5e2,\n                 sdInitial = 0.05, start = start, lower = low, upper = up)\n\n# Euler pseudo-likelihood\nest2 \u003c- psMle(data = samp, delta = delta, method = \"E\", b = b, sigma2 = sigma2,\n              start = start, lower = low, upper = up)\n\n# Shoji--Ozaki pseudo-likelihood\nest3 \u003c- psMle(data = samp, delta = delta, method = \"SO\", b = b, b1 = b1,\n              sigma2 = sigma2, start = start, lower = low, upper = up)\n\n# Approximate MLE based on the WOU process\nest4 \u003c- approxMleWn1D(data = samp, delta = delta, start = start, lower = low,\n                      upper = up)\n\n# Comparison\nest1$par\nest2$par\nest3$par\nest4$par\n```\n\n\u003c!--\n```{r, example4, message=FALSE, fig.asp=1, fig.align='center', cache=TRUE, cache.path=\"README/\"}\n## WN diffusion in 2D\n\n# Sample\nset.seed(12345678)\nN \u003c- 5e2\ndelta \u003c- 0.1\nsamp \u003c- rTrajWn2D(x0 = c(0, 0), alpha = c(0.5, 0.5, 0), mu = c(pi, pi),\n                  sigma = c(1.5, 1.5), N = N, delta = delta)\nplot(samp[, 1], samp[, 2], type = \"n\", ylim = c(-pi, pi),\n     xlab = expression(Theta[list(1,t)]), ylab = expression(Theta[list(2,t)]),\n     axes = FALSE)\nlinesTorus(x = samp[, 1], y = samp[, 2], col = rainbow(N + 1))\ntorusAxis()\n\n# Drift and diffusion\nb \u003c- function(x, pars) driftWn2D(x = x, A = alphaToA(alpha = pars[1:3],\n                                                     sigma = pars[6:7]),\n                                 mu = pars[4:5], sigma = pars[6:7])\njac.b \u003c- function(x, pars, h = 1e-4) {\n  l \u003c- nrow(x)\n  res \u003c- b(x = rbind(cbind(x[, 1] + h, x[, 2]),\n                     cbind(x[, 1] - h, x[, 2]),\n                     cbind(x[, 1], x[, 2] + h),\n                     cbind(x[, 1], x[, 2] - h)), pars = pars)\n  cbind(res[1:l, ] - res[(l + 1):(2 * l), ],\n        res[2 * l + 1:l, ] - res[2 * l + (l + 1):(2 * l), ]) / (2 * h)\n}\nsigma2 \u003c- function(x, pars) matrix(pars[6:7]^2, nrow = length(x) / 2L, ncol = 2)\n\n# Common optimization parameters\nstart \u003c- c(1, 1, 0.5, 0, 0, 1, 1)\nlow \u003c- c(0.01, 0.01, -25, -pi, -pi, 0.01, 0.01)\nup \u003c- c(25, 25, 25, pi, pi, 25, 25)\n\n# Euler pseudo-likelihood\nest1 \u003c- psMle(data = samp, delta = delta, method = \"E\", b = b, sigma2 = sigma2,\n              start = start, lower = low, upper = up)\n\n# Shoji--Ozaki pseudo-likelihood\nest2 \u003c- psMle(data = samp, delta = delta, method = \"SO\", b = b, jac.b = jac.b,\n              sigma2 = sigma2, start = start, lower = low, upper = up)\n\n# Approximate MLE based on the WOU process\nest3 \u003c- approxMleWn2D(data = samp, delta = delta, start = start, lower = low,\n                      upper = up)\n\n# Comparison\nest1$par\nest2$par\nest3$par\n```\n--\u003e\n\n## Replicability of García-Portugués et al. (2019)\n\nThe directories [`/MD`](https://github.com/egarpor/data-langevintorus/tree/master/MD) and [`/simulation`](https://github.com/egarpor/data-langevintorus/tree/master/simulation) in the [data-langevintorus](https://github.com/egarpor/data-langevintorus) repository contain the scripts used in the empirical analyses of the aforementioned paper, as well as their `.RData` outputs.\n\n## References\n\nGarcía-Portugués, E., Sørensen, M., Mardia, K. V., and Hamelryck, T. (2019). Langevin diffusions on the torus: estimation and applications. *Statistics and Computing*, 29(2):1--22. [doi:10.1007/s11222-017-9790-2](https://doi.org/10.1007/s11222-017-9790-2).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fegarpor%2Fsdetorus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fegarpor%2Fsdetorus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fegarpor%2Fsdetorus/lists"}