I am trying to paste a covariance matrix on an existing xlsm
file through the below formula, but it is not working. The Cov Matrix
is an array. Could you please help me with it?
Error I get is: [Errno 13] Permission denied: 'C:\\Users\\gm\\AppData\\Local\\Continuum\\anaconda3\\Files for Python\\Book2.xlsm'
shrunk_covariance = array([[0.44067332, 0.16161585], [0.16161585, 0.80226294]])
Code to create the covariance matrix (you might not need to look at it, but here it is for sake of completeness):
import numpy as np
from sklearn.covariance import LedoitWolf
real_cov = np.array([[.4, .2],[.2, .8]])
np.random.seed(0)
X = np.random.multivariate_normal(mean=[0, 0],cov=real_cov,size=50)
cov = LedoitWolf().fit(X)
shrunk_covariance = cov.covariance_
shrunk_covariance
Code that does not work:
import openpyxl
from openpyxl import load_workbook
wb = load_workbook(r"C:\Users\gm\AppData\Local\Continuum\anaconda3\Files for Python\Book2.xlsm", keep_vba = True)
# grab the active worksheet
ws = wb['Shrunk_CovLast']
# Data can be assigned directly to cells
ws.cell(row=1,column=1).value = print(shrunk_covariance)
# Save the file
wb.save(r"C:\Users\gm\AppData\Local\Continuum\anaconda3\Files for Python\Book2.xlsm")