[Tutor] Statistic-Program Problems! Please Help Quickly!
Steven D'Aprano
steve at pearwood.info
Fri Oct 15 09:15:16 CEST 2010
On Fri, 15 Oct 2010 03:09:57 pm Colleen Glaeser wrote:
> Now, my functions for X, Y, P, and Q are correct, but I have a couple
> of problems when it comes to continuing. First of all, despite what
> my teacher has told me, my method for trying to multiply X,Y,P, and
> Q's results in the functions for B and M are not working.
In case it wasn't clear from my earlier email, this is because the
functions X, Y, P and Q don't return anything, they just print their
result.
> I'm not
> sure if there is a way to make functions into variables or how to
> solve this problem.
>
> Second, I am confused as to what my teacher means to do when it comes
> to inputting different values of x.
The formula for a straight line looks like:
y = m*x + b
where x and y are variables, and m and b are the slope and intercept of
the line. m and b are parameters: you set them once, to choose a line,
and then calculate with the variables. When you change the parameters,
you change the line.
You pick m and b (say, m=2 an b=3) to get a formula y = 2*x + 3, and
then you can put in different values of x to get different values of y:
x 2*x+3 = y
------------
0 2*0+3 = 3
1 2*1+3 = 5
2 2*2+3 = 7
etc.
In this assignment, the values for m and b aren't chosen arbitrarily,
but calculated from the data sets using the functions that you called
X(), Y(), P() etc.
Bringing this back to Python, you first have to calculate your
regression parameters m and b:
m = ... # something goes here
b = ... # something else goes here
Then you need to turn them into a function that takes x as an argument
and returns y. How to do this?
I know earlier I suggested that global variables should be avoided. I
stand by that, but in this case I think that here you should use
globals. There are alternatives, but they are more advanced techniques,
and at this early stage keeping it simple is a good thing. So I might
write a function like this:
def line(x):
return m*x+b
and use it like this:
y = line(5) # try it with x=5
This returns the value of y that goes with the value of x=5, for the
regression line you calculated earlier. Then you move on to the second
set of data:
m = ... # something goes here
b = ... # something else goes here
y = line(95)
which calculates the y that goes with x=95 for the new regression line.
--
Steven D'Aprano
More information about the Tutor
mailing list