Python の統計モジュールは、次のような関数を提供します。 stdev() 、標準偏差の計算に使用できます。 stdev() 関数は、母集団全体ではなく、データのサンプルからの標準偏差のみを計算します。
母集団全体の標準偏差を計算するには、次の関数として知られています。 pstdev() 使用されている。
標準偏差 統計における広がりの尺度です。これは、一連のデータ値の広がり、変動の尺度を定量化するために使用されます。これは分散と非常によく似ており、分散の尺度が得られるのに対し、分散は二乗値を求めます。
標準偏差の低い値はデータの分散が少ないことを示し、標準偏差の高い値はセット内のデータが平均値から離れて分散していることを示します。標準偏差の便利な特性は、分散とは異なり、データと同じ単位で表現されることです。
Standard Deviation is calculated by : where x1, x2, x3.....xn are observed values in sample data, is the mean value of observations andN is the number of sample observations.>
構文: stdev( [データセット], xbar )
パラメーター :
[データ] : 実数値を含む反復可能オブジェクト。
xbar (オプション) : データセットの実際の平均を値として受け取ります。
戻り値の型: パラメータとして渡された値の実際の標準偏差を返します。
例外:
統計エラー パラメータとして渡されたデータセットの値が 2 未満の場合に発生します。
不可能な/精度の低い値 値が次のように指定された場合 xbar データセットの実際の平均値と一致しません。
コード #1 :
Python3
# Python code to demonstrate stdev() function> # importing Statistics module> import> statistics> # creating a simple data - set> sample>=> [>1>,>2>,>3>,>4>,>5>]> # Prints standard deviation> # xbar is set to default value of 1> print>(>'Standard Deviation of sample is % s '> >%> (statistics.stdev(sample)))> |
>
>
出力:
Standard Deviation of the sample is 1.5811388300841898>
コード #2 : さまざまなデータ型セットに対する stdev() のデモを行う
Python3
# Python code to demonstrate stdev()> # function on various range of datasets> # importing the statistics module> from> statistics>import> stdev> # importing fractions as parameter values> from> fractions>import> Fraction as fr> # creating a varying range of sample sets> # numbers are spread apart but not very much> sample1>=> (>1>,>2>,>5>,>4>,>8>,>9>,>12>)> # tuple of a set of negative integers> sample2>=> (>->2>,>->4>,>->3>,>->1>,>->5>,>->6>)> # tuple of a set of positive and negative numbers> # data-points are spread apart considerably> sample3>=> (>->9>,>->1>,>->0>,>2>,>1>,>3>,>4>,>19>)> # tuple of a set of floating point values> sample4>=> (>1.23>,>1.45>,>2.1>,>2.2>,>1.9>)> # Print the standard deviation of> # following sample sets of observations> print>(>'The Standard Deviation of Sample1 is % s'> >%>(stdev(sample1)))> > print>(>'The Standard Deviation of Sample2 is % s'> >%>(stdev(sample2)))> > print>(>'The Standard Deviation of Sample3 is % s'> >%>(stdev(sample3)))> > > print>(>'The Standard Deviation of Sample4 is % s'> >%>(stdev(sample4)))> |
>
>
出力:
The Standard Deviation of Sample1 is 3.9761191895520196 The Standard Deviation of Sample2 is 1.8708286933869707 The Standard Deviation of Sample3 is 7.8182478855559445 The Standard Deviation of Sample4 is 0.41967844833872525>
コード #3 : variance() と stdev() の結果の違いを示す
Python3
# Python code to demonstrate difference> # in results of stdev() and variance()> # importing Statistics module> import> statistics> # creating a simple data-set> sample>=> [>1>,>2>,>3>,>4>,>5>]> # Printing standard deviation> # xbar is set to default value of 1> print>(>'Standard Deviation of the sample is % s '> >%>(statistics.stdev(sample)))> # variance is approximately the> # squared result of what stdev is> print>(>'Variance of the sample is % s'> >%>(statistics.variance(sample)))> |
>
>
出力:
Standard Deviation of the sample is 1.5811388300841898 Variance of the sample is 2.5>
コード #4 : の使用法をデモンストレーションする xbar パラメータ
Python3
# Python code to demonstrate use of xbar> # parameter while using stdev() function> # Importing statistics module> import> statistics> # creating a sample list> sample>=> (>1>,>1.3>,>1.2>,>1.9>,>2.5>,>2.2>)> # calculating the mean of sample set> m>=> statistics.mean(sample)> # xbar is nothing but stores> # the mean of the sample set> # calculating the variance of sample set> print>(>'Standard Deviation of Sample set is % s'> >%>(statistics.stdev(sample, xbar>=> m)))> |
>
>
出力:
Standard Deviation of Sample set is 0.6047037842337906>
コード #5 : 統計エラーを示します
Python3
# Python code to demonstrate StatisticsError> # importing the statistics module> import> statistics> # creating a data-set with one element> sample>=> [>1>]> # will raise StatisticsError> print>(statistics.stdev(sample))> |
>
>
出力:
Traceback (most recent call last): File '/home/f921f9269b061f1cc4e5fc74abf6ce10.py', line 12, in print(statistics.stdev(sample)) File '/usr/lib/python3.5/statistics.py', line 617, in stdev var = variance(data, xbar) File '/usr/lib/python3.5/statistics.py', line 555, in variance raise StatisticsError('variance requires at least two data points') statistics.StatisticsError: variance requires at least two data points> アプリケーション:
- 標準偏差は、統計数学および統計研究の分野では非常に重要です。統計計算の信頼性を測定するためによく使用されます。たとえば、試験の点数を計算する際の誤差の範囲は、同じ試験が複数回実施された場合に予想される結果の標準偏差を計算することによって決定されます。
- これは、利益と損失のマージンを決定するのに役立つだけでなく、財務研究の分野でも非常に役立ちます。標準偏差も重要です。投資収益率の標準偏差は、投資のボラティリティの尺度になります。