README.md 2.34 KB
Newer Older
davidkep's avatar
davidkep committed
1
# nsoptim R Package
davidkep's avatar
davidkep committed
2 3
![Languages: R/C++](https://img.shields.io/badge/language-R%2FC%2B%2B11-blue)
![Lifecycle: Experimental](https://img.shields.io/badge/lifecycle-experimental-orange)
davidkep's avatar
davidkep committed
4

davidkep's avatar
davidkep committed
5 6 7
A **experimental** C++ template library for non-smooth optimization.
The library is wrapped in an R package to facilitate its use in other R packages.
The goal of this library is to streamline the use of modern, fast algorithms for optimization of non-smooth functions.
davidkep's avatar
davidkep committed
8

davidkep's avatar
davidkep committed
9
Due to the early stage of the library, the interface might change considerably in the next few iterations.
davidkep's avatar
davidkep committed
10

davidkep's avatar
davidkep committed
11 12
The C++ header files are in [inst/include](inst/include) and can be used from within other R packages by adding `nsoptim`
to the `LinkingTo` field in the package's DESCRIPTION file.
davidkep's avatar
davidkep committed
13

davidkep's avatar
davidkep committed
14 15 16 17 18 19 20 21
## Example Usage

```cpp

#include <memory>
#include <nsoptim.hpp>  // This also includes the armadillo library

// Alias for a linearized ADMM optimizer operating on the standard LS regression loss and an EN penalty using a dense coefficient vector.
22
using LinearizedAdmmOptimizer = nsoptim::LinearizedAdmmOptimizer<nsoptim::LsRegressionLoss, nsoptim::EnPenalty, nsoptim::RegressionCoefficients<arma::vec>>;
davidkep's avatar
davidkep committed
23 24 25 26 27

typename LinearizedAdmmOptimizer::Coefficients Foo() {
  // Generate dummy data with 100 observations and 10 predictors.
  auto data = std::make_shared<nsoptim::PredictorResponseData>(arma::randn(100, 10), arma::randn(10));

28
  nsoptim::LsRegressionLoss loss(data);  // Create a LS loss function object with the generated data
davidkep's avatar
davidkep committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  nsoptim::EnPenalty penalty1(0.5, 2.4)  // Create an EN penalty function object with alpha=0.5 and lambda=2.4
  nsoptim::EnPenalty penalty2(0.5, 1.5)  // Create an EN penalty function object with alpha=0.5 and lambda=1.5

  // Create an optimizer for the given loss and penalty function, using default parameters for the ADMM algorithm.
  LinearizedAdmmOptimizer optim(loss, penalty1);

  // Compute the optimum for `penalty1`, starting at the 0-vector.
  typename LinearizedAdmmOptimizer::Optimum optimum = optim.Optimize();

  // Change the penalty to `penalty2`.
  optim.penalty(penalty2);
  // Compute the optimum for `penalty2`, starting at the optimum for `penalty1`.
  typename LinearizedAdmmOptimizer::Optimum optimum = optim.Optimize();

  // Only return the coefficients.
  return optimum.coefs;
}
```

davidkep's avatar
davidkep committed
48
## Documentation
davidkep's avatar
davidkep committed
49
The documentation for the library is available at https://nsoptim.readthedocs.org.