Funcions VB.NET per a estadística descriptiva: mitjana, moda i mediana

Enviado por Pedro y clasificado en Inglés

Escrito el en catalán con un tamaño de 13,09 KB

Module Module1

Public Function Datos(ByVal matriz1() As Double) As Double

Dim D As Double

'guarda en D la longitud de la matriz

D = matriz1.Length

Return D

End Function

Public Function Calcula_Media(ByVal Valores() As Double) As Double

Dim ValorMedio As Double

Dim i As Integer

For i = 0 To Valores.Length - 1

ValorMedio = ValorMedio + Valores(i)

Next i

ValorMedio = ValorMedio / Valores.Length

Return ValorMedio

End Function

Public Function Calcula_Desviación_Típica(ByVal valores() As Double) As Double

Dim ValorMedio As Double

Dim i As Integer

For i = 0 To valores.Length - 1

ValorMedio = ValorMedio + valores(i)

Next i

ValorMedio = ValorMedio / valores.Length

Dim Residuo As Double

For i = 0 To valores.Length - 1

Residuo = Residuo + (valores(i) - ValorMedio) ^ 2

Next i

Dim desviación_tipica As Double

For i = 0 To valores.Length - 1

desviación_tipica = Math.Sqrt(Residuo / (valores.Length - 1))

Next i

Return desviación_tipica

End Function

Public Function Calcula_máximo_valor(ByVal valores() As Double) As Double

Dim valormáximo As Double

Dim i, j As Integer

Dim aux As Double

For i = 0 To valores.Length - 1

For j = 0 To valores.Length - 2

If valores(j) > valores(j + 1) Then

'intercambio de variables

aux = valores(j)

valores(j) = valores(j + 1)

valores(j + 1) = aux

End If

Next

Next

valormáximo = valores(valores.Length - 1)

Return valormáximo

End Function

Public Function Calcula_mínimo_valor(ByVal valores() As Double) As Double

Dim valormínimo As Double

Dim i, j As Integer

Dim aux As Double

For i = 0 To valores.Length - 1

For j = 0 To valores.Length - 2

If valores(j) > valores(j + 1) Then

'intercambio de variables

aux = valores(j)

valores(j) = valores(j + 1)

valores(j + 1) = aux

End If

Next j

Next i

valormínimo = valores(0)

Return valormínimo

End Function

Public Function Calcula_Moda(ByVal valores() As Double) As Double

Dim i, j As Integer

Dim aux As Double

For i = 0 To valores.Length - 1

For j = 0 To valores.Length - 2

If valores(j) > valores(j + 1) Then

aux = valores(j)

valores(j) = valores(j + 1)

valores(j + 1) = aux

End If

Next

Next

Dim número_repetido As Double

Dim repeticiones As Double

Dim número_de_repeticiones As Double

Dim número_repetido_máximo As Double

número_de_repeticiones = 0

repeticiones = 0

número_repetido = 0

número_repetido_máximo = 0

For i = 0 To valores.Length - 1

If valores(i) <> número_repetido Then

If repeticiones > número_de_repeticiones Then

número_de_repeticiones = repeticiones

número_repetido_máximo = número_repetido

End If

número_repetido = valores(i)

repeticiones = 1

Else

repeticiones = repeticiones + 1

End If

Next

If repeticiones > número_de_repeticiones Then

número_de_repeticiones = repeticiones

número_repetido_máximo = número_repetido

End If

Return número_repetido_máximo

End Function

Public Function Calcula_Mediana(ByVal Valores() As Double) As Double

Dim i, j As Integer

Dim aux As Double

For i = 0 To Valores.Length - 1

For j = 0 To Valores.Length - 2

If Valores(j) > Valores(j + 1) Then

aux = Valores(j)

Valores(j) = Valores(j + 1)

Valores(j + 1) = aux

End If

Next

Next

Dim M As Double

If (Valores.Length Mod 2) <> 0 Then

M = Valores((Valores.Length - 1) / 2)

End If

If (Valores.Length Mod 2) = 0 Then

M = (Valores(((Valores.Length - 1) / 2) - 0.5) + Valores(((Valores.Length - 1) / 2) + 0.5)) / 2

End If

Return M

End Function

End Module

Entradas relacionadas: