Math.Net Weighted Least Squares dimensions issue - math.net

I'm trying to run a univariate WLS - using WeightedRegression.Weighted(X,y,W) - and am getting the error message Matrix dimensions must agree: op1 is 5836x1, op2 is 5836x1. Whether I make Y a column vector or a matrix (with only one column) does not matter.
From the error message, you can see the matrix (or the vector and matrix) dimensions agree - both have 5836 rows and 1 column.
What am I doing wrong?

What are the dimensions of your weight matrix? I think it expects a diagonal matrix, so in your case 5836x5836.

Related

Exclude indices from Pytorch tensor

I have an MxN table of distances between two distinct sets of points of size M <= N. I would like to find associate to each point of the first set M points in the second set in the following way.
Suppose that the shortest of all pairwise distances is between the i0 point of the first set and the j0 of the second. Then we attribute point i0 of the first set to j0 in the second. For the second pair, I have to find i1 != i0 and j1 != j0 such that the distance is minimal among remaining non-paired points.
I figure that I could do the first step by using torch.min function that will deliver me both minimal value as well as its 2d index in the matrix. But for the next steps I'll need to each time exclude a row a colunm, while keeping their original indices.
In other words, if I have a 3x4 matrix, and my first element is (1,2), I would like to be left with a 2x3 matrix with indices 0,2 and 0,1,3. So that, if my second desired element position in the original matrix is, say (2,3) I will be given (2,3) as a result of performing torch.max on the matrix with excluded row and column, rather than (1,2) again.
P.S. I could reach my goal by replacing the values in row and column I'd like to exclude by, say, positive infinities, but I think the question is still worth asking.

Can I compute the mean of two different axes of a 4D array using np.mean?

The data from my files is stored in 4D arrays in python of shape (64,128,64,3). The code I run is in a grid code format, so the shape tells us that there are 64 cells in the x,128 in the y, and 64 in the z. The 3 is the x, y, and z components of velocity. What I want to do is compute the average x velocity in each direction for every cell in y.
Let's start in the corner of my grid. I want the first element of my average array to be the average of the x velocity of all the x cells and all the z cells in position y[0]. The next element should be the same, but for y[1]. The end result should be an array of shape (128).
I'm fairly new to python, so I could be missing something simple, but I don't see a way to do this with one np.mean statement because you need to sum over two axes (In this case, 1 and 2 I think). I tried
velx_avg = np.mean(ds['u'][:,:,:,0],axis=1)
here, ds is the data set I've loaded in, and the module I've used to load it stores the velocity data under 'u'. This gave me an array of shape (64,64).
What is the most efficient way to produce the result that I want?
You can use the flatten command to make your life here much easier, this takes an np.ndarray and flattens it into one dimension.
The challenge here is trying to find your definition of 'efficient', but you can play around with that yourself. To do what you want, I simply iterate over the array and flatten the x and z component into a continuous array, and then take the mean of that, see below:
velx_avg = np.mean([ds['u'][:, i, :, 0].flatten() for i in range(128)], axis=1)

Estimate torsion for a discrete curve using four points

The curvature of a discrete space curve can be calculated using 3 successive points can be calculated using the Menger curvature (see https://en.wikipedia.org/wiki/Menger_curvature and Calculate curvature for 3 Points (x,y)).
My question is: is there a similar explicit formula for the torsion (https://en.wikipedia.org/wiki/Torsion_of_a_curve or ) using four 4 successive points?
If not an explicit formula, does someone know of an algorithm/package for calculating it? I work in python, but anything will do.
I can imagine the basic steps. Two successive vectors define a plane, and thus 3 successive vectors define two planes. The change in angle between the plane normals is proportional to the torsion. But I need an exact formula, with the calculated torsion having the proper dimension of 1/length^2.
Having some parametrization of curve r(t) (for example, by length of polyline chain) you can calculate three derivatives using 4 points: r', r'', r'''.
Then torsion is:
v = r' x r'' //(vector product)
torsion = (r''' .dot. v) / (v.dot.v) //.dot. is scalar product

how to obtain estimation from regression in excel?

I use datas in excel to produce a graphic.
Then I make a regression, and have an equation. I'd like to know what value would be obtained from the regression (for example, x = 7,6 is the value for which I wanna know an estimation of y).
It is an approximation with a 6 degree polynome.
One wimple method would be this : I have the equation, so I could use it
However, I wondered if there is a fast method to do it? Like I enter 7,6 somewhere to have the result quickly?
if you are looking at a linear regression line (straight line) you could try the forecast formula
=forecast(X, Known Ys, Known Xs)
you could also build your own equation automatically from
=linest(...)
I found the following on a site describing the capabilities of the linest function in excel:
In addition to using LOGEST to calculate statistics for other
regression types, you can use LINEST to calculate a range of other
regression types by entering functions of the x and y variables as the
x and y series for LINEST. For example, the following formula:
=LINEST(yvalues, xvalues^COLUMN($A:$C))
works when you have a single column of y-values and a single column of
x-values to calculate the cubic (polynomial of order 3) approximation
of the form:
y = m1*x + m2*x^2 + m3*x^3 + b
You can adjust this formula to calculate other types of regression,
but in some cases it requires the adjustment of the output values and
other statistics.
or look at:
=trend

Formula for calculating Euclidian direction in Excel

I am calculating Euclidian distance between points in an Excel application, and also need to be able to specify the direction of the difference in two-dimensional location for each pair of points.
Does anyone know how to implement this in Excel?
Below is a simplified illustration of my current Euclidian distance calculation. I have two points, and calculate how far apart Point1 is from Point2. But I would also like to find the direction (in degrees preferably) between Point1 and Point2.
For direction, you could use the angle that the vector from point one to point two makes with respect to the positive x axis:
=DEGREES(ATAN2(B3-B2,C3-C2))
this will return a number between -180 and +180 degrees. The ATAN2 function is given by ATAN2(x,y) = arctan(y/x) with the refinement that it returns pi/2 rather than a division by 0 error if x = 0 and also gives an answer in the appropriate quadrant.

Resources