Define and test a function to generate the covariance matrix for an input vector. For example, if the input is v=[1, 2, 5], then the matrix is v*vT = 1 2 5 2 4 10 5 10 25 (write the code in python
import numpy as np | |
import math | |
stdv = {"ABC":0.3,"XYZ":0.2} | |
tickersCorr = ["ABC","XYZ"] | |
# Assuming a 0.5 correlation here is the correlation matrix | |
c = [[1,0.5],[0.5,1]] | |
def varCovarMatrix(stocksInPortfolio): | |
cm = np.array(c) | |
vcv = [] | |
for eachStock in stocksInPortfolio: | |
row = [] | |
for ticker in stocksInPortfolio: | |
if eachStock == ticker: | |
variance = math.pow(stdv[ticker],2) | |
row.append(variance) | |
else: | |
cov = stdv[ticker]*stdv[eachStock]* cm[tickersCorr.index(ticker)][tickersCorr.index(eachStock)] | |
row.append(cov) | |
vcv.append(row) | |
vcvmat = np.mat(vcv) | |
return vcvmat | |
print(varCovarMatrix(["ABC","XYZ"])) |
Define and test a function to generate the covariance matrix for an input vector. For example, if...
Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as output, c, returns the number of zeros at the beginning of v (number of zero elements before any non-zero element). For example, for input (-5, 5, 11] the output would be 0. If the input is [0, 0, 3, 0, 0, 0], the output would be 2. If the input is [0, 0, 0, 0, 7, 4) the output would be 4. 5) Write...
4. Consider the vector space V = R3 and the matrix 2 -1 -1 2 -1 -1 0 2 We can define an inner product on V by (v, w) = v'Mw. where vt indicates the transpose. Please note this is NOT the standard dot product. It is a inner product different (a) (5 points) Apply the Gram-Schmidt process to the basis E = {e1,e2, e3} (the standard basis) to find an orthogonal basis B. 4. Consider the vector space...
in python def matrixMultiplication(matrix, vector): """Return the product of a matrix and a vector Arguments: matrix: A list of lists representing an nxn matrix vector: A list representing an nx1 matrix Returns: A list representing the nx1 matrix obtained by multiplying the input matrix by the input vector. >>> matrix = [[1, 2, 3], [2, 3, 1], [4, 1, 2]] >>> vector = [1, 2, 3] >>> matrixMultiplication(matrix, vector) ...
Write a function to calculate the normalized sinc This example demonstrates writing test cases for function-based problems. Assessment tests are designed to capture the problem requirements as well as common student errors. The rectangular function (or square-pulse) is commonly used in signal processing applications. The Fourier transform of the rectangular function is the normalized sinc function given by: Write a function normsinc that returns . Your function must: Calculate for each element of the input. If the input is a...
Define a function called collapse() which takes a list as input. Each element of the list will either be an integer, or a list of integers. The function should modify the input list by replacing all of the elements which themselves are lists with the sum of their elements. For example: Test Result vals = [1, 2, 3, 4, 5] collapse(vals) print(vals) [1, 2, 3, 4, 5] vals = [1, [2, 3], 4, 5] collapse(vals) print(vals) [1, 5, 4, 5]...
Matlab: Write a function called minimax that takes M, a matrix input argument and returns mmr, a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row. As a second output argument called mmm, it provides the difference between the maximum and minimum element in the entire matrix. See the code below for an example: >> A = randi(100,3,4) A = 66 94 75 18 4 68 40 71 85 76...
MATLAB: write a function that retuns logical true of vector or matrics or scaler is empty Write a function that retuns logical true of vector or matrics or scaler is empty %% P2: Check for an empty matrix % Write a function myIsEmpty which takes one input (scalar, vector, matrix, ...) % and returns logical true if the input is empty and false otherwise. % DO NOT USE MATLAB's isempty FUNCTION! % % Example: An input of [] should result...
Write the code in python 5. (10 points) Write a function that takes a square matrix and compute the summation of items on both diagonals. Example: 9 13 5 27 1 11X76 6 0 7 102
6. Write a function integerdivision that takes two input numbers and divides the first one with the second one. Question 2 a) Create a vector A with all integers between 5 and 400. 4 Marks ) Given a matrix Z, how to access element of the matrix. c) What is the output of this MATLAB code? A [2,4,10,13;16,3,7,18, 8,4,9,25:3,12,15,17); length(A) size(A) Ans) Ans) 6. Write a function integerdivision that takes two input numbers and divides the first one with the...
How do I generate a banded matrix in Python programming language? I need the function of code required to do this