<a href="https://colab.research.google.com/github/anasselhoud/Data-Science-bootcamp/blob/master/TinyStatistician/TinyStatician_AnassELH.ipynb" target="_parent"></a>

# **TinyStatistician** 
#### Prepared by Anass El Houd

Building a TinyStatistician package using Python Classes. The purpose is to not use predefined funtions that calculates the mean and all the parameters. We can only use 'math' package in order to use mathematical operations.

Let's at the beginning create a class which implements the following functions:


*   **Mean**: computes the mean of a non-empty array a. Given a vector x, we calculte the mean using the following formula:
\begin{align}
\mu=\frac{\sum_{i=1}^{m} x_{i}}{m}
\end{align}

*   **Median**: returns the median or the 50th percentile. 

* **Quartile**: returns the 1st and 3rd percentiles given an array a and the parameter percentile => Quartile(a,percent)

* **Variance**: computes tthe variance given an array a using the following formula:

\begin{align}
\sigma^{2}=\frac{\sum_{i=1}^{m}\left(x_{i}-\mu\right)^{2}}{m}=\frac{\sum_{i=1}^{m}\left[x_{i}-\left(\frac{1}{m} \sum_{j=1}^{m} x_{j}\right)\right]^{2}}{m}
\end{align}
* **Standard Deviation**: The methematical formula to calculate the standard deviation:
\begin{align}
\sigma=\sqrt{\frac{\sum_{i=1}^{m}\left(x_{i}-\mu\right)^{2}}{m}}=\sqrt{\frac{\sum_{i=1}^{m}\left[x_{i}-\left(\frac{1}{m} \sum_{j=1}^{m} x_{j}\right)\right]^{2}}{m}}
\end{align}



```
import math

class TinyStatistician:
  
  @staticmethod
  def mean(a):
    if a==[]:
      return None 
    else:
      S=0
      for i in range(len(a)):
        S=S+a[i]
    return S/(len(a))
  
  @staticmethod
  def median(a):
    if not a:
      return None
    else:
      return a[int(len(a)/2)-1]

  @staticmethod
  def quartile(a,percent):
    if not a:
      return None
    else:
      return a[int(percent*len(a)/100)]


  @staticmethod
  def var(a):
    if not a:
      return None 
    else:
      S=0
      for i in range(len(a)):
        S=S+a[i]
      moy=S/(len(a))
      V=0
      for i in range(len(a)):
        V=V+(a[i]-moy)**2
      Vr=V/(len(a))
      return Vr

  @staticmethod
  def std(a):
    return math.sqrt(tstat.var(a))
```

### Testing results


```
tstat=TinyStatistician()
a=[1,42,300,10,59]

print('Mean of a: ',tstat.mean(a))
print('Median of a: ',tstat.median(a))
print('75th percentile of a: ',tstat.quartile(a,75))
print('Variance of a: ',tstat.var(a))
print('Variance of a: ',tstat.std(a))
```

    Mean of a:  82.4
    Median of a:  42
    75th percentile of a:  10
    Variance of a:  12279.439999999999
    Variance of a:  110.81263465868862



```

```

b9c3120625e1c8a9aae4cda9c8ef9c910a7d4394
ipynb
Jupyter Notebook
TinyStatistician/TinyStatician_AnassELH.ipynb
anasselhoud/Data-Science-projects
6574da12061f3fb6065e15ca1079d9a1b2c5e951
CC0-1.0
2020-10-10T12:47:52.000Z
2020-10-10T12:47:52.000Z
TinyStatistician/TinyStatician_AnassELH.ipynb
anasselhoud/Data-Science-bootcamp
6574da12061f3fb6065e15ca1079d9a1b2c5e951
CC0-1.0
TinyStatistician/TinyStatician_AnassELH.ipynb
anasselhoud/Data-Science-bootcamp
6574da12061f3fb6065e15ca1079d9a1b2c5e951
CC0-1.0
Qwen/Qwen-72B
1. YES
2. YES