{"id":6461,"date":"2025-05-13T15:01:16","date_gmt":"2025-05-13T06:01:16","guid":{"rendered":"https:\/\/blog.since2020.jp\/?p=6461"},"modified":"2025-05-13T15:02:39","modified_gmt":"2025-05-13T06:02:39","slug":"lmmm-on-colab","status":"publish","type":"post","link":"https:\/\/since2020.jp\/media\/lmmm-on-colab\/","title":{"rendered":"Lightweight MMM \u3092 Google Colab \u3067\u5b9f\u884c\u3059\u308b\u65b9\u6cd5\u30102025\u5e745\u6708\u7248\u3011"},"content":{"rendered":"\n<p>Google Colab\u3067Lightweight MMM\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306e\u624b\u9806\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002\u516c\u5f0f\u30c7\u30e2\u306e\u4fee\u6b63\u65b9\u6cd5\u3084\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u3092\u8a73\u3057\u304f\u7d39\u4ecb\u30022025\u5e745\u6708\u6642\u70b9\u3067\u306e\u74b0\u5883\u306b\u5bfe\u5fdc\u3057\u305f\u5b9f\u884c\u65b9\u6cd5\u3092\u8aac\u660e\u3057\u3001\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u306a\u3044\u3088\u3046\u306b\u4e01\u5be7\u306b\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n\n\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p>\u30de\u30fc\u30b1\u30c6\u30a3\u30f3\u30b0\u65bd\u7b56\u306e\u52b9\u679c\u3092\u7d71\u8a08\u7684\u306b\u63a8\u5b9a\u3059\u308b\u624b\u6cd5\u3067\u3042\u308b<strong>Marketing Mix Modeling\uff08MMM\uff09<\/strong>\u306f\u3001\u6700\u8fd1\u306e\u30b5\u30fc\u30c9\u30d1\u30fc\u30c6\u30a3\u30af\u30c3\u30ad\u30fc\u898f\u5236\u306e\u6d41\u308c\u3092\u53d7\u3051\u3066\u3001\u307e\u3059\u307e\u3059\u6ce8\u76ee\u3092\u96c6\u3081\u3066\u3044\u307e\u3059\u3002<\/p>\r\n<p>MMM\u3092\u624b\u8efd\u306b\u8a66\u3057\u3066\u307f\u305f\u3044\u3068\u601d\u3063\u305f\u3068\u304d\u306b\u3001\u300c<strong>Google Colab\u3067Lightweight MMM\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3059\u308b<\/strong>\u300d\u3068\u3044\u3046\u306e\u304c\u9078\u629e\u80a2\u306e\u4e00\u3064\u306b\u306a\u308b\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\r\n<p>\u5b9f\u969b\u3001<span style=\"text-decoration: underline;color: #0000ff\"><a href=\"https:\/\/github.com\/google\/lightweight_mmm\" style=\"color: #0000ff\"><strong>Lightweight MMM\u306e\u516c\u5f0f\u30ea\u30dd\u30b8\u30c8\u30ea<\/strong><\/a><\/span>\u306b\u306fGoogle Colab\u7528\u306e\u30c7\u30e2\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3059\u3057\u3001\u65e5\u672c\u8a9e\u306b\u3088\u308b\u8a18\u4e8b\u3082\u6bd4\u8f03\u7684\u5145\u5b9f\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\r\n<p>[blogcard url=&#8221;https:\/\/github.com\/google\/lightweight_mmm\/blob\/main\/examples\/simple_end_to_end_demo.ipynb&#8221;]<\/p>\r\n<p>\u3057\u304b\u3057\u3001\u3053\u306e\u30c7\u30e2\u304c\u4f5c\u6210\u3055\u308c\u3066\u304b\u3089\u5c11\u3057\u6642\u9593\u304c\u7d4c\u3063\u3066\u3044\u308b\u305f\u3081\u3001<strong>\u6700\u65b0\u306eGoogle Colab\u74b0\u5883\u3067\u305d\u306e\u307e\u307e\u5b9f\u884c\u3059\u308b\u3068\u3001\u3044\u304f\u3064\u304b\u306e\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b<\/strong>\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\r\n<p>\u305d\u3053\u3067\u672c\u30d6\u30ed\u30b0\u8a18\u4e8b\u3067\u306f\u3001Google Colab\u3067Lightweight MMM\u3092\u8a66\u3057\u3066\u307f\u305f\u3044\u3068\u3044\u3046\u65b9\u306b\u5411\u3051\u3066\u30012025\u5e745\u6708\u6642\u70b9\u3067\u306e\u5b9f\u884c\u624b\u9806\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002\u306a\u304a\u3001\u672c\u8a18\u4e8b\u3067\u306fMMM\u306e\u7406\u8ad6\u3084\u30e2\u30c7\u30ea\u30f3\u30b0\u306e\u6280\u8853\u306b\u3064\u3044\u3066\u306f\u6271\u3044\u307e\u305b\u3093\u3002<\/p>\r\n<p><!-- notionvc: baedae99-977d-4d70-8557-710af8cb5a94 --><\/p>\r\n<p><!-- notionvc: eb069ce5-f587-4f95-9545-a6ec2fc279a0 --><\/p>\n\n<h2>\u624b\u98061 : \u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p>\u307e\u305a\u3001Lightweight MMM\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\r\n<p>\u73fe\u6642\u70b9\u3067\u6700\u65b0\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u304c<code>0.1.9<\/code> \u3067\u3059\u3002<code>lightweight_mmm==0.1.9<\/code>\u3068\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u6307\u5b9a\u305b\u305a\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u304c\u3001\u5ff5\u306e\u305f\u3081\u660e\u8a18\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># LightweightMMM\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\r\n!pip install lightweight_mmm==0.1.9<\/code><\/pre>\r\n<\/div>\r\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3001\u300c\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u518d\u8d77\u52d5\u3059\u308b\u300d\u3068\u3044\u3046\u8b66\u544a\u304c\u51fa\u308b\u3068\u601d\u3046\u306e\u3067\u3001[\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u518d\u8d77\u52d5\u3059\u308b]\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_install_warning.png\" alt=\"\" width=\"514\" height=\"247\" class=\"aligncenter size-full wp-image-6584\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_install_warning.png 514w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_install_warning-300x144.png 300w\" sizes=\"(max-width: 514px) 100vw, 514px\" \/><\/p>\r\n<p>&nbsp;<\/p>\r\n<p>\u307e\u305f\u3001\u6b21\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u3082\u51fa\u529b\u3055\u308c\u308b\u306f\u305a\u3067\u3059\u3002<\/p>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_matplotlib_error.png\" alt=\"\" width=\"1246\" height=\"133\" class=\"aligncenter size-full wp-image-6586\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_matplotlib_error.png 1246w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_matplotlib_error-300x32.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_matplotlib_error-1024x109.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_matplotlib_error-768x82.png 768w\" sizes=\"(max-width: 1246px) 100vw, 1246px\" \/><\/p>\r\n<p>&nbsp;<\/p>\r\n<p>\u3069\u3046\u3084\u3089<code>3.8.0<\/code>\u4ee5\u964d\u306e<code>matplotlib<\/code>\u304c\u5fc5\u8981\u307f\u305f\u3044\u306a\u306e\u3067\u3001\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u5909\u66f4\u3057\u307e\u3059\u3002\u3053\u3053\u3067\u3082\u300c\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u518d\u8d77\u52d5\u3059\u308b\u300d\u3068\u3044\u3046\u8b66\u544a\u304c\u51fa\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u518d\u8d77\u52d5\u3092\u30af\u30ea\u30c3\u30af\u3059\u308c\u3070\u3088\u3044\u3067\u3059\u3002<\/p>\r\n<p><!-- notionvc: aca48c3f-394d-463d-9b23-1dd6e741738c --><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># matplotlib\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u5909\u66f4\r\n!pip install -U matplotlib==3.8.0<\/code><\/pre>\r\n<\/div>\r\n<p><!-- notionvc: c2e4ebfa-99e3-4e17-bbb6-637849442faa --><\/p>\n\n<h2>\u624b\u98062 : \u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30dd\u30fc\u30c8<\/h2>\n<p>Lightweight MMM\u306fJAX\u3068NumPyro\u3092\u57fa\u76e4\u306b\u3057\u3066\u3044\u307e\u3059\u3002\u6700\u521d\u306b<code>jax.numpy<\/code>\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3001\u305d\u306e\u5f8c<code>numpyro<\/code>\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import jax.numpy as jnp\r\nimport numpyro<\/code><\/pre>\r\n<\/div>\r\n<p>\u6b21\u306b\u3001<code>lightweight_mmm<\/code>\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># LMMM\u3092import\u3059\u308b\r\nfrom lightweight_mmm import lightweight_mmm\r\nfrom lightweight_mmm import optimize_media\r\nfrom lightweight_mmm import plot\r\nfrom lightweight_mmm import preprocessing\r\nfrom lightweight_mmm import utils<\/code><\/pre>\r\n<\/div>\r\n<p>\u305d\u306e\u4ed6\u3001\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u3042\u308c\u3070\u8aad\u307f\u8fbc\u3093\u3067\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\r\n<p>\u3053\u3053\u3067\u306f\u3001\u624b\u5143\u306ecsv\u30d5\u30a1\u30a4\u30eb\u3092Lightweight MMM\u306e\u5165\u529b\u30c7\u30fc\u30bf\u306b\u3059\u308b\u305f\u3081\u306b\u3001<code>pandas<\/code>\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import pandas as pd<\/code><\/pre>\r\n<\/div>\r\n<p><!-- notionvc: f5d01f0c-94a1-41f9-a3f5-c949eac9246f --><\/p>\r\n<p><!-- notionvc: 1648c378-5ada-421d-931a-49467f2cd0fa --><\/p>\n\n<h2>\u624b\u98063 : \u30c7\u30fc\u30bf\u306e\u6e96\u5099<\/h2>\n<p>\u4eca\u56de\u306f\u9069\u5f53\u306b\u4e71\u6570\u751f\u6210\u3057\u305f\u30c0\u30df\u30fc\u30c7\u30fc\u30bf\u3092csv\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u7528\u610f\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\r\n<p>Google\u30c9\u30e9\u30a4\u30d6\u306bcsv\u30d5\u30a1\u30a4\u30eb\u3092\u683c\u7d0d\u3057\u3066\u3042\u308b\u3082\u306e\u3068\u3057\u3066\u3001\u30c9\u30e9\u30a4\u30d6\u3092\u30de\u30a6\u30f3\u30c8\u3057\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from google.colab import drive\r\ndrive.mount('\/content\/drive')<\/code><\/pre>\r\n<\/div>\r\n<p>csv\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u307f\u3001pandas.Dataframe\u3068\u3057\u3066\u683c\u7d0d\u3057\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u30c7\u30fc\u30bf\u306e\u8aad\u307f\u8fbc\u307f\r\ndf = pd.read_csv('\/content\/drive\/MyDrive\/[your_path]\/[file_name].csv')<\/code><\/pre>\r\n<\/div>\r\n<p>\u6b21\u306b\u3001\u8aac\u660e\u5909\u6570\u3092 \u30e1\u30c7\u30a3\u30a2\u30fb\u5e83\u544a\u95a2\u9023\u306e\u5909\u6570 \/ \u305d\u306e\u4ed6\u306e\u5916\u751f\u5909\u6570 \/ (\u8cbb\u7528\u95a2\u9023\u306e\u5909\u6570; optional) \u306b\u5206\u3051\u3001\u30ea\u30b9\u30c8\u306b\u683c\u7d0d\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u6642\u7cfb\u5217\u3092\u8868\u3059\u30ab\u30e9\u30e0\u3068\u76ee\u7684\u5909\u6570\u3082\u5b9a\u7fa9\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>list_media = [\r\n    'GoogleAdSpend',\r\n    'YouTubeSpend'\r\n]\r\n\r\nlist_extra = [\r\n    'OfficialEvent'\r\n]\r\n\r\ndate_feature = 'Week'\r\ntarget = 'Sales'\r\nSEED = 42\r\ndata_size = df.shape[0]<\/code><\/pre>\r\n<\/div>\r\n<p>\u6b21\u306b\u3001Lightweight MMM\u3067\u6271\u3048\u308b\u3088\u3046\u306b\u3001pandas.Dataframe\u3092jax.numpy\u306b\u5909\u63db\u3057\u307e\u3059\u3002<\/p>\r\n<p><code>lightweight_mmm<\/code>\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u306f<code>utils.dataframe_to_jax()<\/code>\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u3053\u308c\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\r\n<p>[blogcard url=&#8221;https:\/\/github.com\/google\/lightweight_mmm\/blob\/main\/lightweight_mmm\/utils.py&#8221;]<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code><\/code>media_data, extra_features, target, costs = utils.dataframe_to_jax( dataframe = df, media_features = list_media, extra_features = list_extra, date_feature = date_feature, target = target )<\/pre>\r\n<\/div>\r\n<p>\u5b66\u7fd2\u30c7\u30fc\u30bf\u3068\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306b\u5206\u5272\u3057\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u30c7\u30fc\u30bf\u3092\u5206\u5272\u3059\u308b\r\nsplit_point = data_size - 13\r\n\r\n# \u30e1\u30c7\u30a3\u30a2\u95a2\u9023\u306e\u5909\u6570\r\nmedia_data_train = media_data[:split_point, ...]\r\nmedia_data_test = media_data[split_point:, ...]\r\n\r\n# \u5916\u751f\u5909\u6570\r\nextra_features_train = extra_features[:split_point, ...]\r\nextra_features_test = extra_features[split_point:, ...]\r\n\r\n# \u76ee\u7684\u5909\u6570\r\ntarget_train = target[:split_point]<\/code><\/pre>\r\n<\/div>\r\n<p>\u30c7\u30fc\u30bf\u3092\u30b9\u30b1\u30fc\u30ea\u30f3\u30b0\u3057\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>media_scaler = preprocessing.CustomScaler(divide_operation=jnp.mean)\r\nextra_features_scaler = preprocessing.CustomScaler(divide_operation=jnp.mean)\r\ntarget_scaler = preprocessing.CustomScaler(divide_operation=jnp.mean)\r\ncost_scaler = preprocessing.CustomScaler(divide_operation=jnp.mean, multiply_by=0.15)\r\n\r\nmedia_data_train = media_scaler.fit_transform(media_data_train)\r\nextra_features_train = extra_features_scaler.fit_transform(extra_features_train)\r\ntarget_train = target_scaler.fit_transform(target_train)\r\ncosts = cost_scaler.fit_transform(costs)<\/code><\/pre>\r\n<\/div>\n\n<h2>\u624b\u98064 : JAX\u30a8\u30e9\u30fc\u306e\u5bfe\u5fdc<\/h2>\n<p>\u3053\u3053\u307e\u3067\u3067\u30c7\u30fc\u30bf\u306e\u6e96\u5099\u306f\u5b8c\u4e86\u3057\u307e\u3057\u305f\u3002<\/p>\r\n<p>\u6b21\u306b\u30e2\u30c7\u30eb\u3092\u5b66\u7fd2\u3059\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u3053\u3067Lightweight MMM\u3068JAX\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u5e72\u6e09\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\r\n<p>\u5b9f\u969b\u3001\u30e2\u30c7\u30eb\u3092\u30d5\u30a3\u30c3\u30c6\u30a3\u30f3\u30b0\u3059\u308b\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u3066\u307f\u308b\u3068\u3001\u30a8\u30e9\u30fc\u304c\u51fa\u308b\u306f\u305a\u3067\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>mmm = lightweight_mmm.LightweightMMM(model_name=\"carryover\")\r\n\r\nnumber_warmup=1000\r\nnumber_samples=1000\r\n\r\n# MCMC\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u3092\u884c\u3046\r\nmmm.fit(\r\n    media=media_data_train,\r\n    media_prior=costs,\r\n    target=target_train,\r\n    extra_features=extra_features_train,\r\n    number_warmup=number_warmup,\r\n    number_samples=number_samples,\r\n    seed=SEED\r\n)<\/code><\/pre>\r\n<\/div>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_jax_error.png\" alt=\"\" width=\"811\" height=\"361\" class=\"aligncenter size-full wp-image-6585\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_jax_error.png 811w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_jax_error-300x134.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_jax_error-768x342.png 768w\" sizes=\"(max-width: 811px) 100vw, 811px\" \/><\/p>\r\n<p>\u3053\u306e\u30a8\u30e9\u30fc\u306f\u3001<strong>Lightweight MMM\u304c\u60f3\u5b9a\u3059\u308bJAX\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3068Google Colab\u306b\u5165\u3063\u3066\u3044\u308bJAX\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u7570\u306a\u308b\u3053\u3068\u304c\u539f\u56e0<\/strong>\u3067\u8d77\u304d\u3066\u3044\u307e\u3059\u3002<\/p>\r\n<p><code>lightweight_mmm 0.1.9<\/code>\u306f\u3001JAX\u306e\u53e4\u3044\u30d0\u30fc\u30b8\u30e7\u30f3 (<strong>0.3<\/strong>\u7cfb) \u3092\u60f3\u5b9a\u3057\u3066\u5b9f\u88c5\u3055\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u6700\u65b0\u306eGoogle Colab\u74b0\u5883\u306b\u306f JAX <strong>0.4<\/strong>\u7cfb \u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u307e\u3059\u3002<code>lightweight_mmm 0.1.9<\/code> \u3067\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>jax.numpy.where(condition=(data == 0), x=1, y=data)<\/code><\/pre>\r\n<\/div>\r\n<p>\u3068\u3044\u3046\u30ad\u30fc\u30ef\u30fc\u30c9\u5f15\u6570\u4ed8\u304d\u547c\u3073\u51fa\u3057\u306fJAX 0.4\u7cfb \u304b\u3089\u306f\u8a31\u3055\u308c\u305a\u3001<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>jax.numpy.where(cond, x, y)<\/code><\/pre>\r\n<\/div>\r\n<p>\u3068\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\r\n<p>\u6b21\u306e\u3088\u3046\u306bGoogle Colab\u74b0\u5883\u3067JAX\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u30920.3\u7cfb\u306b\u30c0\u30a6\u30f3\u30b0\u30ec\u30fc\u30c9\u3059\u308c\u3070\u3088\u3044\u306f\u305a\u306a\u306e\u3067\u3059\u304c\u3001\u7b46\u8005\u306f\u3046\u307e\u304f\u5b9f\u884c\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>!pip install jax==0.3.25 jaxlib==0.3.25<\/code><\/pre>\r\n<\/div>\r\n<p>\u305d\u3053\u3067\u3001\u5fdc\u6025\u51e6\u7f6e\u3067\u306f\u3042\u308a\u307e\u3059\u304c\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4e00\u6642\u7684\u306b\u30d1\u30c3\u30c1\u3057\u3066\u5bfe\u5fdc\u3057\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import jax.numpy as jnp\r\nimport lightweight_mmm.media_transforms as mt\r\n\r\ndef _apply_exponent_safe(data, exponent):\r\n    exponent_safe = jnp.where((data == 0), 1, data) ** exponent\r\n    return jnp.where((data == 0), 0, exponent_safe)\r\n\r\nmt.apply_exponent_safe = _apply_exponent_safe # fit() \u3088\u308a\u524d\u306b1\u56de\u3060\u3051\u5b9f\u884c<\/code><\/pre>\r\n<\/div>\r\n<p><!-- notionvc: 2b16046d-c3f4-4c33-925c-f1e356bceb28 --><\/p>\n\n<h2>\u624b\u98065 : \u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u63a8\u5b9a<\/h2>\n<p>\u30e2\u30c7\u30eb\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3057\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u3092MCMC\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u306b\u3088\u3063\u3066\u63a8\u5b9a\u3057\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>mmm = lightweight_mmm.LightweightMMM(model_name=\"carryover\")\r\n\r\nnumber_warmup=1000\r\nnumber_samples=1000\r\n\r\n# MCMC\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u3092\u884c\u3046\r\nmmm.fit(\r\n    media=media_data_train,\r\n    media_prior=costs,\r\n    target=target_train,\r\n    extra_features=extra_features_train,\r\n    number_warmup=number_warmup,\r\n    number_samples=number_samples,\r\n    seed=SEED\r\n)<\/code><\/pre>\r\n<\/div>\r\n<p>\u4ee5\u4e0b\u306b\u3088\u308a\u3001\u5404\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u63a8\u5b9a\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u304c\u78ba\u8a8d\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>mmm.print_summary()<\/code><\/pre>\r\n<\/div>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_estimate_result.png\" alt=\"\" width=\"699\" height=\"353\" class=\"aligncenter size-full wp-image-6583\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_estimate_result.png 699w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2025\/05\/mmm_estimate_result-300x152.png 300w\" sizes=\"(max-width: 699px) 100vw, 699px\" \/><\/p>\n\n<h2>\u304a\u308f\u308a\u306b<\/h2>\n<p>\u672c\u30d6\u30ed\u30b0\u8a18\u4e8b\u3067\u306f\u3001Google Colab\u3067Lightweight MMM\u3092\u52d5\u304b\u3059\u305f\u3081\u306b\u3001\u516c\u5f0f\u306e\u30c7\u30e2\u3092\u4fee\u6b63\u3059\u308b\u624b\u9806\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<\/p>\r\n<p>\u672c\u683c\u7684\u306bMMM\u3092\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u904b\u7528\u3059\u308b\u5834\u5408\u306f\u3001\u5c02\u7528\u306e\u74b0\u5883\u3092\u69cb\u7bc9\u3059\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u30c7\u30e2\u3068\u3057\u3066\u8a66\u3057\u3066\u307f\u305f\u3044\u5834\u5408\u306b\u306fGoogle Colab\u3067Lightweight MMM\u3092\u4f7f\u3046\u3068\u3044\u3046\u306e\u306f\u826f\u3044\u9078\u629e\u80a2\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\r\n<p>\u672c\u8a18\u4e8b\u3067\u89e3\u8aac\u3057\u305f\u624b\u9806\u306b\u6cbf\u3063\u3066\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308c\u3070\u3001\u30a8\u30e9\u30fc\u306a\u3057\u3067MMM\u3092\u5b9f\u884c\u3067\u304d\u308b\u306f\u305a\u3067\u3059\u3002\u305c\u3072\u8a66\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\uff01<\/p>\n\n<h2>\u53c2\u8003<\/h2>\n<p>Lightweight MMM\u306e\u516c\u5f0f\u30ea\u30dd\u30b8\u30c8\u30ea<\/p>\r\n<p>[blogcard url=&#8221;https:\/\/github.com\/google\/lightweight_mmm&#8221;]<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>Google Colab\u3067\u52d5\u304b\u305b\u308b\u30c7\u30e2<a href=\"https:\/\/github.com\/google\/lightweight_mmm\/blob\/main\/examples\/simple_end_to_end_demo.ipynb\"><\/a><\/p>\r\n<p>[blogcard url=&#8221;https:\/\/github.com\/google\/lightweight_mmm\/blob\/main\/examples\/simple_end_to_end_demo.ipynb&#8221;]<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><span style=\"text-decoration: underline;color: #0000ff\"><a href=\"https:\/\/tjo.hatenablog.com\/\" style=\"color: #0000ff;text-decoration: underline\"><strong>TJO<\/strong><\/a><\/span>\u6c0f\u306b\u3088\u308b\u65e5\u672c\u8a9e\u7248\u306e\u30c7\u30e2<\/p>\r\n<p>[blogcard url=&#8221;https:\/\/colab.research.google.com\/drive\/1S6BRndbI45vwGFtEejMOegTv8adW_SPE?usp=sharing&#8221;]<\/p>\r\n<p><!-- notionvc: 13881544-74f7-4a6f-9bd0-b34fcf45bc0a --><\/p>","protected":false},"excerpt":{"rendered":"<p>Google Colab\u3067Lightweight MMM\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306e\u624b\u9806\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002\u516c\u5f0f\u30c7\u30e2\u306e\u4fee\u6b63\u65b9\u6cd5\u3084\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u3092\u8a73\u3057\u304f\u7d39\u4ecb\u30022025\u5e745\u6708\u6642\u70b9\u3067\u306e\u74b0\u5883\u306b\u5bfe\u5fdc\u3057\u305f\u5b9f\u884c\u65b9\u6cd5\u3092\u8aac\u660e\u3057\u3001\u30a8\u30e9\u30fc\u304c [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":6587,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","swell_btn_cv_data":"","footnotes":"","_wp_rev_ctl_limit":""},"categories":[1246],"tags":[331,484,39,33],"class_list":["post-6461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-infrastructure","tag-python","tag-484","tag-39","tag-33"],"_links":{"self":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/6461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/comments?post=6461"}],"version-history":[{"count":0,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/6461\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media\/6587"}],"wp:attachment":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media?parent=6461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/categories?post=6461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/tags?post=6461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}