# Packages


```python
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
import yfinance as yf
import yahoofinancials
import seaborn as sn
from scipy.stats import anderson,kstest,shapiro, probplot
 
from IPython.core.display import HTML
HTML("""
<style>
.output_png {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
</style>
""")
```





<style>
.output_png {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
</style>




*Discrete Return*
Misal $S(t)$ adalah di saham pada saat $t$ maka *return* diperoleh sebagai berikut 
\begin{equation}
\frac{S(t+\Delta t)-S(t)}{S(t)}
\end{equation}


```python
stock = 'TLKM.JK'
df = yf.Ticker(stock).history(period = '1y')
df['return'] = df['Close'].pct_change()
df['return']
```




    Date
    2021-02-11         NaN
    2021-02-15    0.009404
    2021-02-16   -0.003106
    2021-02-17   -0.018692
    2021-02-18    0.009524
                    ...   
    2022-02-07    0.014184
    2022-02-08   -0.013986
    2022-02-09    0.011820
    2022-02-10    0.042056
    2022-02-11   -0.002242
    Name: return, Length: 248, dtype: float64




```python
df['return'].plot()
plt.ylabel('% Change')
plt.title('Discrete Return'+ stock)
plt.show()
```

Log Return didefinisikan sebagai berikut :
\begin{equation} 
\ln\left(\frac{S(t+\Delta t)}{S(t)}\right)
\end{equation}


```python
df['log_return'] = np.log(df['Close']) - np.log(df['Close'].shift(1))
df['log_return'].plot()
plt.ylabel('Log Return')
plt.title('Log Return ' + stock)
plt.show()
```

Di kalkulus dipelajari bahwa 
\begin{align*}
\ln(1+x) = x - \frac{x^2}{2} + \frac{x^3}{3} - \dots 
\end{align*}
Sehingga dapat ditinjau pula persamaan 
\begin{align*}
\ln\left(\frac{S(t+\Delta t)}{S(t)}\right)= \ln\left(\frac{S(t+\Delta t)-S(t)+S(t)}{S(t)}\right) = \ln\left(1+\frac{S(t+\Delta t)-S(t)}{S(t)}\right) \approx \frac{S(t+\Delta t)-S(t)}{S(t)}
\end{align*}
untuk suatu nilai $\frac{S(t+\Delta t) - S(t)}{S(t)}$ yang kecil

# Distribusi dari *Return* dan *Log Return*


```python
ax1 = plt.subplot(121)
df['return'].hist(density = True)
sn.kdeplot(df['return'])
ax2 = plt.subplot(122)
probplot(df['return'].dropna(), plot= plt)

plt.show()
```


```python
print('Return mean :', np.round(df['return'].mean(),6))
print('Return variance :', np.round(df.var()['return'],6))
```

    Return mean : 0.001182
    Return variance : 0.000323



```python
ax1 = plt.subplot(121)
df['log_return'].hist(density = True)
sn.kdeplot(df['log_return'])
ax2 = plt.subplot(122)
probplot(df['log_return'].dropna(), plot= plt)

plt.show()
```


```python
print('Return mean :', np.round(df['log_return'].mean(),6))
print('Return variance :', np.round(df.var()['log_return'],6))
```

    Return mean : 0.001023
    Return variance : 0.000316


3e06e6aa7365b68126733a598d30fd78632fccc0
ipynb
Jupyter Notebook
return.ipynb
leonv1602/ak4261-optimisasi-matematika-keuangan
8dc7fd54b6ceaefc80c220359be7c77d6997939d
MIT
return.ipynb
leonv1602/ak4261-optimisasi-matematika-keuangan
8dc7fd54b6ceaefc80c220359be7c77d6997939d
MIT
return.ipynb
leonv1602/ak4261-optimisasi-matematika-keuangan
8dc7fd54b6ceaefc80c220359be7c77d6997939d
MIT
Qwen/Qwen-72B
1. YES
2. YES