Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168728
Image: ubuntu2004

First, we just get 10 random numbers.

data=[random() for _ in range(10)] data
[0.25104490265118429, 0.59577347199168573, 0.24526116814395449, 0.24623493340462133, 0.66774915441382676, 0.24014069151163964, 0.37885267696124048, 0.37394052166300384, 0.93371690082016456, 0.34701573652610196]

Min and max:

min(data)
0.24014069151163964
max(data)
0.93371690082016456

We can calculate the mean directly from the formula.  The "len(data)" just gives us how many data points we have.

mean=sum(data)*1.0/len(data) mean
0.427973015808742

We can also calculate the sample standard deviation directly from the definition.  Note that we use the mean that we calculated above.

variance=sum([ (x-mean)^2 for x in data])*1.0/(len(data)-1) variance
0.0540344172551465

Then the sample standard deviation is the square root of the sample variance.

stdev=sqrt(variance) stdev
0.232453043118705

We can also calculate some of these quantities using libraries in Sage.

import scipy.stats import numpy
numpy.mean(data)
0.42797301580874236

When calculating the sample standard deviation, remember that we divide by n1n-1.  By default, the "std" function calculates the population standard deviation (i.e., it divides by nn).  To get the sample standard deviation, we say "ddof=1".  Then the standard deviation is calculated by dividing by nddof=n1n-\text{ddof}=n-1.

numpy.std(data, ddof=1)
0.23245304311870499

To calculate the quartiles (or any percentile), use the "scoreatpercentile" function and pass the percentile as an integer between 0 and 100.

scipy.stats.scoreatpercentile(data, 25) # 25th percentile
0.24743742571626207
scipy.stats.scoreatpercentile(data, 75) # 75th percentile
0.54154327323407436