public static void demo1() { // Gamma distribution // define distribution parameters double mean = 5; double variance = 1.5; double alpha = mean * mean / variance; double lambda = 1 / (variance / mean); // for tests and debugging use a random engine with CONSTANT seed --> deterministic and reproducible results RandomEngine engine = new MersenneTwister(); // your favourite distribution goes here AbstractDistribution dist = new Gamma(alpha, lambda, engine); // collect random numbers and print statistics int size = 100000; var numbers = new DoubleArrayList(size); for (int i = 0; i < size; i++) { numbers.Add(dist.NextDouble()); } DynamicBin1D bin = new DynamicBin1D(); bin.AddAllOf(numbers); Console.WriteLine(bin); }
public virtual void AddAllOfFromTo(DoubleArrayList list, int from, int to) { for (int i = from; i <= to; i++) { Add(list[i]); } }
/// <summary> /// Adds the part of the specified list between indexes <i>from</i> (inclusive) and <i>to</i> (inclusive) to the receiver. /// </summary> /// <param name="values">the list of which elements shall be added.</param> /// <param name="from">the index of the first element to be added (inclusive).</param> /// <param name="to">the index of the last element to be added (inclusive).</param> public void AddAllOfFromTo(DoubleArrayList values, int from, int to) { //buffer.AddAllOfFromTo(values, from, to); buffer.AddAllOfFromTo(values, from, to); this.isSorted = false; }
/// <summary> /// Finds the first and last indexes of a specific element within a sorted list. /// <summary> /// <returns>int[]</returns> /// <param name="list">cern.colt.list.DoubleArrayList</param> /// <param name="element">the element to search for</param> protected static IntArrayList BinaryMultiSearch(DoubleArrayList list, double element) { int index = list.BinarySearch(element); if (index < 0) { return(null); //not found } int from = index - 1; while (from >= 0 && list[from] == element) { from--; } from++; int to = index + 1; while (to < list.Count && list[to] == element) { to++; } to--; return(new IntArrayList(new int[] { from, to })); }
public virtual double Moment(int k, double c) { if (k < 0) { throw new ArgumentException(Cern.LocalizedResources.Instance().Exception_KMustBePositive); } //checkOrder(k); if (!HasSumOfPowers(k)) { return(Double.NaN); } int maxOrder = System.Math.Min(k, GetMaxOrderForSumOfPowers()); DoubleArrayList sumOfPows = new DoubleArrayList(maxOrder + 1); sumOfPows.Add(Size); sumOfPows.Add(Sum); sumOfPows.Add(SumOfSquares); for (int i = 3; i <= maxOrder; i++) { sumOfPows.Add(GetSumOfPowers(i)); } return(Descriptive.Moment(k, c, Size, sumOfPows.ToArray())); }
/// <summary> /// Computes the specified quantile elements over the values previously added. /// </summary> /// <param name="phis">the quantiles for which elements are to be computed. Each phi must be in the interval (0.0,1.0]. <tt>phis</tt> must be sorted ascending.</param> /// <returns>the approximate quantile elements.</returns> public override DoubleArrayList QuantileElements(DoubleArrayList phis) { if (precomputeEpsilon <= 0.0) { return(base.QuantileElements(phis)); } int quantilesToPrecompute = (int)Utils.EpsilonCeiling(1.0 / precomputeEpsilon); /* * if (phis.Count > quantilesToPrecompute) { * // illegal use case! * // we compute results, but loose explicit approximation guarantees. * return base.QuantileElements(phis); * } */ //select that quantile from the precomputed set that corresponds to a position closest to phi. phis = phis.Copy(); double e = precomputeEpsilon; for (int index = phis.Size; --index >= 0;) { double phi = phis[index]; int i = (int)System.Math.Round(((2.0 * phi / e) - 1.0) / 2.0); // finds closest i = System.Math.Min(quantilesToPrecompute - 1, System.Math.Max(0, i)); double augmentedPhi = (e / 2.0) * (1 + 2 * i); phis[index] = augmentedPhi; } return(base.QuantileElements(phis)); }
public void Trim(int s, int l) { DoubleArrayList elems = SortedElements; Clear(); AddAllOfFromTo(elems, s, elems.Size - 1 - l); }
/// <summary> /// Computes the specified quantile elements over the values previously added. /// </summary> /// <param name="phis">the quantiles for which elements are to be computed. Each phi must be in the interval [0.0,1.0]. <tt>phis</tt> must be sorted ascending.</param> /// <returns>the approximate quantile elements.</returns> public virtual DoubleArrayList QuantileElements(DoubleArrayList phis) { /* * //check parameter */ List <Double> sortedPhiList = phis.Copy().ToList(); sortedPhiList.Sort(); if (!phis.Equals(sortedPhiList)) { throw new ArgumentException(Cern.LocalizedResources.Instance().Exception_PhisMustBeAscending); } //System.out.println("starting to augment missing values, if necessary..."); phis = PreProcessPhis(phis); long[] triggerPositions = new long[phis.Size]; long totalSize = this.bufferSet.TotalSize; for (int i = phis.Size; --i >= 0;) { triggerPositions[i] = Utils.EpsilonCeiling(phis[i] * totalSize) - 1; } //System.out.println("triggerPositions="+cern.colt.Arrays.ToString(triggerPositions)); //System.out.println("starting to determine quantiles..."); //System.out.println(bufferSet); DoubleBuffer[] fullBuffers = bufferSet.GetFullOrPartialBuffers(); double[] quantileElements = new double[phis.Size]; //do the main work: determine values at given positions in sorted sequence return(new DoubleArrayList(bufferSet.GetValuesAtPositions(fullBuffers, triggerPositions))); }
/// <summary> /// Fills the specified array of doubles with random numbers in the interval [0,1). /// </summary> /// <param name="array">An array of double-precision floating point numbers.</param> public void Fill(DoubleArrayList array) { for (int i = 0; i < array.Count; i++) { array[i] = NextDouble(); } }
/// <summary> /// Initializes a new instance of the <see cref="Vector2D"/> class with the specified coordinates. /// </summary> /// <param name="coordinates">An array containing the coordinate parameters.</param> public Vector2D(DoubleArrayList coordinates) { Debug.Assert(coordinates != null); Debug.Assert(coordinates.Count >= 2); _x = coordinates[0]; _y = coordinates[1]; }
/// <summary> /// Constructs and returns a new buffer with the given target. /// </summary> /// <param name="target">the target to flush to.</param> /// <param name="capacity">the number of points the buffer shall be capable of holding before overflowing and flushing to the target.</param> public DoubleBuffer(IDoubleBufferConsumer target, int capacity) { this.target = target; this.capacity = capacity; this.elements = new double[capacity]; this.list = new DoubleArrayList(elements); this.size = 0; }
/// <summary> /// Initializes a new instance of the <see cref="Matrix2D"/> structure with the specified values. /// </summary> /// <param name="elements">An array containing the matrix values in row-major order.</param> public Matrix2D(DoubleArrayList elements) { Debug.Assert(elements != null); Debug.Assert(elements.Count >= 4); _m11 = elements[0]; _m12 = elements[1]; _m21 = elements[2]; _m22 = elements[3]; }
/// <summary> /// Initializes a new instance of the <see cref="Vector3"/> class with the specified coordinates. /// </summary> /// <param name="coordinates">An array containing the coordinate parameters.</param> public Vector3(DoubleArrayList coordinates) : base() { Debug.Assert(coordinates != null); Debug.Assert(coordinates.Count >= 3); m_X = coordinates[0]; m_Y = coordinates[1]; m_Z = coordinates[2]; }
/// <summary> /// Initializes a new instance of the <see cref="Matrix3F"/> structure with the specified values. /// </summary> /// <param name="elements">An array containing the matrix values in row-major order.</param> public Matrix3D(DoubleArrayList elements) { Debug.Assert(elements != null); Debug.Assert(elements.Count >= 9); _m11 = elements[0]; _m12 = elements[1]; _m13 = elements[2]; _m21 = elements[3]; _m22 = elements[4]; _m23 = elements[5]; _m31 = elements[6]; _m32 = elements[7]; _m33 = elements[8]; }
/// <summary> /// Adds a value to the receiver. /// </summary> /// <param name="elements"></param> /// <param name="from"></param> /// <param name="to"></param> public void AddAllOfFromTo(DoubleArrayList elements, int from, int to) { if (!isAllocated) { Allocate(); // lazy buffer allocation can safe memory. } values.AddAllOfFromTo(elements, from, to); this.isSorted = false; }
/// <summary> /// Constructs and returns an empty bin; implicitly calls {@link #setFixedOrder(Boolean) setFixedOrder(false)}. /// </summary> public DynamicBin1D() : base() { this.Clear(); this._elements = new DoubleArrayList(); this._sortedElements = new DoubleArrayList(0); this.FixedOrder = false; this.HasSumOfLogarithms = true; this.HasSumOfInversions = true; }
/// <summary> /// Initializes a new instance of the <see cref="Vector4"/> class with the specified coordinates. /// </summary> /// <param name="coordinates">An array containing the coordinate parameters.</param> public Vector4(DoubleArrayList coordinates) { Debug.Assert(coordinates != null); Debug.Assert(coordinates.Count >= 4); _x = coordinates[0]; _y = coordinates[1]; _z = coordinates[2]; _w = coordinates[3]; }
/// <summary> /// Adds all elements of the specified list to the receiver. /// </summary> /// <param name="list">the list of which all elements shall be added.</param> public void AddAllOf(DoubleArrayList list) { int listSize = list.Size; if (this.size + listSize >= this.capacity) { Flush(); } this.target.AddAllOf(list); }
/// <summary> /// Initializes a new instance of the <see cref="Matrix4F"/> structure with the specified values. /// </summary> /// <param name="elements">An array containing the matrix values in row-major order.</param> public Matrix4D(DoubleArrayList elements) { Debug.Assert(elements != null); Debug.Assert(elements.Count >= 16); _m11 = elements[0]; _m12 = elements[1]; _m13 = elements[2]; _m14 = elements[3]; _m21 = elements[4]; _m22 = elements[5]; _m23 = elements[6]; _m24 = elements[7]; _m31 = elements[8]; _m32 = elements[9]; _m33 = elements[10]; _m34 = elements[11]; _m41 = elements[12]; _m42 = elements[13]; _m43 = elements[14]; _m44 = elements[15]; }
/// <summary> /// Adds the part of the specified list between indexes <tt>from</tt> (inclusive) and <tt>to</tt> (inclusive) to the receiver. /// </summary> /// <param name="values">the list of which elements shall be added.</param> /// <param name="from">the index of the first element to be added (inclusive).</param> /// <param name="to">the index of the last element to be added (inclusive).</param> public void AddAllOfFromTo(DoubleArrayList values, int from, int to) { /* * // the obvious version, but we can do quicker... * double[] theValues = values.ToArray(); * int theSize=values.Count; * for (int i=0; i<theSize; ) add(theValues[i++]); */ double[] valuesToAdd = values.Elements; int k = this.bufferSet.NumberOfElements; int bufferSize = k; double[] bufferValues = null; if (currentBufferToFill != null) { bufferValues = currentBufferToFill.Values.Elements; bufferSize = currentBufferToFill.Size; } for (int i = from - 1; ++i <= to;) { if (SampleNextElement()) { if (bufferSize == k) { // full if (bufferSet.GetFirstEmptyBuffer() == null) { Collapse(); } NewBuffer(); if (!currentBufferToFill.IsAllocated) { currentBufferToFill.Allocate(); } currentBufferToFill.IsSorted = false; bufferValues = currentBufferToFill.Values.Elements; bufferSize = 0; } bufferValues[bufferSize++] = valuesToAdd[i]; if (bufferSize == k) { // full currentBufferToFill.Values.SetSize(bufferSize); currentBufferToFill = null; } } } if (this.currentBufferToFill != null) { this.currentBufferToFill.Values.SetSize(bufferSize); } this.totalElementsFilled += to - from + 1; }
public void assignLabels( LingoProcessingContext context, DoubleMatrix2D stemCos, IntIntOpenHashMap filteredRowToStemIndex, DoubleMatrix2D phraseCos ) { PreprocessingContext preprocessingContext = context.preprocessingContext; int firstPhraseIndex = preprocessingContext.allLabels.firstPhraseIndex; int [] labelsFeatureIndex = preprocessingContext.allLabels.featureIndex; int [] mostFrequentOriginalWordIndex = preprocessingContext.allStems.mostFrequentOriginalWordIndex; int desiredClusterCount = stemCos.columns(); IntArrayList clusterLabelFeatureIndex = new IntArrayList( desiredClusterCount); DoubleArrayList clusterLabelScore = new DoubleArrayList(desiredClusterCount); for (int label = 0; label < desiredClusterCount; label++) { Pair<int, int> stemMax = max(stemCos); Pair<int, int> phraseMax = max(phraseCos); if (stemMax == null && phraseMax == null) { break; } double stemScore = stemMax != null ? stemCos.getQuick(stemMax.objectA, stemMax.objectB) : -1; double phraseScore = phraseMax != null ? phraseCos.getQuick( phraseMax.objectA, phraseMax.objectB) : -1; if (phraseScore > stemScore) { phraseCos.viewRow(phraseMax.objectA).assign(0); phraseCos.viewColumn(phraseMax.objectB).assign(0); stemCos.viewColumn(phraseMax.objectB).assign(0); clusterLabelFeatureIndex.add(labelsFeatureIndex[phraseMax.objectA + firstPhraseIndex]); clusterLabelScore.add(phraseScore); } else { stemCos.viewRow(stemMax.objectA).assign(0); stemCos.viewColumn(stemMax.objectB).assign(0); if (phraseCos != null) { phraseCos.viewColumn(stemMax.objectB).assign(0); } clusterLabelFeatureIndex .add(mostFrequentOriginalWordIndex[filteredRowToStemIndex .get(stemMax.objectA)]); clusterLabelScore.add(stemScore); } } context.clusterLabelFeatureIndex = clusterLabelFeatureIndex.toArray(); context.clusterLabelScore = clusterLabelScore.toArray(); }
protected new DoubleArrayList PreProcessPhis(DoubleArrayList phis) { if (beta > 1.0) { phis = phis.Copy(); for (int i = phis.Size; --i >= 0;) { phis[i] = (2 * phis[i] + beta - 1) / (2 * beta); } } return(phis); }
/// <summary> /// This method was created in VisualAge. /// <summary> /// <returns>double[]</returns> /// <param name="values">cern.it.hepodbms.primitivearray.DoubleArrayList</param> /// <param name="phis">double[]</param> public static DoubleArrayList ObservedEpsilonsAtPhis(DoubleArrayList phis, ExactDoubleQuantileFinder exactFinder, IDoubleQuantileFinder approxFinder, double desiredEpsilon) { DoubleArrayList epsilons = new DoubleArrayList(phis.Count); for (int i = phis.Count; --i >= 0;) { double epsilon = ObservedEpsilonAtPhi(phis[i], exactFinder, approxFinder); epsilons.Add(epsilon); if (epsilon > desiredEpsilon) Console.WriteLine("Real epsilon = " + epsilon + " is larger than desired by " + (epsilon - desiredEpsilon)); } return epsilons; }
/// <summary> /// 3-d OLAP cube operator; Fills all cells of the given vectors into the given histogram. /// If you use Hep.Aida.Ref.Converter.ToString(histo) on the result, the OLAP cube of x-"column" vsd y-"column" vsd z-"column", summing the weights "column" will be printed. /// For example, aggregate sales by product by region by time. /// <p> /// Computes the distinct values of x and y and z, yielding histogram axes that capture one distinct value per bin. /// Then fills the histogram. /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="z"></param> /// <param name="weights"></param> /// <returns>the histogram containing the cube.</returns> /// <excption cref="ArgumentException">if <i>x.Count != y.Count || x.Count != z.Count || x.Count != weights.Count</i>.</excption> public static Hep.Aida.IHistogram3D cube(DoubleMatrix1D x, DoubleMatrix1D y, DoubleMatrix1D z, DoubleMatrix1D weights) { if (x.Size != y.Size || x.Size != z.Size || x.Size != weights.Size) { throw new ArgumentException("vectors must have same size"); } var epsilon = 1.0E-9; var distinct = new DoubleArrayList(); var vals = new double[x.Size]; var sorted = new DoubleArrayList(vals); // compute distinct values of x vals = x.ToArray(); // copy x into vals sorted.Sort(); Cern.Jet.Stat.Descriptive.Frequencies(sorted, distinct, null); // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin if (distinct.Count > 0) { distinct.Add(distinct[distinct.Count - 1] + epsilon); } distinct.TrimToSize(); Hep.Aida.IAxis xaxis = new Hep.Aida.Ref.VariableAxis(distinct.ToArray()); // compute distinct values of y vals = y.ToArray(); sorted.Sort(); Cern.Jet.Stat.Descriptive.Frequencies(sorted, distinct, null); // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin if (distinct.Count > 0) { distinct.Add(distinct[distinct.Count - 1] + epsilon); } distinct.TrimToSize(); Hep.Aida.IAxis yaxis = new Hep.Aida.Ref.VariableAxis(distinct.ToArray()); // compute distinct values of z vals = z.ToArray(); sorted.Sort(); Cern.Jet.Stat.Descriptive.Frequencies(sorted, distinct, null); // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin if (distinct.Count > 0) { distinct.Add(distinct[distinct.Count - 1] + epsilon); } distinct.TrimToSize(); Hep.Aida.IAxis zaxis = new Hep.Aida.Ref.VariableAxis(distinct.ToArray()); Hep.Aida.IHistogram3D histo = new Hep.Aida.Ref.Histogram3D("Cube", xaxis, yaxis, zaxis); return(Histogram(histo, x, y, z, weights)); }
/// <summary> /// Computes the specified quantile elements over the values previously added. /// </summary> /// <param name="phis">the quantiles for which elements are to be computedd Each phi must be in the interval [0.0,1.0]d <i>phis</i> must be sorted ascending.</param> /// <returns>the exact quantile elements.</returns> public DoubleArrayList QuantileElements(DoubleArrayList phis) { this.Sort(); return(Cern.Jet.Stat.Descriptive.Quantiles(this.buffer, phis)); /* * int bufferSize = (int) this.Count; * double[] quantileElements = new double[phis.Count]; * for (int i=phis.Count; --i >=0;) { * int rank=(int)Utils.epsilonCeiling(phis.Get(i)*bufferSize) -1; * quantileElements[i]=buffer.Get(rank); * } * return new DoubleArrayList(quantileElements); */ }
public override DoubleArrayList QuantileElements(DoubleArrayList phis) { /* * The KNOWN quantile finder reads off quantiles from FULL buffers only. * Since there might be a partially full buffer, this method first satisfies this constraint by temporarily filling a few +infinity, -infinity elements to make up a full block. * This is in full conformance with the explicit approximation guarantees. * * For those of you working on online apps: * The approximation guarantees are given for computing quantiles AFTER N elements have been filled, not for intermediate displays. * If you have one thread filling and another thread displaying concurrently, you will note that in the very beginning the infinities will dominate the display. * This could confuse users, because, of course, they don't expect any infinities, even if they "disappear" after a short while. * To prevent panic exclude phi's close to zero or one in the early phases of processing. */ DoubleBuffer partial = this.BufferSet.GetPartialBuffer(); int missingValues = 0; if (partial != null) { // any auxiliary infinities needed? missingValues = BufferSet.BufferSize - partial.Size; if (missingValues <= 0) { throw new InvalidOperationException("Oops! illegal missing values."); } //System.out.println("adding "+missingValues+" infinity elements..."); this.AddInfinities(missingValues, partial); //determine beta (N + Infinity values = beta * N) this.beta = (this.TotalElementsFilled + missingValues) / (double)this.TotalElementsFilled; } else { this.beta = 1.0; } DoubleArrayList quantileElements = base.QuantileElements(phis); // restore state we were in before. // remove the temporarily added infinities. if (partial != null) { RemoveInfinitiesFrom(missingValues, partial); } // now you can continue filling the remaining values, if any. return(quantileElements); }
public Boolean RemoveAllOf(DoubleArrayList list) { Boolean changed = this.Elements.RemoveAll(list); if (changed) { ClearAllMeasures(); InvalidateAll(); //this.Size() = 0; if (FixedOrder) { this.SortedElements.RemoveAll(list); this.isSorted = true; } } return(changed); }
/// <summary> /// Constructs a matrix with a given number of rows and columns. /// All entries are initially <i>0</i>. /// </summary> /// <param name="rows">the number of rows the matrix shall have.</param> /// <param name="columns">the number of columns the matrix shall have.</param> /// <exception cref="ArgumentException">if <i>rows < 0 || columns < 0 || (double)columns * rows > int.MaxValue</i>.</exception> public RCDoubleMatrix2D(int rows, int columns) : base(null) { try { Setup(rows, columns); } catch (ArgumentException exc) { // we can hold rows*columns>int.MaxValue cells ! if (!"matrix too large".Equals(exc.Message)) { throw exc; } } Indexes = new IntArrayList(); Values = new DoubleArrayList(); Starts = new int[rows + 1]; }
public override String ToString() { StringBuilder buf = new StringBuilder(base.ToString()); DoubleArrayList distinctElements = new DoubleArrayList(); IntArrayList freq = new IntArrayList(); Frequencies(distinctElements, freq); if (distinctElements.Size < 100) { // don't cause unintended floods buf.Append("Distinct elements: " + distinctElements + "\n"); buf.Append("Frequencies: " + freq + "\n"); } else { buf.Append("Distinct elements & frequencies not printed (too many)."); } return(buf.ToString()); }
public override void AddAllOfFromTo(DoubleArrayList list, int from, int to) { base.AddAllOfFromTo(list, from, to); if (_sumOfPowers != null) { //int max_k = this.min_k + this.SumOfPowers.Length-1; Descriptive.IncrementalUpdateSumsOfPowers(list, from, to, 3, GetMaxOrderForSumOfPowers(), ref _sumOfPowers); } if (this.HasSumOfInversions) { this.SumOfInversions += Descriptive.SumOfInversions(list, from, to); } if (this.HasSumOfLogarithms) { this.SumOfLogarithms += Descriptive.SumOfLogarithms(list, from, to); } }
public override void AddAllOfFromTo(DoubleArrayList list, int from, int to) { //if (this.arguments == null) setUpCache(); lock (arguments) { // prepare arguments arguments[0] = this.min; arguments[1] = this.max; arguments[2] = this.sum; arguments[3] = this.sum_xx; Descriptive.IncrementalUpdate(list, from, to, ref arguments); // store the new parameters back this.min = arguments[0]; this.max = arguments[1]; this.sum = arguments[2]; this.sum_xx = arguments[3]; this.size += to - from + 1; } }