Commit a22eb892 authored by Thomas Purcell's avatar Thomas Purcell
Browse files

Add standardized value poitner/setters to Node

Will be used for comp_feats
parent 0ccdb08a
......@@ -48,3 +48,96 @@ std::map<std::string, int> Node::primary_feature_decomp() const
}
BOOST_SERIALIZATION_ASSUME_ABSTRACT(Node)
void Node::set_standardized_value(int offset, const bool for_comp) const
{
double* stand_val_ptr;
if(_selected)
{
stand_val_ptr = node_value_arrs::get_stand_d_matrix_ptr(_d_mat_ind);
}
else
{
stand_val_ptr = node_value_arrs::access_temp_stand_storage(_arr_ind);
}
util_funcs::standardize(value_ptr(offset, for_comp), _n_samp, stand_val_ptr);
}
void Node::set_standardized_test_value(int offset, const bool for_comp) const
{
double* val_ptr = value_ptr(offset, for_comp);
double* test_val_ptr = test_value_ptr(offset, for_comp);
double* stand_val_ptr = node_value_arrs::access_temp_stand_storage_test(_arr_ind);
double mean = util_funcs::mean(val_ptr, _n_samp);
double stand_dev = util_funcs::stand_dev(val_ptr, _n_samp, mean);
std::transform(
test_val_ptr,
test_val_ptr + _n_samp_test,
stand_val_ptr,
[&](double val){return (val - mean) / stand_dev;}
);
}
void Node::set_standardized_value(const double* params, int offset, const bool for_comp, const int depth) const
{
double* stand_val_ptr;
if(_selected)
{
stand_val_ptr = node_value_arrs::get_stand_d_matrix_ptr(_d_mat_ind);
}
else
{
stand_val_ptr = node_value_arrs::access_temp_stand_storage(_arr_ind);
}
util_funcs::standardize(value_ptr(params, offset, for_comp, depth), _n_samp, stand_val_ptr);
}
void Node::set_standardized_test_value(const double* params, int offset, const bool for_comp, const int depth) const
{
double* val_ptr = value_ptr(params, offset, for_comp, depth);
double* test_val_ptr = test_value_ptr(params, offset, for_comp, depth);
double* stand_val_ptr = node_value_arrs::access_temp_stand_storage_test(_arr_ind);
double mean = util_funcs::mean(val_ptr, _n_samp);
double stand_dev = util_funcs::stand_dev(val_ptr, _n_samp, mean);
std::transform(
test_val_ptr,
test_val_ptr + _n_samp_test,
stand_val_ptr,
[&](double val){return (val - mean) / stand_dev;}
);
}
double* Node::stand_value_ptr(int offset, const bool for_comp) const
{
if(_selected)
{
return node_value_arrs::get_stand_d_matrix_ptr(_d_mat_ind);
}
set_standardized_value(offset, for_comp);
return node_value_arrs::access_temp_stand_storage(_arr_ind);
}
double* Node::stand_test_value_ptr(int offset, const bool for_comp) const
{
set_standardized_test_value(offset, for_comp);
return node_value_arrs::access_temp_stand_storage_test(_arr_ind);
}
double* Node::stand_value_ptr(const double* params, int offset, const bool for_comp, const int depth) const
{
if(_selected)
{
return node_value_arrs::get_stand_d_matrix_ptr(_d_mat_ind);
}
set_standardized_value(params, offset, for_comp, depth);
return node_value_arrs::access_temp_stand_storage(_arr_ind);
}
double* Node::stand_test_value_ptr(const double* params, int offset, const bool for_comp, const int depth) const
{
set_standardized_test_value(params, offset, for_comp, depth);
return node_value_arrs::access_temp_stand_storage_test(_arr_ind);
}
......@@ -279,6 +279,15 @@ public:
*/
virtual void set_value(int offset=-1, const bool for_comp=false) const = 0;
// DocString: node_set_stand_value
/**
* @brief Set the value of all training samples to the standardized values for the feature inside the central data storage arrays
*
* @param offset (int) Where the current node is in the binary expression tree relative to other nodes at the same depth
* @param for_comp (bool) If true then the evaluation is used for comparing features
*/
void set_standardized_value(int offset=-1, const bool for_comp=false) const;
/**
* @brief The pointer to where the feature's training data is stored
*
......@@ -289,6 +298,16 @@ public:
*/
virtual double* value_ptr(int offset=-1, const bool for_comp=false) const = 0;
/**
* @brief The pointer to where the feature's standardized training data is stored
*
* @param offset (int) Where the current node is in the binary expression tree relative to other nodes at the same depth
* @param for_comp (bool) If true then the evaluation is used for comparing features
*
* @return pointer to the feature's training value
*/
double* stand_value_ptr(int offset=-1, const bool for_comp=false) const;
// DocString: node_set_test_value
/**
* @brief Set the value of all test samples for the feature inside the central data storage array
......@@ -298,6 +317,15 @@ public:
*/
virtual void set_test_value(int offset=-1, const bool for_comp=false) const = 0;
// DocString: node_set_stand_test_value
/**
* @brief Set the value of all test samples to the standardized values for the feature inside the central data storage array
*
* @param offset (int) Where the current node is in the binary expression tree relative to other nodes at the same depth
* @param for_comp (bool) If true then the evaluation is used for comparing features
*/
void set_standardized_test_value(int offset=-1, const bool for_comp=false) const;
/**
* @brief The pointer to where the feature's test data is stored
*
......@@ -308,6 +336,16 @@ public:
*/
virtual double* test_value_ptr(int offset=-1, const bool for_comp=false) const = 0;
/**
* @brief The pointer to where the feature's standardized test data is stored
*
* @param offset (int) Where the current node is in the binary expression tree relative to other nodes at the same depth
* @param for_comp (bool) If true then the evaluation is used for comparing features
*
* @return pointer to the feature's test values
*/
double* stand_test_value_ptr(int offset=-1, const bool for_comp=false) const;
// DocString: node_is_nan
/**
* @brief Check if the feature has a NaN value in it
......@@ -470,6 +508,16 @@ public:
*/
virtual void set_value(const double* params, int offset=-1, const bool for_comp=false, const int depth=1) const = 0;
/**
* @brief Set the value of all training samples to the standardized for the feature inside the central data storage array
*
* @param offset (int) Where the current node is in the binary expression tree relative to other nodes at the same depth
* @param params A pointer to the bias and scale terms for this Node and its children
* @param for_comp (bool) If true then the evaluation is used for comparing features
* @param depth (int) How far down a given Node is from the root OperatorNode
*/
void set_standardized_value(const double* params, int offset=-1, const bool for_comp=false, const int depth=1) const;
/**
* @brief The pointer to where the feature's training data is stored
*
......@@ -481,6 +529,17 @@ public:
*/
virtual double* value_ptr(const double* params, int offset=-1, const bool for_comp=false, const int depth=1) const = 0;
/**
* @brief The pointer to where the feature's standardized training data is stored
*
* @param offset (int) Where the current node is in the binary expression tree relative to other nodes at the same depth
* @param params A pointer to the bias and scale terms for this Node and its children
* @param for_comp (bool) If true then the evaluation is used for comparing features
* @param depth (int) How far down a given Node is from the root OperatorNode
* @returns The pointer to the feature's data
*/
double* stand_value_ptr(const double* params, int offset=-1, const bool for_comp=false, const int depth=1) const;
/**
* @brief Set the value of all test samples for the feature inside the central data storage array
*
......@@ -491,6 +550,16 @@ public:
*/
virtual void set_test_value(const double* params, int offset=-1, const bool for_comp=false, const int depth=1) const = 0;
/**
* @brief Set the value of all test samples to the standardized for the feature inside the central data storage array
*
* @param offset (int) Where the current node is in the binary expression tree relative to other nodes at the same depth
* @param params A pointer to the bias and scale terms for this Node and its children
* @param for_comp (bool) If true then the evaluation is used for comparing features
* @param depth How far down a given Node is from the root OperatorNode
*/
void set_standardized_test_value(const double* params, int offset=-1, const bool for_comp=false, const int depth=1) const;
/**
* @brief The pointer to where the feature's test data is stored
*
......@@ -502,6 +571,17 @@ public:
*/
virtual double* test_value_ptr(const double* params, int offset=-1, const bool for_comp=false, const int depth=1) const = 0;
/**
* @brief The pointer to where the feature's standardized test data is stored
*
* @param offset (int) Where the current node is in the binary expression tree relative to other nodes at the same depth
* @param params A pointer to the bias and scale terms for this Node and its children
* @param for_comp (bool) If true then the evaluation is used for comparing features
* @param depth How far down a given Node is from the root OperatorNode
* @returns The pointer to the feature's data
*/
double* stand_test_value_ptr(const double* params, int offset=-1, const bool for_comp=false, const int depth=1) const;
/**
* @brief The expression of the feature
*
......
......@@ -41,6 +41,18 @@ bool util_funcs::iterate(std::vector<int>& inds, int size, int incriment)
return cont;
}
void util_funcs::standardize(const double* val, const int sz, double* stand_val)
{
double avg = mean(val, sz);
double std = stand_dev(val, sz, avg);
std::transform(
val,
val + sz,
stand_val,
[&](double vv){return (vv - avg) / std;}
);
}
double util_funcs::log_r2(const double* a, const double* b, double* log_a, const int size)
{
std::transform(a, a + size, log_a, [](double aa){return std::log(aa);});
......
......@@ -143,6 +143,15 @@ namespace util_funcs
return std::sqrt(std::accumulate(start, start+size, 0.0, [&vec_mean](double total, double val){return total + std::pow(val - vec_mean, 2.0);}) / size);
};
/**
* @brief Standardize a vector
*
* @param val pointer to the head of the vector to standardize
* @param sz size of the vector
* @param stand_val vector to the output vector
*/
void standardize(const double* val, int sz, double* stand_val);
/**
* @brief Find the norm of a vector
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment