请教我如何重复和列出-Mathematica
我该如何循环这个?
p = Table[RandomChoice[{Heads, Tails}, 2 i + 1], {i, 10}];
v = Count[#, Heads] & /@ p;
c = Count[#, Tails] & /@ p;
f = Abs[v - c];
g = Take[f, LengthWhile[f, # != 3 &] + 1]
谢谢!
编辑
在这个抛硬币游戏中,规则如下:
- 单次游戏包括重复 抛一枚公平的硬币,直到 数量之间的差异 抛出的头和尾部的数量 是三。
- 每次硬币您必须支付 1 美元 翻转,并且您可能无法退出 游戏的进行。
每次结束时您都会收到 8 美元 游戏的玩法。
- 你应该玩这个游戏吗?
- 您预计会赢得多少奖金或 玩了 500 次就输了?
您可以使用电子表格模拟和/或概率推理来回答这些问题。
课程使用 Excel,我正在尝试学习 Mathematica。
How do I loop this?
p = Table[RandomChoice[{Heads, Tails}, 2 i + 1], {i, 10}];
v = Count[#, Heads] & /@ p;
c = Count[#, Tails] & /@ p;
f = Abs[v - c];
g = Take[f, LengthWhile[f, # != 3 &] + 1]
Thanks!
EDIT
In this coin flipping game the rules are as follows :
- A single play consists of repeatedly
flipping a fair coin until the
difference between the number of
heads tossed and the number of tails
is three. - You must pay $1 each time the coin is
flipped, and you may not quit during
the play of the game. You receive $8 at the end of each
play of the game.- Should you play this game?
- How much might you expect to win or
lose after 500 plays?
You may use a spreadsheet simulation and/or reasoning about probabilities to answer these questions.
The class is using Excel, I'm trying to learn Mathematica.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
理论方面多一点
你的游戏是随机游走< /strong> 位于 R1 上。
因此,达到 3 距离的翻转次数的期望值是 32=9,这也是您的成本的期望值。
由于您每场比赛的收入为 8 美元,您将以每场比赛的平均损失率为 1 美元。
请注意,这些数字与 @Mr. 一致。 15000 局游戏的向导结果为
135108 - 120000 = 15108
。A little bit more on the theoretical side
Your game is a random walk on R1.
As such, the expectancy value for the number of flips to get a distance of 3 is 32=9, and that is also the expectancy value for your cost.
As your earning per game is $8, you'll lose at a mean rate of $1 per game.
Note that these figures are consistent with @Mr. Wizard's result of
135108 - 120000 = 15108
for 15000 games.如果我了解抛硬币游戏的规则,并且如果必须使用蒙特卡洛方法,请考虑以下内容:
这个想法是抛一枚硬币,直到一个人赢了三分,然后输出得到的轮数那里。执行此操作 15,000 次,并创建结果列表 (
count
)。您玩 15,000 场游戏所花费的钱只是玩的回合数,或者:
虽然您的奖金为 8 美元 * 15,000 = 120,000 美元,所以这不是一个好玩的游戏。
如果需要统计每个圈数出现的次数,则:
If I understand the rules of the coin flipping game, and if you must use a Monte Carlo method, consider this:
The idea is to flip a coin until one person is winning by three, and then output the number of turns it took to get there. Do this 15,000 times, and create a list of the results (
count
).The money you spent to play 15,000 games is simply the number of turns that were played, or:
While your winnings are $8 * 15,000 = $120,000, so this is not a good game to play.
If you need to count the number of times each number of turns comes up, then:
不确定这是否是完成您想要的事情的最佳方式,但这应该可以帮助您开始。首先,请注意,我将名称 Heads 和 Tails 更改为小写(Heads 是内置符号...)——小写变量名称是避免此类问题的最佳方法。
只需输入您想要运行循环的次数... fun[5] 给出:
注意:因为您可能想对输出执行某些操作,所以使用 Table[] 可能比 Do[] 更好。这将返回一个列表列表。
没什么花哨的!
Not sure if this is the best way to accomplish what you want, but this should get you started. First, note that I changed the names Heads and Tails to lowercase (Heads is a built-in symbol...)---lowercase variable names are the best way to avoid this type of problem.
Simply enter the number of times you want to run the loop... fun[5] gives:
Note: because you'll probably want to do something with the output, using Table[] is probably better than Do[]. This will return a list of lists.
Nothing fancy!
更像 Mathematica 一点。没有定义变量。
感谢 @Mr.Wizard 此答案。
A little more Mathematica-ish. No vars defined.
Credit to @Mr.Wizard for this answer.
我不喜欢抱怨 RTFM 等,但循环非常基本。如果我在文档中心的搜索框中输入“loop”,前几个点击之一包含指向“guide/LoopingConstructs”页面的链接,并且包含指向教程“tutorial/LoopsAndControlStructures”的链接。你读过这些吗?
I don't like bitching about RTFM etc. but looping is pretty basic. If I type "loop" in the search box in the documentation center one of the first few hits contains a link to the page "guide/LoopingConstructs" and this contains a link to the tutorial "tutorial/LoopsAndControlStructures". Have you read these?