# Chiqib ketadigan o'rtacha hisoblash qanday?

Menda bir nechta misol bor, uning uzunligi 10 ga teng. Endi men ularni og'irlik qilmoqchiman, shuning uchun eng qadimgi namunalar yangi mahsulotlarga qaraganda bir oz ko'proq og'irlik olib keladigan o'rtacha qiymatni olishim mumkin.

Misol uchun, Position 1 = 100%, Position 10 = 10% tortish.

Bu qanday chaqiriladi va qanday qilib bunday vazifani to'g'ri yozyapman?

4
@thelaws Matematik savol emasmi? Men faqat C dasturini izlayapman
qo'shib qo'ydi
@ wolfPack88 Bu havolada kod yo'q. Sizga AES uchun Vikipediya sahifasini ko'rsataman, ammo siz buni qanday amalga oshirishni bilib olishingiz mumkin emas.
qo'shib qo'ydi
@ wolfPack88 Bu kabi umumiy vazifa ekan, men uchun kod yozishni hech kim kutmaydi, u erda mavjud bo'lgan (va yaxshi sinovdan/optimallashtirilgan) amalga oshirish juda ko'p bo'lishi kerak.
qo'shib qo'ydi
@ wolfPack88 Bu kabi umumiy vazifa ekan, men uchun kod yozishni hech kim kutmaydi, u erda mavjud bo'lgan (va yaxshi sinovdan/optimallashtirilgan) amalga oshirish juda ko'p bo'lishi kerak.
qo'shib qo'ydi
qo'shib qo'ydi
Men bu savolni mavzu tashqari yopish uchun ovoz beraman, chunki bu matematik savol
qo'shib qo'ydi
qo'shib qo'ydi
Siz nima qilishni so'radingiz, men bir algoritm bilan ta'minladim. SO "siz uchun kodni yozish" xizmati emas va repda 2k likni tashkil qilib, hozirgi kunga qadar kerak.
qo'shib qo'ydi
... "hozirgi kunlarda" emas, "endi bilib oling".
qo'shib qo'ydi
Bu holatda siz tashqaridan so'rayotgan resursdan foydalanishni so'rayapsiz.
qo'shib qo'ydi
Bu holatda siz tashqaridan so'rayotgan resursdan foydalanishni so'rayapsiz.
qo'shib qo'ydi
Agar sizda namunalarning vektoriga murojaat qilishni istasangiz, unda nima uchun matematikani oddiygina qila olmaysiz?
qo'shib qo'ydi
Agar sizda namunalarning vektoriga murojaat qilishni istasangiz, unda nima uchun matematikani oddiygina qila olmaysiz?
qo'shib qo'ydi

## 5 javoblar

Bu siz so'ragan narsalar uchun to'liq bo'lmasligi mumkin, lekin Ustel harakatlanuvchi o'rtacha (EMA) odatda shunday yozgan:

``````double exp_avg(double avg, double sample, double sample_weight)
{
return sample * sample_weight + avg * (1 - sample_weight);
//return avg + (sample - avg) * sample_weight; //equivalent alternative
}
``````

Avvalo, EMA tashkil etilganda, o'rtacha birinchi namunaga teng bo'lishi kerak.

0
qo'shib qo'ydi

Bu siz so'ragan narsalar uchun to'liq bo'lmasligi mumkin, lekin Ustel harakatlanuvchi o'rtacha (EMA) odatda shunday yozgan:

``````double exp_avg(double avg, double sample, double sample_weight)
{
return sample * sample_weight + avg * (1 - sample_weight);
//return avg + (sample - avg) * sample_weight; //equivalent alternative
}
``````

Avvalo, EMA tashkil etilganda, o'rtacha birinchi namunaga teng bo'lishi kerak.

0
qo'shib qo'ydi

Siz so'ragan narsangizni sodda tarzda bajarish.

``````#include
#include
#include

int normalize_weights(double *weights, size_t num_weights)
{
double sum = 0;

for (size_t i = 0; i < num_weights; ++i)
{
if (weights[i] < 0)
return -1;

sum += weights[i];
}

if (sum == 0)
return -1;

for (size_t i = 0; i < num_weights; ++i)
weights[i] /= sum;

return 0;
}

int weighted_avg(double *avg_ptr, const double *samples, size_t num_samples, const double *weights, size_t num_weights)
{
if (num_samples != num_weights)
return -1;

double avg = 0;

for (size_t i = 0; i < num_weights; ++i)
avg += samples[i] * weights[i];

*avg_ptr = avg;

return 0;
}

int main(int argc, char **argv)
{
double weights[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
double samples[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double avg;

if (normalize_weights(weights, sizeof(weights)/sizeof(weights)))
abort();

for (size_t i = 0; i < sizeof(weights)/sizeof(weights); ++i)
printf("Normalized weight[%u] = %lf\n", (unsigned) i, weights[i]);

if (weighted_avg(&avg, samples, sizeof(samples)/sizeof(samples), weights, sizeof(weights)/sizeof(weights)))
abort();

printf("\nWeighted average is %lf\n", avg);

return 0;
}
``````
0
qo'shib qo'ydi

Siz so'ragan narsangizni sodda tarzda bajarish.

``````#include
#include
#include

int normalize_weights(double *weights, size_t num_weights)
{
double sum = 0;

for (size_t i = 0; i < num_weights; ++i)
{
if (weights[i] < 0)
return -1;

sum += weights[i];
}

if (sum == 0)
return -1;

for (size_t i = 0; i < num_weights; ++i)
weights[i] /= sum;

return 0;
}

int weighted_avg(double *avg_ptr, const double *samples, size_t num_samples, const double *weights, size_t num_weights)
{
if (num_samples != num_weights)
return -1;

double avg = 0;

for (size_t i = 0; i < num_weights; ++i)
avg += samples[i] * weights[i];

*avg_ptr = avg;

return 0;
}

int main(int argc, char **argv)
{
double weights[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
double samples[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double avg;

if (normalize_weights(weights, sizeof(weights)/sizeof(weights)))
abort();

for (size_t i = 0; i < sizeof(weights)/sizeof(weights); ++i)
printf("Normalized weight[%u] = %lf\n", (unsigned) i, weights[i]);

if (weighted_avg(&avg, samples, sizeof(samples)/sizeof(samples), weights, sizeof(weights)/sizeof(weights)))
abort();

printf("\nWeighted average is %lf\n", avg);

return 0;
}
``````
0
qo'shib qo'ydi

Siz so'ragan narsangizni sodda tarzda bajarish.

``````#include
#include
#include

int normalize_weights(double *weights, size_t num_weights)
{
double sum = 0;

for (size_t i = 0; i < num_weights; ++i)
{
if (weights[i] < 0)
return -1;

sum += weights[i];
}

if (sum == 0)
return -1;

for (size_t i = 0; i < num_weights; ++i)
weights[i] /= sum;

return 0;
}

int weighted_avg(double *avg_ptr, const double *samples, size_t num_samples, const double *weights, size_t num_weights)
{
if (num_samples != num_weights)
return -1;

double avg = 0;

for (size_t i = 0; i < num_weights; ++i)
avg += samples[i] * weights[i];

*avg_ptr = avg;

return 0;
}

int main(int argc, char **argv)
{
double weights[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
double samples[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double avg;

if (normalize_weights(weights, sizeof(weights)/sizeof(weights)))
abort();

for (size_t i = 0; i < sizeof(weights)/sizeof(weights); ++i)
printf("Normalized weight[%u] = %lf\n", (unsigned) i, weights[i]);

if (weighted_avg(&avg, samples, sizeof(samples)/sizeof(samples), weights, sizeof(weights)/sizeof(weights)))
abort();

printf("\nWeighted average is %lf\n", avg);

return 0;
}
``````
0
qo'shib qo'ydi