3
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ They will converge to (a+b)/2 ... or am I missing something? $\endgroup$ Commented 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$ Commented Apr 10, 2012 at 1:54

2 Answers 2

3
$\begingroup$

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

enter image description here

$\endgroup$
2
$\begingroup$

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
$\endgroup$
5
  • $\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 over Plus@@? $\endgroup$
    – celtschk
    Commented 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.