1#ifndef BASIC_ARRAY_STATISTICS_H
2#define BASIC_ARRAY_STATISTICS_H
7#include "bitrl/base/bitrl_config.h"
39template <
typename VecType>
43 std::mt19937 generator(seed);
44 std::uniform_real_distribution<real_t> distribution(a, b);
46 std::for_each(v.begin(), v.end(), [&](
auto &val) { val = distribution(generator); });
51 template <
typename VecType>
55template <
typename VecType>
59 std::mt19937 generator(seed);
60 std::uniform_real_distribution<float_t> distribution(a, b);
62 std::for_each(v.begin(), v.end(), [&](
auto &val) { val = distribution(generator); });
66template <utils::concepts::
float_or_
integral_vector VectorType>
70 auto size1_ = v1.size();
71 auto size2_ = v2.size();
75 throw std::logic_error(
76 "Sizes not equal. Cannot compute element product of vectors with no equal sizes");
79 VectorType v_(v1.size(), 0);
80 for (
uint_t i = 0; i < v1.size(); ++i)
83 v_[i] = v1[i] * v2[i];
88template <
typename IteratorType>
89typename std::iterator_traits<IteratorType>::value_type
sum(IteratorType begin, IteratorType end,
93 typedef typename std::iterator_traits<IteratorType>::value_type value_type;
94 value_type sum_ = value_type(0);
98 sum_ = std::reduce(std::execution::par, begin, end, sum_);
102 sum_ = std::accumulate(begin, end, sum_);
108template <
typename VectorType>
109typename VectorType::value_type
sum(
const VectorType &vec,
bool parallel =
true)
111 return sum(vec.begin(), vec.end(), parallel);
118template <
typename IteratorType>
119real_t mean(IteratorType begin, IteratorType end,
bool parallel =
true)
122 auto sum_ =
sum(begin, end, parallel);
123 return sum_ /
static_cast<real_t>(std::distance(begin, end));
130template <utils::concepts::
float_or_
integral_vector VectorType>
133 return mean(vector.begin(), vector.end(), parallel);
141 return mean(vector.begin(), vector.end(), parallel);
144template <
typename IteratorType>
145typename std::iterator_traits<IteratorType>::value_type
146variance(IteratorType begin, IteratorType end,
bool parallel =
true)
149 typedef typename std::iterator_traits<IteratorType>::value_type value_type;
150 auto mean_val =
mean(begin, end, parallel);
151 auto size = std::distance(begin, end);
153 std::vector<value_type> diff(size);
154 std::transform(begin, end, diff.begin(), [mean_val](value_type x) { return x - mean_val; });
156 value_type sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
157 return sq_sum /
static_cast<value_type
>(std::distance(begin, end));
166 return variance(vals.begin(), vals.end(), parallel);
184 std::mt19937 generator(seed);
185 std::discrete_distribution<int> distribution(probs.begin(), probs.end());
186 return distribution(generator);
195template <utils::concepts::
integral_vector Vec1, utils::concepts::
float_vector Vec2>
199 std::mt19937 generator(seed);
200 std::discrete_distribution<int> distribution(probs.begin(), probs.end());
201 return choices[distribution(generator)];
204template <utils::concepts::
float_vector Vec>
208 std::mt19937 generator(seed);
209 auto size = std::distance(vals.begin(), vals.end());
210 std::discrete_distribution<int> distribution(0,
static_cast<int>(size));
211 auto rand_idx = distribution(generator);
212 return vals[rand_idx];
219template <utils::concepts::
float_or_
integral_vector Vec>
222 std::for_each(v.begin(), v.end(), [&](
auto &val) { val += choose_value(walk_set, seed); });
230template <
typename T> std::vector<T> &
randomize(std::vector<T> &vec, T a, T b,
uint_t seed = 42)
236template <utils::concepts::
float_or_
integral_vector Vec>
237Vec &
divide(Vec &v1,
typename Vec::value_type val)
241 for (
uint_t i = 0; i < v1.size(); ++i)
248template <utils::concepts::
float_or_
integral_vector Vec> Vec &
add(Vec &v1,
const Vec &v2)
252 for (
uint_t i = 0; i < v1.size(); ++i)
259template <utils::concepts::
float_or_
integral_vector VectorType>
262 VectorType vec_exp(vec.size());
264 static auto func = [&counter](
const auto &data) {
return std::pow(data, counter++); };
265 std::transform(vec.begin(), vec.end(), vec_exp.begin(), func);
269template <utils::concepts::
float_or_
integral_vector VectorType>
270VectorType
exponentiate(
const VectorType &vec,
typename VectorType::value_type v)
272 VectorType vec_exp(vec.size());
274 static auto func = [&counter, v](
const auto &) {
return std::pow(v, counter++); };
275 std::transform(vec.begin(), vec.end(), vec_exp.begin(), func);
280template <utils::concepts::
float_or_
integral_vector VectorType>
285 assert(tensor.dim() == 1 &&
"Invalid tensor dimension. Should be 1");
288 VectorType vec_exp(tensor.size(0));
291 for (
uint_t i = 0; i < static_cast<uint_t>(tensor.size(0)); ++i)
294 auto val = tensor[i].item();
295 vec_exp[i] = val.template to<typename VectorType::value_type>();
298 static auto func = [&counter, v](
auto &val)
300 auto expo = std::pow(v, counter++);
304 std::for_each(vec_exp.begin(), vec_exp.end(), func);
316 std::vector<T> result(vec.size());
318 auto vec_size =
static_cast<uint_t>(vec.size());
320 for (
uint_t i = 0; i < vec_size; ++i)
322 result[i] = std::exp(vec[i] / tau);
323 exp_sum += result[i];
326 for (
uint_t i = 0; i < vec_size; ++i)
328 result[i] /= exp_sum;
343 auto vec_size =
static_cast<uint_t>(vec.size());
345 for (
uint_t i = 0; i < vec_size; ++i)
347 result[i] = std::exp(vec[i] / tau);
348 exp_sum += result[i];
359template <
typename IteratorType>
363 auto vec_size = std::distance(begin, end);
368 for (; begin != end; ++begin)
370 result[counter] = std::exp(*begin / tau);
371 exp_sum += result[counter++];
385 return static_cast<uint_t>(
386 std::distance(vec.begin(), std::max_element(vec.begin(), vec.end())));
396 return static_cast<uint_t>(
397 std::distance(vec.begin(), std::min_element(vec.begin(), vec.end())));
404 auto max_val = vec.maxCoeff();
406 auto result = std::vector<uint_t>();
408 for (
uint_t i = 0; i < static_cast<uint_t>(vec.size()); ++i)
411 if (value == max_val)
424template <
typename VecTp> std::vector<uint_t>
max_indices(
const VecTp &vec)
428 auto max_val = std::max_element(vec.begin(), vec.end());
430 auto result = std::vector<uint_t>();
433 std::for_each(vec.begin(), vec.end(),
438 result.push_back(counter);
451 assert(end <= vec.size() &&
"Invalid end index");
456 return std::vector<real_t>(vec.begin(), vec.begin() + end);
459 return std::vector<real_t>(vec.begin() + end, vec.end());
466template <
typename SequenceTp>
470 if (sequence.size() <= 1)
476 auto begin = sequence.begin();
477 auto prev_val = *begin;
479 auto end = sequence.end();
481 for (; begin != end; ++begin, ++index)
484 auto current_val = *begin;
486 if ((prev_val <= x) && (x < current_val))
491 prev_val = current_val;
501template <
typename VectorType> VectorType
zero_center(
const VectorType &vec,
bool parallel =
true)
504 auto vec_mean =
mean(vec.begin(), vec.end(), parallel);
505 VectorType v(vec.size());
506 std::transform(vec.begin(), vec.end(), v.begin(),
507 [vec_mean](
auto val) { return val - vec_mean; });
519template <
typename VectorType>
520VectorType
normalize(
const VectorType &vec,
typename VectorType::value_type v)
523 VectorType v_(vec.size(), 0);
525 for (
uint_t i = 0; i < v_.size(); ++i)
532template <
typename T> std::vector<T>
normalize_max(
const std::vector<T> &vec)
535 auto max_val = std::max_element(vec.begin(), vec.end());
536 std::vector<T> v_(vec.size(), T(0));
538 for (
uint_t i = 0; i < v_.size(); ++i)
540 v_[i] = vec[i] / *max_val;
545template <
typename T> std::vector<T>
normalize_min(
const std::vector<T> &vec)
548 auto min_val = std::min_element(vec.begin(), vec.end());
549 std::vector<T> v_(vec.size(), T(0));
551 for (
uint_t i = 0; i < v_.size(); ++i)
553 v_[i] = vec[i] / *min_val;
575template <
typename IteratorType>
576typename std::iterator_traits<IteratorType>::value_type
577dot_product(IteratorType bv1, IteratorType ev1, IteratorType bv2, IteratorType ev2)
580 typedef typename std::iterator_traits<IteratorType>::value_type value_type;
582 auto size1_ = std::distance(bv1, ev1);
583 auto size2_ = std::distance(bv2, ev2);
585 if (size1_ != size2_)
587 throw std::logic_error(
588 "Sizes not equal. Cannot compute dot product of vectors with no equal sizes");
591 value_type sum_ = value_type(0);
593 for (; bv1 != ev1; ++bv1, ++bv2)
595 sum_ += (*bv1) * (*bv2);
601template <
typename VectorType>
606 assert(v1.size() == v2.size() &&
"Invalid vector sizes");
607 assert(start_idx < v1.size() &&
"Invalid start_idx");
610 auto v1_begin = v1.begin();
611 std::advance(v1_begin, start_idx);
613 auto v2_begin = v2.begin();
614 std::advance(v2_begin, start_idx);
617 for (; v1_begin != v1.end(); ++v1_begin, ++v2_begin)
Definition vector_math.h:30
Definition lp_metric.h:10
std::iterator_traits< IteratorType >::value_type variance(IteratorType begin, IteratorType end, bool parallel=true)
Definition vector_math.h:146
std::vector< T > normalize_max(const std::vector< T > &vec)
Definition vector_math.h:532
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:220
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:520
Vec::value_type choose_value(const Vec &vals, uint_t seed=42)
Definition vector_math.h:205
Vec & add(Vec &v1, const Vec &v2)
Definition vector_math.h:248
std::vector< real_t > standardize(const std::vector< real_t > &vals, real_t tol=bitsim::consts::TOLERANCE)
Standardize the given vector.
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:230
std::vector< uint_t > max_indices(const DynVec< T > &vec)
Definition vector_math.h:400
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:119
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:383
Vec & divide(Vec &v1, typename Vec::value_type val)
Definition vector_math.h:237
std::vector< T > normalize_min(const std::vector< T > &vec)
Definition vector_math.h:545
uint_t choice(const Vec2 &probs, uint_t seed=42)
choice. Implements similar functionality to numpy.choice function
Definition vector_math.h:181
VectorType element_product(const VectorType &v1, const VectorType &v2)
Definition vector_math.h:67
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:467
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:501
VectorType exponentiate(const VectorType &vec)
Definition vector_math.h:260
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:394
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:313
std::iterator_traits< IteratorType >::value_type sum(IteratorType begin, IteratorType end, bool parallel=true)
Definition vector_math.h:89
std::iterator_traits< IteratorType >::value_type dot_product(IteratorType bv1, IteratorType ev1, IteratorType bv2, IteratorType ev2)
Definition vector_math.h:577
std::vector< real_t > logspace(real_t start, real_t end, uint_t num, real_t base=10.0)
std::vector< T > extract_subvector(const std::vector< T > &vec, uint_t end, bool up_to=true)
Definition vector_math.h:447
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
torch::Tensor torch_tensor_t
torch_tensor_t
Definition bitrl_types.h:95
const real_t TOLERANCE
Tolerance used around the library.
Definition bitsim_consts.h:26
const uint_t INVALID_ID
Invalid id.
Definition bitsim_consts.h:16
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
float float_t
float
Definition bitsim_types.h:25
double real_t
real_t
Definition bitsim_types.h:20
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitsim_types.h:101
static void randomize_(VecType &v, float_t a, float_t b, uint_t seed)
Definition vector_math.h:56
static void randomize_(VecType &v, real_t a, real_t b, uint_t seed)
Definition vector_math.h:40
Definition vector_math.h:32