In a regression Y=aX+b, regr_intercept(Y, X) equals "b" and rregr_slope(Y, X) equals "a"?
You have not supplied much details but here you go.
Regression
A regression line is simply a line
y = ax + b
that is able to compute an output variable y for an input variable x. A line can be described by two parameters, also called coefficients:
the slope a
the intercept b
Finding Slope & Intercept
Suppose you have two numeric columns, Y and X populated with the desired X and Y
CREATE TABLE foo(
id serial PRIMARY KEY,
X integer NOT NULL,
Y integer NOT NULL
);
INSERT INTO foo VALUES (0,10,3);
INSERT INTO foo VALUES (1,20,5);
You can find slope as follows.
SELECT regr_slope(y, x) slope FROM foo;
SELECT regr_intercept(y, x) intercept FROM foo;
Results of query:
slope: 0.2
intercept: 1
SQL Fiddle
Related
This is a continuation of an earlier question I asked here.
If I create a symbolic expression in MatLab
syms L M T
F = M*L/T^2
I want to identify the powers of each dimension M, L, or T. In this case, the answer should be
for M, 1
for L, 1
for T, -2
There is a relatively easy way to do this if the expression F were a polynomial in MatLab employing the coeffs function. However, my expression is clearly not a polynomial as far as MatLab is concerned.
In the end, I will be working with at least two parameters so I will put them in a cell array since I anticipate cellfun will be useful.
V = L/T
param = {F,V};
The final output should be a table where the rows correspond to each dimension, L M and T and the columns are for each parameter F and V.
syms L M T
F = M*L/T^2
[C,T] = coeffs(expand(log(F),'IgnoreAnalyticConstraints',true))
[exp(T).' C.']
It returns the table:
I have to express an AND condition in linear program. The Boolean variable z takes a value 1 if both Boolean variables x and y takes a value 1. Otherwise, z takes a value 0. How do I write it in linear program?
In a pure linear program boolean expressions are not possible.
If you are in an (mixed-)integer program and x,y,z are all binary variables then you can implement the AND by the following.
z >= x+y-1
z <= x
z <= y
Here the first ensures z=1 if x=y=1 and the last two forces z=0 if any of the two is not 1.
As #Erwin Kalvelagen pointed out in the comments this is better relaxed than the formulation using 2z <= x+y.
I am trying to create a simple linear regression model. a.csv contains 8 columns. Var1 month dow Hour is_holiday T load Date. I get an error at X = ones... it says undefined unction times for input arguments of type table.
T = readtable('a.csv')
x1 = T(:, 6);
x2 = T(:,3);
y = T(:,7);
X = [ones(size(x1)) x1 x2 x1.*x2];
b = regress(y,X)
When you slice a table with indexing, in Matlab, you obtain another table, but with a smaller number of rows or columns... so in this part of code:
x1 .* x2
you are trying to multiply two tables together.
If you open your T variable from the workspace window, you should notice that the table defines one header name for each column like Var1, Var2, ... VarN (which corresponds to your first CSV row value, if defined, or defaulted as above). If you want to use the underlying strong typed vectors of your table, you have to call:
T.VarX
Let's suppose that your x1 corresponds to T.Var1, x2 to T.Var2 and your y references T.Var3 then:
X = [ones(size(x1)) T.Var1 T.Var2 (T.Var1 .* T.Var2)];
% ...
b = regress(T.Var3,X)
This way, you don't even need to slice your table.
I have a question about matlab programming about sine curve.
The question is as below:
Consider the definition: function [s1, s2, sums] = sines(pts,amp,f1,f2). The input, pts, is an integer, but amp, f1, and f2 and are not necessarily integers. Output argument s1 is a row vector whose length (number of elements) equals pts. The elements of s1 are the values of the sine function when it is given equally spaced arguments that start at zero and extend through f1 periods of the sine. (Note that we ask for full periods, so if f1 is an integer, both the first and the last element of s1 will be 0 other than a very small rounding error.) The amplitude of the sine wave equals amp. The vector s2 is the same as s1 except that s2 contains f2 periods. The vector sums is the sum of s1 and s2. If f2 is omitted, then it should be set to a value that is 5% greater than f1. If f1 is omitted also, then it should be set to 100. If amp is not provided, then it should default to 1. Finally, if pts is omitted as well, then it should be set to 1000.
Here is what I am confused: how to define step length pts. I used the following method but it fails to work. Please help me to fix it.
function [s1, s2, sums] = sines(pts,amp,f1,f2)
.................
t = linspace(0, 1, pts);
s1=amp*sin(2*pi*f1*t);
s2=amp*sin(2*pi*f2*t);
Thanks.
As far as the part of the code you are confused this should work for you:
n=pts-1
t=0:n;
s1=amp*sin(2*pi*f1/n*t);
s2=amp*sin(2*pi*f2/n*t);
then you sum s1+s2. You still need to handle the missing input if any.
please help me to understand this code:
x(:,i) = mean( (y(:,((i-1)*j+1):i*j)), 2 )';
i can't find it in my book. thanks.
The code you posted can be made more readable using temporary variables:
a = (i-1)*j+1;
b = i*j;
val = y(:,a:b);
x(:,i) = mean( val, 2 )'; %# =mean( val' )
What exactly you do not understand? For meaning of mean , : and ' consult matlab help.
It would help if you said exactly what you don't understand, but here are a few tips:
if you have something like a(r,c), that means matrix a, row r, column c (always in this order). In other words, you should have two elements inside the brackets separated by a comma where the first represents the row, the second the column.
If you have : by itself in one of the sides of the comma, that means "all". Thus, if you had a(r,:), then you would have matrix a, row r, all columns.
If : is not alone in one of the sides of the comma, then it will mean "to". So if you have a(r, z:y), that means matrix a, row r, columns z to y.
Mean = average. The format of the function in Matlab is M = mean(A,dim). A will be the matrix you take the average (or mean) of, M will be the place where the results are going to go. If dim = 1, you will get a row vector with each element being the average of a column. If dim = 2 (as it is in your case), then you should get a column vector, with each element being the average of a row. Be careful, though, because at the end of your code you have ', which means transpose. That means that your column vector will be transformed into a row vector.
OK, so your code:
x(:,i) = mean( (y(:,((i-1)*j+1):i*j)), 2 )';
Start with the bit inside, that is
y(:,((i-1)*j+1):i*j)
So that is saying
matrix y(r,c)
where
r (row) is :, that is, all rows
c (column) is ((i-1)j+1):ij, that is, columns going from (i-1)j+1 until ij
Your code will then get the matrix resulting from that, which I called y(r,c), and will do the following:
mean( (y(r,c), 2 )
so get the result from above and take the mean (average) of each row. As your code has the ' afterwards, that is, you have:
mean( (y(r,c), 2 )'
then it will get the column vector and transform into a row vector. Each element of this row will be the average of a row of y(r,c).
Finally:
x(:,i) = mean( (y(r,c), 2 )';
means that the result of the above will be put in column i of matrix x.
Shouldn't this be x(i,:) instead?
The i-th column of the array x is the average of the i-th group of j columns of the array y.
For example, if i is 1 and j is 3, the 1st column of x is the average of the first three columns of y.