annotate sequences/rndseq.m @ 61:eff6bddf82e3 tip

Finally implemented perceptual brightness thing.
author samer
date Sun, 11 Oct 2015 10:20:42 +0100
parents b1280319413e
children
rev   line source
samer@3 1 % rndseq - Sequence of values sampled from a random variable model
samer@3 2 %
samer@3 3 % rndseq ::
samer@3 4 % model xdom:[[1,XD]] ~ random variable model, data size is xdom,
samer@3 5 % sdom:[[1,SD]], ~ size of sample,
samer@3 6 % -> seq([[xdom sdom]]) ~ size of rndseq is product of xdom and sdom
samer@3 7 %
samer@3 8 % rndseq ::
samer@3 9 % model xdom:[[1,XD]] ~ random variable model, data size is xdom,
samer@3 10 % sdom:[[1,SD]], ~ size of sample,
samer@3 11 % rndstate ~ initial state of generators
samer@3 12 % -> rndseq([[xdom sdom]]) ~ size of rndseq is product of xdom and sdom
samer@3 13 %
samer@3 14 % If an initial rndstate is supplied, rndseq is purely functional
samer@3 15 % and referentially transparent. Otherwise, the initial construction
samer@3 16 % uses the current state of the random generators. After this, the
samer@3 17 % entire sequence is fully determined.
samer@3 18 %
samer@3 19 % EXAMPLE
samer@3 20 %
samer@3 21 % rndseq(gaussian,[2,200]) :: seq [[2,200]]
samer@3 22 % rndseq(dirichlet(3,0.5),6) :: seq [[3,6]]
samer@3 23 function d=rndseq(model,sdom,k)
samer@3 24 if nargin<2, sdom=1; end
samer@3 25 if isa(model,'struct')
samer@3 26 if model.nparams>0,
samer@3 27 error('Model has unbound parameters');
samer@3 28 end
samer@3 29 gen=sampler(model.sample,sdom);
samer@3 30 elseif iscell(model)
samer@3 31 gen=sampler(model{1},sdom);
samer@3 32 end
samer@3 33
samer@3 34 if nargin>2,
samer@3 35 if ~iscell(k), s={k,k}; else s=k; end
samer@3 36 else
samer@3 37 s=getrndstate;
samer@3 38 end
samer@23 39 d=unfold(gen,s);
samer@3 40 end
samer@3 41
samer@3 42 % this creates a rand-state-managed version of g applied to args,
samer@3 43 % ie,
samer@3 44 % sampler ::
samer@3 45 % ((A1,A2,....) -> random B) ~'an action which generates a value'
samer@3 46 % -> (rndstata -> B, rndstate) ~'a deterministic random state transformer'.
samer@3 47 function f=sampler(g,varargin)
samer@3 48 f=@ufn;
samer@3 49 function [x,s]=ufn(s)
samer@3 50 setrndstate(s); x=g(varargin{:});
samer@3 51 s=getrndstate;
samer@3 52 end
samer@3 53 end
samer@3 54