Ridge and lasso regularization - regression

Does anyone know that in linear regression, when calculating the sum of squares residuals (Ridge and Lasso), how can I plot it in the cordine system?
X: [ 1 2 3 4 5]
y: [3 5 2 4 8 ]
SSR: 13.1
slope: 0,9
Ridge (L2): 13,91 - How do I represent this capot regularization as a straight line?
Lasso (L1)? 14 - How do I represent this capot regularization as a straight line?
I've picked out the Ridge and Lasso forces, but I can't plot them in the corduroy system!
Can someone help me?

Related

Variance of the prediction of a two-point change after linear regression

This may be an absolutely basic question, so forgive me if it is.
Let's say I have the following regression output
Call:
lm(formula = y ~ x1 + x2, data = fake)
Residuals:
Min 1Q Median 3Q Max
-2.9434 -0.6851 0.0231 0.6744 3.6313
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.130431 0.056489 -2.309 0.0211 *
x1 0.014597 0.003454 4.226 2.59e-05 ***
x2 -0.025518 0.062429 -0.409 0.6828
---
Now, I want to predict y from new data. I'm specifically interested in the error around my prediction.
Let's say that one row in the new observed dataset has
x1 = 2
x2 = 0
The expected value for y_pred is
y_pred = -0.1304 + 2 * 0.01460
...but what is the standard deviation of that prediction? Can I figure out 95% CIs on that prediction?
And specifically, would I figure those out by applying the std. error of beta_1 twice, once for each unit increase, or do I apply it only once?
EDIT to add: I don't have the original data, just the coefficient estimate and SE...so calculating using matrix algebra won't be possible.

what is the result inside fft bin at different index?

For example if I have 10 samples in the FFT in a system sampled at 100 Hz. Then how are the results represented in FFT bin output ?
The frequency resolution is going to be how many Hz each DFT bin represents. This is, as you have noted, given by fs/N. In this case :--
resolution = 100/10 = 10 hz
Definition of FFT states that frequency range [0,fs] is represented by N point.
But when the result comes :--
X[0] is the constant term (also refereed to as DC bias)
X[1] is the 10 hz
X[2] is the 20 hz
X[3] is the 30 hz
X[4] is the 40 hz
X[5] is the 50 hz
X[6] to X[9] are the complex conjugates of X[4] to X[1] respectively.
so my question is :--
X[6] is the 60hz signal & its value will be complex conjugate of 40 hz signal .. is it right ?
Or X[6] does not contains spectral of 60hz but it is only complex conjugate of X[4] ?
Please suggest.
The frequencies of each bin in the DFT go from $n = 0 to N-1$ where each frequency is given as $n F_s/N$.
Due to the sampling process, for any signals (either real and complex signals) the upper half of this frequency spectrum that extends from $F_s/2$ to 1 sample less than $F_s$ is equivalent to the negative half spectrum (the spectrum that extends from $-F_s/2% to 1 sample less than 0.). This applies to the DFT and all digital signals.
This is demonstrated in the graphics below, showing how an analog spectrum (top line), convolves with the sampling spectrum (second line) of a system sampled at 20 Hz to result in the digital spectrum of a sampled signal. Since the spectrum is unique over a 20 Hz range (and repeats everywhere else), we only need to show the frequencies over any 20 Hz range to represent the signal. This can be identically shown from -10 Hz to +10 Hz, or 0 to 20 Hz.
Representing the sampled spectrum of a real signal sampled at 20 Hz over the range of -10Hz to +10Hz:
The same signal can also be represented over the range of 0 to 20 Hz:
Representing the sampled spectrum of a complex signal sampled at 20 Hz over the range of -10Hz to +10Hz:
The same signal can also be represented over the range of 0 to 20 Hz:
Since the spectrum for the DFT is discrete, the samples go from $n = 0 to N-1$ where each frequency is given as $F_s/n$ as described above. There is also only N samples in the DFT of course but as you rotate the samples in the DFT, you effectively move through the expanded spectrum above. It is helpful to view the spectrum on the surface of a cylinder with a circumference that goes from 0 to $F_s$ where you see that $F_s$ is equivalent to 0, and going backwards from 0 is equivalent to going into the negative half spectrum.
So in your example specifically:
X[0] = DC
X1 = 10
X2 = 20
X3 = 30
X4 = 40
X[5] = 50
X[6] = 60
X[7] = 70
X[8] = 80
X[9] = 90
Can also be represented as
X[0] = DC
X1 = 10
X2 = 20
X3 = 30
X4 = 40
X[5] = -50
X[6] = -40
X[7] = -30
X[8] = -20
X[9] = -10
Note that the command "FFTSHIFT" in MATLAB shifts the DFT vector accordingly to produce the following order representing the range from -F_s/2 to +F_s/2 :
fftshift([X[0], X1, X2, X3, X4, X[5], X[6], X[7], X[8], X[9]]) =
[X[5], X[6], X[7], X[8], X[9], X[0], X1, X2, X3, X4]
Assuming your input signal is purely real, then X[6] is just the complex conjugate of X[4]. The top half of the spectrum is essentially redundant for real signals. See: this question and answer for further details.
Note that the input signal should be bandwidth-limited to fs / 2, otherwise aliasing will occur, so for a baseband signal there should not be any components >= fs / 2.

Octave leasqr only doing one iteration

As I'm trying to fit a function to some experimental data, I've written a function with three inputs, three parameters and one output:
qrfunc = #(x, p) exp(-1*p(1)*x(:,1) - p(2)*x(:,2))+p(3)*x(:,3)+20;
When I generate some input and output values:
pS = [0.5; 0.3; 0.3];
x1 = [1 1 1; 1 1.1 1; 1 1.1 1.1; 2 1.2 2];
y1 = qrfunc(x1, pS);
And call the leasqr function:
pin =[1; 1; 1];
[f1, p1, kvg1, iter1, corp1, covp1, covr1, stdresid1, Z1, r21] = leasqr(x1, y1, pin, qrfunc, 0.0001);
This works correct, the function makes 7 iterations and provides the right outputs.
But when I load x1 from my experimental data (a text file with three columns, about 1500 lines) as well as my y1 (a text file with the same amount of lines) and run the same function, it only makes one iteration, and does not change the parameters.
It even shows that the error margins are very high:
sqrt(diag(covp1))
ans =
3.0281e+004
3.7614e+005
1.9477e-002
What am I doing wrong? There are no error messages, no 'Convergence not achieved' or anything like that...
Edit:
The data is loaded with the command:
load "input.txt"
load "output.txt"
Proof of loading:
size(input)
ans =
1540 3
The first few lines from my input file:
10 0.4 5
20 0.4 5
30 0.4 5
40 0.4 5
50 0.4 5
The second and third parameters have different values further down the line.

Octave program to solve ODE

I have an ODE
dy=x dx, y(0)=2
The solution of this equation is y =x^2/2 + K.
K become 2.
Now I have to plot graph.
When
x=0, y=2
x=1, y=2.5
x=2, y=4
x=3, y=6.5
x=4, y=10
I have to write an Octave program to generate these values
My code is test.m
function xdot = f (x,t)
xdot=x;
endfunction
x=lsode("f",2,(t=linspace(0,4,5)));
#plot(t,x)
x
I run the pgm in cmd but it gives
2.0000
5.4366
14.7781
40.1711
109.1963
The expected result is
2
2.5
4
6.5
10
Please help me..
Finally got the answer..
Use
xdot=t;
instead of
xdot=x;

Retrieving coefficients of polynomial from DFT using inverse DFT

I am trying to multiply two polynomials using DFT and I don't know how to get the last bit from the DFT of their multiplication.
So there's p(x) = x - 4, dft -3, i-4, -5, -i-4
And q(x) = x^2-1, dft 0, -2, 0, -2
degree(pq) = 3
So we get the 4th roots of unity 1, i, -1, -i
dft for pq is 0, 8-2i, 0, 8+2i.
Could someone please tell me how to get the coefficients for pq now from its dft?
Thanks!
The first thing to understand is that multiplying two polynomials is the same as convolving the coefficients.
octave:1> p=[0 0 1 -4];
octave:2> q=[0 1 0 -1];
octave:3> conv(p,q)
ans =
0 0 0 1 -4 -1 4
Secondly, understand the conditions under which circular convolution is equivalent to linear convolution.
(Also, your DFT coeffs seem to be wrong)