使用python scipy.optimize进行优化,无法转换第8个参数`g'

时间:2017-07-15 15:32:35

标签: python optimization minimize portfolio

我正在尝试使用Python的scipy.optimize.minimize来最大化下面的功能(这是一种获得多元化投资组合的方法,称为最大化多样化)。但是,总会有一些问题。

目标函数:

max f = x'* sigma / sqrt(x' C x)

C是N * N(N = 800)的协方差矩阵,sigma是N * 1的C的对角元素, x是N * 1

的可变数组

约束:x> = 0且sum(x)= 1

<script>

    var hitTemplate = document.querySelector('#hit-template').textContent;

    const search = instantsearch({
      appId: '{{ craft.searchPlus.getAlgoliaApplicationId }}',
      apiKey: '{{ craft.searchPlus.getAlgoliaSearchApiKey }}',
      indexName: 'products',
      urlSync: true
    });

    search.addWidget(
      instantsearch.widgets.infiniteHits({
        container: '#infinite-hits-container',
        templates: {
          empty: 'No results',
          item: hitTemplate
        },
        showMoreLabel: 'Load More Deals',
        hitsPerPage: 80
      })
    );

    search.addWidget(
      instantsearch.widgets.menu({
        container: '#womensCategories',
        attributeName: 'womensCategory.title',
        limit: 50,
        templates: {
          header: 'Womens'
        }
      })
    );

    search.addWidget(
      instantsearch.widgets.menu({
        container: '#mensCategories',
        attributeName: 'mensCategory.title',
        limit: 50,
        templates: {
          header: 'Mens'
        }
      })
    );


    search.addWidget(
      instantsearch.widgets.refinementList({
        container: '#stores',
        attributeName: 'retailer',
        operator: 'or',
        limit: 10,
        templates: {
          header: 'Store'
        }
      })
    );

    search.addWidget(
      instantsearch.widgets.refinementList({
        container: '#gender',
        attributeName: 'gender',
        operator: 'or',
        limit: 2,
        templates: {
          header: 'Gender'
        }
      })
    );

    search.addWidget(
      instantsearch.widgets.refinementList({
        container: '#size',
        attributeName: 'size.sizing',
        operator: 'or',
        limit: 20,
        templates: {
          header: 'Sizing'
        }
      })
    );


    search.addWidget(
      instantsearch.widgets.numericRefinementList({
        container: '#price',
        attributeName: 'salePrice',
        options: [
          {name: 'All'},
          {end: 20, name: 'less than 20'},
          {end: 50, name: 'less than 50'},
          {start: 50, end: 100, name: 'between 50 and 100'},
          {start: 100, end: 300, name: 'Expensive'},
          {start: 300, name: 'Very Expensive'}
        ],
        templates: {
          header: 'Price'
        }
      })
    );


    search.start();

</script>

错误是:

def obj_md(x,mat):
    diag = np.diagonal(mat)
    return(-x.dot(diag)/x.dot(mat).dot(x.T))
 def jac_deri(x,mat):
    ## x=x0
    diag = np.sqrt(np.diagonal(mat))
    sigma = np.sqrt(x.dot(mat).dot(x.T))
    avg_w = x.dot(diag)
    return(- diag.T/sigma + ((mat.dot(x.T)).T)*avg_w/sigma**3)
x0=np.array([[1.0/mat.shape[0]]*mat.shape[0]])
res=minimize(obj_md,x0,args=(mat),constraints=({'type': 'eq', 'fun':    lambda x: np.sum(x)-1}),bounds = [(0,1)]*len(x0[0]),tol=0.0001)

任何人都可以帮我解决问题。谢谢你的loooooooooooot!

0 个答案:

没有答案