```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
```


```python
stock = 'TLKM.JK'
tlkm = yf.Ticker(stock).history(period = '2y', interval = '1mo',actions=False)
tlkm.dropna(inplace = True)
tlkm['log_return'] = np.log(tlkm['Close']) - np.log(tlkm['Close'].shift(1))
tlkm['log_return'].dropna(inplace = True)

stock = 'BBNI.JK'
bbni = yf.Ticker(stock).history(period = '2y', interval = '1mo',actions=False)
bbni.dropna(inplace = True)
bbni['log_return'] = np.log(bbni['Close']) - np.log(bbni['Close'].shift(1))
bbni['log_return'].dropna(inplace = True)
bbni['log_return']

stock = 'ASII.JK'
asii = yf.Ticker(stock).history(period = '2y', interval = '1mo',actions=False)
asii.dropna(inplace = True)
asii['log_return'] = np.log(asii['Close']) - np.log(asii['Close'].shift(1))
asii['log_return'].dropna(inplace = True)
asii['log_return']

df = pd.concat([tlkm['log_return'],bbni['log_return'],asii['log_return']], axis = 1, keys =['tlkm', 'bbni', 'asii']).dropna()
df = df[:-1]
```


```python
df.mean()
```




    tlkm    0.019182
    bbni    0.032105
    asii    0.017229
    dtype: float64




```python
df.cov()
```




<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>tlkm</th>
      <th>bbni</th>
      <th>asii</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>tlkm</th>
      <td>0.006196</td>
      <td>0.004734</td>
      <td>-0.000863</td>
    </tr>
    <tr>
      <th>bbni</th>
      <td>0.004734</td>
      <td>0.011494</td>
      <td>0.001603</td>
    </tr>
    <tr>
      <th>asii</th>
      <td>-0.000863</td>
      <td>0.001603</td>
      <td>0.007762</td>
    </tr>
  </tbody>
</table>
</div>



# TLKM dan BBNI


```python
df.mean().iloc[:2]
```




    tlkm    0.019182
    bbni    0.032105
    dtype: float64




```python
Q = df.cov().iloc[:2][['tlkm','bbni']]
Q
```




<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>tlkm</th>
      <th>bbni</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>tlkm</th>
      <td>0.006196</td>
      <td>0.004734</td>
    </tr>
    <tr>
      <th>bbni</th>
      <td>0.004734</td>
      <td>0.011494</td>
    </tr>
  </tbody>
</table>
</div>




```python
import sympy as sym
x = sym.Symbol('x')
```


```python
u = np.array([[0],[1]])
v = np.array([[1],[-1]])
y1 = sym.solve(sym.expand((u + v*x).T.dot(Q).dot((u + v*x)).reshape((1))[0]).diff(x),x)
```


```python
sym.expand((u + v*x).T.dot(Q).dot((u + v*x)).reshape((1))[0]).diff(x).diff(x)
```




$\displaystyle 0.0164429521011939$




```python
print('Diperoleh besar proporsi untuk saham pertama adalah : ',y1[0])
```

    Diperoleh besar proporsi untuk saham pertama adalah :  0.822258088558763



```python
retur = y1[0]*df.mean().iloc[0] + (1-y1[0])*df.mean().iloc[1]
var = y1[0]**2*Q.iloc[0][0]**2 + (1-y1[0])**2*Q.iloc[1][1]**2 + 2*y1[0]*(1-y1[0])*Q.iloc[0][1]
print('Diperoleh total return', retur*100, 'dan total variansi', var*100)
```

    Diperoleh total return 2.14790463192366 dan total variansi 0.141393865913339


6a32710db03de6c014276b6ea375f9e69af4ba2c
ipynb
Jupyter Notebook
optimization.ipynb
leonv1602/ak4261-optimisasi-matematika-keuangan
8dc7fd54b6ceaefc80c220359be7c77d6997939d
MIT
optimization.ipynb
leonv1602/ak4261-optimisasi-matematika-keuangan
8dc7fd54b6ceaefc80c220359be7c77d6997939d
MIT
optimization.ipynb
leonv1602/ak4261-optimisasi-matematika-keuangan
8dc7fd54b6ceaefc80c220359be7c77d6997939d
MIT
Qwen/Qwen-72B
1. YES
2. YES