Advertisement
Guest User

Untitled

a guest
Jun 13th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. >>> random_portafolios
  2.  
  3. AAPL weight MSFT weight XOM weight JNJ weight JPM weight AMZN weight GE weight FB weight T weight
  4. 0 0.188478 0.068795 0.141632 0.147974 0.178185 0.040370 0.020516 0.047275 0.166774
  5. 1 0.236818 0.008540 0.082680 0.088380 0.453573 0.021001 0.014043 0.089811 0.005155
  6. 2 0.179750 0.071711 0.050107 0.089424 0.080108 0.106136 0.155139 0.073487 0.194138
  7. 3 0.214392 0.015681 0.034284 0.276342 0.118263 0.002101 0.057484 0.000317 0.281137
  8. 4 0.301469 0.099750 0.046454 0.093279 0.020095 0.073545 0.178752 0.146486 0.040168
  9. 5 0.132916 0.006199 0.305137 0.032262 0.090356 0.169671 0.205602 0.003686 0.054172
  10.  
  11. >>> StockReturns.head()
  12.  
  13. AAPL MSFT XOM TWTR JPM AMZN GE FB T
  14. Date
  15. 2017-01-04 -0.001164 -0.004356 -0.011069 0.025547 0.001838 0.004657 0.000355 0.015660 -0.005874
  16. 2017-01-05 0.005108 0.000000 -0.014883 0.013642 -0.009174 0.030732 -0.005674 0.016682 -0.002686
  17. 2017-01-06 0.011146 0.008582 -0.000499 0.004681 0.000123 0.019912 0.002853 0.022707 -0.019930
  18. 2017-01-09 0.009171 -0.003170 -0.016490 0.019220 0.000741 0.001168 -0.004979 0.012074 -0.012641
  19. 2017-01-10 0.001049 -0.000335 -0.012829 -0.007429 0.002837 -0.001280 -0.002859 -0.004404 0.000278
  20.  
  21. def complex_computation():
  22. WeightedReturns = StockReturns.mul(arr, axis=1)
  23. ReturnsDaily= WeightedReturns.sum(axis=1)
  24. mean_retorns_daily = np.mean(ReturnsDaily)
  25. Returns = ((1+mean_retorns_daily)**252)
  26. cov_mat =StockReturns.cov()
  27. cov_mat_annual = cov_mat*252
  28. Volatility= np.sqrt(np.dot(arr.T, np.dot(cov_mat_annual, arr)))
  29. return Returns, Volatility
  30.  
  31. def func(row):
  32. random_portafolios['Volatility'].append(Volatility)
  33. Returns, Volatility = complex_computation(row.values)
  34. return pd.Series({'NewColumn1': Retturns,
  35. 'NewColumn2': Volatility})
  36.  
  37. def run_apply(random_portafolios):
  38. df_result = random_portafolios.apply(func, axis=1)
  39. return df_result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement