| Title: | Structural Forest for the Heterogeneous Newsvendor Model |
|---|---|
| Description: | Implements the structural forest methodology for the heterogeneous newsvendor model. The package provides tools to prepare data, fit honest newsvendor trees and forests, and obtain point and distributional predictions for demand decisions under uncertainty. |
| Authors: | Mengfei Li [aut, cre] |
| Maintainer: | Mengfei Li <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-05-20 04:10:00 UTC |
| Source: | https://github.com/murphylicn/sfhnv |
Fit an SFHNV random forest
build_random_forest( data, honest_ratio = 1, min_size = 50, max_depth = 50, num_trees = 100, feature_choose = "sqrt", parallel = TRUE, approximate = FALSE, max_candidates = 256, leaf_round_digits = 1L )build_random_forest( data, honest_ratio = 1, min_size = 50, max_depth = 50, num_trees = 100, feature_choose = "sqrt", parallel = TRUE, approximate = FALSE, max_candidates = 256, leaf_round_digits = 1L )
data |
A data frame or output from |
honest_ratio |
Ratio of the estimation subsample to the splitting subsample. |
min_size |
Minimum number of observations in each child node. |
max_depth |
Maximum depth of each tree. |
num_trees |
Number of trees to build. |
feature_choose |
Strategy for selecting features at each split. One of
|
parallel |
Logical; if |
approximate |
Logical; if |
max_candidates |
Maximum candidate split points per feature when |
leaf_round_digits |
Rounding control for demand samples in leaf CDF estimation. |
A list of SFHNV trees.
data <- data.frame(x1 = rnorm(200), x2 = rnorm(200), D = rnorm(200), Q = rnorm(200)) forest <- build_random_forest(data, num_trees = 5, min_size = 20)data <- data.frame(x1 = rnorm(200), x2 = rnorm(200), D = rnorm(200), Q = rnorm(200)) forest <- build_random_forest(data, num_trees = 5, min_size = 20)
Converts a data frame with outcome quantities into a numeric matrix representation used by the Structural Forest for the Heterogeneous Newsvendor (SFHNV) estimators.
NW_prepare(data)NW_prepare(data)
data |
A |
A list with prepared matrices (X), outcomes (D, Q), binary indicators (z),
feature names, and the dimensions n and p.
data <- data.frame(x1 = rnorm(100), x2 = rnorm(100), D = rnorm(100), Q = rnorm(100)) prep <- NW_prepare(data) str(prep)data <- data.frame(x1 = rnorm(100), x2 = rnorm(100), D = rnorm(100), Q = rnorm(100)) prep <- NW_prepare(data) str(prep)
Builds an honest tree that estimates the structural parameter of the heterogeneous newsvendor model using the SFHNV algorithm.
NW_Tree( data, honest_ratio = 1, min_size = 50, max_depth = 50, features = NULL, approximate = FALSE, max_candidates = 256, leaf_round_digits = 1L )NW_Tree( data, honest_ratio = 1, min_size = 50, max_depth = 50, features = NULL, approximate = FALSE, max_candidates = 256, leaf_round_digits = 1L )
data |
A data frame or output from |
honest_ratio |
Ratio of the estimation subsample to the splitting subsample. |
min_size |
Minimum number of observations in each child node. |
max_depth |
Maximum depth of the tree. |
features |
Optional subset of features (names or indices) to consider at each split. |
approximate |
Logical; if |
max_candidates |
Maximum candidate split points per feature when |
leaf_round_digits |
Control the rounding of demand samples when fitting leaf CDFs. Use negative values to disable rounding. |
A list representing the fitted tree.
data <- data.frame(x1 = rnorm(200), x2 = rnorm(200), D = rnorm(200), Q = rnorm(200)) tree <- NW_Tree(data, min_size = 20, max_depth = 5) preds <- predict_tree(tree, data)data <- data.frame(x1 = rnorm(200), x2 = rnorm(200), D = rnorm(200), Q = rnorm(200)) tree <- NW_Tree(data, min_size = 20, max_depth = 5) preds <- predict_tree(tree, data)
Predict SFHNV random forest conditional CDF values
predict_cdf_forest( forest, observations, d_values, parallel = TRUE, agg = "mean", trim_prop = 0.05 )predict_cdf_forest( forest, observations, d_values, parallel = TRUE, agg = "mean", trim_prop = 0.05 )
forest |
A list of trees produced by |
observations |
Data frame of new observations. |
d_values |
Scalar or vector of demand thresholds. |
parallel |
Logical; if |
agg |
Aggregation strategy across trees ( |
trim_prop |
Trimming proportion when |
Numeric vector of CDF estimates.
data <- data.frame(x1 = rnorm(100), x2 = rnorm(100), D = rnorm(100), Q = rnorm(100)) forest <- build_random_forest(data, num_trees = 3, min_size = 15) predict_cdf_forest(forest, data, d_values = 0)data <- data.frame(x1 = rnorm(100), x2 = rnorm(100), D = rnorm(100), Q = rnorm(100)) forest <- build_random_forest(data, num_trees = 3, min_size = 15) predict_cdf_forest(forest, data, d_values = 0)
Predict conditional CDF values from an SFHNV tree
predict_cdf_tree(tree, observations, d_values)predict_cdf_tree(tree, observations, d_values)
tree |
An object produced by |
observations |
Data frame of new observations containing the same features as the training data. |
d_values |
Either a scalar demand threshold applied to all observations, or a numeric vector with one value per observation. |
Numeric vector of CDF values.
data <- data.frame(x = rnorm(50), D = rnorm(50), Q = rnorm(50)) tree <- NW_Tree(data, min_size = 10, max_depth = 3) predict_cdf_tree(tree, data, d_values = 0)data <- data.frame(x = rnorm(50), D = rnorm(50), Q = rnorm(50)) tree <- NW_Tree(data, min_size = 10, max_depth = 3) predict_cdf_tree(tree, data, d_values = 0)
Predict SFHNV random forest point estimates
predict_forest(forest, observations, trim_prop = 0.05, parallel = TRUE)predict_forest(forest, observations, trim_prop = 0.05, parallel = TRUE)
forest |
A list of trees produced by |
observations |
Data frame of new observations. |
trim_prop |
Optional trimming proportion used in the robust aggregation. |
parallel |
Logical; if |
Numeric vector of aggregated predictions.
data <- data.frame(x1 = rnorm(100), x2 = rnorm(100), D = rnorm(100), Q = rnorm(100)) forest <- build_random_forest(data, num_trees = 3, min_size = 15) predict_forest(forest, data)data <- data.frame(x1 = rnorm(100), x2 = rnorm(100), D = rnorm(100), Q = rnorm(100)) forest <- build_random_forest(data, num_trees = 3, min_size = 15) predict_forest(forest, data)
Predict SFHNV tree point estimates
predict_tree(tree, observations)predict_tree(tree, observations)
tree |
An object produced by |
observations |
Data frame of new observations containing the same features as the training data. |
Numeric vector of predicted structural parameters.
data <- data.frame(x = rnorm(50), D = rnorm(50), Q = rnorm(50)) tree <- NW_Tree(data, min_size = 10, max_depth = 3) predict_tree(tree, data)data <- data.frame(x = rnorm(50), D = rnorm(50), Q = rnorm(50)) tree <- NW_Tree(data, min_size = 10, max_depth = 3) predict_tree(tree, data)