VB 数理统计实用算法-假设检验,visual basic 代码。
源代码在线查看: 平均数检验f1.frm
VERSION 5.00
Begin VB.Form frmCalculate
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "单总体平均数假设检验"
ClientHeight = 4710
ClientLeft = 60
ClientTop = 450
ClientWidth = 5790
LinkTopic = "Form1"
ScaleHeight = 4710
ScaleWidth = 5790
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton cmdRenew
Caption = "重 作"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 2160
TabIndex = 22
Top = 0
Width = 1215
End
Begin VB.TextBox txtQ
Alignment = 2 'Center
Appearance = 0 'Flat
Height = 270
Left = 3120
TabIndex = 4
Top = 2040
Width = 1455
End
Begin VB.TextBox txtN
Alignment = 2 'Center
Appearance = 0 'Flat
Height = 270
Left = 4200
TabIndex = 3
Top = 1320
Width = 975
End
Begin VB.TextBox txtD
Alignment = 2 'Center
Appearance = 0 'Flat
Height = 270
Left = 2520
TabIndex = 2
ToolTipText = "无总体标准差时,用样本标准差代替"
Top = 1320
Width = 975
End
Begin VB.OptionButton Option3
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "Ho:u < = uo + C"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 375
Left = 120
TabIndex = 18
Top = 1920
Width = 2175
End
Begin VB.TextBox txtUo
Alignment = 2 'Center
Appearance = 0 'Flat
Height = 270
Left = 4200
TabIndex = 1
Top = 720
Width = 975
End
Begin VB.TextBox txtU
Alignment = 2 'Center
Appearance = 0 'Flat
Height = 270
Left = 2520
TabIndex = 0
ToolTipText = "通常原假设中的C为0。如果原假设为H:u-u0 Top = 720
Width = 975
End
Begin VB.OptionButton Option2
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "Ho:u > = uo + C"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 375
Left = 120
TabIndex = 15
Top = 1440
Width = 2055
End
Begin VB.OptionButton Option1
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "Ho:u = uo + C"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 375
Left = 120
TabIndex = 14
Top = 960
Value = -1 'True
Width = 1935
End
Begin VB.CommandButton cmdExit
Caption = "退 出"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 1080
TabIndex = 6
Top = 0
Width = 1095
End
Begin VB.CommandButton cmdCheck
Caption = "检 验"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 0
TabIndex = 5
Top = 0
Width = 1095
End
Begin VB.Line Line1
X1 = 0
X2 = 5760
Y1 = 2400
Y2 = 2400
End
Begin VB.Label Label9
Alignment = 2 'Center
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "显著性水平(通常为 0.05 或 0.01)"
ForeColor = &H80000008&
Height = 255
Left = 2400
TabIndex = 21
Top = 1800
Width = 3015
End
Begin VB.Label Label7
Alignment = 2 'Center
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "样本数"
ForeColor = &H80000008&
Height = 255
Left = 4200
TabIndex = 20
Top = 1080
Width = 975
End
Begin VB.Label Label4
Alignment = 2 'Center
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "总体或样本标准差"
ForeColor = &H80000008&
Height = 255
Left = 2280
TabIndex = 19
ToolTipText = "在下面的文本框键入标准差"
Top = 1080
Width = 1575
End
Begin VB.Label Label3
Alignment = 2 'Center
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "样本平均数"
ForeColor = &H80000008&
Height = 255
Left = 4200
TabIndex = 17
ToolTipText = "在下面的文本框键入样本平均数"
Top = 480
Width = 975
End
Begin VB.Label Label2
Alignment = 2 'Center
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "总体平均数"
ForeColor = &H80000008&
Height = 255
Left = 2400
TabIndex = 16
ToolTipText = "在下面的文本框键入总体平均数"
Top = 480
Width = 1215
End
Begin VB.Label Label1
Alignment = 2 'Center
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "原假设"
BeginProperty Font
Name = "隶书"
Size = 21.75
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 375
Left = 360
TabIndex = 13
Top = 480
Width = 1455
End
Begin VB.Label lblD
Alignment = 2 'Center
Appearance = 0 'Flat
AutoSize = -1 'True
BackColor = &H80000005&
BorderStyle = 1 'Fixed Single
BeginProperty Font
Name = "楷体_GB2312"
Size = 15.75
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 345
Left = 2760
TabIndex = 12
Top = 4200
Width = 225
End
Begin VB.Label Label8
Alignment = 2 'Center
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "检验结论"
BeginProperty Font
Name = "隶书"
Size = 26.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 615
Left = 120
TabIndex = 11
Top = 3600
Width = 5415
End
Begin VB.Label lblZQ
Alignment = 2 'Center
Appearance = 0 'Flat
BackColor = &H80000005&
BorderStyle = 1 'Fixed Single
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 375
Left = 3840
TabIndex = 10
Top = 3120
Width = 1455
End
Begin VB.Label Label6
Alignment = 1 'Right Justify
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "给定显著性水平下的临界值:"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 375
Left = 120
TabIndex = 9
Top = 3120
Width = 3735
End
Begin VB.Label lblZ
Alignment = 2 'Center
Appearance = 0 'Flat
BackColor = &H80000005&
BorderStyle = 1 'Fixed Single
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 375
Left = 3840
TabIndex = 8
Top = 2640
Width = 1455
End
Begin VB.Label Label5
Alignment = 1 'Right Justify
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "检验值:"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 375
Left = 240
TabIndex = 7
Top = 2640
Width = 3615
End
End
Attribute VB_Name = "frmCalculate"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'平均数假设检验
Option Explicit
Private Sub Form_Load()
Label5.Visible = False: Label6.Visible = False
Label8.Visible = False: lblZ.Visible = False
lblZQ.Visible = False: lblD.Visible = False
Line1.Visible = False: cmdRenew.Visible = False
End Sub
'计算
Private Sub cmdCheck_Click()
'u是总体平均值,u0是样本平均值,d是总体或样本标准差
Dim u As Single, u0 As Single, d As Single
'n是样本数,Q是显著性水平
Dim n As Integer, Q As Double
'Z是检验值,ZQ是临界值
Dim Z As Double, ZQ As Double
Dim sngZ As Single, sngZQ As Single
Label5.Visible = True: Label6.Visible = True
Label8.Visible = True: lblZ.Visible = True
lblZQ.Visible = True: lblD.Visible = True
u0 = Val(txtUo): u = Val(txtU): d = Val(txtD)
n = Val(txtN): Q = Val(txtQ)
Line1.Visible = True: cmdRenew.Visible = True
'原假设为H0:u=u0的情况属于双尾检测,其余两种情况都是单尾检测
'即Option1所代表的情况属于双尾检测,Option2和Option3都是单尾检测
If Option1.Value = True Then Q = Q / 2 '双尾检测
Z = (u0 - u) * Sqr(n) / d
sngZ = Z '检验值
lblZ = sngZ
'大样本时求正态分布的分位数,小样本时求t分布的分位数
If n > 17 Then PNorm Q, ZQ Else PT_DIST n - 1, Q, ZQ
ZQ = Sgn(Z) * ZQ '临界值
sngZQ = ZQ
lblZQ = sngZQ
If Abs(Z) > Abs(ZQ) Then
lblD = "拒绝H0,接受H1"
Else
lblD = "接受H0,拒绝H1"
End If
End Sub
'重作
Private Sub cmdRenew_Click()
Label5.Visible = False: Label6.Visible = False
Label8.Visible = False: lblZ.Visible = False
lblZQ.Visible = False: lblD.Visible = False
Line1.Visible = False
txtUo = "": txtU = "": txtD = ""
txtN = "": txtQ = ""
End Sub
'退出
Private Sub cmdExit_Click()
Unload Me
End
End Sub