Difference between revisions of "Shuffle"

 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
[[Category:Distribution Functions]]
 
[[Category:Distribution Functions]]
[[Category:Doc Status U]] <!-- For Lumina use, do not change -->
+
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
 
   
 
   
[[What's new in Analytica 4.0?]] >
 
  
= Function Shuffle(a [, i]) =
+
== Function Shuffle(a '', i'') ==
 
   
 
   
Shuffle returns a random reordering (permutation) of the values in array a over index i. If you omit i, it shuffles over Run. [Since build 4.0.0.48.] You can use it generate an independent random sample from an existing probability distribution, a.  If a contains dimensions other than i, it shuffles each slice over those other dimensions independently over i.
+
[[Shuffle]] returns a random reordering (permutation) of the values in array «a» over index «i». If you omit «i», by default it shuffles over [[Run]] -- i.e. it shuffles a random sample. You can use it generate an independent random sample from an existing probability distribution, «a».
  
== Declaration ==
+
== Independent Shuffles ==
  
  Shuffle(a: Array[i]; i: IndexType = Run)
+
If «a» has two dimensions, say <code>J</code> and [[Run]], <code>Shuffle(a, J)</code> shuffles the sample over <code>J</code> independently for each value of [[Run]]. <code>Shuffle(a, Run)</code> -- or <code>Shuffle(a)</code>, which is equivalent, because it assumes [[Run]] by default -- will shuffle each sample independently for each value of <code>J</code>.
  
If you want to shuffle the slices of a multidimensional array over index i, without shuffling the values within each slice, use this method:
+
Like other distribution functions, you can add an extra dimension using the «Over» parameter. For example, if <code>B</code> is a sample with no indexes other than [[Run]],
 +
:<code>Shuffle(B, Over: J)</code>
  
A[@I = Shuffle(@I,I)]
+
returns an array of samples indexed by <code>J</code>, where the sample for each value of <code>J</code> is shuffled independently.
  
This shuffles A over index I, but does not shuffle each slice of A for each value of I.
+
If you want to shuffle the slices of a multidimensional array over index «i», without shuffling the values within each slice, use this method:
 +
 
 +
:<code>A[@I = Shuffle(@I, I)]</code>
 +
 
 +
This shuffles <code>A</code> over index <code>I</code>, but does not shuffle each slice of <code>A</code> for each value of <code>I</code>.
 +
 
 +
== Shuffling along the implicit index ==
 +
 
 +
You cannot shuffle along the implicit index, since the «i» parameter defaults to be the [[Run]] index when omitted. So if you want to shuffle along an implicit index, things get tricky.
 +
 
 +
If you need to shuffle a list, then make sure your list is held by a local variable so that it has a name.  Once it has a name, you have a way to refer to it.  So for example, this does not have the intended result
 +
:<code>Shuffle(1..100)</code>
 +
 
 +
but this does work because we have a way to name the implicit dimension
 +
:<code>Var L:= 1..100 Do Shuffle(L, L)</code>
 +
 
 +
If you have a multidimensional array, <code>A</code>, where one index is implicit and you want to shuffle along the implicit dimension, you can't obtain a name for the implicit index from <code>A</code>, so you have to introduce a name somehow. This requires creating a new implicit or explicit dimension that has a name, and reindexing the array onto this.  This is done as follows.
 +
 
 +
:<code>Var I := 1..Size(A, ListLen: true) Do Shuffle(Slice(A, I), I)</code>
 +
 
 +
==See Also==
 +
* [[Random]]
 +
* [[Slice]]
 +
* [[Distribution Functions]]

Latest revision as of 00:39, 10 February 2016


Function Shuffle(a , i)

Shuffle returns a random reordering (permutation) of the values in array «a» over index «i». If you omit «i», by default it shuffles over Run -- i.e. it shuffles a random sample. You can use it generate an independent random sample from an existing probability distribution, «a».

Independent Shuffles

If «a» has two dimensions, say J and Run, Shuffle(a, J) shuffles the sample over J independently for each value of Run. Shuffle(a, Run) -- or Shuffle(a), which is equivalent, because it assumes Run by default -- will shuffle each sample independently for each value of J.

Like other distribution functions, you can add an extra dimension using the «Over» parameter. For example, if B is a sample with no indexes other than Run,

Shuffle(B, Over: J)

returns an array of samples indexed by J, where the sample for each value of J is shuffled independently.

If you want to shuffle the slices of a multidimensional array over index «i», without shuffling the values within each slice, use this method:

A[@I = Shuffle(@I, I)]

This shuffles A over index I, but does not shuffle each slice of A for each value of I.

Shuffling along the implicit index

You cannot shuffle along the implicit index, since the «i» parameter defaults to be the Run index when omitted. So if you want to shuffle along an implicit index, things get tricky.

If you need to shuffle a list, then make sure your list is held by a local variable so that it has a name. Once it has a name, you have a way to refer to it. So for example, this does not have the intended result

Shuffle(1..100)

but this does work because we have a way to name the implicit dimension

Var L:= 1..100 Do Shuffle(L, L)

If you have a multidimensional array, A, where one index is implicit and you want to shuffle along the implicit dimension, you can't obtain a name for the implicit index from A, so you have to introduce a name somehow. This requires creating a new implicit or explicit dimension that has a name, and reindexing the array onto this. This is done as follows.

Var I := 1..Size(A, ListLen: true) Do Shuffle(Slice(A, I), I)

See Also

Comments


You are not allowed to post comments.