最近在做bot,有一个算法需要根据已知数据拟合高斯函数求x~max~,索性就把过程记录了下来
高斯函数
高斯函数以大数学家约翰·卡尔·弗里德里希·高斯的名字命名。高斯函数应用范围很广,在自然科学、社会科学、数学以及工程学等领域都能看到它的身影
对于这次要写的算法,高斯函数可以对数据进行拟合和表征,从而预测值
高斯函数可写为:
$$y=ae^{\frac{-(x-b)^2}{2c^2}}$$其中 a 为波峰 ymax , b 为波峰对应的 x 值 xmax , c 为 y=xmax 与 x=0.607ymax 的交点到两边的距离
数据生成
数据生成程序由C++编写
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| #include <math.h>
#include <time.h>
#include <iostream>
#include <random>
using namespace std;
// #pragma GCC optimize(2)
// #pragma G++ optimize(2)
void gaosi(double x, double a, double b, double c) {
double y, flag = -(x - b) * (x - b) / (2 * c * c);
y = a * exp(flag);
cout << x << " " << y << "\n";
}
int main() {
default_random_engine e(time(0));
uniform_real_distribution<double> u(0.1, 20.1);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
double a, b, c;
cin >> a >> b >> c;
double mod = c * 5, index = b - 2.5 * c;
for (int i = 0; i < 20; i++) {
double swap = u(e);
while (swap > mod) {
swap -= mod;
}
swap += index;
gaosi(swap, a, b, c);
}
return 0;
}
|
高斯拟合
拟合的算法我参考了CSDN上的这篇文章
但是似乎总是有哪里不对,但是又分析不上来