1#ifndef BASIC_ARRAY_STATISTICS_H
2#define BASIC_ARRAY_STATISTICS_H
7#include "cuberl/base/cubeai_config.h"
36 template<
typename VecType>
41 template<
typename VecType>
45 std::mt19937 generator(seed);
46 std::uniform_real_distribution<real_t> distribution(a, b);
48 std::for_each(v.begin(),
51 val = distribution(generator);
58 template<
typename VecType>
62 template<
typename VecType>
66 std::mt19937 generator(seed);
67 std::uniform_real_distribution<float_t> distribution(a, b);
69 std::for_each(v.begin(),
72 val = distribution(generator);
77template<utils::concepts::
float_or_
integral_vector VectorType>
81 auto size1_ = v1.size();
82 auto size2_ = v2.size();
85 throw std::logic_error(
"Sizes not equal. Cannot compute element product of vectors with no equal sizes");
88 VectorType v_(v1.size(), 0);
89 for(
uint_t i=0; i < v1.size(); ++i){
91 v_[i] = v1[i] * v2[i];
96template<
typename IteratorType>
97typename std::iterator_traits<IteratorType>::value_type
98sum(IteratorType begin, IteratorType end,
bool parallel=
true){
100 typedef typename std::iterator_traits<IteratorType>::value_type value_type;
101 value_type sum_ = value_type(0);
104 sum_ = std::reduce(std::execution::par, begin, end, sum_);
107 sum_ = std::accumulate(begin, end, sum_);
114template<
typename VectorType>
115typename VectorType::value_type
116sum(
const VectorType& vec,
bool parallel=
true){
117 return sum(vec.begin(), vec.end(), parallel);
124template<
typename IteratorType>
126mean(IteratorType begin, IteratorType end,
bool parallel=
true){
128 auto sum_ =
sum(begin, end, parallel);
129 return sum_ /
static_cast<real_t>(std::distance(begin, end));
137template<utils::concepts::
float_or_
integral_vector VectorType>
139mean(
const VectorType& vector,
bool parallel=
true){
140 return mean(vector.begin(), vector.end(), parallel);
149 return mean(vector.begin(), vector.end(), parallel);
153template<
typename IteratorType>
154typename std::iterator_traits<IteratorType>::value_type
155variance(IteratorType begin, IteratorType end,
bool parallel=
true){
157 typedef typename std::iterator_traits<IteratorType>::value_type value_type;
158 auto mean_val =
mean(begin, end, parallel);
159 auto size = std::distance(begin, end);
161 std::vector<value_type> diff(size);
162 std::transform(begin, end, diff.begin(),
163 [mean_val](value_type x) { return x - mean_val; });
165 value_type sq_sum = std::inner_product(diff.begin(),
168 return sq_sum /
static_cast<value_type
>(std::distance(begin, end));
178variance(
const std::vector<real_t>& vals,
bool parallel=
true){
179 return variance(vals.begin(), vals.end(), parallel);
195template<utils::concepts::
float_vector Vec2>
199 std::mt19937 generator(seed);
200 std::discrete_distribution<int> distribution(probs.begin(), probs.end());
201 return distribution(generator);
211template<utils::concepts::
integral_vector Vec1, utils::concepts::
float_vector Vec2>
215 std::mt19937 generator(seed);
216 std::discrete_distribution<int> distribution(probs.begin(), probs.end());
217 return choices[distribution(generator)];
220template<utils::concepts::
float_vector Vec>
221typename Vec::value_type
224 std::mt19937 generator(seed);
225 auto size = std::distance(vals.begin(), vals.end());
226 std::discrete_distribution<int> distribution(0,
static_cast<int>(size));
227 auto rand_idx = distribution(generator);
228 return vals[rand_idx];
235template<utils::concepts::
float_or_
integral_vector Vec>
238 std::for_each(v.begin(), v.end(),
240 val += choose_value(walk_set, seed);
257template<utils::concepts::
float_or_
integral_vector Vec>
258Vec&
divide(Vec& v1,
typename Vec::value_type val){
261 for(
uint_t i=0; i<v1.size(); ++i){
267template<utils::concepts::
float_or_
integral_vector Vec>
268Vec&
add(Vec& v1,
const Vec& v2){
271 for(
uint_t i=0; i<v1.size(); ++i){
278template<utils::concepts::
float_or_
integral_vector VectorType>
281 VectorType vec_exp(vec.size());
283 static auto func = [&counter](
const auto& data){
284 return std::pow(data, counter++);
286 std::transform(vec.begin(), vec.end(),
287 vec_exp.begin(), func);
291template<utils::concepts::
float_or_
integral_vector VectorType>
294 VectorType vec_exp(vec.size());
296 static auto func = [&counter, v](
const auto&){
297 return std::pow(v, counter++);
299 std::transform(vec.begin(), vec.end(),
300 vec_exp.begin(), func);
306template<utils::concepts::
float_or_
integral_vector VectorType>
308exponentiate(
const torch_tensor_t tensor,
typename VectorType::value_type v){
311 assert(tensor.dim() == 1 &&
"Invalid tensor dimension. Should be 1");
314 VectorType vec_exp(tensor.size(0));
317 for(
uint_t i=0; i < static_cast<uint_t>(tensor.size(0)); ++i){
319 auto val = tensor[i].item();
320 vec_exp[i] = val.template to<typename VectorType::value_type>();
324 static auto func = [&counter, v](
auto& val){
325 auto expo = std::pow(v, counter++);
329 std::for_each(vec_exp.begin(), vec_exp.end(),func);
344 std::vector<T> result(vec.size());
346 auto vec_size =
static_cast<uint_t>(vec.size());
348 for(
uint_t i=0; i<vec_size; ++i){
349 result[i] = std::exp(vec[i] / tau);
350 exp_sum += result[i];
353 for(
uint_t i=0; i<vec_size; ++i){
354 result[i] /= exp_sum;
371 auto vec_size =
static_cast<uint_t>(vec.size());
373 for(
uint_t i=0; i<vec_size; ++i){
374 result[i] = std::exp(vec[i] / tau);
375 exp_sum += result[i];
388template<
typename IteratorType>
391 auto vec_size = std::distance(begin, end);
396 for(; begin != end; ++begin){
397 result[counter] = std::exp(*begin / tau);
398 exp_sum += result[counter++];
412template <
typename VectorType>
415 return static_cast<uint_t>(std::distance(vec.begin(), std::max_element(vec.begin(), vec.end())));
424template <
typename VectorType>
427 return static_cast<uint_t>(std::distance(vec.begin(), std::min_element(vec.begin(), vec.end())));
435 auto max_val = vec.maxCoeff();
437 auto result = std::vector<uint_t>();
439 for(
uint_t i=0; i<static_cast<uint_t>(vec.size()); ++i){
441 if(value == max_val){
453template<
typename VecTp>
458 auto max_val = std::max_element(vec.begin(), vec.end());
460 auto result = std::vector<uint_t>();
463 std::for_each(vec.begin(), vec.end(),
466 result.push_back(counter);
480 assert(end <= vec.size() &&
"Invalid end index");
484 return std::vector<real_t>(vec.begin(), vec.begin() + end);
487 return std::vector<real_t>(vec.begin() + end, vec.end());
496template<
typename SequenceTp>
498bin_index(
const typename SequenceTp::value_type& x,
const SequenceTp& sequence){
500 if(sequence.size() <= 1){
505 auto begin = sequence.begin();
506 auto prev_val = *begin;
508 auto end = sequence.end();
510 for(; begin != end; ++begin, ++index){
512 auto current_val = *begin;
514 if( (prev_val <= x) && (x < current_val)){
518 prev_val = current_val;
528template<
typename VectorType>
532 auto vec_mean =
mean(vec.begin(), vec.end(), parallel);
533 VectorType v(vec.size());
534 std::transform(vec.begin(), vec.end(),
536 [vec_mean](
auto val){return val - vec_mean;});
549template<
typename VectorType>
551normalize(
const VectorType& vec,
typename VectorType::value_type v){
553 VectorType v_(vec.size(), 0);
555 for(
uint_t i=0; i<v_.size(); ++i){
566 auto max_val = std::max_element(vec.begin(),
568 std::vector<T> v_(vec.size(), T(0));
570 for(
uint_t i=0; i<v_.size(); ++i){
571 v_[i] = vec[i] / *max_val;
580 auto min_val = std::min_element(vec.begin(),
582 std::vector<T> v_(vec.size(), T(0));
584 for(
uint_t i=0; i<v_.size(); ++i){
585 v_[i] = vec[i] / *min_val;
608template<
typename IteratorType>
609typename std::iterator_traits<IteratorType>::value_type
611 IteratorType bv2, IteratorType ev2) {
613 typedef typename std::iterator_traits<IteratorType>::value_type value_type;
615 auto size1_ = std::distance(bv1, ev1);
616 auto size2_ = std::distance(bv2, ev2);
618 if(size1_ != size2_){
619 throw std::logic_error(
"Sizes not equal. Cannot compute dot product of vectors with no equal sizes");
622 value_type sum_ = value_type(0);
624 for(; bv1 != ev1; ++bv1, ++bv2){
625 sum_ += (*bv1) * (*bv2);
632template<
typename VectorType>
637 assert(v1.size() == v2.size() &&
"Invalid vector sizes");
638 assert(start_idx < v1.size() &&
"Invalid start_idx");
641 auto v1_begin = v1.begin();
642 std::advance(v1_begin, start_idx);
644 auto v2_begin = v2.begin();
645 std::advance(v2_begin, start_idx);
648 for(; v1_begin != v1.end(); ++v1_begin, ++v2_begin){
const uint_t INVALID_ID
Invalid id.
Definition bitrl_consts.h:21
const real_t TOLERANCE
Tolerance used around the library.
Definition bitrl_consts.h:31
double real_t
real_t
Definition bitrl_types.h:23
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitrl_types.h:74
std::size_t uint_t
uint_t
Definition bitrl_types.h:43
float float_t
float
Definition bitrl_types.h:28
uint_t arg_min(const VectorType &vec)
Returns the index of the element that has the minimum value in the array. Implementation taken from h...
Definition vector_math.h:426
VectorType element_product(const VectorType &v1, const VectorType &v2)
Definition vector_math.h:79
VectorType zero_center(const VectorType &vec, bool parallel=true)
zero_center. Subtracts the mean value of the given vector from every value of the vector
Definition vector_math.h:530
std::iterator_traits< IteratorType >::value_type dot_product(IteratorType bv1, IteratorType ev1, IteratorType bv2, IteratorType ev2)
Definition vector_math.h:610
std::vector< real_t > logspace(real_t start, real_t end, uint_t num, real_t base=10.0)
Vec & add(Vec &v1, const Vec &v2)
Definition vector_math.h:268
VectorType normalize(const VectorType &vec, typename VectorType::value_type v)
Normalize the values of the given vector with the given value. Essentially this function divides the ...
Definition vector_math.h:551
real_t mean(IteratorType begin, IteratorType end, bool parallel=true)
mean Compute the mean value of the values in the provided iterator range
Definition vector_math.h:126
std::vector< T > extract_subvector(const std::vector< T > &vec, uint_t end, bool up_to=true)
Definition vector_math.h:477
std::vector< uint_t > max_indices(const DynVec< T > &vec)
Definition vector_math.h:432
void randomize_vec(Vec &v, const Vec &walk_set, uint_t seed=42)
Given a vector of intergal or floating point values and a set of values to choose from,...
Definition vector_math.h:237
std::vector< real_t > standardize(const std::vector< real_t > &vals, real_t tol=bitrl::consts::TOLERANCE)
Standardize the given vector.
Vec::value_type choose_value(const Vec &vals, uint_t seed=42)
Definition vector_math.h:222
std::vector< T > & randomize(std::vector< T > &vec, T a, T b, uint_t seed=42)
Fill in the given vector with random values.
Definition vector_math.h:252
Vec & divide(Vec &v1, typename Vec::value_type val)
Definition vector_math.h:258
uint_t arg_max(const VectorType &vec)
Returns the index of the element that has the maximum value in the array. Implementation taken from h...
Definition vector_math.h:414
std::vector< T > normalize_min(const std::vector< T > &vec)
Definition vector_math.h:578
VectorType exponentiate(const VectorType &vec)
Definition vector_math.h:280
uint_t bin_index(const typename SequenceTp::value_type &x, const SequenceTp &sequence)
bin_index. Compute sequnce[i - 1] <= x sequnce[i] and returns the index. Sequence should be sorted
Definition vector_math.h:498
std::vector< T > softmax_vec(const std::vector< T > &vec, real_t tau=1.0)
applies softmax operation to the elements of the vector and returns a vector with the result
Definition vector_math.h:342
uint_t choice(const Vec2 &probs, uint_t seed=42)
choice. Implements similar functionality to numpy.choice function
Definition vector_math.h:197
std::iterator_traits< IteratorType >::value_type variance(IteratorType begin, IteratorType end, bool parallel=true)
Definition vector_math.h:155
std::iterator_traits< IteratorType >::value_type sum(IteratorType begin, IteratorType end, bool parallel=true)
Definition vector_math.h:98
std::vector< T > normalize_max(const std::vector< T > &vec)
Definition vector_math.h:564
Various utilities used when working with RL problems.
Definition cuberl_types.h:16
Definition vector_math.h:31