urgente - calculadora estadistica en visual c sharp

20
Uso del modo de estadísticas Cuando use el modo de estadísticas, podrá especificar los datos para los que desee calcular estadísticas y luego realizar los cálculos. Cuando especifique los datos, aparecerán en el área de historial y los números de los valores especificados aparecerán en el área de cálculo. 1. Haga clic en el menú Ver y, a continuación, haga clic en Estadísticas. 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 botón correspondiente al cálculo estadístico que desee realizar: Botón Función Media de los valores Media del cuadrado de los valores Suma de los valores Suma del cuadrado de los valores Desviación estándar Desviación estándar de población Uso del historial de cálculos

Upload: igor-santiago-monrroy-peralta

Post on 27-Dec-2015

23 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Urgente - Calculadora Estadistica en Visual c Sharp

Uso del modo de estadísticas

Cuando use el modo de estadísticas, podrá especificar los datos para los que desee

calcular estadísticas y luego realizar los cálculos. Cuando especifique los datos,

aparecerán en el área de historial y los números de los valores especificados

aparecerán en el área de cálculo.

1. Haga clic en el menú Ver y, a continuación, haga clic en Estadísticas.

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 botón correspondiente al cálculo estadístico que desee realizar:

Botón Función

Media de los valores

Media del cuadrado de los valores

Suma de los valores

Suma del cuadrado de los valores

Desviación estándar

Desviación estándar de población

Uso del historial de cálculos

Page 2: Urgente - Calculadora Estadistica en Visual c Sharp

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: 

Page 3: Urgente - Calculadora Estadistica en Visual c Sharp

Control Name Text

Label   Values:

Label   x

Label   P(x)

TextBox txtX  

TextBox txtPofX  

Button btnAdd Add

ListView lvwValuesFullRowSelect: TrueGridLines: TrueView: 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:

Page 4: Urgente - Calculadora Estadistica en Visual c Sharp

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 ProbabilityDistribution121. {22. publicpartialclassExercise : Form23. {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) values51. foreach (Distribution d in values)52. sum += d.PofX;53. txtSum.Text = sum.ToString();54. }55. }56.57. publicclassDistribution58. {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. }

Page 5: Urgente - Calculadora Estadistica en Visual c Sharp

}

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 x77. 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 x93. try94. {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. try105. {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 Distributionvalue115. dist = new Distribution(x, p);116. // Add the value to the list117. values.Add(dist);118.119. ShowValues();120.121. // Calculate the sum of the P(x) values

Page 6: Urgente - Calculadora Estadistica en Visual c Sharp

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 thefirstrequirement131. if (sum != 1) // The first rule is not respected132. {133. MessageBox.Show("The first rule is not respected",134. "ProbabilityDistribution");135. return;136. }137.138. // Test thesecondrequirement139. foreach (Distribution d in values)140. {141. if ((d.PofX< 0.00) || (d.PofX> 1)) // The second rule is

not respected142. {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 

Page 7: Urgente - Calculadora Estadistica en Visual c Sharp

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 lvwValuesFullRowSelect: TrueGridLines: TrueView: 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

Page 8: Urgente - Calculadora Estadistica en Visual c Sharp

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

Page 9: Urgente - Calculadora Estadistica en Visual c Sharp

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 = Σ[x2 . P(x)] - µ2

Application: CalculatingtheVariance

1. Change the design of the form as follows: 

Control (Name) Text

ListView lvwValuesFullRowSelect: TrueGridLines: TrueView: 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:

Page 10: Urgente - Calculadora Estadistica en Visual c Sharp

TextBox txtVariance  

2. Double-clicktheCalculatebutton

3. Change the file as follows:

4. . . . No Change5.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 Change25.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();

Page 11: Urgente - Calculadora Estadistica en Visual c Sharp

}

55. Returntotheform

56. Toexecute, press F5

57. Test the form with some values: 

Page 12: Urgente - Calculadora Estadistica en Visual c Sharp

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: 

Page 13: Urgente - Calculadora Estadistica en Visual c Sharp

Control (Name) Text

ListView

lvwValues

FullRowSelect: TrueGridLines: TrueView: 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;

Page 14: Urgente - Calculadora Estadistica en Visual c Sharp

12. usingSystem.Drawing;13. usingSystem.Linq;14. usingSystem.Text;15. usingSystem.Windows.Forms;16.17. namespace ProbabilityDistribution118. {19. publicpartialclassExercise : Form20. {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 x58. 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. {

Page 15: Urgente - Calculadora Estadistica en Visual c Sharp

68. MessageBox.Show("You must enter the P(x) value.",69. "ProbabilityDistribution");70. return;71. }72.73. // Getthevaluefor x74. try75. {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. try86. {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 Distributionvalue96. dist = new Distribution(x, p);97. // Add the value to the list98. values.Add(dist);99.100. ShowValues();101.102. // Calculate the sum of the P(x) values103. 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 distribution112. if (sum != 1) // The first rule is not respected113. {114. MessageBox.Show("The first rule is not respected",115. "ProbabilityDistribution");116. return;117. }118.119. foreach (Distribution d in values)120. {121. // Checkthesecondrequirement122. if ((d.PofX< 0.00) || (d.PofX> 1))123. {124. MessageBox.Show("The second rule is not respected",

Page 16: Urgente - Calculadora Estadistica en Visual c Sharp

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 = "";

Page 17: Urgente - Calculadora Estadistica en Visual c Sharp

179. }180.181. private void btnClose_Click(object sender, EventArgs e)182. {183. Close();184. }185. }186.187. publicclassDistribution188. {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: 

Page 18: Urgente - Calculadora Estadistica en Visual c Sharp

200. ClickCalculate 

201. Close the form and return to your programming environment