text: import numpy as np
  import pandas as pd
  import yfinance as yf
  import riskfolio.ConstraintsFunctions as cf
  import matplotlib.pyplot as plt
  import riskfolio.PlotFunctions as plf
  import riskfolio.Portfolio as pf
  
  yf.pdr_override()
  
  # Date range
  start = '2016-01-01'
  end = '2019-12-30'
  
  # Tickers of assets
  assets = ['JCI', 'TGT', 'CMCSA', 'CPB', 'MO', 'NBL', 'APA', 'MMC', 'JPM',
            'ZION', 'PSA', 'AGN', 'BAX', 'BMY', 'LUV', 'PCAR', 'TXT', 'DHR',
            'DE', 'MSFT', 'HPQ', 'SEE', 'VZ', 'CNP', 'NI']
  assets.sort()
  
  # Downloading data
  data = yf.download(assets, start = start, end = end)
  data = data.loc[:,('Adj Close', slice(None))]
  data.columns = assets
  
  # Calculating returns
  Y = data[assets].pct_change().dropna()
  print(Y.head())
  
  # Building the portfolio object
  port = pf.Portfolio(returns=Y)
  # Calculating optimum portfolio
  # Select method and estimate input parameters:
  
  method_mu='hist' # Method to estimate expected returns based on historical data.
  method_cov='hist' # Method to estimate covariance matrix based on historical data.
  port.assets_stats(method_mu=method_mu, method_cov=method_cov, d=0.94)
  
  # Estimate optimal portfolio:
  model='Classic' # Could be Classic (historical), BL (Black Litterman) or FM (Factor Model)
  rm = 'MV' # Risk measure used, this time will be variance
  obj = 'Sharpe' # Objective function, could be MinRisk, MaxRet, Utility or Sharpe
  hist = True # Use historical scenarios for risk measures that depend on scenarios
  rf = 0 # Risk free rate
  l = 0 # Risk aversion factor, only useful when obj is 'Utility'
  
  w = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=l, hist=hist)
  print(w.T)
  
  # Plotting the composition of the portfolio
  ax = plf.plot_pie(w=w, title='Sharpe Mean Variance', others=0.05, nrow=25, cmap = "tab20",
                    height=6, width=10, ax=None)
  points = 50 # Number of points of the frontier
  frontier = port.efficient_frontier(model=model, rm=rm, points=points, rf=rf, hist=hist)
  print(frontier.T.head())
  
  # Plotting the efficient frontier
  label = 'Max Risk Adjusted Return Portfolio' # Title of point
  mu = port.mu # Expected returns
  cov = port.cov # Covariance matrix
  returns = port.returns # Returns of the assets
  ax = plf.plot_frontier(w_frontier=frontier, mu=mu, cov=cov, returns=returns, rm=rm,
                         rf=rf, alpha=0.01, cmap='viridis', w=w, label=label,
                         marker='*', s=16, c='r', height=6, width=10, ax=None)
  
  # Plotting efficient frontier composition
  ax = plf.plot_frontier_area(w_frontier=frontier, cmap="tab20", height=6, width=10, ax=None)
  rm = 'CVaR' # Risk measure
  w = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=l, hist=hist)
  print(w.T)
  
  ax = plf.plot_pie(w=w, title='Sharpe Mean CVaR', others=0.05, nrow=25, cmap = "tab20",
                    height=6, width=10, ax=None)
  points = 50 # Number of points of the frontier
  frontier = port.efficient_frontier(model=model, rm=rm, points=points, rf=rf, hist=hist)
  print(frontier.T.head())
  
  label = 'Max Risk Adjusted Return Portfolio' # Title of point
  ax = plf.plot_frontier(w_frontier=frontier, mu=mu, cov=cov, returns=returns, rm=rm,
                         rf=rf, alpha=0.01, cmap='viridis', w=w, label=label,
                         marker='*', s=16, c='r', height=6, width=10, ax=None)
  
  # Plotting efficient frontier composition
  ax = plf.plot_frontier_area(w_frontier=frontier, cmap="tab20", height=6, width=10, ax=None)
  
  # Risk Measures available:
  #
  # 'MV': Standard Deviation.
  # 'MAD': Mean Absolute Deviation.
  # 'MSV': Semi Standard Deviation.
  # 'FLPM': First Lower Partial Moment (Omega Ratio).
  # 'SLPM': Second Lower Partial Moment (Sortino Ratio).
  # 'CVaR': Conditional Value at Risk.
  # 'WR': Worst Realization (Minimax)
  # 'MDD': Maximum Drawdown of uncompounded returns (Calmar Ratio).
  # 'ADD': Average Drawdown of uncompounded returns.
  # 'CDaR': Conditional Drawdown at Risk of uncompounded returns.
  
  rms = ['MV', 'MAD', 'MSV', 'FLPM', 'SLPM',
         'CVaR', 'WR', 'MDD', 'ADD', 'CDaR']
  w_s = pd.DataFrame([])
  
  for i in rms:
      w = port.optimization(model=model, rm=i, obj=obj, rf=rf, l=l, hist=hist)
      w_s = pd.concat([w_s, w], axis=1)
  w_s.columns = rms
  w_s.style.format("{:.2%}").background_gradient(cmap='YlGn')
  
  # Plotting a comparison of assets weights for each portfolio
  fig = plt.gcf()
  fig.set_figwidth(14)
  fig.set_figheight(6)
  ax = fig.subplots(nrows=1, ncols=1)
  w_s.plot.bar(ax=ax)
  
  asset_classes = {'Assets': ['JCI','TGT','CMCSA','CPB','MO','NBL','APA','MMC',
                              'JPM','ZION','PSA','AGN','BAX','BMY','LUV','PCAR',
                              'TXT','DHR','DE','MSFT','HPQ','SEE','VZ','CNP','NI'], 
                   'Industry': ['Consumer Discretionary','Consumer Discretionary',
                                'Consumer Discretionary', 'Consumer Staples',
                                'Consumer Staples','Energy','Energy','Financials',
                                'Financials','Financials','Financials','Health Care',
                                'Health Care','Health Care','Industrials','Industrials',
                                'Industrials','Industrials','Industrials',
                                'Information Technology','Information Technology',
                                'Materials','Telecommunications Services','Utilities',
                                'Utilities'] }
  asset_classes = pd.DataFrame(asset_classes)
  asset_classes = asset_classes.sort_values(by=['Assets'])
  
  constraints = {'Disabled': [False, False, False, False, False],
                 'Type': ['All Assets', 'Classes', 'Classes', 'Classes',
                          'Classes'],
                 'Set': ['', 'Industry', 'Industry', 'Industry', 'Industry'],
                 'Position': ['', 'Financials', 'Utilities', 'Industrials',
                              'Consumer Discretionary'],
                 'Sign': ['<=', '<=', '<=', '<=', '<='],
                 'Weight': [0.10, 0.2, 0.2, 0.2, 0.2],
                 'Type Relative': ['', '', '', '', ''],
                 'Relative Set': ['', '', '', '', ''],
                 'Relative': ['', '', '', '', ''],
                 'Factor': ['', '', '', '', '']}
  constraints = pd.DataFrame(constraints)
  print(constraints)
  
  A, B = cf.assets_constraints(constraints, asset_classes)
  
  port.ainequality = A
  port.binequality = B
  
  model = 'Classic'
  rm = 'MV'
  obj = 'Sharpe'
  rf = 0
  
  w = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=l, hist=hist)
  print(w.T)
  
  ax = plf.plot_pie(w=w, title='Sharpe Mean Variance', others=0.05, nrow=25, cmap = "tab20",
                    height=6, width=10, ax=None)
  
  w_classes = pd.concat([asset_classes.set_index('Assets'), w], axis=1)
  print(w_classes)
  
  w_classes = w_classes.groupby(['Industry']).sum()
  print(w_classes)
  ax = plf.plot_pie(w=w_classes, title='Sharpe Mean Variance', others=0.05, nrow=25,
                    cmap = "tab20", height=6, width=10, ax=None)