Highly Adaptive Lasso Conditional Density Estimation

Authors: Nima Hejazi, David Benkeser, and Mark van der Laan

What’s haldensify?

The haldensify R package is designed to provide facilities for nonparametric conditional density estimation based on a flexible procedure proposed initially by Dı́az and van der Laan (2011). The core of the implemented methodology involves recovering conditional density estimates by performing pooled hazards regressions so as to assess the conditional hazard that an observed value falls in a given bin over the (conditional) support of the variable of interest. Such conditional density estimates are useful, for example, in causal inference problems in which the generalized propensity score (for continuous-valued exposures) must be estimated (Dı́az and van der Laan 2012, 2018; Dı́az and Hejazi 2020). haldensify implements this condtional density estimation strategy specifically for use only with the highly adaptive lasso (Benkeser and van der Laan 2016; van der Laan 2017; van der Laan and Benkeser 2018; Coyle, Hejazi, and van der Laan 2020; Hejazi, Coyle, and van der Laan 2020).


For standard use, we recommend installing the package from CRAN via


To contribute, install the development version of haldensify from GitHub via remotes:



A simple example illustrates how haldensify may be used to train a highly adaptive lasso model to obtain conditional density estimates:

#> haldensify v0.1.5: Highly Adaptive Lasso Conditional Density Estimation

# simulate data: W ~ U[-4, 4] and A|W ~ N(mu = W, sd = 0.25)
n_train <- 100
w <- runif(n_train, -4, 4)
a <- rnorm(n_train, w, 0.25)

# HAL-based density estimate of A|W
haldensify_fit <- haldensify(
  A = a, W = w,
  n_bins = 10, grid_type = "equal_range",
  lambda_seq = exp(seq(-1, -10, length = 100)),
  # arguments passed to hal9001::fit_hal()
  max_degree = 5, smoothness_orders = 0,
  num_knots = NULL, reduce_basis = 0.05
#> HAL Conditional Density Estimation
#> Number of bins over support of A: 10
#> CV-selected lambda: 0.0016
#> Summary of fitted HAL:
#> Warning in summary.hal9001(x$hal_fit): Coefficients for many lambda exist --
#> Summarizing coefficients corresponding to minimum lambda.
#>          coef               term
#>  1:  5.977489        (Intercept)
#>  2: 10.481606 [ I(bin_id >= 1) ]
#>  3: 10.440778 [ I(W >= -2.371) ]
#>  4: -9.663779 [ I(W >= -3.546) ]
#>  5:  8.965791 [ I(bin_id >= 5) ]
#>  6:  8.621709 [ I(bin_id >= 6) ]
#>  7:  8.621184 [ I(bin_id >= 4) ]
#>  8:  8.299777 [ I(bin_id >= 8) ]
#>  9: -8.253294  [ I(W >= -3.12) ]
#> 10:  8.091661 [ I(bin_id >= 3) ]

We can also visualize the empirical risk (with respect to density loss) in terms of the solution path of the lasso regularization parameter:

# just use the built-in plot method

Finally, we can obtain conditional density estimates from the trained model on the training (or on new) data:

# use the built-in predict method to get predictions
pred_haldensify <- predict(haldensify_fit, new_A = a, new_W = w)
#> [1] 0.8677402 0.4276165 0.4430710 0.5334161 0.8721339 0.6149775

For more details, check out the package vignette on the corresponding pkgdown site.


If you encounter any bugs or have any specific feature requests, please file an issue.


Contributions are very welcome. Interested contributors should consult our contribution guidelines prior to submitting a pull request.


After using the haldensify R package, please cite the following:

      author = {Hejazi, Nima S and Benkeser, David C and {van der Laan},
        Mark J},
      title = {{haldensify}: Highly adaptive lasso conditional density
        estimation },
      year  = {2020},
      howpublished = {\url{https://github.com/nhejazi/haldensify}},
      note = {{R} package version 0.0.5},
      url = {https://doi.org/10.5281/zenodo.3698329},
      doi = {10.5281/zenodo.3698329}


The development of this software was supported in part through grants from the National Library of Medicine (award number T32 LM012417) and the National Institute of Allergy and Infectious Diseases (award number R01 AI074345) of the National Institutess of Health.


Benkeser, David, and Mark J van der Laan. 2016. “The Highly Adaptive Lasso Estimator.” In Proceedings of the International Conference on Data Science and Advanced Analytics. IEEE International Conference on Data Science and Advanced Analytics, 2016:689. NIH Public Access.

Coyle, Jeremy R, Nima S Hejazi, and Mark J van der Laan. 2020. hal9001: The Scalable Highly Adaptive Lasso. https://doi.org/10.5281/zenodo.3558313.

Dı́az, Iván, and Nima S Hejazi. 2020. “Causal Mediation Analysis for Stochastic Interventions.” Journal of the Royal Statistical Society: Series B (Statistical Methodology). https://doi.org/10.1111/rssb.12362.

Dı́az, Iván, and Mark J van der Laan. 2011. “Super Learner Based Conditional Density Estimation with Application to Marginal Structural Models.” The International Journal of Biostatistics 7 (1): 1–20.

———. 2012. “Population Intervention Causal Effects Based on Stochastic Interventions.” Biometrics 68 (2): 541–49.

———. 2018. “Stochastic Treatment Regimes.” In Targeted Learning in Data Science: Causal Inference for Complex Longitudinal Studies, 167–80. Springer Science & Business Media.

Hejazi, Nima S, Jeremy R Coyle, and Mark J van der Laan. 2020. “hal9001: Scalable Highly Adaptive Lasso Regression in R.” Journal of Open Source Software. https://doi.org/10.21105/joss.02526.

van der Laan, Mark J. 2017. “A Generally Efficient Targeted Minimum Loss Based Estimator Based on the Highly Adaptive Lasso.” The International Journal of Biostatistics 13 (2).

van der Laan, Mark J, and David Benkeser. 2018. “Highly Adaptive Lasso (HAL).” In Targeted Learning in Data Science, 77–94. Springer.