-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multinomial.cs
152 lines (147 loc) · 5.89 KB
/
Multinomial.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
150
151
152
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;
namespace VISAP商科应用
{
public partial class Multinomial : Form
{
public static string[] Classification;
public static int[] CountTimes;
StringBuilder Result = new StringBuilder();
public static List<int> Alphas = new List<int>();
//Alphas是狄里克利函数的一系列参数
public Multinomial()
{
InitializeComponent();
button_PosteriorDensity.Enabled = false;
Tabulation.RenewColsItems(MainForm.MainDT, comboBox_Cols);
}
private void comboBox_Cols_TextChanged(object sender, EventArgs e)
{
int ColNum = Tabulation.FindCol(MainForm.MainDT, comboBox_Cols.Text);
if (ColNum != -1)
{
Classification = Tabulation.Classification(MainForm.MainDT, ColNum);
int len = Classification.Length;
if (len > 0)
{
listBox_MultiChoices.Items.Clear();
listBox_MultiChoices.Items.AddRange(Classification);
textBox_Times.Clear();
for (int i = 0; i < len; i++)
{
textBox_Times.AppendText("0\r\n");
}
CountTimes = Tabulation.LikilihoodCount(Classification, MainForm.MainDT, ColNum);
StringBuilder OutPut = new StringBuilder();
for (int i = 0; i < len; i++)
{
OutPut.Append(Classification[i]);
OutPut.Append("\t");
OutPut.Append(CountTimes[i].ToString());
OutPut.Append("\r\n");
}
textBox_Likihood.Clear();
textBox_Likihood.AppendText(OutPut.ToString());
}
}
}
private void textBox_Times_TextChanged(object sender, EventArgs e)
{
string[] Str = textBox_Times.Text.Replace("\r", "").Split('\n');
int sum = 0;
int Temp = 0;
foreach (string EachStr in Str)
{
if (Int32.TryParse(EachStr, out Temp))
{
sum += Temp;
}
}
label_SampleSize.Text = "您的先验信息相当于"+sum.ToString()+ "个样本";
}
private void button_Generate_Click(object sender, EventArgs e)
{
Result.Clear();
string[] Str = textBox_Times.Text.Replace("\r", "").Split('\n');
List<int> Nums = new List<int>();
int Temp = 0;
foreach (string EachStr in Str)
{
if (Int32.TryParse(EachStr, out Temp))
{
Nums.Add(Temp);
}
}
int ClassLen = Classification.Length;
int CountTimesLen = CountTimes.Length;
if (Stat.DirichletAlphas(Nums,ClassLen, ref Alphas))
{
if (ClassLen != 0 && CountTimesLen != 0)
{
Result.Append("先验分布: ");
Result.Append("Dirichlet(");
for (int i = 0; i < ClassLen; i++)
{
Result.Append(Alphas[i].ToString());
if (i < ClassLen - 1)
{
Result.Append(", ");
}
}
Result.Append(")\r\n");
Stat.ConjugateDirichlet(ref Alphas, CountTimes);
Result.Append("似然函数: ");
Result.Append("Binomial(");
double p =0;
double sum = CountTimes.Sum();
Result.Append(sum.ToString());
Result.Append(", ");
for (int i = 0; i < ClassLen; i++)
{
p= ((double)CountTimes[i]) / sum;
Result.Append(MathV.NumberPolish(p.ToString()).Trim());
if (i < ClassLen - 1)
{
Result.Append(", ");
}
}
Result.Append(")\r\n");
Result.Append("Dirichlet(");
for (int i = 0; i < ClassLen; i++)
{
Result.Append(Alphas[i].ToString());
if (i < ClassLen - 1)
{
Result.Append(", ");
}
}
Result.Append(")\r\n");
Result.Append("后验分布等效样本量: ");
Result.Append((Alphas.Sum() - Alphas.Count).ToString());
Result.Append("\r\n");
MainForm.S.richTextBox1.AppendText(Result.ToString());
MainForm.S.richTextBox1.Select();//让RichTextBox获得焦点
MainForm.S.richTextBox1.Select(MainForm.S.richTextBox1.TextLength, 0);//将插入符号置于文本结束处
MainForm.S.richTextBox1.ScrollToCaret();
button_PosteriorDensity.Enabled = true;
}
}
else
{
MessageBox.Show("输入次数缺失!");
}
}
private void button_PosteriorDensity_Click(object sender, EventArgs e)
{
DirichletPosterior DirPostForm = new DirichletPosterior();
DirPostForm.Show();
}
}
}