Calculating the ridge parameter for given ridge estimates - regression

Suppose response and covariate data are below:
(1.4, 0.0), (1.4, -2.0), (0.8, 0.0), (0.4,2.0).
I want to find the ridge parameter k, for which the ridge estimates are (1, -1/8) by applying the penalty parameter to slope.

Related

Split a 3D polyline with an equal distance interval

I want to split a 3D polyline with an equal distance interval. A lot of answers work well for a 2D case, e.g. Splitting MultiLine or LineString into equal segments of particular length using GeoPandas and shapely, Shapely was used to split a 2D polyline. However, Shapely is a planar geometry library and z, the height above or below the plane, is ignored in geometric analysis, which can not used to handle a 3D case.
Any suggestion will be appreciate, python example would be better. Thanks again.
The following code is a 3D points input, while getting a 2D distance result.
from shapely.geometry import LineString, Point, MultiPoint
import numpy as np
line = LineString([(0, 0, 0), (2, 1, 1), (3, 2, 2), (3.5, 1, 1), (5, 2, 2)])
distance_delta = 0.9
# generate the equidistant points
distances = np.arange(0, line.length, distance_delta)
points = MultiPoint([line.interpolate(distance) for distance in distances] + [line.boundary.geoms[1]])

multi label problem with intermediate labels

I am trying to create a model for the following problem
id input (diagnoses) elapsed_days output (medication)
1 [2,3,4] 0 [3,4]
1 [4,5,6] 7 [1]
1 [2,3] 56 [6,3]
2 [6,5,9,10] 0 [5,3,1]
Rather than a single label for the different codes over time, there are labels at each time period.
I am think that my arch would be [input] -> [embedding for diagnoses] -> [append normalized elapsed days to embeddings]
-> [LSTM] -> [FFNs] -> [labels over time]
I am familiar with how to set this up if there were a single label per id. Given there are labels for each row (i.e. multiple per id), should I be passing the hidden layers of the LSTM through the FFN and then assigning the labels? I would really appreciate if somebody could point me to a reference/blog/github/anything for this kind of problem or suggest an alternative approach here.
Assuming the [6,3] is equal to [3, 6].
You can use Sigmoid activation with Binary Cross-Entropy loss function (nn.BCELoss class) instead of Softmax Cross-Entropy (nn.CrossEntropyLoss class).
But the output ground truth instead of integers like when using nn.CrossEntropyLoss. You need to make them sort of one hot encoding instead. For example, if the desired output is [6, 3] and the output has 10 nodes. The y_true has to be [0, 0, 0, 1, 0, 0, 1, 0, 0, 0].
Depending on how you implement your data generator, this is one way to do it.
output = [3, 6]
out_tensor = torch.zeros(10)
out_tensor[output] = 1
But if [6,3] is not equal to [3, 6]. Then more information about this is needed.

Is there some "scale invariant" substitute for the softmax function?

It is very common tu use softmax function for converting an array of values in an array of probabilities. In general, the function amplifies the probability of the greater values of the array.
However, this function is not scale invariant. Let us consider an example:
If we take an input of [1, 2, 3, 4, 1, 2, 3], the softmax of that is [0.024, 0.064, 0.175, 0.475, 0.024, 0.064, 0.175]. The output has most of its weight where the '4' was in the original input. That is, softmax highlights the largest values and suppress values which are significantly below the maximum value. However, if the input were [0.1, 0.2, 0.3, 0.4, 0.1, 0.2, 0.3] (which sums to 1.6) the softmax would be [0.125, 0.138, 0.153, 0.169, 0.125, 0.138, 0.153]. This shows that for values between 0 and 1 softmax, in fact, de-emphasizes the maximum value (note that 0.169 is not only less than 0.475, it is also less than the initial proportion of 0.4/1.6=0.25).
I would need a function that amplifies differences between values in an array, emphasizing the greatest values and that is not so affected by the scale of the numbers in the array.
Can you suggest some function with these properties?
As Robert suggested in the comment, you can use temperature. Here is a toy realization in Python using numpy:
import numpy as np
def softmax(preds):
exp_preds = np.exp(preds)
sum_preds = np.sum(exp_preds)
return exp_preds / sum_preds
def softmax_with_temperature(preds, temperature=0.5):
preds = np.log(preds) / temperature
preds = np.exp(preds)
sum_preds = np.sum(preds)
return preds / sum_preds
def check_softmax_scalability():
base_preds = [1, 2, 3, 4, 1, 2, 3]
base_preds = np.asarray(base_preds).astype("float64")
for i in range(1,3):
print('logits: ', base_preds*i,
'\nsoftmax: ', softmax(base_preds*i),
'\nwith temperature: ', softmax_with_temperature(base_preds*i))
Calling check_softmax_scalability() would return:
logits: [1. 2. 3. 4. 1. 2. 3.]
softmax: [0.02364054 0.06426166 0.1746813 0.474833 0.02364054 0.06426166
0.1746813 ]
with temperature: [0.02272727 0.09090909 0.20454545 0.36363636 0.02272727 0.09090909
0.20454545]
logits: [2. 4. 6. 8. 2. 4. 6.]
softmax: [0.00188892 0.01395733 0.10313151 0.76204449 0.00188892 0.01395733
0.10313151]
with temperature: [0.02272727 0.09090909 0.20454545 0.36363636 0.02272727 0.09090909
0.20454545]
But the scale invariance comes with a cost: as you increase temperature, the output values will come closer to each other. Increase it too much, and you will have an output that looks like a uniform distribution. In your case, you should pick a low value for temperature to emphasize the maximum value.
You can read more about how temperature works here.

Which is the correct way of cufft plan configuration in case of 3d fft's?

Lets say I have a 3 dimensional(x=256+2,y=256,z=128) array and I want to compute the FFT (forward and inverse) using cuFFT. And I have a fftw compatible data layout lets say the padding is in the x direction as shown in the size above(+2). How my plan should be ?
//forward
cufftPlan3d(&plan, z, y, x, CUFFT_R2C)
//or
cufftPlan3d(&plan, x, y, z, CUFFT_R2C)
and for inverse ?
//Inverse
cufftPlan3d(&plan, x, y, z, CUFFT_C2R)
//or
cufftPlan3d(&plan, z, y, x, CUFFT_C2R)
If the data is stored in column major order how is that going to affect the plan creation ?
If you are using cufftPlan3d, the right way to do it would be to use
cufftplan3d(&plan, x, y, z, type);
Here x means the first dimension, y means the second and z means the third.
In your case, you can use them as is without any issue.
All parameters are the same for both forward and inverse, except type which changes from CUFFT_R2C to CUFFT_C2R.
If you are going to use cufftplanMany, you will need to do something like this.
int dims[] = {z, y, x}; // reversed order
cufftPlanMany(&plan, 3, dims, NULL, 1, 0, NULL, 1, 0, type, batch);
cufftPlanMany is useful if you are doing batched operations, or if you working with non contiguous data.

Replicating glTranslate

I need to come up with a function similar to glTranslate to know how things work. I've read that glTranslate parameters supply the x, y, z coordinates by which matrix is translated. I'm supposed to relate it to the translation formula:
x' = x + tx; t = translation factor
So is this correct?
new matrix = current matrix + ((transformation coordinates x, y, z)*current matrix)
The current matrix is taken care by the glMatrixMode correct? And as for the multiplication, I assume this involves glMultMatrix but do how I achieve this given glMultMatrix has a constant as parameter and glTranslate provide coordinates?
So is this correct?
new matrix = current matrix + ((transformation coordinates x, y, z)*current matrix)
No.
The correct implementation is
M' = M · T
where
| 1 0 0 x |
| 0 1 0 y |
T = | 0 0 1 z |
| 0 0 0 1 |
The current matrix is taken care by the glMatrixMode correct?
Yes
And as for the multiplication, I assume this involves glMultMatrix
In the actual OpenGL driver code probably not, but semantically yes
But do how I achieve this given glMultMatrix has a constant as parameter
For the call glMultMatrix(T), OpenGL performs the operation
M' = M · T
So all what you have to do is creating a translation matrix T like shown above (be aware the OpenGL is column major, so it looks "transposed" in the code) and pass that to glMultiMatrix.
GLfloat T[16] = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
x, y, z, 1 };
glMultMatrixf(T);
However you shouldn't use the old fixed function pipeline and the OpenGL matrix manipulation methods at all. OpenGL is not a very good math library. Better use something like GLM or Eigen or linmath.h and self defined mat4 shader uniforms; or if you insist on using fixed function use glLoadMatrix to load readily prepared matrices.