├── .gitignore ├── README.md ├── main-sequence.stan ├── ms-scatter.stan ├── LICENSE.txt └── ms-imf.stan /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This example of non-linear model fitting sprang out of a [Twitter exchange](https://twitter.com/farrwill/status/972215383636705280) with James Guillochon asking for examples of fitting a model to x-y data with errors in both dimensions. 2 | -------------------------------------------------------------------------------- /main-sequence.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int Nobs; /* Number of observations */ 3 | 4 | real logLobs[Nobs]; /* natural log of luminosity observed. */ 5 | real sigma_logL[Nobs]; /* Uncertainty on each measurement. */ 6 | 7 | real Tobs[Nobs]; /* Observed temperature. */ 8 | real sigma_Tobs[Nobs]; /* Temperature uncertainty */ 9 | } 10 | 11 | parameters { 12 | real beta; /* Power-law slope L = T^beta */ 13 | realTtrue[Nobs]; /* True temperature */ 14 | } 15 | 16 | transformed parameters { 17 | real Ltrue[Nobs]; 18 | real logLtrue[Nobs]; 19 | 20 | for (i in 1:Nobs) { 21 | Ltrue[i] = Ttrue[i]^beta; 22 | logLtrue[i] = log(Ltrue[i]); 23 | } 24 | } 25 | 26 | model { 27 | /* Uniform prior on beta between limits. */ 28 | /* Uniform prior on temperature between limits. */ 29 | 30 | Tobs ~ normal(Ttrue, sigma_Tobs); 31 | logLobs ~ normal(logLtrue, sigma_logL); 32 | } 33 | -------------------------------------------------------------------------------- /ms-scatter.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int Nobs; /* Number of observations */ 3 | 4 | real logLobs[Nobs]; /* natural log of luminosity observed. */ 5 | real sigma_logL[Nobs]; /* Uncertainty on each measurement. */ 6 | 7 | real Tobs[Nobs]; /* Observed temperature. */ 8 | real sigma_Tobs[Nobs]; /* Temperature uncertainty */ 9 | } 10 | 11 | parameters { 12 | real beta; /* Power-law slope L = T^beta */ 13 | real sigma_scatter; 14 | realTtrue[Nobs]; /* True temperature */ 15 | real logLtrue[Nobs]; 16 | } 17 | 18 | transformed parameters { 19 | real Ltrue[Nobs]; 20 | 21 | Ltrue = exp(logLtrue); 22 | } 23 | 24 | model { 25 | /* Uniform prior on beta between limits. */ 26 | /* Uniform prior on temperature between limits. */ 27 | /* Broad, Cauchy prior on sigma_scatter, with scale 0.1 */ 28 | sigma_scatter ~ cauchy(0, 0.1); 29 | 30 | /* Scatter in the relation: */ 31 | for (i in 1:Nobs) { 32 | logLtrue[i] ~ normal(beta*log(Ttrue[i]), sigma_scatter); 33 | } 34 | 35 | Tobs ~ normal(Ttrue, sigma_Tobs); 36 | logLobs ~ normal(logLtrue, sigma_logL); 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Will M. Farr 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /ms-imf.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int Nobs; /* Number of observations */ 3 | 4 | real logLobs[Nobs]; /* natural log of luminosity observed. */ 5 | real sigma_logL[Nobs]; /* Uncertainty on each measurement. */ 6 | 7 | real Tobs[Nobs]; /* Observed temperature. */ 8 | real sigma_Tobs[Nobs]; /* Temperature uncertainty */ 9 | } 10 | 11 | parameters { 12 | real beta; /* Power-law slope L = T^beta */ 13 | real gamma; /* p(T) ~ T^-(gamma+1), appropriate for a power-law IMF. */ 14 | real sigma_scatter; 15 | realTtrue[Nobs]; /* True temperature */ 16 | real logLtrue[Nobs]; 17 | } 18 | 19 | transformed parameters { 20 | real Ltrue[Nobs]; 21 | real alpha; /* Corresponds to the exponent in the IMF. */ 22 | 23 | Ltrue = exp(logLtrue); 24 | alpha = 1.0 + 3.0*gamma/8.0; 25 | } 26 | 27 | model { 28 | /* Uniform prior on beta between limits. */ 29 | /* Broad, Cauchy prior on sigma_scatter, with scale 0.1 */ 30 | sigma_scatter ~ cauchy(0, 0.1); 31 | 32 | /* Pareto distribution for Ttrue 33 | 34 | In stan, pareto(xmin, alpha) gives a distribution of 35 | 36 | x ~ alpha xmin^alpha / x^(alpha+1) 37 | 38 | */ 39 | Ttrue ~ pareto(0.8, gamma); 40 | 41 | /* Scatter in the relation: */ 42 | for (i in 1:Nobs) { 43 | logLtrue[i] ~ normal(beta*log(Ttrue[i]), sigma_scatter); 44 | } 45 | 46 | Tobs ~ normal(Ttrue, sigma_Tobs); 47 | logLobs ~ normal(logLtrue, sigma_logL); 48 | } 49 | --------------------------------------------------------------------------------