0
$\begingroup$

I have data of the form:

X       Y
3.53    0
4.93    50
5.53    60
6.21    70
7.37    80
9.98    90
16.56   100

And I want to find out $n$ so that this can be fit to a function of the form:

$$y = \frac{a}{x^n} + b$$

I am trying to determine $n$ by Box-Cox transformation. Can n be determined with Box-Cox transformation?

$\endgroup$
  • 1
    $\begingroup$ Why not just use a nonlinear regression? $\endgroup$ – Wolfgang Bangerth May 3 '15 at 21:32
  • $\begingroup$ How do I find n then? $\endgroup$ – nxkryptor May 4 '15 at 4:13
  • $\begingroup$ It's a three-parameter nonlinear regression. Excel can do it. Gnuplot can do it. Octave and Matlab and Maple and Mathematica all can do it. Just search for the term in the documentation of whatever software you use. $\endgroup$ – Wolfgang Bangerth May 4 '15 at 11:23
  • $\begingroup$ I don't know how to do this particular fit in Excel, unless you make the change of variable $z=1/x$. Is there a direct way? $\endgroup$ – nicoguaro May 7 '15 at 16:01
1
$\begingroup$

As they mentioned (and scolded you), why are you trying to use that method?

If a non-linear regression is enough you can see the code below, the option curve_fit did not work. So I used minimize in this case, the initial guess is obtained from a linear regression of $y$ and $(1/x)$.

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize

def func(coef, x, y):
    a = coef[0]
    b = coef[1]
    n = coef[2]
    return np.linalg.norm(y - a/x**n - b)

x = np.array([3.53, 4.93, 5.53, 6.21, 7.37, 9.98, 16.56])
y = np.array([0, 50, 60, 70, 80, 90, 100])


# The initial values are obtained from a linear regression between
# y and (1/x)
res = minimize(func, [-446, 136, 1], args=(x, y))
a, b, n = res.x
print a, b, n
x_vec = np.linspace(3.5, 17)
y_vec = a/x_vec**n + b

plt.plot(x, y, 'ko')
plt.plot(x_vec, y_vec)
plt.xlabel("x")
plt.xlabel("y")
plt.savefig("regression.png", dpi=300)

The output is

-1181.72495119 105.101516419 1.91866624841

And the image

enter image description here

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.