-1

Maybe the title seems bit confusing.

For example, there are three functions such as sin(x), 3 sine(x) and sin(x)+1. X would be from 1 to 100. How can I draw lines of mean with standard deviation (+ and -) for these three function values. I think that maybe I should calculate mean and standard deviation of three function values (sin(x), 3 sin(x) and sin(x)+1) at each x. However, I am not sure how I can do it with python. I know there are some function of standard deviation and mean in Scipy. Is that applicable for this case? Maybe this is stupid question. However, I am pretty novice. I really appreciate any help.

Best regards,

Isaac

Isaac
  • 885
  • 2
  • 15
  • 35
  • Do you mean how do you calculate confidence intervals? I'm assuming that you're not trying to "draw" the graphs on the screen? – Joel Cornett Feb 27 '13 at 08:02
  • I mean, the distribution of any function given a constant parameter is a degenerate distribution with a variance (hence standard deviation) of 0. So this is not what you want. – Joel Cornett Feb 27 '13 at 08:05
  • 1
    @Joel, I think he wants the moments, integrated or summed over the interval. E.g., for the stddev, `\int_1^100 (x * sin(x))`, or `\sum_1^100 (x * sin(x)) / n`. Isaac, does this looks like what you want? –  Feb 27 '13 at 09:34
  • @Evert: Oh I see. That makes more sense. – Joel Cornett Feb 27 '13 at 18:23
  • Thank you for Joel and Evert. I apologize for late answer. Actually I want to make one graph with standard deviation mean from several different graphes. As Evert said, integration or summation would be better description. Do you think that I should sum those functions up and devided by the number of functions? – Isaac Feb 27 '13 at 19:34
  • @Isaac, it now sounds like you want to calculate the mean and stddev for every point separately, from the function values. I first thought you wanted something like average power (= the rms of a voltage sine wave). –  Feb 28 '13 at 17:17
  • @Isaac, I've now extended my answer to include a few other possibilities, though it's still not clear what your goal/question is. –  Feb 28 '13 at 17:26

2 Answers2

1

I'm not exactly sure what you mean, but perhaps the following is a useful example:

>>> import numpy as np
>>> x = np.arange(1,100)
>>> m = (sin(x)+1).mean()
>>> s = (sin(x)+1).std()
>>> print m, s
1.00383024876 0.710743876537

[edit after some further clarification]

If, however, you want the average per x-point of the various functions, something like this would work:

>>> y = np.array([sin(x), 3*sin(x), sin(x)+1])
>>> m = y.mean(axis=0)
>>> s = y.std(axis=0)

which would give you 100 means and 100 stddevs.

If you want the average of the combined function, you're essentially back to the first example:

>>> m = (sin(x) + 3*sin(x) + sin(x)+1).mean()
>>> s = (sin(x) + 3*sin(x) + sin(x)+1).std()
>>> print m, s
1.01915124381 3.55371938269

Which option is the one applicable for you depends on the context of your question; I have no clue about that.

0
import numpy as np

def function_1(X):
    return np.sin(X) 

def function_2(X):
    return 3. * np.sin(X) 

def function_3(X):
    return np.sin(X + 1.) 

X = np.arange(100)

# mean
print function_1(X).mean()

# std dev
print function_1(X).std()

# to plot
from matplotlib import pyplot as mp
mp.plot(X, function_1(X))
mp.hlines(function_1(X).mean(), 0, 100)
mp.show()

and so on... But do you really need to plot the "mean" of a sine function? Think about it ...

danodonovan
  • 19,636
  • 10
  • 70
  • 78
  • Thank you danodonovan. If I understand correctly,your code seems only plot the standard deviation and mean of function_1(x). Am I right? What I want to do is combining those three function into one graph with standard deviation and mean. Actually, What I really want to do is combining more than hundred spectra to one graph with mean and standard deviation. I just want to get idea. Thank you again. – Isaac Feb 27 '13 at 19:44
  • How are you combining the functions? You could multiply, add, "convolve" - you can do all this with `function_*` ie `mean = function_1(x) + function_2(x) + function_3(x)` etc. – danodonovan Feb 27 '13 at 19:47
  • Thank you danodonovan, I should think more. Isaac – Isaac Feb 27 '13 at 19:57