Given data in an array. Find skewness of the data distribution.
Skewness is a measure of the asymmetry of data distribution. Skewness is an asymmetry in a statistical distribution, in which the curve appears distorted or skewed either to the left or to the right. Skewness can be quantified to define the extent to which a distribution differs from a normal distribution. Skewness can be calculated as
Where gamma is called skewness
sigma is called standard deviation and sigma square can be calculated as
N is number of population and
mu is called mean of data.
Examples :
Input : arr[] = {2.5, 3.7, 6.6, 9.1, 9.5, 10.7, 11.9, 21.5, 22.6, 25.2}
Output : 0.777001
Input : arr[] = {5, 20, 40, 80, 100}
Output : 0.0980392
Implementation:
C++
#include<bits/stdc++.h>
using
namespace
std;
float
mean(
float
arr[],
int
n)
{
float
sum = 0;
for
(
int
i = 0; i < n; i++)
sum = sum + arr[i];
return
sum / n;
}
float
standardDeviation(
float
arr[],
int
n)
{
float
sum = 0;
for
(
int
i = 0; i < n; i++)
sum = (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n));
return
sqrt
(sum / n);
}
float
skewness(
float
arr[],
int
n)
{
float
sum = 0;
for
(
int
i = 0; i < n; i++)
sum = (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n));
return
sum / (n * standardDeviation(arr, n) *
standardDeviation(arr, n) *
standardDeviation(arr, n) *
standardDeviation(arr, n));
}
int
main()
{
float
arr[] = {2.5, 3.7, 6.6, 9.1,
9.5, 10.7, 11.9, 21.5,
22.6, 25.2};
int
n =
sizeof
(arr)/
sizeof
(arr[0]);
cout << skewness(arr, n);
return
0;
}
Java
import
java.io.*;
class
GFG {
static
double
mean(
double
arr[],
int
n)
{
double
sum =
0
;
for
(
int
i =
0
; i < n; i++)
sum = sum + arr[i];
return
sum / n;
}
static
double
standardDeviation(
double
arr[],
int
n)
{
double
sum =
0
;
for
(
int
i =
0
; i < n; i++)
sum = (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n));
return
Math.sqrt(sum / n);
}
static
double
skewness(
double
arr[],
int
n)
{
double
sum =
0
;
for
(
int
i =
0
; i < n; i++)
sum = (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n));
return
sum / (n * standardDeviation(arr, n) *
standardDeviation(arr, n) *
standardDeviation(arr, n) *
standardDeviation(arr, n));
}
public
static
void
main (String[] args)
{
double
arr[] = {
2.5
,
3.7
,
6.6
,
9.1
,
9.5
,
10.7
,
11.9
,
21.5
,
22.6
,
25.2
};
int
n = arr.length;
System.out.println(skewness(arr, n));
}
}
Python3
from
math
import
sqrt
def
mean(arr, n):
summ
=
0
for
i
in
range
(n):
summ
=
summ
+
arr[i]
return
summ
/
n
def
standardDeviation(arr,n):
summ
=
0
for
i
in
range
(n):
summ
=
(arr[i]
-
mean(arr, n))
*
(arr[i]
-
mean(arr, n))
return
sqrt(summ
/
n)
def
skewness(arr, n):
summ
=
0
for
i
in
range
(n):
summ
=
(arr[i]
-
mean(arr, n))
*
(arr[i]
-
mean(arr, n))
*
(arr[i]
-
mean(arr, n))
return
summ
/
(n
*
standardDeviation(arr, n)
*
standardDeviation(arr, n)
*
standardDeviation(arr, n)
*
standardDeviation(arr, n))
arr
=
[
2.5
,
3.7
,
6.6
,
9.1
,
9.5
,
10.7
,
11.9
,
21.5
,
22.6
,
25.2
]
n
=
len
(arr)
print
(
'%.6f'
%
skewness(arr, n))
C#
using
System;
class
GFG {
static
float
mean(
double
[]arr,
int
n)
{
double
sum = 0;
for
(
int
i = 0; i < n; i++)
sum = sum + arr[i];
return
(
float
)sum / n;
}
static
float
standardDeviation(
double
[]arr,
int
n)
{
double
sum = 0 ;
for
(
int
i = 0; i < n; i++)
sum = (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n));
return
(
float
)Math.Sqrt(sum / n);
}
static
float
skewness(
double
[]arr,
int
n)
{
double
sum = 0;
for
(
int
i = 0; i < n; i++)
sum = (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n));
return
(
float
)sum / (n * standardDeviation(arr, n) *
standardDeviation(arr, n) *
standardDeviation(arr, n) *
standardDeviation(arr, n));
}
public
static
void
Main ()
{
double
[]arr = { 2.5, 3.7, 6.6, 9.1,
9.5, 10.7, 11.9, 21.5,
22.6, 25.2 };
int
n = arr.Length;
Console.WriteLine(skewness(arr, n));
}
}
PHP
<?php
function
mean(
$arr
,
$n
)
{
$sum
= 0;
for
(
$i
= 0;
$i
<
$n
;
$i
++)
$sum
=
$sum
+
$arr
[
$i
];
return
$sum
/
$n
;
}
function
standardDeviation(
$arr
,
$n
)
{
$sum
= 0;
for
(
$i
= 0;
$i
<
$n
;
$i
++)
$sum
= (
$arr
[
$i
] - mean(
$arr
,
$n
)) *
(
$arr
[
$i
] - mean(
$arr
,
$n
));
return
sqrt(
$sum
/
$n
);
}
function
skewness(
$arr
,
$n
)
{
$sum
= 0;
for
(
$i
= 0;
$i
<
$n
;
$i
++)
$sum
= (
$arr
[
$i
] - mean(
$arr
,
$n
)) *
(
$arr
[
$i
] - mean(
$arr
,
$n
)) *
(
$arr
[
$i
] - mean(
$arr
,
$n
));
return
$sum
/ (
$n
* standardDeviation(
$arr
,
$n
) *
standardDeviation(
$arr
,
$n
) *
standardDeviation(
$arr
,
$n
) *
standardDeviation(
$arr
,
$n
));
}
$arr
=
array
(2.5, 3.7, 6.6, 9.1, 9.5,
10.7, 11.9, 21.5, 22.6, 25.2);
$n
=
count
(
$arr
);
echo
skewness(
$arr
,
$n
);
?>
Javascript
<script>
function
mean(arr, n)
{
let sum = 0;
for
(let i = 0; i < n; i++)
sum = sum + arr[i];
return
sum / n;
}
function
standardDeviation(arr, n)
{
let sum = 0 ;
for
(let i = 0; i < n; i++)
sum = (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n));
return
Math.sqrt(sum / n);
}
function
skewness(arr, n)
{
let sum = 0;
for
(let i = 0; i < n; i++)
sum = (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n));
return
sum / (n * standardDeviation(arr, n) *
standardDeviation(arr, n) *
standardDeviation(arr, n) *
standardDeviation(arr, n));
}
let arr =
[ 2.5, 3.7, 6.6, 9.1, 9.5, 10.7, 11.9, 21.5, 22.6, 25.2 ];
let n = arr.length;
document.write(skewness(arr, n).toFixed(6));
</script>
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!