Search code examples
pythonstandardsdeviation

By Python. making standard deviation and mean lines with different graphes


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


Solution

  • 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.