weighted mean (Descriptive Statistics)


Import

#include <archr/stats/weighted_mean.h>
#include <archr/stats/weighted_mean.hpp>
import archr.stats
atomsLoads('archr')

Synopsis

 float archr_stats_weighted_mean_f32f32(const float *first, size_t nbelem, const float *second);
(1)
 double archr_stats_weighted_mean_f64f64(const double *first, size_t nbelem, const double *second);
(2)
 int8_t archr_stats_weighted_mean_i8i8(const int8_t *first, size_t nbelem, const int8_t *second);
(3)
 int16_t archr_stats_weighted_mean_i16i16(const int16_t *first, size_t nbelem, const int16_t *second);
(4)
 int32_t archr_stats_weighted_mean_i32i32(const int32_t *first, size_t nbelem, const int32_t *second);
(5)
 int64_t archr_stats_weighted_mean_i64i64(const int64_t *first, size_t nbelem, const int64_t *second);
(6)
 uint8_t archr_stats_weighted_mean_u8u8(const uint8_t *first, size_t nbelem, const uint8_t *second);
(7)
 uint16_t archr_stats_weighted_mean_u16u16(const uint16_t *first, size_t nbelem, const uint16_t *second);
(8)
 uint32_t archr_stats_weighted_mean_u32u32(const uint32_t *first, size_t nbelem, const uint32_t *second);
(9)
 uint64_t archr_stats_weighted_mean_u64u64(const uint64_t *first, size_t nbelem, const uint64_t *second);
(10)
 float weighted_mean(const float *first, size_t nbelem, const float *second);
(1)
 double weighted_mean(const double *first, size_t nbelem, const double *second);
(2)
 int8_t weighted_mean(const int8_t *first, size_t nbelem, const int8_t *second);
(3)
 int16_t weighted_mean(const int16_t *first, size_t nbelem, const int16_t *second);
(4)
 int32_t weighted_mean(const int32_t *first, size_t nbelem, const int32_t *second);
(5)
 int64_t weighted_mean(const int64_t *first, size_t nbelem, const int64_t *second);
(6)
 uint8_t weighted_mean(const uint8_t *first, size_t nbelem, const uint8_t *second);
(7)
 uint16_t weighted_mean(const uint16_t *first, size_t nbelem, const uint16_t *second);
(8)
 uint32_t weighted_mean(const uint32_t *first, size_t nbelem, const uint32_t *second);
(9)
 uint64_t weighted_mean(const uint64_t *first, size_t nbelem, const uint64_t *second);
(10)
 float weighted_mean(const float *first1, const float *last1, const float *first2);
(11)
 double weighted_mean(const double *first1, const double *last1, const double *first2);
(12)
 int8_t weighted_mean(const int8_t *first1, const int8_t *last1, const int8_t *first2);
(13)
 int16_t weighted_mean(const int16_t *first1, const int16_t *last1, const int16_t *first2);
(14)
 int32_t weighted_mean(const int32_t *first1, const int32_t *last1, const int32_t *first2);
(15)
 int64_t weighted_mean(const int64_t *first1, const int64_t *last1, const int64_t *first2);
(16)
 uint8_t weighted_mean(const uint8_t *first1, const uint8_t *last1, const uint8_t *first2);
(17)
 uint16_t weighted_mean(const uint16_t *first1, const uint16_t *last1, const uint16_t *first2);
(18)
 uint32_t weighted_mean(const uint32_t *first1, const uint32_t *last1, const uint32_t *first2);
(19)
 uint64_t weighted_mean(const uint64_t *first1, const uint64_t *last1, const uint64_t *first2);
(20)
template <typename Range> typename Range::value_type weighted_mean(const Range& data1, const Range& data2);
(21)
subroutine archr_stats_weighted_mean_f32f32(real(4) :: r, real(4), dimension(*), parameter :: first, integer(4) :: nbelem, real(4), dimension(*), parameter :: second)
(1)
subroutine archr_stats_weighted_mean_i32i32(integer(4) :: r, integer(4), dimension(*), parameter :: first, integer(4) :: nbelem, integer(4), dimension(*), parameter :: second)
(2)
subroutine archr_stats_weighted_mean_u32u32(integer(4) :: r, integer(4), dimension(*), parameter :: first, integer(4) :: nbelem, integer(4), dimension(*), parameter :: second)
(3)
def weighted_mean(first, second):
    return r
(1)
function r = archr_stats_weighted_mean(first, second)
(1)

Description

This function computes the weighted mean of the elements stored in:

Parameters

first

the beginning of the range of elements to process.

last

the end of the range of elements to process.

second

the beginning of the range of weights.

nbelem

the number of elements to process.

Return value

The weighted mean of the elements in the given range using the weight read from the second range :

$$W(X,W) = \frac{\sum_{i=1}^{n} X[i]*W[i]}{\sum_{i=1}^{n} W[i]}$$

Example

#include <archr/stats/weighted_mean.h>
#include <stdio.h>

int main()
{
  float v[] = { 1,   2,    3,    4,   5 }
      , w[] = { 1,0.5f,1/3.f,0.25f,0.2f };

  printf("weighted_mean({1,2,3},{1,0.5,0.3}) = %f\n", archr_stats_weighted_mean_f32f32(&v[0],3,&w[0]));

  return 0;
}

Possible Output

weighted_mean({1,2,3,4,5},{1,0.5,0.3,0.25,0.2}) = 2.189781
weighted_mean({1,2,3},{1,0.5,0.3}) = 1.636364
#include <archr/stats/weighted_mean.hpp>
#include <iostream>
#include <vector>

int main()
{
  std::vector<float> v{1,2,3,4,5}, w{1,0.5f,1/3.f,0.25f,0.2f};

  std::cout << "weighted_mean({1,2,3,4,5},{1,0.5,0.3,0.25,0.2}) = " << archr::stats::weighted_mean(v,w) << "\n";
  std::cout << "weighted_mean({1,2,3},{1,0.5,0.3}) = " << archr::stats::weighted_mean(v.data(),3,w.data()) << "\n";
  std::cout << "weighted_mean({1,2,3},{1,0.5,0.3}) = " << archr::stats::weighted_mean(v.data(),v.data()+3,w.data()) << "\n";

  return 0;
}

Possible Output

weighted_mean({1,2,3,4,5},{1,0.5,0.3,0.25,0.2}) = 2.18978
weighted_mean({1,2,3},{1,0.5,0.3}) = 1.63636
weighted_mean({1,2,3},{1,0.5,0.3}) = 1.63636

Possible Output