Lesson 4: Bokeh: web interface

常態分佈(高斯分佈)為自然界最常見的分佈情形

高斯分佈的平均值:平均值會落在中心的對稱線上,並構成左右對稱的單一峰值分佈. 在此理想狀況時,此分布的平均數, 中位數,與眾數重合(同一數值).

分布範圍: 一般用標準偏差\(\sigma\)(standard deviation)來描述分布的集中趨勢:
在正負一個標準差的範圍內, 將包含68.3%的數值.
在正負兩個標準差的範圍內, 將包含95.4%的數值.
在正負三個標準差的範圍內, 將包含99.7%的數值.


在實際應用上,常考慮一組數據具有近似於常態分布的機率分布。若其假設正確,則約68.3%數值分布在距離平均值有1個標準差之內的範圍,約95.4%數值分布在距離平均值有2個標準差之內的範圍,以及約99.7%數值分布在距離平均值有3個標準差之內的範圍。稱為「68-95-99.7法則」


半高寬(Full width at half maximum,縮寫為FWHM):
指在函數的一個峰當中,前後兩個函數值等於峰值一半的點之間的距離, 在高斯分佈中高斯函數的半高寬為\(2\sqrt{2 \ln 2}\times \sigma=2.355\sigma\)

誤差(error)

量測一個物理量的數值時,所得數值的分佈通常滿足常態分佈. 也就是說,如果我們量測同一個物理量N次. 當N夠大時, 這N個數據點的平均值\(\bar{x}\)大約就是這個物理量的值(分布的中線,峰值). 當我們只考慮單一量測\(x_i\)時, 該次量測的誤差為(\(x_i-\bar{x}\))
當我們考慮N個量測\(x_i\)時, 所有量測的平均誤差為 \( \frac { \Sigma _{i=1 \rightarrow N} (x_i-\bar{x}) }{N} \)
如果多次量測的值都滿足常態分佈, 當量測次數接近無窮多次時,所有量測數值的平均值即是量測物理量的理論值(真值), 這將使得上式在N趨近於無限大時也會趨近於零.


import numpy as np
from bokeh.layouts import row, column
from bokeh.models import CustomJS, Slider
from bokeh.plotting import figure, output_file, show, ColumnDataSource

mean_slider = Slider(start=3, end=10, value=6.5, step=0.01, title="Mean")
width_slider = Slider(start=0.1, end=3, value=0.35, step=0.01, title="Width")
high_slider = Slider(start=0, end=110, value=100, step=.1, title="High")

x = np.linspace(4,12, 1000)
y = 100*np.exp(-(6.5-x)*(6.5-x)/0.35/0.35/2)
source = ColumnDataSource(data=dict(x=x, y=y))

plot = figure(y_range=(-0.1, 110), plot_width=800, plot_height=400, tooltips=[("x", "$x"), ("y", "$y")])

plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

callback = CustomJS(args=dict(source=source, amp=mean_slider, freq=width_slider, high=high_slider),
                    code="""
    const data = source.data;
    const A = amp.value;
    const w = freq.value;
    const h = high.value;
    const x = data['x']
    const y = data['y']
    for (var i = 0; i < x.length; i++) {
        y[i] = h*Math.pow(2.71, -(x[i]-A)*(x[i]-A)/w/w/2);
    }
    source.change.emit();
""")

mean_slider.js_on_change('value', callback)
width_slider.js_on_change('value', callback)
high_slider.js_on_change('value', callback)

layout = row(
    plot,
    column(mean_slider, width_slider, high_slider),
)

output_file("gaussian.html", title="gaussian.py example")

show(layout)