Você está na página 1de 19

Uso del modo de estadsticas

Cuando use el modo de estadsticas, podr especificar los datos para los que desee calcular
estadsticas y luego realizar los clculos. Cuando especifique los datos, aparecern en el rea de
historial y los nmeros de los valores especificados aparecern en el rea de clculo.
1. Haga clic en el men Ver y, a continuacin, haga clic en Estadsticas.
2. Escriba o haga clic en el primer grupo de datos y haga clic en Agregar para agregar los
datos al conjunto de datos.
3. Haga clic en el botn correspondiente al clculo estadstico que desee realizar:
Botn Funcin

Media de los valores

Media del cuadrado de los valores

Suma de los valores

Suma del cuadrado de los valores

Desviacin estndar

Desviacin estndar de poblacin

Uso del historial de clculos





Microsoft Visual C# Application: Probability
Distribution


In statistics, a random variable is a numerical value gotten by chance. For example,
if you have a coin and throw it up. When it falls, either of the sides, the face or the
back, should have the same likelihood of being selected, that is, of facing up. The
face that shows up has a value referred to as a random variable. Thelikelihood of
somethingoccuringiscalled a probability.
A random variable is usually expressed as x.
A probability distribution is the probability that a random variable would occur.
In most calculations, both the random variables and the probabilities (of each
random variable occuring) are given to you, as a list or in a table.
The sum of all probabilities is expressed as:
P(x)
To have an effective probability distribution, the values considered must follow these
two requirements:

i. (x) = 1: The sum of all probabilities must be equal to 1
ii. 0 P(x) 1: Each value of the probabilities must be between 0 and 1 included
If these two requirements are met, then you can perform your calculations.
Application: StartingtheApplication

1. Start Microsoft Visual Studio
2. To create a new application, on the main menu, click File -> New Project...
3. In the middle list, click Windows Forms Application
4. ChangetheNameto ProbabilityDistribution1
5. Click OK
6. In the Solution Explorer, right-click Form1.cs and click Rename
7. Type Exercise.cs and press Enter
8. Design the form as follows:


Control Name Text
Label

Values:
Label

x
Label

P(x)
TextBox

txtX
TextBox

txtPofX
Button

btnAdd Add
ListView

lvwValues
FullRowSelect: True
GridLines: True
View: Details
Columns
(Name) Text TextAlign Width
colX x 40
colPofX P(x) Center
Label

Number of Values:
TextBox

txtCount
Label

P(x):
TextBox

txtSum

9. Double-click an unoccupied area of the form
10. Change the file as follows:
11. usingSystem;
12. usingSystem.Collections.Generic;
13. usingSystem.ComponentModel;
14. usingSystem.Data;
15. usingSystem.Drawing;
16. usingSystem.Linq;
17. usingSystem.Text;
18. usingSystem.Windows.Forms;
19.
20. namespace ProbabilityDistribution1
21. {
22. publicpartialclassExercise : Form
23. {
24. List<Distribution>values;
25.
26. publicExercise()
27. {
28. InitializeComponent();
29. }
30.
31. private void Exercise_Load(object sender, EventArgs e)
32. {
33. values = new List<Distribution>();
34. }
35.
36. voidShowValues()
37. {
38. double sum = 0.00;
39. lvwValues.Items.Clear();
40.
41. foreach(Distribution dist in values)
42. {
43. ListViewItemlviValue = new ListViewItem(dist.X.ToString());
44. lviValue.SubItems.Add(dist.PofX.ToString());
45. lvwValues.Items.Add(lviValue);
46. }
47.
48. txtCount.Text = values.Count.ToString();
49.
50. // Calculate the sum of the P(x) values
51. foreach (Distribution d in values)
52. sum += d.PofX;
53. txtSum.Text = sum.ToString();
54. }
55. }
56.
57. publicclassDistribution
58. {
59. public int X { get; set; }
60. public double PofX { get; set; }
61.
62. public Distribution(int x, double p)
63. {
64. X = x;
65. PofX = p;
66. }
67. }
}
68. Return to the form and double-click the Add button
69. Implement the event as follows:
70. private void btnAdd_Click(object sender, EventArgs e)
71. {
72. int x = 0;
73. double p = 0.00, sum = 0.00;
74. Distributiondist = null;
75.
76. // Check that the user entered a value for x
77. if (txtX.Text.Length == 0)
78. {
79. MessageBox.Show("You must enter the x value.",
80. "ProbabilityDistribution");
81. return;
82. }
83.
84. // Test that the user entered a value for P(x)
85. if (txtPofX.Text.Length == 0)
86. {
87. MessageBox.Show("You must enter the P(x) value.",
88. "ProbabilityDistribution");
89. return;
90. }
91.
92. // Getthevaluefor x
93. try
94. {
95. x = int.Parse(txtX.Text);
96. }
97. catch (FormatException)
98. {
99. MessageBox.Show("The value you entered is invalid.",
100. "ProbabilityDistribution");
101. }
102.
103. // Get the value for P(x)
104. try
105. {
106. p = double.Parse(txtPofX.Text);
107. }
108. catch (FormatException)
109. {
110. MessageBox.Show("The value you entered is invalid.",
111. "ProbabilityDistribution");
112. }
113.
114. // Create a Distributionvalue
115. dist = new Distribution(x, p);
116. // Add the value to the list
117. values.Add(dist);
118.
119. ShowValues();
120.
121. // Calculate the sum of the P(x) values
122. foreach (Distribution d in values)
123. sum += d.PofX;
124. txtSum.Text = sum.ToString("F");
125.
126. txtX.Text = "";
127. txtPofX.Text = "";
128. txtX.Focus();
129.
130. // Test thefirstrequirement
131. if (sum != 1) // The first rule is not respected
132. {
133. MessageBox.Show("The first rule is not respected",
134. "ProbabilityDistribution");
135. return;
136. }
137.
138. // Test thesecondrequirement
139. foreach (Distribution d in values)
140. {
141. if ((d.PofX< 0.00) || (d.PofX> 1)) // The second rule is
not respected
142. {
143. MessageBox.Show("The second rule is not respected",
144. "ProbabilityDistribution");
145. return;
146. }
147. }
}
148. Returntotheform
149. Toexecute, press F5
150. Enter values and click Add each time


151. Close the form and return to your programming environment
The Mean of a Probability Distribution

The mean is the average of a set. To calculate it for a probability distribution, use the
following formula:
= [x . P(x)]
Application: Calculating the Mean of Probability
Distribution

1. Change the design of the form as follows:


Control Name Text
ListView

lvwValues
FullRowSelect: True
GridLines: True
View: Details
Columns
(Name) Text TextAlign Width
colX x 40
colPofX P(x) Center
colXTimesPofX x . P(x) Center 100
Label

Mean of ProbabilityDistribution:
TextBox

txtMean

2. Double-clicktheAddbutton
3. change the event as follows:
4. voidShowValues()
5. {
6. double sum = 0.00;
7. double mean = 0.00;
8. lvwValues.Items.Clear();
9.
10. foreach (Distribution dist in values)
11. {
12. ListViewItemlviValue = new ListViewItem(dist.X.ToString());
13. lviValue.SubItems.Add(dist.PofX.ToString());
14. mean += dist.X * dist.PofX;
15. lviValue.SubItems.Add(dist.X.ToString() + " * " +
16. dist.PofX.ToString() + " = " +
17. mean.ToString());
18. lvwValues.Items.Add(lviValue);
19. }
20.
21. foreach (Distribution d in values)
22. sum += d.PofX;
23.
24. txtCount.Text = values.Count.ToString();
25. txtSum.Text = sum.ToString();
26. txtMean.Text = mean.ToString();
}
27. Toexecute, press F5
28. Type some values and click Add after each


29. Close the form and return to your programming environment
The Variance for a Probability Distribution

A variance is a process of representing a probability distribution by showing by how
much the values of a series are away of the mean.
The formula to calculate the variance for a probability distribution is:

2
= [(x - )
2
. P(x)]
Another formula you can use is:

2
= [x
2
. P(x)] -
2

Application: CalculatingtheVariance

1. Change the design of the form as follows:


Control (Name) Text
ListView

lvwValues
FullRowSelect: True
GridLines: True
View: Details
Columns
(Name) Text TextAlign Width
colX x 40
colPofX P(x) Center
colXTimesPofX x . P(x) Center 100
colVariance (x - )
2
* P(x) 180
Label

Variance of ProbabilityDistribution:
TextBox

txtVariance

2. Double-clicktheCalculatebutton
3. Change the file as follows:
4. . . . No Change
5.
6. voidShowValues()
7. {
8. lvwValues.Items.Clear();
9.
10. foreach (Distribution dist in values)
11. {
12. ListViewItemlviValue = new ListViewItem(dist.X.ToString());
13. lviValue.SubItems.Add(dist.PofX.ToString());
14.
15. lviValue.SubItems.Add(dist.X.ToString() + " * " +
16. dist.PofX.ToString() + " = " +
17. (dist.X * dist.PofX).ToString());
18. lvwValues.Items.Add(lviValue);
19. }
20.
21. txtCount.Text = values.Count.ToString();
22. }
23.
24. . . . No Change
25.
26. private void btnCalculate_Click(object sender, EventArgs e)
27. {
28. double mean = 0.00, variance = 0.00;
29.
30. foreach (Distribution dist in values)
31. mean += (dist.X * dist.PofX);
32.
33. lvwValues.Items.Clear();
34.
35. foreach (Distribution dist in values)
36. {
37. ListViewItemlviValue = new ListViewItem(dist.X.ToString());
38. lviValue.SubItems.Add(dist.PofX.ToString());
39.
40. lviValue.SubItems.Add(dist.X.ToString() + " * " +
41. dist.PofX.ToString() + " = " +
42. (dist.X * dist.PofX).ToString());
43. lviValue.SubItems.Add("(" + dist.X.ToString() + " - " +
44. mean.ToString() + ") * " +
45. dist.PofX.ToString() + " = " +
46. Math.Pow(dist.X - mean, 2) * dist.PofX);
47. lvwValues.Items.Add(lviValue);
48. }
49.
50. foreach (Distribution dist in values)
51. variance += Math.Pow(dist.X - mean, 2) * dist.PofX;
52.
53. txtMean.Text = mean.ToString();
54. txtVariance.Text = variance.ToString();
}
55. Returntotheform
56. Toexecute, press F5
57. Test the form with some values:


58. ClickCalculate


59. Close the form and return to your programming environment
The Standard Deviation for a Probability Distribution

The standard deviation is a value that indicates by how much the elements of a
series vary from the mean. The formula to calculate the standard deviation for a
probability distribution is:

Application: Calculating the Standard Deviation

1. Change the design of the form as follows:


Control (Name) Text
ListView

lvwValues
FullRowSelect: True
GridLines: True
View: Details
Label

Standard Deviation of Probability Distribution:
TextBox

txtStdDev
Button

btnClear Clear
Button

btnClose Close

2. Double-clicktheCalculatebutton
3. Returntotheform
4. Double-clickthe Clear button
5. Returntotheform
6. Double-clicktheClosebutton
7. Change the file as follows:
8. usingSystem;
9. usingSystem.Collections.Generic;
10. usingSystem.ComponentModel;
11. usingSystem.Data;
12. usingSystem.Drawing;
13. usingSystem.Linq;
14. usingSystem.Text;
15. usingSystem.Windows.Forms;
16.
17. namespace ProbabilityDistribution1
18. {
19. publicpartialclassExercise : Form
20. {
21. List<Distribution>values;
22.
23. publicExercise()
24. {
25. InitializeComponent();
26. }
27.
28. private void Exercise_Load(object sender, EventArgs e)
29. {
30. values = new List<Distribution>();
31. }
32.
33. voidShowValues()
34. {
35. lvwValues.Items.Clear();
36.
37. foreach (Distribution dist in values)
38. {
39. ListViewItemlviValue = new ListViewItem(dist.X.ToString());
40. lviValue.SubItems.Add(dist.PofX.ToString());
41.
42. lviValue.SubItems.Add(dist.X.ToString() + " * " +
43. dist.PofX.ToString() + " = " +
44. (dist.X *
dist.PofX).ToString());
45. lvwValues.Items.Add(lviValue);
46. }
47.
48. txtCount.Text = values.Count.ToString();
49. }
50.
51. private void btnAdd_Click_1(object sender, EventArgs e)
52. {
53. int x = 0;
54. double p = 0.00, sum = 0.00;
55. Distributiondist = null;
56.
57. // Check that the user entered a value for x
58. if (txtX.Text.Length == 0)
59. {
60. MessageBox.Show("You must enter the x value.",
61. "ProbabilityDistribution");
62. return;
63. }
64.
65. // Test that the user entered a value for P(x)
66. if (txtPofX.Text.Length == 0)
67. {
68. MessageBox.Show("You must enter the P(x) value.",
69. "ProbabilityDistribution");
70. return;
71. }
72.
73. // Getthevaluefor x
74. try
75. {
76. x = int.Parse(txtX.Text);
77. }
78. catch (FormatException)
79. {
80. MessageBox.Show("The value you entered is invalid.",
81. "ProbabilityDistribution");
82. }
83.
84. // Get the value for P(x)
85. try
86. {
87. p = double.Parse(txtPofX.Text);
88. }
89. catch (FormatException)
90. {
91. MessageBox.Show("The value you entered is invalid.",
92. "ProbabilityDistribution");
93. }
94.
95. // Create a Distributionvalue
96. dist = new Distribution(x, p);
97. // Add the value to the list
98. values.Add(dist);
99.
100. ShowValues();
101.
102. // Calculate the sum of the P(x) values
103. foreach (Distribution d in values)
104. sum += d.PofX;
105. txtSum.Text = sum.ToString("F");
106.
107. txtX.Text = "";
108. txtPofX.Text = "";
109. txtX.Focus();
110.
111. // Test the requirements of probability distribution
112. if (sum != 1) // The first rule is not respected
113. {
114. MessageBox.Show("The first rule is not respected",
115. "ProbabilityDistribution");
116. return;
117. }
118.
119. foreach (Distribution d in values)
120. {
121. // Checkthesecondrequirement
122. if ((d.PofX< 0.00) || (d.PofX> 1))
123. {
124. MessageBox.Show("The second rule is not respected",
125. "ProbabilityDistribution");
126. return;
127. }
128. }
129. }
130.
131. private void btnCalculate_Click(object sender, EventArgs
e)
132. {
133. double mean = 0.00, variance = 0.00;
134. double sum = 0.00, stddev = 0.00;
135.
136. foreach (Distribution dist in values)
137. mean += (dist.X * dist.PofX);
138.
139. lvwValues.Items.Clear();
140.
141. foreach (Distribution dist in values)
142. {
143. ListViewItemlviValue = new ListViewItem(dist.X.ToString());
144. lviValue.SubItems.Add(dist.PofX.ToString());
145.
146. lviValue.SubItems.Add(dist.X.ToString() + " * " +
147. dist.PofX.ToString() + " = " +
148. (dist.X *
dist.PofX).ToString());
149. lviValue.SubItems.Add("(" + dist.X.ToString() + " - " +
150. mean.ToString() + ")
2
* " +
151. dist.PofX.ToString() + " = " +
152. Math.Pow(dist.X - mean, 2) * dist.PofX);
153. lvwValues.Items.Add(lviValue);
154. }
155.
156. foreach (Distribution dist in values)
157. variance += Math.Pow(dist.X - mean, 2) *
dist.PofX;
158.
159. foreach (Distribution dist in values)
160. sum += Math.Pow(dist.X, 2) * dist.PofX;
161.
162. stddev = Math.Sqrt(sum - Math.Pow(mean, 2));
163.
164. txtMean.Text = mean.ToString();
165. txtVariance.Text = variance.ToString();
166. txtStdDev.Text = stddev.ToString();
167. }
168.
169. private void btnClear_Click(object sender, EventArgs e)
170. {
171. txtX.Text = "";
172. txtPofX.Text = "";
173. lvwValues.Items.Clear();
174. txtCount.Text = "";
175. txtSum.Text = "";
176. txtMean.Text = "";
177. txtVariance.Text = "";
178. txtStdDev.Text = "";
179. }
180.
181. private void btnClose_Click(object sender, EventArgs e)
182. {
183. Close();
184. }
185. }
186.
187. publicclassDistribution
188. {
189. public int X { get; set; }
190. public double PofX { get; set; }
191.
192. public Distribution(int x, double p)
193. {
194. X = x;
195. PofX = p;
196. }
197. }
}
198. Toexecute, press F5
199. Test the form with some values:


200. ClickCalculate


201. Close the form and return to your programming environment

Você também pode gostar