forked from pald22/covShrinkage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovDiag.py
118 lines (93 loc) · 4.47 KB
/
covDiag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 12 16:07:10 2021
@author: Patrick Ledoit
"""
# function sigmahat=covCor(Y,k)
#
# Y (N*p): raw data matrix of N iid observations on p random variables
# sigmahat (p*p): invertible covariance matrix estimator
#
# Shrinks towards constant-correlation matrix:
# the target preserves the variances of the sample covariance matrix
# all the correlation coefficients of the target are the same
#
# If the second (optional) parameter k is absent, not-a-number, or empty,
# then the algorithm demeans the data by default, and adjusts the effective
# sample size accordingly. If the user inputs k = 0, then no demeaning
# takes place; if (s)he inputs k = 1, then it signifies that the data Y has
# already been demeaned.
#
# This version: 01/2021, based on the 04/2014 version
###########################################################################
# This file is released under the BSD 2-clause license.
# Copyright (c) 2014-2021, Olivier Ledoit and Michael Wolf
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###########################################################################
def covDiag(Y,k = None):
#Pre-Conditions: Y is a valid pd.dataframe and optional arg- k which can be
# None, np.nan or int
#Post-Condition: Sigmahat dataframe is returned
import numpy as np
import pandas as pd
import math
# de-mean returns if required
N,p = Y.shape # sample size and matrix dimension
#default setting
if k is None or math.isnan(k):
mean = Y.mean(axis=0)
Y = Y.sub(mean, axis=1) #demean
k = 1
#vars
n = N-k # adjust effective sample size
#Cov df: sample covariance matrix
sample = pd.DataFrame(np.matmul(Y.T.to_numpy(),Y.to_numpy()))/n
# compute shrinkage target
target = pd.DataFrame(np.diag(np.diag(sample.to_numpy())))
# estimate the parameter that we call pi in Ledoit and Wolf (2003, JEF)
Y2 = pd.DataFrame(np.multiply(Y.to_numpy(),Y.to_numpy()))
sample2= pd.DataFrame(np.matmul(Y2.T.to_numpy(),Y2.to_numpy()))/n # sample matrix of squared returns
piMat=pd.DataFrame(sample2.to_numpy()-np.multiply(sample.to_numpy(),sample.to_numpy()))
pihat = sum(piMat.sum())
# estimate the parameter that we call gamma in Ledoit and Wolf (2003, JEF)
gammahat = np.linalg.norm(sample.to_numpy()-target,ord = 'fro')**2
# diagonal part of the parameter that we call rho
rho_diag = np.sum(np.diag(piMat))
# off-diagonal part of the parameter that we call rho
rho_off = 0
# compute shrinkage intensity
rhohat = rho_diag + rho_off
kappahat = (pihat - rhohat) / gammahat
shrinkage = max(0 , min(1 , kappahat/n))
# compute shrinkage estimator
sigmahat = shrinkage*target + (1-shrinkage) * sample;
return sigmahat
import pandas as pd
df = pd.read_csv(r'C:\Users\Patrick Ledoit\Documents\Python\translation\input1.csv')
df = df.T.reset_index().T.reset_index(drop=True)
df = df.astype(float)
sigmahat = covDiag(df)
print(sigmahat)