```python
from statsmodels.tsa.seasonal import seasonal_decompose
import matplotlib.pyplot as plt
import scipy.stats as st
import seaborn as sns
import pandas as pd
from scipy.stats import norm, uniform, expon, t, probplot
import scipy.stats as st
from scipy.integrate import quad
from sympy.solvers import solve
from sympy import Symbol
import numpy as np
from pandas import Series, DataFrame
```


```python
AirP = pd.read_csv("AirPassengers.csv")
AirP.head()
AirP["TravelDate"] = pd.to_datetime(AirP["TravelDate"])
AirP.set_index("TravelDate", inplace = True)
AirP.head()
AirP.plot()
plt.xlabel("Reisedatum")
plt.ylabel("Anzahl Passagiere (in 1000)")
plt.show()
```


```python
AirP.head()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Passengers</th>
    </tr>
    <tr>
      <th>TravelDate</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1949-01-01</th>
      <td>112</td>
    </tr>
    <tr>
      <th>1949-02-01</th>
      <td>118</td>
    </tr>
    <tr>
      <th>1949-03-01</th>
      <td>132</td>
    </tr>
    <tr>
      <th>1949-04-01</th>
      <td>129</td>
    </tr>
    <tr>
      <th>1949-05-01</th>
      <td>121</td>
    </tr>
  </tbody>
</table>
</div>




```python
AusBeer = pd.read_csv("AustralianBeer-2.csv", sep = ";", header = 0)
AusBeer1 = AusBeer.copy()
AusBeer1.head()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Quarter</th>
      <th>megalitres</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1956Q1</td>
      <td>284.4</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1956Q2</td>
      <td>212.8</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1956Q3</td>
      <td>226.9</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1956Q4</td>
      <td>308.4</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1957Q1</td>
      <td>262.0</td>
    </tr>
  </tbody>
</table>
</div>




```python
AusBeer1["Quarter"] = pd.to_datetime(AusBeer["Quarter"])
AusBeer1.set_index("Quarter", inplace = True)
AusBeer1.head()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>megalitres</th>
    </tr>
    <tr>
      <th>Quarter</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1956-01-01</th>
      <td>284.4</td>
    </tr>
    <tr>
      <th>1956-04-01</th>
      <td>212.8</td>
    </tr>
    <tr>
      <th>1956-07-01</th>
      <td>226.9</td>
    </tr>
    <tr>
      <th>1956-10-01</th>
      <td>308.4</td>
    </tr>
    <tr>
      <th>1957-01-01</th>
      <td>262.0</td>
    </tr>
  </tbody>
</table>
</div>




```python
AusBeer1.describe()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>megalitres</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>count</th>
      <td>154.000000</td>
    </tr>
    <tr>
      <th>mean</th>
      <td>408.267532</td>
    </tr>
    <tr>
      <th>std</th>
      <td>97.598588</td>
    </tr>
    <tr>
      <th>min</th>
      <td>212.800000</td>
    </tr>
    <tr>
      <th>25%</th>
      <td>325.425000</td>
    </tr>
    <tr>
      <th>50%</th>
      <td>427.450000</td>
    </tr>
    <tr>
      <th>75%</th>
      <td>466.950000</td>
    </tr>
    <tr>
      <th>max</th>
      <td>600.000000</td>
    </tr>
  </tbody>
</table>
</div>




```python
AusBeer1.loc["1980-9" : "1990-3"].plot()
```


```python
AusBeer = pd.read_csv("AustralianBeer-2.csv", sep = ";", header = 0)
AusEl = pd.read_csv("AustralianElectricity-2.csv", sep = ";")
Aussie = AusBeer.copy()
# Hier wird der Datensatz um eine Spalte kilowatt erweitert
Aussie["kilowatt"] = AusEl["kilowatt"]
Aussie["Quarter"] = pd.to_datetime(Aussie["Quarter"])
Aussie.set_index("Quarter", inplace = True)
Aussie.loc["1970-9": "1994-1"].plot(subplots = True)
plt.show()
```


```python
def boxcox(x,lambd):
    return np.log(x) if (lambd==0) else (x**lambd-1)/lambd

AirP["l_2"] = boxcox(AirP["Passengers"],2)
AirP["l_0"] = boxcox(AirP["Passengers"],0)
AirP["l_-05"] = boxcox(AirP["Passengers"],-.5)
plt.subplot(221)
AirP["Passengers"].plot()
plt.title("Original")
plt.xlabel("")
plt.subplot(222)
AirP["l_-05"].plot()
plt.title("lambda = -0.5")
plt.xlabel("")
plt.subplot(223)
AirP["l_2"].plot()
plt.title("lambda = 2")
plt.xlabel("")
plt.subplot(224)
AirP["l_0"].plot()
plt.title("lambda = 0")
plt.xlabel("")
plt.show()
```


```python
AirQ = pd.read_csv("AirQualityUCI.csv", sep = ";", decimal = ",")
AirQ1 = AirQ.copy()
# pandas kennt das Zeitformat in der Tabelle nicht:
# Punkt muss durch - ersetzt werden
AirQ1["Time"] = AirQ1["Time"].str.replace(".", ":")
AirQ1["Date"] = pd.to_datetime(AirQ1["Date"] + " " + AirQ1["Time"])
AirQ1.set_index("Date", inplace = True)
# Einige Werte der Temperatur sind -200. Diese Zeilen werden weggelassen
AirQ1 = AirQ1[AirQ1["T"] > -20]
AirQ1["T"].plot()
plt.show()
```


```python
AirQ4 = AirQ1.loc["2004-3-10" : "2004-3-30" , "T"]
AirQ4.plot()
```


```python
AirQ1['Hour'] = AirQ1.index.hour
AirQ4 = AirQ1.loc["2004-3-10" : "2004-3-30"]
sns.boxplot(data=AirQ1, x='Hour', y="T")
```


```python
sns.set(rc={'figure.figsize':(11, 4)})
AirQ1['Hour'] = AirQ1.index.hour
AirQ4 = AirQ1.loc["2004-3-10" : "2004-3-30"]
sns.boxplot(data=AirQ1, x='Hour', y="T")
```


```python
AirQ1 = AirQ.copy()
# Einige Werte der Temperatur sind -200. Diese Zeilen werden weggelassen
AirQ1 = AirQ1[AirQ1["T"] > -20]
AirQ1["Time"] = AirQ1["Time"].str.replace(".", ":")
AirQ1["Date"] = pd.to_datetime(AirQ1["Date"] + " " + AirQ1["Time"])
AirQ1.set_index("Date", inplace = True)
# Resample to weekly frequency, aggregating with mean
AirQ1_weekly_mean = AirQ1['T'].resample('Y').mean()
AirQ1_weekly_mean.head(3)
print(AirQ1_weekly_mean.head(3))
```

    Date
    2004-12-31    20.861479
    2005-12-31    10.017496
    Freq: A-DEC, Name: T, dtype: float64



```python
from pandas.plotting import lag_plot
AirQ4 = AirQ.copy()
AirQ4 = AirQ4[AirQ4["T"] > -20]
AirQ4["Time"] = AirQ4["Time"].str.replace(".", ":")
AirQ4["Date"] = pd.to_datetime(AirQ4["Date"] + " " + AirQ4["Time"])
AirQ4.set_index("Date", inplace = True)
plt.subplot(121)
lag_plot(AirQ4)
plt.subplot(122)
lag_plot(AirQ4, 10)
plt.show()
```


```python
seasonal_decompose(AirP["Passengers"], model = "additive", freq = 12).plot()
plt.show()
```


```python
seasonal_decompose(np.log(AirP["Passengers"]), model = "add").resid.plot()
plt.show()
```


```python
pip install stldecompose
```

    Collecting stldecompose
      Downloading https://files.pythonhosted.org/packages/7f/0f/cc3cf33ae4ec1ffc83a2951dc37b5cdb49f9166c0e1de1a61e11b6d954fe/stldecompose-0.0.5-py2.py3-none-any.whl
    Requirement already satisfied: pandas in c:\users\flori\anaconda3\lib\site-packages (from stldecompose) (0.25.3)
    Requirement already satisfied: scipy in c:\users\flori\anaconda3\lib\site-packages (from stldecompose) (1.3.2)
    Requirement already satisfied: matplotlib in c:\users\flori\anaconda3\lib\site-packages (from stldecompose) (3.1.1)
    Requirement already satisfied: statsmodels in c:\users\flori\anaconda3\lib\site-packages (from stldecompose) (0.10.1)
    Requirement already satisfied: numpy in c:\users\flori\anaconda3\lib\site-packages (from stldecompose) (1.17.4)
    Requirement already satisfied: python-dateutil>=2.6.1 in c:\users\flori\anaconda3\lib\site-packages (from pandas->stldecompose) (2.8.1)
    Requirement already satisfied: pytz>=2017.2 in c:\users\flori\anaconda3\lib\site-packages (from pandas->stldecompose) (2019.3)
    Requirement already satisfied: cycler>=0.10 in c:\users\flori\anaconda3\lib\site-packages (from matplotlib->stldecompose) (0.10.0)
    Requirement already satisfied: kiwisolver>=1.0.1 in c:\users\flori\anaconda3\lib\site-packages (from matplotlib->stldecompose) (1.1.0)
    Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\users\flori\anaconda3\lib\site-packages (from matplotlib->stldecompose) (2.4.5)
    Requirement already satisfied: patsy>=0.4.0 in c:\users\flori\anaconda3\lib\site-packages (from statsmodels->stldecompose) (0.5.1)
    Requirement already satisfied: six>=1.5 in c:\users\flori\anaconda3\lib\site-packages (from python-dateutil>=2.6.1->pandas->stldecompose) (1.13.0)
    Requirement already satisfied: setuptools in c:\users\flori\anaconda3\lib\site-packages (from kiwisolver>=1.0.1->matplotlib->stldecompose) (42.0.2.post20191203)
    Installing collected packages: stldecompose
    Successfully installed stldecompose-0.0.5
    Note: you may need to restart the kernel to use updated packages.



```python
from stldecompose import decompose
decompose(np.log(AirP["Passengers"]), period = 12).plot();
```


```python
seasonal_decompose(AirP["Passengers"], model = "additive", freq = 12).plot()
```


```python
plt.subplot(211)
seasonal_decompose(AirP["Passengers"], model = "additive", freq = 12).resid.plot()
plt.subplot(212)
decompose((AirP["Passengers"]), period = 12).resid.plot();
plt.show()
```


```python
AirP["Passengers"]
```




    TravelDate
    1949-01-01    112
    1949-02-01    118
    1949-03-01    132
    1949-04-01    129
    1949-05-01    121
                 ... 
    1960-08-01    606
    1960-09-01    508
    1960-10-01    461
    1960-11-01    390
    1960-12-01    432
    Name: Passengers, Length: 144, dtype: int64




```python

```

9f5ae9030b1c4ad0ef46e5f2a68cd95cd406e62a
ipynb
Jupyter Notebook
Lernphase/SW10/Skript.ipynb
florianbaer/STAT
7cb86406ed99b88055c92c1913b46e8995835cbb
MIT
Lernphase/SW10/Skript.ipynb
florianbaer/STAT
7cb86406ed99b88055c92c1913b46e8995835cbb
MIT
Lernphase/SW10/Skript.ipynb
florianbaer/STAT
7cb86406ed99b88055c92c1913b46e8995835cbb
MIT
Qwen/Qwen-72B
1. YES
2. YES