32 #ifndef VSMC_CORE_WEIGHT_HPP 33 #define VSMC_CORE_WEIGHT_HPP 43 inline double weight_ess(std::size_t N,
const double *first)
45 return 1 /
dot(N, first, 1, first, 1);
52 mul(N, 1 / std::accumulate(first, first + N, 0.0), first, first);
59 double wmax = *(std::max_element(first, first + N));
60 for (std::size_t i = 0; i != N; ++i)
77 double ess()
const {
return ess_; }
79 const double *
data()
const {
return data_.data(); }
83 template <
typename OutputIter>
86 std::copy(data_.begin(), data_.end(), first);
89 template <
typename RandomIter>
100 std::fill(data_.begin(), data_.end(), 1.0 /
resample_size());
104 template <
typename InputIter>
105 void set(InputIter first)
107 std::copy_n(first,
size(), data_.begin());
111 template <
typename RandomIter>
112 void set(RandomIter first,
int stride)
119 template <
typename InputIter>
127 void mul(
const double *first)
133 void mul(
double *first) {
mul(const_cast<const double *>(first)); }
135 template <
typename RandomIter>
136 void mul(RandomIter first,
int stride)
143 template <
typename InputIter>
146 std::copy_n(first,
size(), data_.begin());
150 template <
typename RandomIter>
158 template <
typename InputIter>
161 log(
size(), data_.data(), data_.data());
169 log(
size(), data_.data(), data_.data());
170 add(
size(), data_.data(), first, data_.data());
176 template <
typename RandomIter>
179 log(
size(), data_.data(), data_.data());
185 template <
typename URNG>
188 return draw_(eng, data_.begin(), data_.end(),
true);
199 void post_set() { ess_ = normalize(
false); }
204 ess_ = normalize(
true);
207 double normalize(
bool use_log)
209 double *w = data_.data();
211 const std::size_t k = 1000;
212 const std::size_t m =
size() / k;
213 const std::size_t l =
size() % k;
214 for (std::size_t i = 0; i != m; ++i, w += k)
215 normalize_eval(k, w, accw, use_log);
216 normalize_eval(l, w, accw, use_log);
219 return 1 /
dot(
size(), data_.data(), 1, data_.data(), 1);
222 void normalize_eval(std::size_t n,
double *w,
double &accw,
bool use_log)
226 accw = std::accumulate(w, w + n, accw);
250 double ess()
const {
return std::numeric_limits<double>::quiet_NaN(); }
254 const double *
data()
const {
return nullptr; }
256 template <
typename OutputIter>
261 template <
typename RandomIter>
270 template <
typename InputIter>
275 template <
typename RandomIter>
276 void set(RandomIter, int)
280 template <
typename InputIter>
285 template <
typename RandomIter>
290 template <
typename InputIter>
295 template <
typename RandomIter>
300 template <
typename InputIter>
305 template <
typename RandomIter>
310 template <
typename URNG>
323 #endif // VSMC_CORE_WEIGHT_HPP T dot(std::size_t n, const T *x, std::size_t incx, const T *y, std::size_t incy)
Computes a vector-vector dot product.
#define VSMC_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
void mul(std::size_t n, const float *a, const float *b, float *y)
void add_log(RandomIter first, int stride)
void add_log(const double *first)
void read_weight(RandomIter first, int stride) const
void set_log(InputIter first)
void weight_normalize_log(std::size_t N, double *first)
Normalize logarithm weights such that the maximum is zero.
typename std::conditional< std::is_scalar< T >::value, std::vector< T, AlignedAllocator< T >>, std::vector< T >>::type Vector
AlignedVector for scalar type and std::vector for others.
void set_log(RandomIter first, int stride)
void add_log(RandomIter, int)
void add_log(double *first)
size_type draw(URNG &) const
void mul(InputIter first)
void mul(RandomIter, int)
void copy(std::size_t n, const T *x, std::size_t incx, T *y, std::size_t incy)
Copies vector to another vector.
void set_log(RandomIter, int)
void read_resample_weight(double *) const
void read_weight(OutputIter) const
const double * resample_data() const
void exp(std::size_t n, const float *a, float *y)
size_type draw(URNG &eng) const
const double * resample_data() const
const double * data() const
void weight_normalize(std::size_t N, double *first)
Normalize weights such that the summation is one.
An empty weight set class.
typename WeightTypeTrait< T >::type WeightType
void mul(const double *first)
void add(std::size_t n, const float *a, const float *b, float *y)
void read_weight(RandomIter, int) const
void read_resample_weight(double *first) const
double weight_ess(std::size_t N, const double *first)
Compute the ess given normalized weights.
const double * data() const
size_type resample_size() const
size_type resample_size() const
void read_weight(OutputIter first) const
void log(std::size_t n, const float *a, float *y)
void mul(RandomIter first, int stride)
void add_log(InputIter first)