Skip to content

Commit

Permalink
fix James' drift PR, add ringidx.FIx fixed len ring index, add rest o…
Browse files Browse the repository at this point in the history
…f random distributions based on gonum distuv funs
  • Loading branch information
Randall C. O'Reilly committed Apr 15, 2021
1 parent b91438e commit 16a70c9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 161 deletions.
157 changes: 17 additions & 140 deletions erand/rndparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/rand"

"github.com/goki/ki/kit"
"gonum.org/v1/gonum/stat/distuv"
)

// RndParams provides parameterized random number generation according to different distributions
Expand All @@ -27,11 +28,11 @@ func (rp *RndParams) Gen(thr int) float64 {
case Uniform:
return UniformMeanRange(rp.Mean, rp.Var, thr)
case Binomial:
return rp.Mean + Binom(int(rp.Par), rp.Var, thr)
return rp.Mean + Binom(rp.Par, rp.Var, thr)
case Poisson:
return rp.Mean + Poiss(rp.Var, thr)
case Gamma:
return rp.Mean + Gam(rp.Var, int(rp.Par), thr)
return rp.Mean + Gam(rp.Var, rp.Par, thr)
case Gaussian:
return rp.Mean + Gauss(rp.Var, thr)
case Beta:
Expand Down Expand Up @@ -123,132 +124,23 @@ func UniformMeanRange(mean, rnge float64, thr int) float64 {
}

// Binom returns binomial with n trials (par) each of probability p (var)
func Binom(n int, p float64, thr int) float64 {
/*
var j int
static int nold=(-1);
double am,em,g,angle,p,bnl,sq,t,y;
static double pold=(-1.0),pc,plog,pclog,en,oldg;
p=(pp <= 0.5 ? pp : 1.0-pp);
am=n*p;
if (n < 25) {
bnl=0.0;
for (j=1;j<=n;j++)
if (MTRnd::GenRandRes53(thr_no) < p) bnl += 1.0;
}
else if (am < 1.0) {
g=exp(-am);
t=1.0;
for (j=0;j<=n;j++) {
t *= MTRnd::GenRandRes53(thr_no);
if (t < g) break;
}
bnl=(j <= n ? j : n);
}
else {
if (n != nold) {
en=n;
oldg=gamma_ln(en+1.0);
nold=n;
}
if (p != pold) {
pc=1.0-p;
plog=log(p);
pclog=log(pc);
pold=p;
}
sq=sqrt(2.0*am*pc);
do {
do {
angle=pi*MTRnd::GenRandRes53(thr_no);
y=tan(angle);
em=sq*y+am;
} while (em < 0.0 || em >= (en+1.0));
em=floor(em);
t=1.2*sq*(1.0+y*y)*exp(oldg-gamma_ln(em+1.0)
-gamma_ln(en-em+1.0)+em*plog+(en-em)*pclog);
} while (MTRnd::GenRandRes53(thr_no) > t);
bnl=em;
}
if (p != pp) bnl=n-bnl;
return bnl;
*/
return 0
func Binom(n, p float64, thr int) float64 {
pd := distuv.Binomial{N: n, P: p}
return pd.Rand()
}

// Poiss returns poisson variable, as number of events in interval, with event rate (lmb = Var) plus mean
// Poiss returns poisson variable, as number of events in interval,
// with event rate (lmb = Var) plus mean
func Poiss(lmb float64, thr int) float64 {
/* static double sq,alxm,g,oldm=(-1.0);
double em,t,y;
if (xm < 12.0) {
if (xm != oldm) {
oldm=xm;
g=exp(-xm);
}
em = -1;
t=1.0;
do {
em += 1.0;
t *= MTRnd::GenRandRes53(thr_no);
} while (t > g);
}
else {
if (xm != oldm) {
oldm=xm;
sq=sqrt(2.0*xm);
alxm=log(xm);
g=xm*alxm-gamma_ln(xm+1.0);
}
do {
do {
y=tan(pi*MTRnd::GenRandRes53(thr_no));
em=sq*y+xm;
} while (em < 0.0);
em=floor(em);
t=0.9*(1.0+y*y)*exp(em*alxm-gamma_ln(em+1.0)-g);
} while (MTRnd::GenRandRes53(thr_no) > t);
}
return em; */
return 0
pd := distuv.Poisson{Lambda: lmb}
return pd.Rand()
}

// Gam represents maximum entropy distribution with two parameters: scaling parameter (Var)
// and shape parameter k (Par) plus mean
func Gam(v float64, k int, thr int) float64 {
/*
if (a < 1) {
double u = MTRnd::GenRandRes53(thr_no);
return gamma_dev(1.0 + a, b, thr_no) * pow (u, 1.0 / a);
}
{
double x, v, u;
double d = a - 1.0 / 3.0;
double c = (1.0 / 3.0) / sqrt (d);
while (true) {
do {
x = gauss_dev(thr_no);
v = 1.0 + c * x;
}
while (v <= 0);
v = v * v * v;
u = MTRnd::GenRandRes53(thr_no);
if (u < 1 - 0.0331 * x * x * x * x)
break;
if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
break;
}
return b * d * v;
}
*/
return 0.1
// Gam represents maximum entropy distribution with two parameters:
// scaling parameter (Var, Beta) and shape parameter k (Par, Alpha)
func Gam(v, k float64, thr int) float64 {
pd := distuv.Gamma{Alpha: k, Beta: v}
return pd.Rand()
}

// Gauss returns gaussian (normal) random number with given standard deviation
Expand All @@ -258,27 +150,12 @@ func Gauss(stdev float64, thr int) float64 {

// Beta returns beta random number with two shape parameters a > 0 and b > 0
func Bet(a, b float64, thr int) float64 {
x1 := Gam(a, 1.0, thr)
x2 := Gam(b, 1.0, thr)
x1 := Gam(a, 1, thr)
x2 := Gam(b, 1, thr)

return x1 / (x1 + x2)
}

// static double UniformDen(double x, double range)
// { double rval = 0.0; if(fabs(x) <= range) rval = 1.0 / (2.0 * range); return rval; }
// // #CAT_Float uniform density at x with given range on either size of 0 (subtr mean from x before)
// static double BinomDen(int n, int j, double p);
// // #CAT_Float binomial density at j with n trials (par) each of probability p (var)
// static double PoissonDen(int j, double l);
// // #CAT_Float poisson density with parameter l (var)
// static double GammaDen(int j, double l, double t);
// // #CAT_Float gamma density at time t with given number of stages (par), lmb (var)
// static double GaussDen(double x, double stdev);
// // #CAT_Float gaussian (normal) density for given standard deviation (0 mean)
// static double BetaDen(double x, double a, double b);
// // #CAT_Float beta density at value 0 < x < 1 for shape parameters a, b
//

// BoolProp returns boolean true/false with given probability.
func BoolProb(p float64, thr int) bool {
return (ZeroOne(thr) < p)
Expand Down
11 changes: 4 additions & 7 deletions patgen/configvocab.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/emer/etable/etensor"
"github.com/emer/etable/tsragg"
"github.com/goki/ki/ints"
)

// Vocab is a map of named tensors that contain patterns used for creating
Expand Down Expand Up @@ -130,20 +129,18 @@ func AddVocabDrift(mp Vocab, name string, rows int, pctDrift float32, copyFrom s
trow := tsr.SubSpace([]int{0})
trow.CopyFrom(cprow)
nOn := NOnInTensor(cprow)
rmdr := 0.0 // remainder carryover in drift, JWA new re: ROR
drift := float64(nOn) * float64(pctDrift+derr) // precise fractional amount of drift, JWA new re: ROR
rmdr := 0.0 // remainder carryover in drift, JWA new re: ROR
drift := float64(nOn) * float64(pctDrift) // precise fractional amount of drift, JWA new re: ROR
for i := 1; i < rows; i++ {
srow := tsr.SubSpace([]int{i - 1})
trow := tsr.SubSpace([]int{i})
trow.CopyFrom(srow)
//JWA, below new
curDrift := math.Round(float64(nOn) * (float64(pctDrift)+rmdr))) // integer amount
nDrift = int(curDrift)
curDrift := math.Round(float64(nOn) * (float64(pctDrift) + rmdr)) // integer amount
nDrift := int(curDrift)
if nDrift > 0 {
FlipBits(trow, nDrift, nDrift, 1, 0)
}
rmdr += drift - curDrift // accumulate remainder
//JWA, above new
}
return tsr, nil
}
Expand Down
20 changes: 6 additions & 14 deletions prjn/pooltile.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,13 @@ func (pt *PoolTile) Connect(send, recv *etensor.Shape, same bool) (sendn, recvn
for sui := 0; sui < sNu; sui++ {
si := sis + sui
off := ri*sNtot + si
if off < cons.Len() {
if off < cons.Len() && ri < len(rnv) && si < len(snv) {
// if !pt.SelfCon && same && ri == si {
// continue
// }
cons.Values.Set(off, true)
if ri < len(rnv) {
rnv[ri]++
}
if si < len(snv) {
snv[si]++
}
rnv[ri]++
snv[si]++
}
}
}
Expand Down Expand Up @@ -179,14 +175,10 @@ func (pt *PoolTile) ConnectRecip(send, recv *etensor.Shape, same bool) (sendn, r
si := ris + sui
// note: indexes reversed here
off := ri*sNtot + si
if off < cons.Len() {
if off < cons.Len() && ri < len(rnv) && si < len(snv) {
cons.Values.Set(off, true)
if ri < len(rnv) {
rnv[ri]++
}
if si < len(snv) {
snv[si]++
}
rnv[ri]++
snv[si]++
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions ringidx/fixlen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package ringidx

// FIx is a fixed-length ring index structure -- does not grow
// or shrink dynamically.
type FIx struct {
Zi int `desc:"the zero index position -- where logical 0 is in physical buffer"`
Len int `desc:"the length of the buffer -- wraps around at this modulus"`
}

// Idx returns the physical index of the logical index i.
// i must be < Len.
func (fi *FIx) Idx(i int) int {
i += fi.Zi
if i >= fi.Len {
i -= fi.Len
}
return i
}

// IdxIsValid returns true if given index is valid: >= 0 and < Len
func (fi *FIx) IdxIsValid(i int) bool {
return i >= 0 && i < fi.Len
}

// Shift moves the zero index up by n.
func (fi *FIx) Shift(n int) {
fi.Zi = fi.Idx(n)
}

0 comments on commit 16a70c9

Please sign in to comment.