java.util.Collection
与えられた分布(org.apache.commons.math.distribution
)に従ってそれらの要素を返すような無限のjava.util.Iterator
を作成するもっとも簡単な方法は何ですか?与えられた分布を持つ無限イテレータの作成
2
A
答えて
4
List<Object> l = new ArrayList<Object>(coll);
Iterator<Object> i = new Iterator<Object>() {
public boolean hasNext() { return true; }
public Object next() {
return coll.get(distribution.nextInt(0, l.size());
}
}
あなたの問題は、nextInt
メソッドを実装するためにApacheのライブラリにDistribution
クラスを変換する方法を、次にあります。 Distribution
インターフェイスから実際にこれをどうやって行うことができるのかは明らかではありません。
distribution
(上記)としてdsitributionをemprirical
を使用してEmpiricalDistribution
(random
パッケージで)データセットを生成することであるガウス分布のため
+0
これは、1行目に匹敵しないかっこですか?匿名のクラスにすることを意味しましたか? –
+0
現場でうれしい –
+0
しかし、数学的な側面はこの問題の最も難しい部分です...これまでのところ質問 –
1
ソリューション
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.SortedMap;
import java.util.Map.Entry;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.ImmutableSortedMap.Builder;
/**
* Endless sequence with gaussian distribution.
*
* @param <T> the type of the elements
* @author Michael Locher
*/
public class GaussianSequence<T> implements Iterable<T>, Iterator<T> {
private static final int HISTOGRAMM_SAMPLES = 50000;
private static final int HISTOGRAMM_ELEMENTS = 100;
private static final int HISTOGRAMM_LENGTH = 80;
private static final double DEFAULT_CUTOFF = 4.0;
private final List<T> elements;
private final int maxIndex;
private final Random rnd;
private final double scaling;
private final double halfCount;
/**
* Creates this.
* @param rnd the source of randomness to use
* @param elements the elements to deliver
*/
public GaussianSequence(final Random rnd, final Collection<T> elements) {
this(rnd, DEFAULT_CUTOFF, elements);
}
private GaussianSequence(final Random rnd, final double tailCutOff, final Collection<T> elements) {
super();
this.rnd = rnd;
this.elements = new ArrayList<T>(elements);
if (this.elements.isEmpty()) {
throw new IllegalArgumentException("no elements provided");
}
this.maxIndex = this.elements.size() - 1;
this.halfCount = this.elements.size()/2.0;
this.scaling = this.halfCount/tailCutOff;
}
/**
* {@inheritDoc}
*/
@Override
public Iterator<T> iterator() {
return this;
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasNext() {
return true;
}
/**
* {@inheritDoc}
*/
@Override
public void remove() {
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc}
*/
@Override
public T next() {
return this.elements.get(sanitizeIndex(determineNextIndex()));
}
private int determineNextIndex() {
final double z = this.rnd.nextGaussian();
return (int) (this.halfCount + (this.scaling * z));
}
private int sanitizeIndex(final int index) {
if (index < 0) {
return 0;
}
if (index > this.maxIndex) {
return this.maxIndex;
}
return index;
}
/**
* Prints a histogramm to stdout.
* @param args not used
*/
public static void main(final String[] args) {
final PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, Charset.forName("UTF-8")), true);
plotHistogramm(new Random(), out);
}
private static void plotHistogramm(final Random rnd, final PrintWriter out) {
// build elements
final Multimap<Integer, Integer> results = ArrayListMultimap.create();
final List<Integer> elements = Lists.newArrayListWithCapacity(HISTOGRAMM_ELEMENTS);
for (int i = 1; i < HISTOGRAMM_ELEMENTS; i++) {
elements.add(i);
}
// sample sequence
final Iterator<Integer> randomSeq = new GaussianSequence<Integer>(rnd, elements);
for (int j = 0; j < HISTOGRAMM_SAMPLES; j++) {
final Integer sampled = randomSeq.next();
results.put(sampled, sampled);
}
// count and sort results
final Builder<Integer, Integer> r = ImmutableSortedMap.naturalOrder();
for (final Entry<Integer, Collection<Integer>> e : results.asMap().entrySet()) {
final int count = e.getValue().size();
r.put(e.getKey(), count);
}
// plot results
final SortedMap<Integer, Integer> sortedAndCounted = r.build();
final double histogramScale = (double) HISTOGRAMM_LENGTH/Collections.max(sortedAndCounted.values());
for (final Entry<Integer, Integer> e : sortedAndCounted.entrySet()) {
out.format("%3d [%4d]", e.getKey(), e.getValue());
final StringBuilder c = new StringBuilder();
final int lineLength = (int) (histogramScale * e.getValue());
for (int i = 0; i < lineLength; i++) {
c.append('*');
}
out.println(c);
}
}
}
関連する問題
- 1. オーケー与えられた分布
- 2. 与えられた分布で文字を生成する
- 3. 与えられた分散と平均のmatlabの一様分布を持つ数列
- 4. 与えられたプロパティでmatlabの均一分布遅延を生成する
- 5. カウントNo。マトリックスから与えられた制約を持つ部分行列の?
- 6. R - 与えられた関数を使った逆累積分布法
- 7. 与えられた式の上限を見つけるには
- 8. 与えられた分布に基づいてデータフレームをサンプリングする
- 9. 2つのデータリストを与えられたmathematicaの数値積分
- 10. 与えられたネットワークを4つのサブネットに分割する
- 11. 複数の与えられた値を持つPythonでマスクされた配列を作成する
- 12. 与えられた2つの点から線を作成する
- 13. 与えられた分布、平均、SD、スキュー、尖度をRでどのように生成するか?
- 14. ハイチャート - 与えられたxの値を持つポイントスタート
- 15. 与えられたリストから2つのリストを作る
- 16. 共分散行列を持つ正規分布ランダムベクトルの生成
- 17. Python - 与えられた値から始まって与えられた長さでリストを作成する
- 18. 与えられた次数分布に基づいて確率的有向ネットワークをRで生成する
- 19. テンソルとして与えられたインデックスからホットベクトルを1つ作成する
- 20. BigtableIO与えられた接頭辞を持つキーを読む
- 21. 期限が与えられ、範囲が与えられた月の印刷期限
- 22. 与えられた作業ディレクトリを持つemacsでシェルを開く
- 23. Rails:無限レベルのtodo_itemsを持つToDoリストを作成する
- 24. 与えられたリストのサブリストのリストを作成します。
- 25. のJavaスパーク - 与えられた権限を持つファイルを書き込むための
- 26. ランダムな分布を持つ一様分布
- 27. 入力が与えられた場合のプロキシ型の作成
- 28. 与えられたPR_SEARCH_KEYを持つすべてのメッセージを見つける
- 29. 与えられた数のノードとエッジを持つグラフを見つけるアルゴリズム
- 30. スパーク - 主成分分析 - PCAは多くの主成分を必要とするが、0を与えられた。
利用可能な解決策は、かなりの複雑に見える... http://en.wikipedia.org/wiki/Inverse_transform_samplingを参照してください。 CERNのcoltライブラリが役立つかもしれません:http://acs.lbl.gov/~hoschek/colt/api/cern/jet/random/AbstractDistribution.html#nextInt()">cern.jet.random.AbstractDistribution#nextInt –