-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72f98f2
commit a9076ae
Showing
22 changed files
with
217 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
#temporario | ||
*.jpg | ||
*.png | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +0,0 @@ | ||
:::LabIFSC2._arrays.linspace | ||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
Nesta seção, explicarei em mais detalhes como a biblioteca propaga incertezas, o método usado é mais geral, mas ainda assim compatível com o da apostila. Nos testes unitários da biblioteca, comparamos os erros calculados pelo LabIFSC2 com as bibliotecas [uncertainties](https://pythonhosted.org/uncertainties/) e [LabIFSC](https://github.com/gjvnq/LabIFSC), chegando a um acordo geralmente de \(10^{-3}\) para erros pequenos, onde os métodos devem ser equivalentes. | ||
|
||
## Apostila | ||
A apostila se baseia principalmente no GUM (Guide to the Expression of Uncertainty in Measurement)[^1]. O método é uma propagação linear baseada em uma série de Taylor. | ||
|
||
Começamos com uma série de Taylor de uma função de \(N\) variáveis, ou seja, uma boa aproximação para **pequenas variações da função**: | ||
|
||
$$f(\mathbf{x}+\Delta \mathbf{x})\approx f(\mathbf{x})+\sum_{i} \frac{\partial f}{\partial x_{i}}\Delta x_{i}$$ | ||
|
||
$$(f(\mathbf{x}+\Delta \mathbf{x})-f(\mathbf{x}))²\approx (\sum_{i} \frac{\partial f}{\partial x_{i}}\Delta x_{i})²$$ | ||
|
||
Note que o lado esquerdo nada mais é que a variação da função \(\Delta f\). Se pegarmos o valor esperado, teremos a variância de \(f\) (\(\sigma²_{f}\)): | ||
|
||
$$\mathbb{E}[(f(\mathbf{x}+\Delta \mathbf{x})-f(\mathbf{x}))²]\approx \mathbb{E}[(\sum_{i} \frac{\partial f}{\partial x_{i}}\Delta x_{i})²]$$ | ||
|
||
Supondo que \(\Delta x_i\) tenha uma distribuição simétrica \(\mathbb{E}(\Delta x_{i})=0\), os termos cruzados \(\mathbb{E}(\Delta x_{i}\Delta x_{j})=\mathbb{E}(\Delta x_{i})\mathbb{E}(\Delta x_{j})=0\) desaparecem (se supormos que são totalmente independentes). | ||
|
||
Logo: | ||
|
||
$$\sigma²_{f}=\sum_{i} \left(\frac{\partial f}{\partial x_{i}}\sigma_{x_{i}}\right)²$$ | ||
|
||
Se quisermos em termos do desvio padrão e não da variância, temos: | ||
|
||
$$\sigma_{f}=\sqrt{\sum_{i} \left(\frac{\partial f}{\partial x_{i}}\sigma_{x_{i}}\right)²}$$ | ||
|
||
Essa é essencialmente a fórmula usada na apostila. Para o caso de uma variável, se reduz a \(\sigma_{f}=|\frac{\partial f}{\partial x}\sigma_{x}|\). A diferença é que a apostila ignora a raiz quadrada na expressão de incertezas com mais de uma variável, superestimando assim o erro. | ||
|
||
Pensando intuitivamente, erros não podem simplesmente se somar, visto que, pela sua natureza aleatória, é esperado que existam erros que acabem "compensando" outros. | ||
|
||
## Monte Carlo | ||
Imagine que temos uma medida indireta \(y\) que depende de um conjunto de \(N\) medidas: | ||
|
||
$$y=f(x_1,x_2,\dots,x_n)$$ | ||
|
||
Cada variável \(x_i\) tem sua PDF ([probability density function](https://en.wikipedia.org/wiki/Probability_density_function)), que é uma forma matemática de dizer que não temos certeza sobre seus valores. Por simplicidade, assumimos que medidas diretas têm distribuições gaussianas (centradas em uma média \(\mu\) e uma variância \(\sigma²\)). | ||
|
||
O Método Monte Carlo consiste em simular diversas medidas experimentais no computador (usando um gerador de números aleatórios com as respectivas PDFs). Dessa forma, geramos um histograma de possíveis valores de \(y\); esse histograma é a PDF de \(y\). | ||
|
||
O interessante desse método é que o histograma de \(y\) não necessariamente é analítico (geralmente com formatos bem estranhos para incertezas grandes). Esse histograma é utilizado para diversas coisas na biblioteca: | ||
|
||
- Ser usado como PDF para outra propagação de incerteza | ||
- Cálculo do intervalo de confiança | ||
- A média e o desvio padrão da PDF são usados no `print(medida)` | ||
|
||
### Exemplo com a gravidade | ||
Retornando ao exemplo da estimativa da gravidade usando um pêndulo, mas agora com incertezas maiores em \(T\) e \(L\) (para que os efeitos fiquem mais visíveis). | ||
|
||
A classe `Medida` possui um atributo chamado `histograma`, onde estão guardados os histogramas. No dia a dia, esse atributo deve ser raramente acessado, mas para fins didáticos ele é interessante. | ||
|
||
```py | ||
--8<-- "tests/test_doc_gravidade_histograma.py:8:15" | ||
``` | ||
|
||
Repare como \(T\) e \(L\) são gaussianas (\(\mu_L=15cm\), \(\sigma_L=1cm\)) e (\(\mu_T=780ms\), \(\sigma_T=80ms\)). | ||
|
||
<img src="./images/gravidade_histograma.jpg" width=600> | ||
|
||
Já o histograma de \(g\) é centralizado em \(10m/s²\), mas observe que ele possui uma cauda para a direita. A distribuição não é simétrica, logo, não é gaussiana. Se usássemos \(g\) para outros cálculos, esse desvio de uma gaussiana provavelmente iria se amplificando. Esse fato não é observado no método GUM, que assume linearidade e basicamente tudo é uma gaussiana. | ||
|
||
|
||
|
||
[^1]: O método GUM é amplamente utilizado em metrologia e calibragem de equipamentos. Existem diversas referências para quem quiser aprender mais. Eu, pessoalmente, achei um material introdutório e interessante em: | ||
|
||
Kirkup, L., Frenkel, R. B. (2006). An Introduction to Uncertainty in Measurement: Using the GUM (Guide to the Expression of Uncertainty in Measurement). Reino Unido: Cambridge University Press. | ||
|
||
[^2]: O principal material usado na implementação do Monte Carlo foi o próprio material suplementar do GUM sobre Monte Carlo. É interessante notar que esse material explicitamente considera o método Monte Carlo como uma forma mais precisa de calcular incertezas: | ||
|
||
“Evaluation of Measurement Data — Supplement 1 to the ‘Guide to the Expression of Uncertainty in Measurement’ — Propagation of Distributions Using a Monte Carlo Method,” 2008. https://doi.org/10.59161/JCGM101-2008. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,33 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
from LabIFSC2 import * | ||
|
||
campo_magnético=arrayM([210,90,70,54,39,32,33,27,22,20],1,'muT') | ||
distancias=linspace(1,10,10,0.01,'cm') | ||
distancias=linspaceM(1,10,10,0.01,'cm') | ||
|
||
unidade_x='cm' | ||
unidade_y='muT' | ||
|
||
regressao=regressao_potencia(distancias,campo_magnético) | ||
print(regressao) | ||
print(regressao) | ||
#MLeiDePotencia(a=(2,0 ± 0,1)x10² cm⁰⋅⁹⁸⁶³⁵⁵·µT, b=(-9,9 ± 0,3)x10⁻¹ ) | ||
plt.style.use('ggplot') | ||
plt.errorbar(nominais(distancias,unidade_x),nominais(campo_magnético,unidade_y), | ||
xerr=incertezas(distancias,unidade_x),yerr=incertezas(campo_magnético,unidade_y), | ||
plt.errorbar(x=nominais(distancias,unidade_x), | ||
y=nominais(campo_magnético,unidade_y), | ||
xerr=incertezas(distancias,unidade_x), | ||
yerr=incertezas(campo_magnético,unidade_y), | ||
fmt='o',label='Dados experimentais',color='red') | ||
|
||
x=linspace(1,10,100,0,unidade_x) | ||
plt.plot(nominais(x,unidade_x),nominais(regressao(x),unidade_y),color='blue', | ||
x=linspaceM(1,10,100,0,unidade_x) | ||
plt.plot(nominais(x,unidade_x), | ||
nominais(regressao(x),unidade_y), | ||
color='blue', | ||
label="Curva teórica") | ||
plt.fill_between(nominais(x,unidade_x),curva_min(regressao(x),unidade_y), | ||
curva_max(regressao(x),unidade_y),color='blue',alpha=0.3) | ||
|
||
plt.fill_between(x=nominais(x,unidade_x), | ||
y1=curva_min(regressao(x),unidade_y), | ||
y2=curva_max(regressao(x),unidade_y), | ||
color='blue',alpha=0.3) | ||
plt.legend() | ||
#plt.savefig('teste.jpg') | ||
plt.savefig('docs/images/graficos_fitting.jpg',dpi=300) | ||
plt.cla() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.