diff --git a/DESCRIPTION b/DESCRIPTION index 6d8025fb..d3cacb1f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: fastcpd Type: Package Title: Fast Change Point Detection Based on Dynamic Programming with Pruning with Sequential Gradient Descent -Version: 0.2.8 +Version: 0.2.9 Authors@R: c( person("Xingchi", "Li", email = "anthony.li@stat.tamu.edu", role = c("aut", "cre", "cph"), diff --git a/NEWS.md b/NEWS.md index d0734a9b..65a5c487 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# fastcpd 0.2.9 + +* Fix Poisson regression bug related to `lfactorial`. + # fastcpd 0.2.8 * Make penalized linear regression estimated coefficients output sparse. diff --git a/README.Rmd b/README.Rmd index c80e878a..dc6fdea4 100644 --- a/README.Rmd +++ b/README.Rmd @@ -28,7 +28,7 @@ find change points in a fast manner. It is easy to install and extensible to all kinds of change point problems with a user specified cost function apart from the built-in cost functions. -If you'd like to learn how to use the fastcpd effectively, please refer to the following references: +If you'd like to learn how to use the fastcpd effectively, please refer to: * [Sequential Gradient Descent and Quasi-Newton's Method for Change-Point Analysis](https://proceedings.mlr.press/v206/zhang23b.html) @@ -45,7 +45,11 @@ install.packages("fastcpd") pak::pak("doccstat/fastcpd") ``` -If you're compiling from source, you can run `pak::pkg_system_requirements("fastcpd")`, to see the complete set of system packages needed on your machine. +If you're compiling from source, you can run +`pak::pkg_system_requirements("fastcpd")`, to see the complete set of system +packages needed on your machine. + +[documentation & examples](https://fastcpd.xingchi.li/reference/fastcpd.html#ref-examples) ## Dependency @@ -58,10 +62,6 @@ If you're compiling from source, you can run `pak::pkg_system_requirements("fast * [glmnet](https://glmnet.stanford.edu/), for penalized regression. * [ggplot2](https://github.com/tidyverse/ggplot2), for data visualization. -## Documentation - -[documentation & examples](https://fastcpd.xingchi.li/reference/fastcpd.html#ref-examples) - ## Contact us Encountered a bug or unintended behavior? diff --git a/README.md b/README.md index 25b278a6..f4a1c5b4 100644 --- a/README.md +++ b/README.md @@ -1,53 +1,72 @@ - - # fastcpd -[![CRAN status](https://www.r-pkg.org/badges/version/fastcpd)](https://cran.r-project.org/package=fastcpd) [![R-CMD-check](https://github.com/doccstat/fastcpd/workflows/R-CMD-check/badge.svg)](https://github.com/doccstat/fastcpd/actions) [![Codecov test coverage](https://codecov.io/gh/doccstat/fastcpd/branch/main/graph/badge.svg)](https://app.codecov.io/gh/doccstat/fastcpd?branch=main) [![Last Commit](https://img.shields.io/github/last-commit/doccstat/fastcpd)](https://github.com/doccstat/fastcpd) [![r-universe](https://doccstat.r-universe.dev/badges/fastcpd)](https://doccstat.r-universe.dev) +[![CRAN +status](https://www.r-pkg.org/badges/version/fastcpd)](https://cran.r-project.org/package=fastcpd) +[![R-CMD-check](https://github.com/doccstat/fastcpd/workflows/R-CMD-check/badge.svg)](https://github.com/doccstat/fastcpd/actions) +[![Codecov test +coverage](https://codecov.io/gh/doccstat/fastcpd/branch/main/graph/badge.svg)](https://app.codecov.io/gh/doccstat/fastcpd?branch=main) +[![Last +Commit](https://img.shields.io/github/last-commit/doccstat/fastcpd)](https://github.com/doccstat/fastcpd) +[![r-universe](https://doccstat.r-universe.dev/badges/fastcpd)](https://doccstat.r-universe.dev) ## Overview -The fastcpd (**fast** **c**hange **p**oint **d**etection) is a fast implmentation of change point detection methods in R. The **fastcpd** package is designed to find change points in a fast manner. It is easy to install and extensible to all kinds of change point problems with a user specified cost function apart from the built-in cost functions. +The fastcpd (**fast** **c**hange **p**oint **d**etection) is a fast +implmentation of change point detection methods in R. The **fastcpd** +package is designed to find change points in a fast manner. It is easy +to install and extensible to all kinds of change point problems with a +user specified cost function apart from the built-in cost functions. -If you'd like to learn how to use the fastcpd effectively, please refer to the following references: +If you’d like to learn how to use the fastcpd effectively, please refer +to: -- [Sequential Gradient Descent and Quasi-Newton's Method for Change-Point Analysis](https://proceedings.mlr.press/v206/zhang23b.html) +- [Sequential Gradient Descent and Quasi-Newton’s Method for + Change-Point + Analysis](https://proceedings.mlr.press/v206/zhang23b.html) ## Installation -``` {.r} +``` r # Install from CRAN, (not yet available) install.packages("fastcpd") ``` -``` {.r} +``` r # Install the development version from GitHub # install.packages("pak") pak::pak("doccstat/fastcpd") ``` -If you're compiling from source, you can run `pak::pkg_system_requirements("fastcpd")`, to see the complete set of system packages needed on your machine. +If you’re compiling from source, you can run +`pak::pkg_system_requirements("fastcpd")`, to see the complete set of +system packages needed on your machine. + +[documentation & +examples](https://fastcpd.xingchi.li/reference/fastcpd.html#ref-examples) ## Dependency `library(fastcpd)` will load the following packages: -- [Rcpp](https://github.com/RcppCore/Rcpp), for C++ source code compilation. -- [RcppArmadillo](https://github.com/RcppCore/RcppArmadillo), for fast linear algebra. -- [fastglm](https://github.com/jaredhuling/fastglm), for fast generalized linear models. -- [DescTools](https://github.com/AndriSignorell/DescTools), for Winsorizing Poisson data. -- [glmnet](https://glmnet.stanford.edu/), for penalized regression. -- [ggplot2](https://github.com/tidyverse/ggplot2), for data visualization. - -## Documentation - -[documentation & examples](https://fastcpd.xingchi.li/reference/fastcpd.html#ref-examples) +- [Rcpp](https://github.com/RcppCore/Rcpp), for C++ source code + compilation. +- [RcppArmadillo](https://github.com/RcppCore/RcppArmadillo), for fast + linear algebra. +- [fastglm](https://github.com/jaredhuling/fastglm), for fast + generalized linear models. +- [DescTools](https://github.com/AndriSignorell/DescTools), for + Winsorizing Poisson data. +- [glmnet](https://glmnet.stanford.edu/), for penalized regression. +- [ggplot2](https://github.com/tidyverse/ggplot2), for data + visualization. ## Contact us Encountered a bug or unintended behavior? -1. File a ticket at [GitHub Issues](https://github.com/doccstat/fastcpd/issues). +1. File a ticket at [GitHub + Issues](https://github.com/doccstat/fastcpd/issues). 2. Contact the developers specified in [DESCRIPTION](/DESCRIPTION). diff --git a/src/fastcpd.cc b/src/fastcpd.cc index 006976f3..108e43e7 100755 --- a/src/fastcpd.cc +++ b/src/fastcpd.cc @@ -94,8 +94,17 @@ Rcpp::List negative_log_likelihood( arma::vec y = data.col(0); arma::mat x = data.cols(1, data.n_cols - 1); arma::colvec u = x * theta_nonnull; - Rcpp::NumericVector y_factorial = Rcpp::lfactorial(Rcpp::wrap(y)); - return Rcpp::List::create(Rcpp::Named("value") = arma::accu(-y % u + arma::exp(u) + arma::vec(y_factorial.begin(), y_factorial.size(), false))); + + arma::colvec y_factorial(y.n_elem); + for (unsigned int i = 0; i < y.n_elem; i++) { + double log_factorial = 0; + for (int j = 1; j <= y(i); ++j) { + log_factorial += std::log(j); + } + y_factorial(i) = log_factorial; + } + + return Rcpp::List::create(Rcpp::Named("value") = arma::accu(-y % u + arma::exp(u) + y_factorial)); } }