加入新推出的
Discord 社区,展开实时讨论,获得同行支持,并直接与 Meridian 团队互动!
运行模型
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
运行模型时,您需要添加模型规范,然后运行命令,对先验分布和后验分布进行抽样。
模型采用了马尔可夫链蒙特卡洛 (MCMC) 算法从后验分布中抽样。Meridian 使用 No-U-Turn 抽样方法,此方法可以自适应调整步长和核。
如要运行模型,请执行以下操作:
添加模型规范。
示例:
model_spec = spec.ModelSpec(
prior=prior_distribution.PriorDistribution(),
media_effects_dist='log_normal',
hill_before_adstock=False,
max_lag=8,
unique_sigma_for_each_geo=False,
media_prior_type='roi',
roi_calibration_period=None,
rf_prior_type='coefficient',
rf_roi_calibration_period=None,
organic_media_prior_type='contribution',
organic_rf_prior_type='contribution',
non_media_treatments_prior_type='contribution',
knots=None,
baseline_geo=None,
holdout_id=None,
control_population_scaling_id=None,
)
运行以下命令,从先验分布和后验分布中进行抽样。根据需要配置形参:
meridian = model.Meridian(input_data=data, model_spec=model_spec)
meridian.sample_prior(500)
meridian.sample_posterior(n_chains=7, n_adapt=500, n_burnin=500, n_keep=1000)
形参 |
说明 |
n_chains |
要并行抽样的链数量。为了减少内存消耗,您可以使用一个整数列表,按顺序进行 MCMC 抽样调用。如果使用列表,序列中的每个元素都对应一个 n_chains 实参,用以调用 windowed_adaptive_nuts 。 |
n_adapt |
每条链上在步长和核调整期间进行的 MCMC 抽样次数。这些抽样结果始终会被排除在最终样本之外。 |
n_burnin |
在确定步长和核后,每条链上要额外排除的 MCMC 抽样次数。这些额外的抽样可能是为了确保所有链在完成自适应调整后都能达到稳定分布状态,但在实际应用中,我们通常会发现这些链在自适应调整期间就能达到稳定分布状态,因此 n_burnin=0 就够了。 |
n_keep |
每条链上要保留的 MCMC 抽样次数,抽取的样本将用于模型分析和生成结果。 |
接下来,运行建模诊断以评估收敛性、查看分布和评估模型拟合度。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-04。
[null,null,["最后更新时间 (UTC):2025-08-04。"],[[["\u003cp\u003eThe model requires a defined model specification, which includes parameters like prior distribution, media effects, and other configurations.\u003c/p\u003e\n"],["\u003cp\u003eTo run the model, you first define the model specification and then use the \u003ccode\u003emeridian\u003c/code\u003e object to sample from both the prior and posterior distributions.\u003c/p\u003e\n"],["\u003cp\u003eMarkov Chain Monte Carlo (MCMC) methods are used for sampling from the posterior distribution, utilizing the No-U-Turn sampling method with step size and kernel adaptation.\u003c/p\u003e\n"],["\u003cp\u003eParameters like \u003ccode\u003en_chains\u003c/code\u003e, \u003ccode\u003en_adapt\u003c/code\u003e, \u003ccode\u003en_burnin\u003c/code\u003e, and \u003ccode\u003en_keep\u003c/code\u003e control the MCMC sampling process, determining the number of parallel chains, adaptation draws, burn-in draws, and draws to keep.\u003c/p\u003e\n"]]],["The process involves specifying a model using `ModelSpec`, defining parameters like prior distributions, media effects, and lag. Then, use the `Meridian` class to run the model by sampling from prior and posterior distributions. The posterior sampling employs the No-U-Turn algorithm with step size and kernel adaptation, and requires parameter configuration like `n_chains`, `n_adapt`, `n_burnin`, and `n_keep` to control the Markov Chain Monte Carlo draws. After sampling, you run model diagnostics to assess the quality of the results.\n"],null,["# Run the model\n\nWhen you run the model, you add your model specification, and then run the\ncommands to sample the prior distribution and the posterior distribution.\n\nMarkov Chain Monte Carlo (MCMC) algorithms are used to sample from the posterior\ndistribution. Meridian uses the\n[No-U-Turn](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/mcmc/windowed_adaptive_nuts)\nsampling method with step size and kernel adaptation.\n\nTo run the model:\n\n1. Add your model specification.\n\n **Example:** \n\n model_spec = spec.ModelSpec(\n prior=prior_distribution.PriorDistribution(),\n media_effects_dist='log_normal',\n hill_before_adstock=False,\n max_lag=8,\n unique_sigma_for_each_geo=False,\n media_prior_type='roi',\n roi_calibration_period=None,\n rf_prior_type='coefficient',\n rf_roi_calibration_period=None,\n organic_media_prior_type='contribution',\n organic_rf_prior_type='contribution',\n non_media_treatments_prior_type='contribution',\n knots=None,\n baseline_geo=None,\n holdout_id=None,\n control_population_scaling_id=None,\n )\n\n2. Run the following commands to sample from the prior and posterior\n distribution. Configure the parameters as needed:\n\n meridian = model.Meridian(input_data=data, model_spec=model_spec)\n meridian.sample_prior(500)\n meridian.sample_posterior(n_chains=7, n_adapt=500, n_burnin=500, n_keep=1000)\n\n | Parameter | Description |\n |------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n | `n_chains` | The number of chains to be sampled in parallel. To reduce memory consumption, you can use a list of integers to allow for sequential MCMC sampling calls. Given a list, each element in the sequence corresponds to the `n_chains` argument for a call to `windowed_adaptive_nuts`. |\n | `n_adapt` | The number of MCMC draws per chain, during which step size and kernel are adapted. These draws are always excluded. |\n | `n_burnin` | An additional number of MCMC draws, per chain, to be excluded after the step size and kernel are fixed. These additional draws may be needed to ensure that all chains reach the stationary distribution after adaptation is completed, but in practice we often find that the chains reach the stationary distribution during adaptation and that `n_burnin=0` is sufficient. |\n | `n_keep` | The number of MCMC draws, per chain, to keep for the model analysis and results. |\n\nNext, run modeling diagnostics to [assess convergence, check the distributions,\nand assess the model fit](/meridian/docs/user-guide/model-diagnostics)."]]