5

I have about 100 7x7 matrices of dependent variables (so 49 dependent variables). My independent variable is time. I am doing a physics project in which I am supposed to get a matrix function (every element of the matrix is a function of time) by solving an ODE. I used numpy's ODE solver which goves me numerical answers of my matrix function evaluated at different times. Now with these matrices and times I want to find a time-dependent expression for each element matrix to get the time-dependent matrix. I have heard that what I should do is find a hat matrix and I guess that the predicted or fitted values would be my 7x7 matrices and the response values would be the array of times. So how can I find this hat matrix in Python?

I was originally thinking of doing polynomial regression in scikit-learn using their LinearRegression model. Will that work? Is there a possible way in StatsModel, or better, in scipy or numpy?

Basically I want to go from:

enter image description here

to:

enter image description here

Clearly, I will use more test cases, but this is the overall idea. So I will have univariate X (X will be an array of the different times) and multivariate Y (Y will be the matrices evaluated at different times)

In the above example, t=1 would be included in the X array and the Y array would have the first matrix

1 Answer 1

6

Given that the task you would like to do is the classical linear regression:

Using the matrix notation in numpy (you would have to manually account for an intercept by adding a row of ones to X) :

import numpy as np
a = np.linalg.inv(np.dot(X.T,X))
c = np.dot(X.T,Y)
b = np.dot(a,c)

Using numpy

np.polyfit(X,Y,1)

Using scipy:

scipy.linalg.solve(X,Y)

scipy.stats.linregr(X,Y)

and many more

Sign up to request clarification or add additional context in comments.

11 Comments

now this would give me a single time dependent matrix? or like a matrix function?
and why do you define variables a, b, and c?
a,b,c: just for the readability. This would give you the coefficients of the linear fit.
I want a 'matrix function' does this give one? Or will it give the elements of this 'matrix' function?
So you would like to do a linear regression of the for Y = b*X ? And try to find the coefficients, stored in b ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.