# 程序员炒股，如何计算股票投资组合的风险和收益

```import pandas as pd

df = pd.DataFrame({'rx': pd.Series([5,6,8,9,12,15], index=['1','2','3','4','5','6']),                   'px': pd.Series([0.15, 0.2, 0.3, 0.2, 0.1, 0.05], index=['1','2','3','4','5','6'])})
df['px*rx'] = df['rx']*df['px']
exp_return = df['px*rx'].sum()print(df)print('Expected Return is: ', exp_return, '%')```

`     px  rx  px*rx1  0.15   5   0.752  0.20   6   1.203  0.30   8   2.404  0.20   9   1.805  0.10  12   1.206  0.05  15   0.75Expected Return is:  8.1 %`

```df['rx-ri'] = df['rx'] - exp_return
df['variance'] = df['px'] * (df['rx-ri']**2)
var_portfolio = df['variance'].sum()print(df)print("The variance of the portfolio is: ", var_portfolio)```

`     px  rx  px*rx  rx-ri  variance1  0.15   5   0.75   -3.1    1.44152  0.20   6   1.20   -2.1    0.88203  0.30   8   2.40   -0.1    0.00304  0.20   9   1.80    0.9    0.16205  0.10  12   1.20    3.9    1.52106  0.05  15   0.75    6.9    2.3805The variance of the portfolio is:  6.39`

```df_port = pd.DataFrame({'stock': pd.Series(['A','B','C','D','E'], index=['A','B','C','D','E']),                        'amount': pd.Series([100,60,200,50,40], index=['A','B','C','D','E'])})
total_invest = df_port['amount'].sum()
df_port['weights'] = df_port['amount'] / total_investprint(df_port)print("The sum of the weights is: ", round(df_port['weights'].sum()))```

`   amount stock   weightsA     100     A  0.222222B      60     B  0.133333C     200     C  0.444444D      50     D  0.111111E      40     E  0.088889The sum of the weights is:  1.0`

```df_port['stock_ret'] = pd.Series([5, 10, 7,6,6], index=['A','B','C','D','E'])
df_port['wiri']=df_port['stock_ret']*df_port['weights']print(df_port)print("The expected return from the portfolio is: ", round(df_port['wiri'].sum(), 2))```

`   amount stock   weights  stock_ret      wiriA     100     A  0.222222          5  1.111111B      60     B  0.133333         10  1.333333C     200     C  0.444444          7  3.111111D      50     D  0.111111          6  0.666667E      40     E  0.088889          6  0.533333The expected return from the portfolio is:  6.76`

```df_cov = pd.DataFrame({'A': pd.Series([6.39, -5.74, 3.65, -1.36, 9.25], index=['A','B','C','D','E']),                       'B': pd.Series([-5.74, 5.14, 9.36, 5.78, -8.52], index=['A','B','C','D','E']),                       'C': pd.Series([3.65, 9.36, 3.58, 7.85, 1.11], index=['A','B','C','D','E']),                       'D': pd.Series([-1.36, 5.78, 7.85, 4.87, 7.25], index=['A','B','C','D','E']),                       'E': pd.Series([9.25, -8.52, 1.11, 7.25, 4.63], index=['A','B','C','D','E'])})
df_cov['weight']=df_port['weights']print(df_cov)```

`      A     B     C     D     E    weightA  6.39 -5.74  3.65 -1.36  9.25  0.222222B -5.74  5.14  9.36  5.78 -8.52  0.133333C  3.65  9.36  3.58  7.85  1.11  0.444444D -1.36  5.78  7.85  4.87  7.25  0.111111E  9.25 -8.52  1.11  7.25  4.63  0.088889`
```df_port_var = pd.DataFrame()
df_port_var['A'] = df_cov['A']*df_cov['weight']*df_cov.loc['A', 'weight']
df_port_var['B'] = df_cov['B']*df_cov['weight']*df_cov.loc['B', 'weight']
df_port_var['C'] = df_cov['C']*df_cov['weight']*df_cov.loc['C', 'weight']
df_port_var['D'] = df_cov['D']*df_cov['weight']*df_cov.loc['D', 'weight']
df_port_var['E'] = df_cov['E']*df_cov['weight']*df_cov.loc['E', 'weight']
df_port_var['row_sum'] = df_port_var['A'] + df_port_var['B'] + df_port_var['C'] + df_port_var['D'] + df_port_var['E']
port_var_box = df_port_var['row_sum'].sum()print(df_port_var)print("The variance of the portfolio is: ", round(port_var_box, 2))```

`          A         B         C         D         E   row_sumA  0.315556 -0.170074  0.360494 -0.033580  0.182716  0.655111B -0.170074  0.091378  0.554667  0.085630 -0.100978  0.460622C  0.360494  0.554667  0.707160  0.387654  0.043852  2.053827D -0.033580  0.085630  0.387654  0.060123  0.071605  0.571432E  0.182716 -0.100978  0.043852  0.071605  0.036583  0.233778The variance of the portfolio is:  3.97`

1036 篇文章96 人订阅

0 条评论

## 相关文章

38711

1032

3795

3558

2420

1232

2074

### 《科学》副主编：影响因子不应被用于评价科学家

“影响因子不应被用于评价科学家。”10月29日，《科学》杂志副主编瓦尔达·文森（Valda Vinson）在2018年世界生命科学大会期间接受媒体专访时表示，基...

1052

2088

42810