Code Monkey home page Code Monkey logo

sparsepolicytree's Introduction

Sparse Policy Tree

An R / Rust Package to implement an exhaustive tree search for policy-learning. Aims to extend and speed up work done with the policytree package.

Usage

There’s just one function, sparse_policy_tree(). Use it just as you would the policy_tree() function from the policytree package.

Trees aren’t guaranteed to be exactly the same as those produced by policytree in that some leaves may be left unpruned (working on this), but they should give the same predictions.

n <- 400
p <- 4
d <- 3
depth <- 2
# Classification task taken from policytree tests
# Continuous X
X <- round(matrix(rnorm(n * p), n, p),2)
colnames(X) <- letters[1:ncol(X)]
Y <- matrix(0, n, d)
best.tree <- policytree:::make_tree(X, depth = depth, d = d)
best.action <- policytree:::predict_test_tree(best.tree, X)
Y[cbind(1:n, best.action)] <- 100 * runif(n)
best.reward <- sum(Y[cbind(1:n, best.action)])

tree <- sparse_policy_tree(X,Y,2)

tree
#> policy_tree object 
#> Tree depth:  2 
#> Actions:  1 2 3 
#> Variable splits: 
#> (1) split_variable: b  split_value: -0.73 
#>   (2) split_variable: a  split_value: -3.36 
#>     (4) * action: 3 
#>     (5) * action: 3 
#>   (3) split_variable: a  split_value: 0.57 
#>     (6) * action: 3 
#>     (7) * action: 2

Installation:

Installing Rust:

You must have Rust installed to compile this package. The rust website provides an excellent installation script that has never caused me any issues.

On Linux, you can install Rust with:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

On Windows, I use the rust installation wizard, found here.

Installing Package from Github:

Once you install rust, you should be able to install the package with:

devtools::install_github("Yale-Medicaid/sparsepolicytree")

Benchmarks:

Below are the results from a series of benchmarks to gauge the speed of the package. These were run on an old 4-core, 8-thread server that I have access to, and I think should be pretty representative of the speed a user can expect to see if they run the algorithm on all cores of a modern data-science laptop.

Number of Observations Number of Distinct Predictor Values Number of Predictors Number of Treatments Time
10^2 2 30 20 0.003s
10^3 2 30 20 0.015s
10^4 2 30 20 0.15s
10^5 2 30 20 1.57s
10^6 2 30 20 17s
10^2 30 30 20 0.052s
10^3 30 30 20 0.240s
10^4 30 30 20 3.041s
10^5 30 30 20 100s
10^6 30 30 20 21 min
10^2 10^2 30 20 0.41s
10^3 10^3 30 20 40s
10^4 10^4 30 20 70 min

sparse_policy_tree is dominant when the number of values a variable can take is small (say, under 200), but performs poorly when the number of unique values is large. For dense variables, you are generally better off using the policytree package, which is more developed,and will return faster while running on a only single core.

Limiting the Number of Threads:

To constrain the number of cores the program uses, you can set the RAYON_NUM_THREADS variable before running the search. At present, this variable is read at the construction of the multithreading thread pool, and so must be set once each R session. In a future version, I’ll work to include a fix so that the number of threads can be included in an argument to the sparse_policy_tree function.

Here’s an example of how to set the function to run on an single core in R:

Sys.setenv(RAYON_NUM_THREADS=1)

sparsepolicytree's People

Contributors

beniaminogreen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

sparsepolicytree's Issues

Prune nodes produced by parallel_policy_tree

parallel_policy_tree currently will not prune nodes branch nodes that recommend same treatment for both of their leaves.

Additionally, the algorithm is allowed to make the same split twice (see image below). This behavior should be eliminated, as it creates a superfluous branch which no units can ever be assigned to.

Image

This does not impact the policy assignments that the trees make, so it's low priority, but it should be fixed.

Windows Compilation Error

Describe the bug
When I use devtools::install_github to install your package, I get the following terminal output:

devtools::install_github("Yale-Medicaid/sparsepolicytree")
Downloading GitHub repo Yale-Medicaid/sparsepolicytree@HEAD
── R CMD build ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
✔  checking for file 'C:\Users\gmoor\AppData\Local\Temp\RtmpGwuz2P\remotes4e7c779a4f87\Yale-Medicaid-sparsepolicytree-682e472/DESCRIPTION'
─  preparing 'sparsepolicytree':
✔  checking DESCRIPTION meta-information ...
─  cleaning src
─  checking for LF line-endings in source and make files and shell scripts
─  checking for empty or unneeded directories
─  building 'sparsepolicytree_0.0.0.9000.tar.gz'
   
Installing package into 'C:/Users/gmoor/AppData/Local/R/win-library/4.3'
(as 'lib' is unspecified)
* installing *source* package 'sparsepolicytree' ...
** using staged installation
** libs
using C compiler: 'gcc.exe (GCC) 12.2.0'
rm -Rf sparsepolicytree.dll ./rust/target/x86_64-pc-windows-gnu/release/libsparsepolicytree.a entrypoint.o
gcc  -I"C:/PROGRA~1/R/R-43~1.1/include" -DNDEBUG     -I"C:/rtools43/x86_64-w64-mingw32.static.posix/include"     -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -c entrypoint.c -o entrypoint.o
mkdir -p ./rust/target/libgcc_mock
cd ./rust/target/libgcc_mock && \
        touch gcc_mock.c && \
        gcc -c gcc_mock.c -o gcc_mock.o && \
        ar -r libgcc_eh.a gcc_mock.o && \
        cp libgcc_eh.a libgcc_s.a
C:\rtools43\x86_64-w64-mingw32.static.posix\bin\ar.exe: creating libgcc_eh.a
# CARGO_LINKER is provided in Makevars.ucrt for R >= 4.2
export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER="x86_64-w64-mingw32.static.posix-gcc.exe" && \
        export LIBRARY_PATH="${LIBRARY_PATH};/c/Users/gmoor/AppData/Local/Temp/Rtmp6N8SkW/R.INSTALL481c4fe821ec/sparsepolicytree/src/./rust/target/libgcc_mock" && \
        cargo +stable-gnu build --target=x86_64-pc-windows-gnu --lib --release --manifest-path=./rust/Cargo.toml --target-dir ./rust/target
   Compiling autocfg v1.1.0
   Compiling winapi-x86_64-pc-windows-gnu v0.4.0
   Compiling winapi v0.3.9
   Compiling crossbeam-utils v0.8.13
   Compiling proc-macro2 v1.0.47
   Compiling cfg-if v1.0.0
   Compiling unicode-ident v1.0.5
   Compiling quote v1.0.21
   Compiling syn v1.0.103
   Compiling scopeguard v1.1.0
   Compiling rayon-core v1.10.1
   Compiling extendr-engine v0.3.1
   Compiling rawpointer v0.2.1                                                                                                                                                   
   Compiling num_cpus v1.14.0
   Compiling matrixmultiply v0.3.2                                                                                                                                               
   Compiling either v1.8.0                                                                                                                                                       
   Compiling extendr-api v0.3.1                                                                                                                                                  
   Compiling paste v1.0.9
   Compiling lazy_static v1.4.0
   Compiling itertools v0.10.5
   Compiling iter_utils v0.1.0
   Compiling num-traits v0.2.15                                                                                                                                                  
   Compiling memoffset v0.7.1                                                                                                                                                    
   Compiling crossbeam-epoch v0.9.12                                                                                                                                             
   Compiling num-integer v0.1.45                                                                                                                                                 
   Compiling binary_search_tree v0.2.2                                                                                                                                           
   Compiling crossbeam-channel v0.5.6
   Compiling crossbeam-deque v0.8.2
   Compiling libR-sys v0.3.0
   Compiling num-complex v0.4.2
   Compiling ordered-float v3.4.0
   Compiling rayon v1.6.0
   Compiling ndarray v0.15.6                                                                                                                                                     
   Compiling extendr-macros v0.3.1
   Compiling sparsepolicytree v0.1.0 (C:\Users\gmoor\AppData\Local\Temp\Rtmp6N8SkW\R.INSTALL481c4fe821ec\sparsepolicytree\src\rust)
    Finished release [optimized] target(s) in 25.75s
gcc -shared -s -static-libgcc -o sparsepolicytree.dll tmp.def entrypoint.o -L./rust/target/x86_64-pc-windows-gnu/release -lsparsepolicytree -lws2_32 -ladvapi32 -luserenv -lbcrypt -LC:/rtools43/x86_64-w64-mingw32.static.posix/lib/x64 -LC:/rtools43/x86_64-w64-mingw32.static.posix/lib -LC:/PROGRA~1/R/R-43~1.1/bin/x64 -lR
C:\rtools43\x86_64-w64-mingw32.static.posix\bin/ld.exe: ./rust/target/x86_64-pc-windows-gnu/release/libsparsepolicytree.a(std-ca5208825e97b4ba.std.687851ba-cgu.0.rcgu.o): in function `std::sys::windows::fs::open_link_no_reparse':
/rustc/90c541806f23a127002de5b4038be731ba1458ca/library\std\src\sys\windows/fs.rs:800: undefined reference to `NtCreateFile'
C:\rtools43\x86_64-w64-mingw32.static.posix\bin/ld.exe: /rustc/90c541806f23a127002de5b4038be731ba1458ca/library\std\src\sys\windows/fs.rs:829: undefined reference to `RtlNtStatusToDosError'
C:\rtools43\x86_64-w64-mingw32.static.posix\bin/ld.exe: ./rust/target/x86_64-pc-windows-gnu/release/libsparsepolicytree.a(std-ca5208825e97b4ba.std.687851ba-cgu.0.rcgu.o): in function `std::sys::windows::handle::Handle::synchronous_read':
/rustc/90c541806f23a127002de5b4038be731ba1458ca/library\std\src\sys\windows/handle.rs:241: undefined reference to `NtReadFile'
C:\rtools43\x86_64-w64-mingw32.static.posix\bin/ld.exe: /rustc/90c541806f23a127002de5b4038be731ba1458ca/library\std\src\sys\windows/handle.rs:272: undefined reference to `RtlNtStatusToDosError'
C:\rtools43\x86_64-w64-mingw32.static.posix\bin/ld.exe: ./rust/target/x86_64-pc-windows-gnu/release/libsparsepolicytree.a(std-ca5208825e97b4ba.std.687851ba-cgu.0.rcgu.o): in function `std::sys::windows::handle::Handle::synchronous_write':
/rustc/90c541806f23a127002de5b4038be731ba1458ca/library\std\src\sys\windows/handle.rs:290: undefined reference to `NtWriteFile'
C:\rtools43\x86_64-w64-mingw32.static.posix\bin/ld.exe: /rustc/90c541806f23a127002de5b4038be731ba1458ca/library\std\src\sys\windows/handle.rs:318: undefined reference to `RtlNtStatusToDosError'
collect2.exe: error: ld returned 1 exit status
no DLL was created
ERROR: compilation failed for package 'sparsepolicytree'
* removing 'C:/Users/gmoor/AppData/Local/R/win-library/4.3/sparsepolicytree'
Warning message:
In i.p(...) :
  installation of package 'C:/Users/gmoor/AppData/Local/Temp/RtmpGwuz2P/file4e7c16885b1b/sparsepolicytree_0.0.0.9000.tar.gz' had non-zero exit status

To Reproduce
Steps to reproduce the behavior:

  1. Install R 4.3.1
  2. Install rustc 1.70.0 using rustup 1.26.0
  3. Install devtools 2.4.5
  4. Run devtools::install_github("Yale-Medicaid/sparsepolicytree")

Expected behavior
It should install the sparsepolicytree package

Desktop:

  • OS: Windows 11
  • Version: 22H2

Additional context
This issue seems to match the symptoms documented here, which makes me believe it's an issue with the package rather than an issue with my environment.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.