I am trying to calculate a simple covariance between two sets of variables, A and B.
I have tried using np.cov but keep getting an error. Here is my code:
A = [0.9926091942041072, 1.061714327721826, 1.0265041173124954, 0.9951867947618452, 1.0088789910677542, 1.0311964643047378, 1.0102610638000837, 0.9938920748980862, 0.9932010622471253, 1.0102518875232147, 1.0377091811047627, 1.0126812158655407, 1.024082246211189, 1.0158418136322864, 0.9968269661402218, 1.0152756526272513, 1.0161312543052163, 1.0082621539118897, 1.000863860478383, 1.0149090879481482, 1.0257377128094862, 1.0143175596402034, 1.0272708695601882, 1.0470885847902296, 0.969660907591942, 0.9991165032752222, 0.9818186527155475, 1.018035671890957, 1.0195669797037932, 1.014264708551927, 1.0229736114889536, 1.0152843776025082, 0.9600068929863864, 0.9776589863074681, 0.9427444615401563, 1.0156116713602954, 1.0565584741829954, 1.0178303071662445, 1.0355993979985592, 0.9477512225359872, 1.0501765937036716, 1.0366552832532343, 0.9670873232291204, 1.029218291630716, 0.9984977331866834, 1.0427245372236105, 1.0404779542014235, 0.9982851376533006, 0.9162087912087912, 0.8747059878638559, 1.1268441029331537, 1.002465295303235, 1.0618849777881292, 1.0551012969754443, 1.0700646873242192, 0.9607720459045056, 1.0165625929229853, 1.0593588206043234, 1.0371214066594323, 1.01003176192137, 1.0236388797364084, 1.0230363364345436, 1.0524253125558474, 0.9968142888234633, 1.0311071655649795, 1.014108202443281, 1.0377570196391572, 0.9524308595788337, 1.0114914777343913]
B = [1.0890006323549237, 1.02369887762, 0.8897884816828672, 1.0173449762041809, 1.0200264950011384, 1.0924192621263968, 1.0296416700838122, 1.0437492160639452, 0.9520117340866326, 1.037960609361761, 1.0457738524613378, 1.1182987937505222, 1.0516294550306338, 1.0161376655515924, 1.0653696147821805, 0.9711210006067469, 1.0031681042169647, 1.0722943893266268, 0.9911141101369138, 1.001326651623591, 1.0924412797674594, 0.9970134157376116, 1.0122993225773476, 0.9634149608576082, 1.0409046617971554, 0.9732825677377946, 1.092460641542099, 1.0166233503215536, 1.0089582262489865, 1.121039602904811, 1.0409054827210698, 0.9944722770335108, 0.8659232874731542, 0.8589712290780868, 0.9385155420428142, 1.1139154801500422, 1.06734792118655, 1.093960143033656, 0.9539080076422815, 1.0088635510287498, 1.0634670948089167, 0.9987419032169179, 1.0636062022463137, 1.0793239034760296, 1.1157473322103129, 1.0533471785960162, 1.1279834517874192, 0.9979306169474778, 0.8431391392646959, 1.0378834694429897, 1.1380336776151547, 1.1155582442469687, 1.1053947135970927, 1.2274576847800656, 0.9814597926760701, 1.0111894186161288, 1.003546146367367, 1.0895710481633136, 1.0451457661413661, 0.988685366143134, 0.9265538202448533, 1.081574164466458, 0.9618502455686553, 1.025039723968611, 1.1168132251204137, 1.0209329849200186, 1.0008669535282233, 0.9678287840792698]
x = np.array([A, B])
covariance = np.cov(x)
However I get the following error:
TypeError: unsupported operand type(s) for /: 'list' and 'int'
Could someone please help me. Thanks
CodePudding user response:
Your two lists have different lengths (69 and 68), thus numpy creates an object array (an array of 2 list items)
Use the same length to construct a valid array:
x = np.array([A[:68], B[:68]])
np.cov(x)
output:
array([[0.0013957 , 0.00095963],
[0.00095963, 0.00484376]])
CodePudding user response:
Array A length: 69 Array B length: 68
So you have to either downgrade A or upgrade B array