В качестве принудительной функции, чтобы узнать больше о байесовском моделировании / научиться использовать сетчатый пакет, я пытался повторить упражнения «Байесовские методы для хакеров» в R (оригинал блокнот Jupyter ). Однако при попытке запустить MCM C я получаю ошибку TypeError, но я недостаточно знаком с PYMC3 или Reticulate, чтобы знать, где происходит ошибка. Ниже я включил репекс моего выпуска:
Представление
library(reticulate)
library(readr)
pm <- import("pymc3", convert = FALSE)
tt <- import("theano.tensor")
py <- import_builtins()
np <- import("numpy")
scipy <- import("scipy")
txtdata = read_csv("https://raw.githubusercontent.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/master/Chapter1_Introduction/data/txtdata.csv", col_names = F)
#> Parsed with column specification:
#> cols(
#> X1 = col_double()
#> )
count_data = np$array(txtdata$X1)
n_count_data = length(count_data)
model = pm$Model()
with(model, {
alpha = 1.0 / np$mean(count_data)
#Define the Two lambda parameters with hyperparameter alpha
lambda_1 = pm$Exponential("lambda_1", alpha)
lambda_2 = pm$Exponential("lambda_2", alpha)
#Set Priors for Tau as a Uniform Variable
tau = pm$DiscreteUniform("tau", lower=0, upper=n_count_data - 1)
#Create an Array of Different Switch Point Varaibles
idx = np$arange(n_count_data) # Index
#Create new random variable lambda with our checkpoint
lambda_ = pm$math$switch(tt$gt(tau, idx), lambda_1, lambda_2)
#Observation combines the lambda variable with the actual data
observation = pm$Poisson("obs", lambda_, observed=count_data)
step = pm$Metropolis()
trace = pm$sample(10000, tune=5000,step=step)
})
#> Error in py_call_impl(callable, dots$args, dots$keywords): TypeError: 'float' object cannot be interpreted as an integer
#>
#> Detailed traceback:
#> File "C:\Users\Josh\miniconda3\lib\site-packages\pymc3\sampling.py", line 439, in sample
#> trace = _mp_sample(**sample_args)
#> File "C:\Users\Josh\miniconda3\lib\site-packages\pymc3\sampling.py", line 979, in _mp_sample
#> strace.setup(draws + tune, idx + chain, step.stats_dtypes)
#> File "C:\Users\Josh\miniconda3\lib\site-packages\pymc3\backends\ndarray.py", line 199, in setup
#> dtype=self.var_dtypes[varname])
Создано в 2020-04-27 пакетом представ. (v0.3.0)
sessionInfo()
#> R version 4.0.0 (2020-04-24)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 18363)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=English_United States.1252
#> [2] LC_CTYPE=English_United States.1252
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C
#> [5] LC_TIME=English_United States.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] readr_1.3.1 reticulate_1.15
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.4.6 knitr_1.28 magrittr_1.5 hms_0.5.3
#> [5] lattice_0.20-41 R6_2.4.1 rlang_0.4.5 fansi_0.4.1
#> [9] stringr_1.4.0 highr_0.8 tools_4.0.0 grid_4.0.0
#> [13] xfun_0.13 cli_2.0.2 htmltools_0.4.0 ellipsis_0.3.0
#> [17] assertthat_0.2.1 yaml_2.2.1 digest_0.6.25 tibble_3.0.0
#> [21] lifecycle_0.2.0 crayon_1.3.4 Matrix_1.2-18 vctrs_0.2.4
#> [25] curl_4.3 glue_1.4.0 evaluate_0.14 rmarkdown_2.1
#> [29] stringi_1.4.6 pillar_1.4.3 compiler_4.0.0 jsonlite_1.6.1
#> [33] pkgconfig_2.0.3