code: def _erc_weights_slsqp(
          x0,
          cov,
          b,
          maximum_iterations,
          tolerance
  ):
      
  
      def fitness(weights, covar):
          # total risk contributions
          # trc = weights*np.matmul(covar,weights)/np.sqrt(np.matmul(weights.T,np.matmul(covar,weights)))
  
          # instead of using the true definition for trc we will use the optimization on page 5
          trc = weights * np.matmul(covar, weights)
          
          n = len(trc)
          # sum of squared differences of total risk contributions
          sse = 0.
          for i in range(n):
              for j in range(n):
                  #switched from squared deviations to absolute deviations to avoid numerical instability
                  sse += np.abs(trc[i] - trc[j])
          # minimizes metric
          return sse
  
      #nonnegative
      bounds = [(0,None) for i in range(len(x0))]
      # sum of weights must be equal to 1
      constraints = (
          {
              'type': 'eq',
              'fun': lambda W: sum(W) - 1.
          }
      )
      options = {
          'maxiter':maximum_iterations
      }
  
      optimized = minimize(
          fitness,
          x0,
          (cov),
          method='SLSQP',
          constraints=constraints,
          bounds=bounds,
          options=options,
          tol=tolerance
      )
      # check if success
      if not optimized.success:
          raise Exception(optimized.message)
  
      # return weight vector
      return optimized.x
docs: Calculates the equal risk contribution / risk parity weights given
          a DataFrame of returns.
  
      Args:
      * x0 (np.array): Starting asset weights.
      * cov (np.array): covariance matrix.
      * b (np.array): Risk target weights. By definition target total risk contributions are all equal which makes this redundant.
      * maximum_iterations (int): Maximum iterations in iterative solutions.
      * tolerance (float): Tolerance level in iterative solutions.
  
      Returns:
      np.array {weight}
  
      You can read more about ERC at
      http://thierry-roncalli.com/download/erc.pdf