来自因子负载和因子协方差的协方差矩阵?

时间:2018-10-12 16:01:21

标签: python pandas numpy optimization matrix-multiplication

我有一个包含6个资产的列表,其中包含4个因子的加载向量,并且我具有这4个因子的协方差矩阵。如何使用这两个输入来构建6x6“资产”协方差矩阵?

import pandas as pd
import numpy as np
## Python 2.7

加载向量:

raw_asset_loadings = {'asset1': [.25, .25, .25, .25], 
              'asset2': [.0, 0,.5,.5], 
              'asset3': [0,0,1.25,.25], 
              'asset4': [0,0,.25,0], 
              'asset5': [.85,0,0,.15],
              'asset6': [.7,.15,0,.15]}
asset_loadings = pd.DataFrame(data=raw_asset_loadings)

产生

    asset1  asset2  asset3  asset4  asset5  asset6
  0 0.25    0.0     0.00    0.00    0.85    0.70
  1 0.25    0.0     0.00    0.00    0.00    0.15
  2 0.25    0.5     1.25    0.25    0.00    0.00
  3 0.25    0.5     0.25    0.00    0.15    0.15

和因子协方差矩阵:

raw_cov = {0: [.04, .008, .12, .0], 
       1: [.008, .01,.0015,0], 
       2: [.0012,.0015,.0036,.0], 
       3: [.0,.0,.0,.0004]}
factor_cov = pd.DataFrame(data=raw_cov)

产生

    0       1       2       3
0   0.040   0.0080  0.0012  0.0000
1   0.008   0.0100  0.0015  0.0000
2   0.120   0.0015  0.0036  0.0000
3   0.000   0.0000  0.0000  0.0004

我以为这样可以,但是我得到的是4x6矩阵,而不是所需的6x6矩阵:

covy = (np.matmul(np.matmul(asset_loadings.T,factor_cov),factor_cov))

关于如何从4个因素中创建6x6的任何想法?

1 个答案:

答案 0 :(得分:1)

最后乘以错误的术语。答案是:

import { HttpClient } from '@angular/common/http';
...
constructor(private http: HttpClient) {}
....
getServices(url: string): Promise < any > {
  return this.http.get(CoordinadoresService.BASEURL + url)
    .toPromise();
}

getOffices() {
  this.oficinas["coordinadores"] = [];
  let data = this.util.getLocalStorage("coordinadores");
  let promises = [];

  if (data) {

    for (let i = 0; i < Object.keys(data.coordinadores).length; i++) {
      let url = `getOficinas/${data.coordinadores[Object.keys(data.coordinadores)[i]].ip}/${Object.keys(data.coordinadores)[i]}`;
      promises.push(this.getDataFromAPI(url));
    }

    Promise.all(promises).then(data => {
      console.log('Both promises have resolved', data);
    });
  }
}

private getDataFromAPI(url) {
  return this.services.getServices(url)
    .then(
      response => {
        response["coordinador"] = response.coordinador;
        this.oficinas["coordinadores"].push(response)
        return response;
      },
      err => err
    );
}

第一篇关于stackoverflow的文章,当然,我出去喝咖啡10分钟后,我得到了自己的答案。...