c语言 随机种子 monte carlo,GitHub - LJY404/bizforecast-exercise: 商业化蒙特卡洛模拟练习...
README
蒙特卡洛模擬可以幫助分析在商業上一些復雜的場景, 包括了衡量風險以及計算某項目在特定條件下可產生的收益. 我們將通過一個簡化的例子表現蒙特卡洛模擬的應用.
Often, Monte Carlo simulation can come in handy to calculate risk or evaluate investments in projects. This is a simple demonstration.
背景
小象數據科學是剛成立不久的事業部. 主要針對9到12歲的兒童進行數據科學基礎培訓, 以促進培育國家未來數據人才的步伐. 經過3個月的試點, 管理層希望對業務采集的數據進行分析, 衡量該業務的風險點與可持續性.
你作為商業分析組的一員, 將通過數據模擬優先回答以下問題:
以【月】為單位的利潤是怎么樣的一個分布? 以百分之95的置信度而言, 利潤在什么范圍區間?
解答: 服從正太分布
library(tidyverse)
## ─ Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ─
## ? ggplot2 3.1.0 ? purrr 0.3.2
## ? tibble 2.0.0 ? dplyr 0.7.8
## ? tidyr 0.8.2 ? stringr 1.3.1
## ? readr 1.3.1 ? forcats 0.3.0
## ─ Conflicts ──────────────────────────────────────── tidyverse_conflicts() ─
## ? dplyr::filter() masks stats::filter()
## ? dplyr::lag() masks stats::lag()
library(ggthemes)
library(ggplot2)
library(rio)
library(infer)
set.seed(1212)
N=1000
R=rnorm(N, mean=0.4, sd=0.05)
M=runif(5,350,400)
L=runif(5,3000,4000)
S=L*R #收入
INCOME=M*S
Cpl=runif(5,8,10)
H=20000
Expenses=Cpl*L+H #成本
Profit=INCOME-Expenses
如果我們設置一個月的利潤目標為10萬元, 達成目標的概率是多少? 虧損風險 (利潤小于0的概率) 是多少?
利潤的累積分布函數 (cumulative distribution)? (繪圖即可)
經過第一輪的探索, 你與業務分享你的結果. 業務發現你的模擬出現了嚴重的漏洞. 以他們的業務經驗, 單個栗子的成本與其轉化率是強關聯關系的. 你提取了以往投放的數據 (見附帶表格 csv), 發現果真如此. 于是, 你更新了你的模型. 請以新模型回答以上3點問題.
data
cor_matr = cor(data) #相關矩陣
cor_matr
## cost_per_lead conversion_rate
## cost_per_lead 1.0000000 0.9882867
## conversion_rate 0.9882867 1.0000000
data %>%
ggplot(aes(cost_per_lead, conversion_rate)) +
geom_line(size = 1, color = "grey30") #數據實際分布圖
test_mod
test_mod
##
## Call:
## lm(formula = conversion_rate ~ cost_per_lead, data = data)
##
## Coefficients:
## (Intercept) cost_per_lead
## -0.18104 0.02456
predict(test_mod,newdata=data.frame(cost_per_lead=9.8),interval="confidence")
## fit lwr upr
## 1 0.05964785 0.0578207 0.06147499
knitr::opts_chunk$set(echo = FALSE)
如果業務提出一個產品方案, 在增加固定成本百分之25至30的前提下, 每天的栗子數量可以提升百分之15至25. 請問你會如何評估該方案?
管理層認為單個栗子的成本過高, 具有很大的優化空間. 他們希望把虧損風險控制在百分之35之內. 請問根據該需求, 單個栗子成本的上線是多少?
業務信息
利潤 (Profit) = 收入 (Revenue) - 開銷 (Expenses)
注意: 這里所有指標均以【天】為單位
* 收入為個單毛利 (Profit margin, M) 乘與銷量 (Sales, S);
* 個單毛利為均勻 (uniform) 分布, 一單 350 元至 400 元;
* 銷量為栗子數量 (Number of leads, L) 乘與轉化率 (Conversion rate, R);
* 栗子數量為均勻分布, 一天 3000 個至 4000 個;
* 轉化率為正態 (normal) 分布, 均值為 4%, 偏差為 0.5%;
* 開銷為固定成本 (Fixed overheads, H) 加上栗子成本;
* 固定成本 (研發, 辦公) 平攤為每天 2 萬元;
* 栗子成本為栗子數量乘于單個栗子成本 (Cost per lead, Cpl);
* 單個栗子成本為均勻分布, 單個 8 元至 10 元;
Profit = Income - Expenses
Income = Profit Margin per Sale (M) * Sales (S)
M assumes an uniform dist. from $350 to $400
S = Number of Leads (L) * Conversion Rate (R)
L assumes an uniform dist. with from 3000 to 4000
R assumes a normal dist. with mean of 4% and sd of 0.5%
Expenses = Fixed Overhead (H) + Total Cost of the Leads (C)
H assumes a constant of $20000
C = Cost Per Lead (Cpl) * Number of Leads (L)
Cpl assumes an uniform dist. from $8 to $10
Profit = Leads * Conversion Rate * Profit Margin per Sale - (Cost per Lead * Leads + Fixed Overhead)
幫助
你可能會用到一下函數:
sample replicate which rnorm runif rbeta lm predict geom_point geom_line stat_ecdf
請設置隨機種子為 1212
總結
以上是生活随笔為你收集整理的c语言 随机种子 monte carlo,GitHub - LJY404/bizforecast-exercise: 商业化蒙特卡洛模拟练习...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 专色印刷的相关知识
- 下一篇: android网络请求框架汇总