-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBayesEstimation.cs
149 lines (141 loc) · 4.84 KB
/
BayesEstimation.cs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace 统计图形界面1
{
public partial class BayesEstimation : Form
{
public BayesEstimation()
{
InitializeComponent();
trackBar1.Maximum = 20;
string a = textBox_a.Text;
string b = textBox_b.Text;
if (a.Trim() != "" && b.Trim() != "" && a != null && b != null)
{
DrawPrior(Convert.ToDouble(a), Convert.ToDouble(b));
}
}
double Gammln(double value){
double [] temp = new double [3];
int i;
double [] data = new double [] {76.180091729471457, -86.505320329416776, 24.014098240830911, -1.231739572450155, 0.001208650973866179, -0.000005395239384953};
temp[0] = value;
temp[1] = value + 5.5;
temp[1] = temp[1] - (value + 0.5) * Math.Log(temp[1]);
temp[2] = 1.0000000001900149;
for (i = 0;i < 6;i++){
temp[0] += 1;
temp[2] += data[i] / temp[0];
}
temp[0] = Math.Log(2.5066282746310007 * temp[2] / value) - temp[1];
return temp[0];
}
double Gamma_Call(double value) {
if (value < 1 ){
return Math.Exp(Gammln(value));
}
else if(value == 1){
return 1;
}
value -= 1;
return value * Gamma_Call(value);
}
double Gamma(double value ){
//概率统计里的gamma函数.注意value的值不要大过171且不能且当value<=0时《sin(value*pi)<>0
double ret = 0;
//bool IsTrue;
if (value > 171 ){
//IsTrue = false;
MessageBox.Show("输入伽玛函数的值不可大于171!");
}
else if (value > 0)
{
ret = Gamma_Call(value);
//MessageBox.Show(ret.ToString());
}
else
{
ret = Math.Sin(Math.PI * value);
if (ret == 0)
{
}
else
{
ret = Math.PI / ret;
ret = ret / Gamma_Call(1 - value);
}
}
return ret;
}
void DrawPrior(double a, double b)
{
chart_prior.Series.Clear();
Series series = new Series();
series.ChartType = SeriesChartType.Spline;
series.BorderWidth = 3;
series.MarkerSize = 6;
series.LegendText = "Beta先验";
double MaxValue = 0;
//series.Color = Color.Blue;
double y = 0;
//MessageBox.Show("Gamma(a + b)"+Gamma(a + b));
//MessageBox.Show("Gamma(a)" + Gamma(a) + "Gamma(b)" + Gamma(b));
double factor = Gamma(a + b) / (Gamma(a) * Gamma(b));
for (double x = 0; x <= 1; x = x + 0.01)
{
y = factor * Math.Pow(x, a - 1) * Math.Pow(1-x, b - 1);
if (x == 0)
{
MaxValue = y;
}
else
{
if (MaxValue < y)
{
MaxValue = y;
}
}
series.Points.AddXY(x, y);
//MessageBox.Show("x = " + x.ToString() + " y = " + y.ToString());
}
chart_prior.Series.Add(series);
var XAxis = chart_prior.ChartAreas[0].AxisX;
XAxis.Maximum = 1;
XAxis.Minimum =0;
var YAxis = chart_prior.ChartAreas[0].AxisY;
YAxis.Maximum = Math.Ceiling(MaxValue);
YAxis.Minimum = 0;
textBox_PriorMean.Text = MathV.round((a / (a + b)).ToString(),4,0);
textBox_PriorVariance.Text = MathV.round(((a * b) / ((a + b) * (a + b) * (a + b + 1))).ToString(),4,0);
}
private void BayesEstimation_Load(object sender, EventArgs e)
{
}
private void button_GeneratePrior_Click(object sender, EventArgs e)
{
string a = textBox_a.Text;
string b = textBox_b.Text;
if (a.Trim() != "" && b.Trim() != "" && a != null && b != null)
{
DrawPrior(Convert.ToDouble(a), Convert.ToDouble(b));
}
}
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
int Position = trackBar1.Value;
double a = Convert.ToDouble(Position + 1);
double b = Convert.ToDouble(20-Position + 1);
textBox_a.Text = a.ToString();
textBox_b.Text = b.ToString();
DrawPrior(a, b);
}
}
}