I have a table as
Input
A B
3 20
5 30
6 35
I need an algorithm to find out the formula(Equation) associated with the two columns A and B
Output
B=(A+1)*5
One relatively simple approach would be to use least squares curve fitting for a variety of families of curves (say polynomials up to degree n-2, exponentials, power laws) and look for the one with minimal residual. This would give you approximate formulas (unless you only accepted the curve with zero residual), but perhaps that's okay for your application?
Assuming the formula you want is a polynomial.
What we know? For a list of A values, we have their B values. With "n" A values, the best polynomial we can find is of (n-1)th degree. Why?
Basically I'm solving a linear system like the following:
x + Ay + (A^2)z = B
With the example:
x + 3y + 9z = 20
x + 5y + 25z = 30
x + 6y + 35z = 35
After solving this, we can find that (x, y, z) = (5, 5, 0). This means that our polynomial is 5 + 5A + 0(A^2), that is basically the same B = (A+1)*5 you showed in the example.
We can solve the system using any method. Don't know if it will help, but I'll throw some code here to solve it with Gaussian elimination (in Python):
def solve(A, B):
n = len(A)
M = [[a**i for i in range(n)]+[b] for a,b in zip(A,B)]
for i in range(n):
M[i] = [x / M[i][i] for x in M[i]]
for j in range(n):
if j==i: continue
M[j] = [xj - xi * M[j][i] for xi, xj in zip(M[i], M[j])]
return [M[i][-1] for i in range(n)]
print solve([3,5,6], [20, 30, 35])
If formula is first order linear like in example then you are looking for:
http://en.wikipedia.org/wiki/Linear_regression
or this for higher order linear equations
http://en.wikipedia.org/wiki/Polynomial_regression
Related
The task is to find the amount of distinct pairs of {x, y} that fits the equation 1/x + 1/y = 1/n, with n being the input given by the user. Different ordering of x and y does not count as a new pair.
For example, the value n = 2 will mean 1/n = 1/2. 1/2 can be formed with two pairs of {x, y}, whcih are 6 and 3 and 4 and 4.
The value n = 3 will mean 1/n = 1/3. 1/3 can be formed with two pairs of {x, y}, which are 4 and 12 and 6 and 6.
The mathematical equation of 1/x + 1/y = 1/n can be converted to y = nx/(x-n) where if y and x in said converted equation are whole, they count as a pair of {x, y}. Using said converted formula, I will iterate n times starting from x = n + 1 and adding x by 1 per iteration to find whether nx % (x - n) == 0; if it yields true, the x and y are a new distinct pair.
I found the answer to limit my iteration by n times by manually computing the answers and finding the number of repetitions 'pattern'. x also starts with n+1 because otherwise, division by zero will happen or y will result in a negative number. The modulo operator is to indicate that the y attained is whole.
Questions:
Is there a mathematical explanation behind why the iteration is limited to n times? I found out that the limit of iteration is n times by doing manual computation and finding the pattern: that I only need to iterate n times to find the amount of distinct pairs.
Is there another way to find the amount of distinct pairs {x, y} other than my method above, which is by finding the VALUES of distinct pairs itself and then summing the amount of distinct pair? Is there a quick mathematical formula I'm not aware of?
For reference, my code can be seen here: https://gist.github.com/TakeNoteIAmHere/596eaa2ccf5815fe9bbc20172dce7a63
Assuming that x,y,n > 0 we have
Observation 1: both, x and y must be greater than n
Observation 2: since (x,y) and (y,x) do not count as distinct, we can assume that x <= y.
Observation 3: x = y = 2n is always a solution and if x > 2n then y < x (thus no new solution)
This means the possible values for x are from n+1 up to 2n.
A little algebra convers the equation
1/x + 1/y = n
into
(x-n)*(y-n) = n*n
Since we want a solution in integers, we seek integers f, g so that
f*g = n*n
and then the solution for x and y is
x = f+n, y = g+n
I think the easiest way to proceed is to factorise n, ie write
n = (P[1]^k[1]) * .. *(P[m]^k[m])
where the Ps are distinct primes, the ks positive integers and ^ denotes exponentiation.
Then the possibilities for f and g are
f = P[1]^a[1]) * .. *(P[m]^a[m])
g = P[1]^b[1]) * .. *(P[m]^b[m])
where the as and bs satisfy, for each i=1..m
0<=a[i]<=2*k[i]
b[i] = 2*k[i] - a[i]
If we just wanted to count the number of solutions, we would just need to count the number of fs, ie the number of distinct sequences a[]. But this is just
Nall = (2*k[1]+1)*... (2*[k[m]+1)
However we want to count the solution (f,g) and (g,f) as being the same. There is only one case where f = g (because the factorisation into primes is unique, we can only have f=g if the a[] equal the b[]) and so the number we seek is
1 + (Nall-1)/2
Given a series on number, how can be find a polynomial which generalizes the series. And than with this generalization one should be able to find out any term in the series.
While searching on net I found out that one can use Langrange's Interpolation technique. How accurate is the method for generalizing the series?
Can we use some other method to find a polynomial?
There are several algorithms which will generate a polynomial matching a finite series, as "justhalf" identified Lagrange's interpolation is one technique.
In general, if you are given a function with n points, you can uniquely define a polynomial of degree n-1 (or sometimes less) which matches at every point.
Consider the series with only two term, "2, 4". As this has only two terms (n=2), there is a polynomial of degree 1 which will generate the series. The general form is y = ax+b and we need to find a and b:
y = ax + b
So
2 = a⋅1 + b => 2 = a + b
4 = a⋅2 + b => 4 = 2a + b
Therefore a = 2 and b = 0.
y = 2x
You can see if you substitute x=1 and x=2 you get the values 2 and 4 respectively.
If the series was 2,4,8 then you would need a polynomial of degree 3-1 = 2, say y = ax^2 + bx + c (where these a and b are new values, not necessarily the same as the a and b for the previous case).
Then you would know that:
2 = a⋅1² + b⋅1 + c => 2 = a + b + c (i)
4 = a⋅2² + b⋅2 + c => 4 = 4a + 2b + c (ii)
8 = a⋅3² + b⋅3 + c => 8 = 9a + 3b + c (iii)
You can solve these equations to find a, b and c:
Subtract (i) from (ii):
2 = 3a + b (iv)
Subtract (ii) from (iii)
4 = 5a + b (v)
Subtract (iv) from (v)
2 = 2a => a = 1
So from (iv)
2 = 3⋅1 + b = 3 + b => b = -1
From (i)
2 = a + b + c = 1 + -1 + c = c => c = 2
So the polynomial y = ax² + bx + c = x² - x + 2 agrees at the three points
Verify:
1² - 1 + 2 = 2
2² - 2 + 2 = 4
3² - 3 + 2 = 8
As we wanted.
But note that this polynomial y = x² - x + 2 also exactly generates the series with only the first 2 terms, "2, 4". So this series with only two terms is satisfied by two polynomials, y = 2x and y = x² - x + 2. Despite agreeing on the first two values 2,4 these are very different polynomials.
In general, if you have a series of n terms then there is a unique polynomial of degree n-1 which will generate the series. In general, there will be no polynomials of degree less than n-1 which will exactly generate it (you may get lucky, but its not generally true). There are an infinite number of polynomials of degree greater then n-1 which will generate the data.
Usually in numerical analysis you try and generate a polynomial of degree less than n-1 which approximates the data (doesn't match exactly, but minimises error). Exact solutions of degree n-1 are unstable, in that tiny changes to the input series produces very different equations. This is not so true of polynomial approximations of degree less than n-1. As many physical measurements have inherent error, using lower degree polynomials minimises the impact of measurement errors.
Lets now consider the series 2, 4, 8, 16
You can produce a polynomial of degree 3 (y = ax³ + bx² + cx + d) which exactly matches these data points using exactly the same approach. This (again) is just solving a set of linear simultaneous equations. This is essentially how Lagrange's algorithm works; we have solved the equations by hand instead of using matrix notation (as Lagrange does).
But given 2,4,8,16 most people would think that the equation is y = 2x. This is not a polynomial equation, so can't be expressed as a polynomial.
For the series 2,4,8 we derived the polynomial y = x² - x + 2. If we tried to extrapolate to find the next value, plugging x=4 will give us y = 4² - 4 + 2 = 14. The term after (x=5) that would be y = 5² - 5 + 2 = 22. As x gets larger, y = x² - x + 2 becomes an increasingly bad approximation to y = 2x. In fact no polynomial will grow as fast as y = 2x.
So ...
If you have n points, you can always find a unique polynomial of degree n-1 (or sometimes less) which will generate exactly those n points for x=1,2,3..n. This is not often used for real life problems, because these solutions are unstable (small changes to input produce large changes to the polynomial).
If you have n points, there are an infinite number of polynomials of degree n or greater which will produce the series. These all have identical values for x = 1, 2, ... n but will disagree on the n+1, n+2 etc terms.
Typically a polynomial approximation of degree less than n-1 is used. It won't usually be an exact fit, but will often show the general shape of the curve. For 8 points you might try and find a polynomial of degree 4 (y = ax⁴ + cx³ + dx² + e) which minimises the error. As a rule of thumb, a polynomial of degree of about n/2 is often used. This is more art than science; usually you have some idea of what the underlying (correct) formula is, and this helps select the degree of the approximating polynomial.
Polynomial approximations can work reasonably well for interpolation (finding a value between two data points) but are hopeless for extrapolation. As we have no knowledge at all of what the "next" value is a series might be (it could be anything), no formula can successfully predict it.
I hope this is useful. Producing a polynomial which exactly generates a finite series is not hard ... its simply solving n linear simultaneous equations with n variables (the coefficients of xn-1, xn-2, ... x², x, and the constant term). This is what we have done above and how Lagrange works. However, in physical systems it may not be particularly meaningful. User beware.
I'm looking to construct an algorithm which gives the arrangements with repetition of n sequences of a given step S (which can be a positive real number), under the constraint that the sum of all combinations is k, with k a positive integer.
My problem is thus to find the solutions to the equation:
x 1 + x 2 + ⋯ + x n = k
where
0 ≤ x i ≤ b i
and S (the step) a real number with finite decimal.
For instance, if 0≤xi≤50, and S=2.5 then xi = {0, 2.5 , 5,..., 47.5, 50}.
The point here is to look only through the combinations having a sum=k because if n is big it is not possible to generate all the arrangements, so I would like to bypass this to generate only the combinations that match the constraint.
I was thinking to start with n=2 for instance, and find all linear combinations that match the constraint.
ex: if xi = {0, 2.5 , 5,..., 47.5, 50} and k=100, then we only have one combination={50,50}
For n=3, we have the combination for n=2 times 3, i.e. {50,50,0},{50,0,50} and {0,50,50} plus the combinations {50,47.5,2.5} * 3! etc...
If xi = {0, 2.5 , 5,..., 37.5, 40} and k=100, then we have 0 combinations for n=2 because 2*40<100, and we have {40,40,20} times 3 for n=3... (if I'm not mistaken)
I'm a bit lost as I can't seem to find a proper way to start the algorithm, knowing that I should have the step S and b as inputs.
Do you have any suggestions?
Thanks
You can transform your problem into an integer problem by dividing everything by S: We want to find all integer sequences y1, ..., yn with:
(1) 0 ≤ yi ≤ ⌊b / S⌋
(2) y1 + ... + yn = k / S
We can see that there is no solution if k is not a multiple of S. Once we have reduced the problem, I would suggest using a pseudopolynomial dynamic programming algorithm to solve the subset sum problem and then reconstruct the solution from it. Let f(i, j) be the number of ways to make sum j with i elements. We have the following recurrence:
f(0,0) = 1
f(0,j) = 0 forall j > 0
f(i,j) = sum_{m = 0}^{min(floor(b / S), j)} f(i - 1, j - m)
We can solve f in O(n * k / S) time by filling it row by row. Now we want to reconstruct the solution. I'm using Python-style pseudocode to illustrate the concept:
def reconstruct(i, j):
if f(i,j) == 0:
return
if i == 0:
yield []
return
for m := 0 to min(floor(b / S), j):
for rest in reconstruct(i - 1, j - m):
yield [m] + rest
result = reconstruct(n, k / S)
result will be a list of all possible combinations.
What you are describing sounds like a special case of the subset sum problem. Once you put it in those terms, you'll find that Pisinger apparently has a linear time algorithm for solving a more general version of your problem, since your weights are bounded. If you're interested in designing your own algorithm, you might start by reading Pisinger's thesis to get some ideas.
Since you are looking for all possible solutions and not just a single solution, the dynamic programming approach is probably your best bet.
I am reading an algorithms textbook and I am stumped by this question:
Suppose we want to compute the value x^y, where x and y are positive
integers with m and n bits, respectively. One way to solve the problem is to perform y - 1 multiplications by x. Can you give a more efficient algorithm that uses only O(n) multiplication steps?
Would this be a divide and conquer algorithm? y-1 multiplications by x would run in theta(n) right? .. I don't know where to start with this question
I understand this better in an iterative way:
You can compute x^z for all powers of two: z = (2^0, 2^1, 2^2, ... ,2^(n-1))
Simply by going from 1 to n and applying x^(2^(i+1)) = x^(2^i) * x^(2^i).
Now you can use these n values to compute x^y:
result = 1
for i=0 to n-1:
if the i'th bit in y is on:
result *= x^(2^i)
return result
All is done in O(n)
Apply a simple recursion for divide and conquer.
Here i am posting a more like a pseudo code.
x^y :=
base case: if y==1 return x;
if y%2==0:
then (x^2)^(y/2;
else
x.(x^2)^((y-1)/2);
The y-1 multiplications solution is based on the identity x^y = x * x^(y-1). By repeated application of the identity, you know that you will decrease y down to 1 in y-1 steps.
A better idea is to decrease y more "energically". Assuming an even y, we have x^y = x^(2*y/2) = (x^2)^(y/2). Assuming an odd y, we have x^y = x^(2*y/2+1) = x * (x^2)^(y/2).
You see that you can halve y, provided you continue the power computation with x^2 instead of x.
Recursively:
Power(x, y)=
1 if y = 0
x if y = 1
Power(x * x, y / 2) if y even
x * Power(x * x, y / 2) if y odd
Another way to view it is to read y as a sum of weighted bits. y = b0 + 2.b1 + 4.b2 + 8.b3...
The properties of exponentiation imply:
x^y = x^b0 . x^(2.b1) . x^(4.b2) . x^(8.b2)...
= x^b0 . (x^2)^b1 . (x^4)^b2 . (x^8)^b3...
You can obtain the desired powers of x by squaring, and the binary decomposition of y tells you which powers to multiply.
Using assorted matrix math, I've solved a system of equations resulting in coefficients for a polynomial of degree 'n'
Ax^(n-1) + Bx^(n-2) + ... + Z
I then evaulate the polynomial over a given x range, essentially I'm rendering the polynomial curve. Now here's the catch. I've done this work in one coordinate system we'll call "data space". Now I need to present the same curve in another coordinate space. It is easy to transform input/output to and from the coordinate spaces, but the end user is only interested in the coefficients [A,B,....,Z] since they can reconstruct the polynomial on their own. How can I present a second set of coefficients [A',B',....,Z'] which represent the same shaped curve in a different coordinate system.
If it helps, I'm working in 2D space. Plain old x's and y's. I also feel like this may involve multiplying the coefficients by a transformation matrix? Would it some incorporate the scale/translation factor between the coordinate systems? Would it be the inverse of this matrix? I feel like I'm headed in the right direction...
Update: Coordinate systems are linearly related. Would have been useful info eh?
The problem statement is slightly unclear, so first I will clarify my own interpretation of it:
You have a polynomial function
f(x) = Cnxn + Cn-1xn-1 + ... + C0
[I changed A, B, ... Z into Cn, Cn-1, ..., C0 to more easily work with linear algebra below.]
Then you also have a transformation such as: z = ax + b that you want to use to find coefficients for the same polynomial, but in terms of z:
f(z) = Dnzn + Dn-1zn-1 + ... + D0
This can be done pretty easily with some linear algebra. In particular, you can define an (n+1)×(n+1) matrix T which allows us to do the matrix multiplication
d = T * c ,
where d is a column vector with top entry D0, to last entry Dn, column vector c is similar for the Ci coefficients, and matrix T has (i,j)-th [ith row, jth column] entry tij given by
tij = (j choose i) ai bj-i.
Where (j choose i) is the binomial coefficient, and = 0 when i > j. Also, unlike standard matrices, I'm thinking that i,j each range from 0 to n (usually you start at 1).
This is basically a nice way to write out the expansion and re-compression of the polynomial when you plug in z=ax+b by hand and use the binomial theorem.
If I understand your question correctly, there is no guarantee that the function will remain polynomial after you change coordinates. For example, let y=x^2, and the new coordinate system x'=y, y'=x. Now the equation becomes y' = sqrt(x'), which isn't polynomial.
Tyler's answer is the right answer if you have to compute this change of variable z = ax+b many times (I mean for many different polynomials). On the other hand, if you have to do it just once, it is much faster to combine the computation of the coefficients of the matrix with the final evaluation. The best way to do it is to symbolically evaluate your polynomial at point (ax+b) by Hörner's method:
you store the polynomial coefficients in a vector V (at the beginning, all coefficients are zero), and for i = n to 0, you multiply it by (ax+b) and add Ci.
adding Ci means adding it to the constant term
multiplying by (ax+b) means multiplying all coefficients by b into a vector K1, multiplying all coefficients by a and shifting them away from the constant term into a vector K2, and putting K1+K2 back into V.
This will be easier to program, and faster to compute.
Note that changing y into w = cy+d is really easy. Finally, as mattiast points out, a general change of coordinates will not give you a polynomial.
Technical note: if you still want to compute matrix T (as defined by Tyler), you should compute it by using a weighted version of Pascal's rule (this is what the Hörner computation does implicitely):
ti,j = b ti,j-1 + a ti-1,j-1
This way, you compute it simply, column after column, from left to right.
You have the equation:
y = Ax^(n-1) + Bx^(n-2) + ... + Z
In xy space, and you want it in some x'y' space. What you need is transformation functions f(x) = x' and g(y) = y' (or h(x') = x and j(y') = y). In the first case you need to solve for x and solve for y. Once you have x and y, you can substituted those results into your original equation and solve for y'.
Whether or not this is trivial depends on the complexity of the functions used to transform from one space to another. For example, equations such as:
5x = x' and 10y = y'
are extremely easy to solve for the result
y' = 2Ax'^(n-1) + 2Bx'^(n-2) + ... + 10Z
If the input spaces are linearly related, then yes, a matrix should be able to transform one set of coefficients to another. For example, if you had your polynomial in your "original" x-space:
ax^3 + bx^2 + cx + d
and you wanted to transform into a different w-space where w = px+q
then you want to find a', b', c', and d' such that
ax^3 + bx^2 + cx + d = a'w^3 + b'w^2 + c'w + d'
and with some algebra,
a'w^3 + b'w^2 + c'w + d' = a'p^3x^3 + 3a'p^2qx^2 + 3a'pq^2x + a'q^3 + b'p^2x^2 + 2b'pqx + b'q^2 + c'px + c'q + d'
therefore
a = a'p^3
b = 3a'p^2q + b'p^2
c = 3a'pq^2 + 2b'pq + c'p
d = a'q^3 + b'q^2 + c'q + d'
which can be rewritten as a matrix problem and solved.