So I want to plot two graphs (on the same plot). One which is the $\frac{\sum_{i=0}^n x_i}{n}$ and the other $\frac{\sum_{i=0}^n x_i}{n-1}$ where x is a set created by RandomInteger[{a,b},n]
and then have n slowly increase. I was wondering if this would be a sort of nice convergence plot over a large interval. The problem is as seen I want all of them to use the one {n,c,d} table for their iteration but Sum wants a copy, Plot wants a copy and RandomInteger wants the length of it. How can I use one of it for all of those?
$\begingroup$
$\endgroup$
2
-
$\begingroup$ They will converge to (a+b)/2 ... or am I missing something? $\endgroup$– Dr. belisariusCommented Apr 10, 2012 at 1:53
-
1$\begingroup$ p0[a_, b_, 1] := p0[a, b, 1] = RandomInteger[{a, b}]; p0[a_, b_, n_] := p0[a, b, n] = (p0[a, b, n - 1] (n - 1) + RandomInteger[{a, b}])/n; p1[a_, b_, n_] := p0[a, b, n] n/(n - 1); ListLinePlot[{Table[p0[0, 10, k], {k, 2, 50000}], Table[p1[0, 10, k], {k, 2, 50000}]}] $\endgroup$– Dr. belisariusCommented Apr 10, 2012 at 1:54
Add a comment
|
2 Answers
$\begingroup$
$\endgroup$
Adding an additional (optional) parameter for increment, define
datalistPlot[a_Integer, b_Integer, c_Integer, d_Integer, e_Integer: 1] :=
Module[{left = Mean@RandomInteger[{a, b}, c]},{
Transpose@
Table[{left=(n - 1) left/n + RandomInteger[{a, b}]/n, n left/(n - 1)}, {n, c, d, e}],
{c, d},
(a + b)/2
}]
// ListLinePlot[#[[1]],
GridLines -> {None, {{#[[3]], Directive[Red, Thick]}}},
DataRange -> #[[2]], PlotRange -> {Automatic, #[[3]] + {-1, 1}}] &
and use as
datalistPlot[list[0, 10, 10, 500, 10]
to get
$\begingroup$
$\endgroup$
5
Assuming you want each of the summations over two different randomly generated lists:
{Total@ RandomInteger[{a, b}, #]/#,
Total@ RandomInteger[{a, b}, #]/(# - 1)} & /@ Range[2, n] //
Transpose // ListLinePlot
-
$\begingroup$
Total[]
might be a useful thing here. $\endgroup$ Commented May 4, 2012 at 3:10 -
$\begingroup$ @J.M. Yes you are right... some pre version 5 habits showing up here :) $\endgroup$ Commented May 4, 2012 at 12:20
-
$\begingroup$ Don't worry; you aren't alone. I haven't fully shaken off old Mathematica habits of mine as well... $\endgroup$ Commented May 4, 2012 at 12:23
-
$\begingroup$ Is there any advantage of
Total
overPlus@@
? $\endgroup$– celtschkCommented May 4, 2012 at 12:41 -
$\begingroup$ Some cursory experiments seem to suggest Total is probably faster. $\endgroup$ Commented May 4, 2012 at 12:59