Example #1
0
        /// <summary>
        /// Returns the sampler for a specified rng, data type and distribution spec
        /// </summary>
        /// <param name="rng">The random stream</param>
        /// <param name="spec">The distribution specifier</param>
        /// <typeparam name="T">The sample point type</typeparam>
        public static ISampler <T> Sampler <T>(this MklRng rng, IDistributionSpec <T> spec)
            where T : unmanaged
        {
            var sampler = default(ISampler <T>);

            switch (spec.DistKind)
            {
            case DistKind.Uniform:
                sampler = rng.UniformSampler <T>(UniformSpec.From(spec));
                break;

            case DistKind.UniformBits:
                sampler = rng.UniformBitsSampler <T>(UniformBitsSpec.From(spec));
                break;

            case DistKind.Bernoulli:
                sampler = rng.BernoulliSampler <T>(BernoulliSpec.From(spec));
                break;

            case DistKind.Gaussian:
                sampler = rng.GaussianSampler <T>(GaussianSpec.From(spec));
                break;

            default:
                throw unsupported <T>();
            }

            return(sampler);
        }
Example #2
0
File: Sampler.x.cs Project: 0xCM/z0
        /// <summary>
        /// Returns the sampler for a specified rng, data type and distribution spec
        /// </summary>
        /// <param name="rng">The random stream</param>
        /// <param name="spec">The distribution specifier</param>
        /// <typeparam name="T">The sample point type</typeparam>
        public static IRngSampler <T> Sampler <T>(this MklRng rng, IDistributionSpec <T> spec)
            where T : unmanaged
        {
            var sampler = default(IRngSampler <T>);

            switch (spec.DistKind)
            {
            case DistributionKind.Uniform:
                sampler = rng.UniformSampler <T>(Distributions.uniform(spec));
                break;

            case DistributionKind.UniformBits:
                sampler = rng.UniformBitsSampler <T>(Distributions.uniformbits(spec));
                break;

            case DistributionKind.Bernoulli:
                sampler = rng.BernoulliSampler <T>(Distributions.bernoulli(spec));
                break;

            case DistributionKind.Gaussian:
                sampler = rng.GaussianSampler <T>(Distributions.gaussian(spec));
                break;

            default:
                throw Unsupported.define <T>();
            }

            return(sampler);
        }
Example #3
0
 public Sampler(MklRng src, S distspec, int?bufferLen = null)
 {
     this.Source       = src;
     this.BufferLength = bufferLen ?? Pow2.T14;
     this.Buffer       = MemorySpan.Alloc <T>(this.BufferLength);
     this.Remaining    = 0;
     this.DistSpec     = distspec;
 }
Example #4
0
 public Sampler(MklRng src, S distspec, int?bufferLen = null)
 {
     this.Source       = src;
     this.BufferLength = bufferLen ?? Pow2.T14;
     this.Buffer       = new T[BufferLength];
     this.Remaining    = 0;
     this.DistSpec     = distspec;
 }
Example #5
0
        public static ISampler <T> BernoulliSampler <T>(this MklRng rng, BernoulliSpec <T> spec)
            where T : unmanaged
        {
            var sampler = default(ISampler <T>);

            if (typeof(T) == typeof(int))
            {
                sampler = samplers.bernoulli(rng, spec) as ISampler <T>;
            }
            else
            {
                throw unsupported <T>();
            }
            return(sampler);
        }
Example #6
0
        public static ISampler <T> GaussianSampler <T>(this MklRng rng, GaussianSpec <T> spec)
            where T : unmanaged
        {
            var sampler = default(ISampler <T>);

            if (typeof(T) == typeof(float))
            {
                sampler = samplers.gaussian(rng, spec.ToFloat32()) as ISampler <T>;
            }
            else if (typeof(T) == typeof(double))
            {
                sampler = samplers.gaussian(rng, spec.ToFloat64()) as ISampler <T>;
            }
            else
            {
                throw unsupported <T>();
            }
            return(sampler);
        }
Example #7
0
        public void CreateMt2203Generators()
        {
            var gencount   = Pow2.T08;
            var samplesize = Pow2.T16;
            var seeds      = Random.Array <uint>(gencount);
            var streams    = new MklRng[gencount];

            for (var i = 0; i < gencount; i++)
            {
                streams[i] = rng.mt2203(seeds[i], i);
            }

            var bufferF64 = new double[samplesize];
            var bufferU32 = new uint[samplesize];
            var bufferI32 = new int[samplesize];
            var ufRange   = closed(1.0, 250.0);

            for (var i = 0; i < gencount; i++)
            {
                var stream = streams[i];
                sample.uniform(stream, ufRange, bufferF64);
                Dataset.Load(bufferF64, 1).Extrema();
                var max = Dataset.Load(bufferF64, 1).Max()[0];
                Claim.lteq(max, ufRange.Right);
                Claim.neq(max, 0);

                sample.bits(stream, bufferU32);

                sample.bernoulli(stream, .40, bufferI32);
                for (var j = 0; j < samplesize; j++)
                {
                    Claim.yea(bufferI32[j] == 0 || bufferI32[j] == 1);
                }

                sample.gaussian(stream, .75, .75, bufferF64);
                sample.laplace(stream, .5, .5, bufferF64);
            }

            for (var i = 0; i < gencount; i++)
            {
                streams[i].Dispose();
            }
        }
Example #8
0
        public static ISampler <T> UniformBitsSampler <T>(this MklRng rng, UniformBitsSpec <T>?spec = null)
            where T : unmanaged
        {
            var sampler = default(ISampler <T>);
            var _spec   = spec ?? UniformBitsSpec.Define <T>();

            if (typeof(T) == typeof(uint))
            {
                sampler = samplers.bits(rng, _spec.ToUInt32()) as ISampler <T>;
            }
            else if (typeof(T) == typeof(ulong))
            {
                sampler = samplers.bits(rng, _spec.ToUInt64()) as ISampler <T>;
            }
            else
            {
                throw unsupported <T>();
            }
            return(sampler);
        }
Example #9
0
File: Sampler.x.cs Project: 0xCM/z0
        public static IRngSampler <T> UniformSampler <T>(this MklRng rng, UniformSpec <T>?spec = null)
            where T : unmanaged
        {
            var _spec   = spec ?? Distributions.uniform <T>(Numeric.minval <T>(), Numeric.maxval <T>());
            var sampler = default(IRngSampler <T>);

            if (typeof(T) == typeof(int))
            {
                sampler = samplers.uniform(rng, _spec.ToInt32()) as IRngSampler <T>;
            }
            else if (typeof(T) == typeof(float))
            {
                sampler = samplers.uniform(rng, _spec.ToFloat32()) as IRngSampler <T>;
            }
            else if (typeof(T) == typeof(double))
            {
                sampler = samplers.uniform(rng, _spec.ToFloat64()) as IRngSampler <T>;
            }
            else
            {
                throw no <T>();
            }
            return(sampler);
        }
Example #10
0
        public static ISampler <T> UniformSampler <T>(this MklRng rng, UniformSpec <T>?spec = null)
            where T : unmanaged
        {
            var _spec   = spec ?? UniformSpec.Define <T>(PrimalInfo.minval <T>(), PrimalInfo.maxval <T>());
            var sampler = default(ISampler <T>);

            if (typeof(T) == typeof(int))
            {
                sampler = samplers.uniform(rng, _spec.ToInt32()) as ISampler <T>;
            }
            else if (typeof(T) == typeof(float))
            {
                sampler = samplers.uniform(rng, _spec.ToFloat32()) as ISampler <T>;
            }
            else if (typeof(T) == typeof(double))
            {
                sampler = samplers.uniform(rng, _spec.ToFloat64()) as ISampler <T>;
            }
            else
            {
                throw unsupported <T>();
            }
            return(sampler);
        }
Example #11
0
 public static void laplace(MklRng stream, double mean, double beta, Span <double> dst)
 => VSL.vdRngLaplace(0, stream.Source, dst.Length, ref first(dst), mean, beta).ThrowOnError();
Example #12
0
 public static void exp(MklRng stream, float dx, float beta, Span <float> dst)
 => VSL.vsRngExponential(0, stream.Source, dst.Length, ref first(dst), dx, beta).ThrowOnError();
Example #13
0
 public static void gamma(MklRng stream, double alpha, double dx, double beta, Span <double> dst)
 => VSL.vdRngGamma(VslGammaMethod.GNorm, stream.Source, dst.Length, ref first(dst), alpha, dx, beta).ThrowOnError();
Example #14
0
 public static void poisson(MklRng stream, double alpha, Span <int> dst)
 => VSL.viRngPoisson(0, stream.Source, dst.Length, ref first(dst), alpha).ThrowOnError();
Example #15
0
 public static void exp(MklRng stream, double dx, double beta, Span <double> dst)
 => VSL.vdRngExponential(0, stream.Source, dst.Length, ref first(dst), dx, beta).ThrowOnError();
Example #16
0
 public GammaSampler(MklRng src, GammaSpec <T> spec, int?buferLen = null)
     : base(src, spec, buferLen)
 {
 }
Example #17
0
 public UniformBitsSampler(MklRng src, int?buferLen = null)
     : base(src, default, buferLen)
 {
 }
Example #18
0
 public static IRngSampler <int> uniform(MklRng src, int min, int max, int?capacity = null)
 => new UniformSampler <int>(src, (min, max), capacity);
Example #19
0
 public static IRngSampler <ulong> bits(MklRng src, UniformBitsSpec <ulong> spec, int?capacity = null)
 => new UniformBitsSampler <ulong>(src, capacity);
Example #20
0
 public static void laplace(MklRng stream, float a, float b, Span <float> dst)
 => VSL.vsRngLaplace(0, stream.Source, dst.Length, ref first(dst), a, b).ThrowOnError();
Example #21
0
 public static IRngSampler <uint> bits32(MklRng src, int?capacity = null)
 => new UniformBitsSampler <uint>(src, capacity);
Example #22
0
 public static void uniform(MklRng stream, float min, float max, Span <float> dst)
 => VSL.vsRngUniform(0, stream.Source, dst.Length, ref first(dst), min, max).ThrowOnError();
Example #23
0
 public static IRngSampler <int> uniform(MklRng src, Interval <int> range, int?capacity = null)
 => new UniformSampler <int>(src, range, capacity);
Example #24
0
 public static void uniform(MklRng stream, Interval <double> range, Span <double> dst)
 => VSL.vdRngUniform(0, stream.Source, dst.Length, ref first(dst), range.Left, range.Right).ThrowOnError();
Example #25
0
 public PoissonSampler(MklRng src, PoissonSpec <T> spec, int?buferLen = null)
     : base(src, spec, buferLen)
 {
 }
Example #26
0
 public static void uniform(MklRng stream, double min, double max, Span <double> dst)
 => VSL.vdRngUniform(0, stream.Source, dst.Length, ref first(dst), min, max).ThrowOnError();
Example #27
0
 public UniformSampler(MklRng src, UniformSpec <T> distspec, int?buferLen = null)
     : base(src, distspec, buferLen)
 {
 }
Example #28
0
 public static void bits(MklRng stream, Span <ulong> dst)
 => VSL.viRngUniformBits64(0, stream.Source, dst.Length, ref first(dst)).ThrowOnError();
Example #29
0
 public GammaSampler(MklRng src, T alpha, T dx, T beta, int?buferLen = null)
     : base(src, GammaSpec <T> .Define(alpha, dx, beta), buferLen)
 {
 }
Example #30
0
 public BernoulliSampler(MklRng src, BernoulliSpec <double> spec, int?buferLen = null)
     : base(src, spec, buferLen)
 {
 }