/*! @file det.txt @author Parikshit Ram @brief Tutorial for how to perform density estimation with Density Estimation Trees (DET). @page dettutorial Density Estimation Tree (DET) tutorial @section intro_det_tut Introduction DETs perform the unsupervised task of density estimation using decision trees. Using a trained density estimation tree (DET), the density at any particular point can be estimated very quickly (O(log n) time, where n is the number of points the tree is built on). The details of this work is presented in the following paper: @code @inproceedings{ram2011density, title={Density estimation trees}, author={Ram, P. and Gray, A.G.}, booktitle={Proceedings of the 17th ACM SIGKDD International Conference on Knowledge Discovery and Data Mining}, pages={627--635}, year={2011}, organization={ACM} } @endcode \b mlpack provides: - a \ref cli_det_tut "simple command-line executable" to perform density estimation and related analyses using DETs - a \ref dtree_det_tut "generic C++ class (DTree)" which provides various functionality for the DETs - a set of functions in the namespace \ref dtutils_det_tut "mlpack::det" to perform cross-validation for the task of density estimation with DETs @section toc_det_tut Table of Contents A list of all the sections this tutorial contains. - \ref intro_det_tut - \ref toc_det_tut - \ref cli_det_tut - \ref cli_ex1_de_tut - \ref cli_ex2_de_test_tut - \ref cli_ex4_de_vi_tut - \ref cli_ex6_de_save - \ref cli_ex7_de_load - \ref dtree_det_tut - \ref dtree_pub_func_det_tut - \ref dtutils_det_tut - \ref dtutils_util_funcs - \ref further_doc_det_tut @section cli_det_tut Command-Line mlpack_det The command line arguments of this program can be viewed using the \c -h option: @code $ mlpack_det -h Density Estimation With Density Estimation Trees This program performs a number of functions related to Density Estimation Trees. The optimal Density Estimation Tree (DET) can be trained on a set of data (specified by --training_file or -t) using cross-validation (with number of folds specified by --folds). This trained density estimation tree may then be saved to a model file with the --output_model_file (-M) option. The variable importances of each dimension may be saved with the --vi_file (-i) option, and the density estimates on each training point may be saved to the file specified with the --training_set_estimates_file (-e) option. This program also can provide density estimates for a set of test points, specified in the --test_file (-T) file. The density estimation tree used for this task will be the tree that was trained on the given training points, or a tree stored in the file given with the --input_model_file (-m) parameter. The density estimates for the test points may be saved into the file specified with the --test_set_estimates_file (-E) option. Options: --folds (-f) [int] The number of folds of cross-validation to perform for the estimation (0 is LOOCV) Default value 10. --help (-h) Default help info. --info [string] Get help on a specific module or option. Default value ''. --input_model_file (-m) [string] File containing already trained density estimation tree. Default value ''. --max_leaf_size (-L) [int] The maximum size of a leaf in the unpruned, fully grown DET. Default value 10. --min_leaf_size (-l) [int] The minimum size of a leaf in the unpruned, fully grown DET. Default value 5. --output_model_file (-M) [string] File to save trained density estimation tree to. Default value ''. --test_file (-T) [string] A set of test points to estimate the density of. Default value ''. --test_set_estimates_file (-E) [string] The file in which to output the estimates on the test set from the final optimally pruned tree. Default value ''. --training_file (-t) [string] The data set on which to build a density estimation tree. Default value ''. --training_set_estimates_file (-e) [string] The file in which to output the density estimates on the training set from the final optimally pruned tree. Default value ''. --verbose (-v) Display informational messages and the full list of parameters and timers at the end of execution. --version (-V) Display the version of mlpack. --vi_file (-i) [string] The file to output the variable importance values for each feature. Default value ''. For further information, including relevant papers, citations, and theory, consult the documentation found at http://www.mlpack.org or included with your distribution of mlpack. @endcode @subsection cli_ex1_de_tut Plain-vanilla density estimation We can just train a DET on the provided data set \e S. Like all datasets \b mlpack uses, the data should be row-major (\b mlpack transposes data when it is loaded; internally, the data is column-major -- see \ref matrices "this page" for more information). @code $ mlpack_det -t dataset.csv -v @endcode By default, \c mlpack_det performs 10-fold cross-validation (using the \f$\alpha\f$-pruning regularization for decision trees). To perform LOOCV (leave-one-out cross-validation), which can provide better results but will take longer, use the following command: @code $ mlpack_det -t dataset.csv -f 0 -v @endcode To perform k-fold crossvalidation, use \c -f \c k (or \c --folds \c k). There are certain other options available for training. For example, in the construction of the initial tree, you can specify the maximum and minimum leaf sizes. By default, they are 10 and 5 respectively; you can set them using the \c -M (\c --max_leaf_size) and the \c -N (\c --min_leaf_size) options. @code $ mlpack_det -t dataset.csv -M 20 -N 10 @endcode In case you want to output the density estimates at the points in the training set, use the \c -e (\c --training_set_estimates_file) option to specify the output file to which the estimates will be saved. The first line in density_estimates.txt will correspond to the density at the first point in the training set. Note that the logarithm of the density estimates are given, which allows smaller estimates to be saved. @code $ mlpack_det -t dataset.csv -e density_estimates.txt -v @endcode @subsection cli_ex2_de_test_tut Estimation on a test set Often, it is useful to train a density estimation tree on a training set and then obtain density estimates from the learned estimator for a separate set of test points. The \c -T (\c --test_file) option allows specification of a set of test points, and the \c -E (\c --test_set_estimates_file) option allows specification of the file into which the test set estimates are saved. Note that the logarithm of the density estimates are saved; this allows smaller values to be saved. @code $ mlpack_det -t dataset.csv -T test_points.csv -E test_density_estimates.txt -v @endcode @subsection cli_ex4_de_vi_tut Computing the variable importance The variable importance (with respect to density estimation) of the different features in the data set can be obtained by using the \c -i (\c --vi_file ) option. This outputs the absolute (as opposed to relative) variable importance of the all the features into the specified file. @code $ mlpack_det -t dataset.csv -i variable_importance.txt -v @endcode @subsection cli_ex6_de_save Saving trained DETs The \c mlpack_det program is capable of saving a trained DET to a file for later usage. The \c --output_model_file or \c -M option allows specification of the file to save to. In the example below, a DET trained on \c dataset.csv is saved to the file \c det.xml. @code $ mlpack_det -t dataset.csv -M det.xml -v @endcode @subsection cli_ex7_de_load Loading trained DETs A saved DET can be used to perform any of the functionality in the examples above. A saved DET is loaded with the \c --input_model_file or \c -m option. The example below loads a saved DET from \c det.xml and outputs density estimates on the dataset \c test_dataset.csv into the file \c estimates.csv. @code $ mlpack_det -m det.xml -T test_dataset.csv -E estimates.csv -v @endcode @section dtree_det_tut The 'DTree' class This class implements density estimation trees. Below is a simple example which initializes a density estimation tree. @code #include using namespace mlpack::det; // The dataset matrix, on which to learn the density estimation tree. extern arma::Mat data; // Initialize the tree. This function also creates and saves the bounding box // of the data. Note that it does not actually build the tree. DTree<> det(data); @endcode @subsection dtree_pub_func_det_tut Public Functions The function \c Grow() greedily grows the tree, adding new points to the tree. Note that the points in the dataset will be reordered. This should only be run on a tree which has not already been built. In general, it is more useful to use the \c Trainer() function found in \ref dtutils_det_tut. @code // This keeps track of the data during the shuffle that occurs while growing the // tree. arma::Col oldFromNew(data.n_cols); for (size_t i = 0; i < data.n_cols; i++) oldFromNew[i] = i; // This function grows the tree down to the leaves. It returns the current // minimum value of the regularization parameter alpha. size_t maxLeafSize = 10; size_t minLeafSize = 5; double alpha = det.Grow(data, oldFromNew, false, maxLeafSize, minLeafSize); @endcode Note that the alternate volume regularization should not be used (see ticket #238). To estimate the density at a given query point, use the following code. Note that the logarithm of the density is returned. @code // For a given query, you can obtain the density estimate. extern arma::Col query; extern DTree* det; double estimate = det->ComputeValue(&query); @endcode Computing the \b variable \b importance of each feature for the given DET. @code // The data matrix and density estimation tree. extern arma::mat data; extern DTree* det; // The variable importances will be saved into this vector. arma::Col varImps; // You can obtain the variable importance from the current tree. det->ComputeVariableImportance(varImps); @endcode @section dtutils_det_tut 'namespace mlpack::det' The functions in this namespace allows the user to perform tasks with the 'DTree' class. Most importantly, the \c Trainer() method allows the full training of a density estimation tree with cross-validation. There are also utility functions which allow printing of leaf membership and variable importance. @subsection dtutils_util_funcs Utility Functions The code below details how to train a density estimation tree with cross-validation. @code #include using namespace mlpack::det; // The dataset matrix, on which to learn the density estimation tree. extern arma::Mat data; // The number of folds for cross-validation. const size_t folds = 10; // Set folds = 0 for LOOCV. const size_t maxLeafSize = 10; const size_t minLeafSize = 5; // Train the density estimation tree with cross-validation. DTree<>* dtree_opt = Trainer(data, folds, false, maxLeafSize, minLeafSize); @endcode Note that the alternate volume regularization should be set to false because it has known bugs (see #238). To print the class membership of leaves in the tree into a file, see the following code. @code extern arma::Mat labels; extern DTree* det; const size_t numClasses = 3; // The number of classes must be known. extern string leafClassMembershipFile; PrintLeafMembership(det, data, labels, numClasses, leafClassMembershipFile); @endcode Note that you can find the number of classes with \c max(labels) \c + \c 1. The variable importance can also be printed to a file in a similar manner. @code extern DTree* det; extern string variableImportanceFile; const size_t numFeatures = data.n_rows; PrintVariableImportance(det, numFeatures, variableImportanceFile); @endcode @section further_doc_det_tut Further Documentation For further documentation on the DTree class, consult the \ref mlpack::det::DTree "complete API documentation". */ ----- this option is not available in DET right now; see #238! ----- @subsection cli_alt_reg_tut Alternate DET regularization The usual regularized error \f$R_\alpha(t)\f$ of a node \f$t\f$ is given by: \f$R_\alpha(t) = R(t) + \alpha |\tilde{t}|\f$ where \f{ R(t) = -\frac{|t|^2}{N^2 V(t)}. \f} \f$V(t)\f$ is the volume of the node \f$t\f$ and \f$\tilde{t}\f$ is the set of leaves in the subtree rooted at \f$t\f$. For the purposes of density estimation, there is a different form of regularization: instead of penalizing the number of leaves in the subtree, we penalize the sum of the inverse of the volumes of the leaves. With this regularization, very small volume nodes are discouraged unless the data actually warrants it. Thus, \f[ R_\alpha'(t) = R(t) + \alpha I_v(\tilde{t}) \f] where \f[ I_v(\tilde{t}) = \sum_{l \in \tilde{t}} \frac{1}{V(l)}. \f] To use this form of regularization, use the \c -R flag. @code $ mlpack_det -t dataset.csv -R -v @endcode