本文整理汇总了Golang中github.com/gonum/stat.Mean函数的典型用法代码示例。如果您正苦于以下问题:Golang Mean函数的具体用法?Golang Mean怎么用?Golang Mean使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Mean函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestMarginal
func TestMarginal(t *testing.T) {
for _, test := range []struct {
mu []float64
sigma *mat64.SymDense
marginal []int
}{
{
mu: []float64{2, 3, 4},
sigma: mat64.NewSymDense(3, []float64{2, 0.5, 3, 0.5, 1, 0.6, 3, 0.6, 10}),
marginal: []int{0},
},
{
mu: []float64{2, 3, 4},
sigma: mat64.NewSymDense(3, []float64{2, 0.5, 3, 0.5, 1, 0.6, 3, 0.6, 10}),
marginal: []int{0, 2},
},
{
mu: []float64{2, 3, 4, 5},
sigma: mat64.NewSymDense(4, []float64{2, 0.5, 3, 0.1, 0.5, 1, 0.6, 0.2, 3, 0.6, 10, 0.3, 0.1, 0.2, 0.3, 3}),
marginal: []int{0, 3},
},
} {
normal, ok := NewNormal(test.mu, test.sigma, nil)
if !ok {
t.Fatalf("Bad test, covariance matrix not positive definite")
}
marginal, ok := normal.MarginalNormal(test.marginal, nil)
if !ok {
t.Fatalf("Bad test, marginal matrix not positive definite")
}
dim := normal.Dim()
nSamples := 1000000
samps := mat64.NewDense(nSamples, dim, nil)
for i := 0; i < nSamples; i++ {
normal.Rand(samps.RawRowView(i))
}
estMean := make([]float64, dim)
for i := range estMean {
estMean[i] = stat.Mean(mat64.Col(nil, i, samps), nil)
}
for i, v := range test.marginal {
if math.Abs(marginal.mu[i]-estMean[v]) > 1e-2 {
t.Errorf("Mean mismatch: want: %v, got %v", estMean[v], marginal.mu[i])
}
}
marginalCov := marginal.CovarianceMatrix(nil)
estCov := stat.CovarianceMatrix(nil, samps, nil)
for i, v1 := range test.marginal {
for j, v2 := range test.marginal {
c := marginalCov.At(i, j)
ec := estCov.At(v1, v2)
if math.Abs(c-ec) > 5e-2 {
t.Errorf("Cov mismatch element i = %d, j = %d: want: %v, got %v", i, j, c, ec)
}
}
}
}
}
示例2: checkEntropy
func checkEntropy(t *testing.T, i int, x []float64, e entropyer, tol float64) {
tmp := make([]float64, len(x))
for i, v := range x {
tmp[i] = -e.LogProb(v)
}
entropy := stat.Mean(tmp, nil)
if !floats.EqualWithinAbsOrRel(entropy, e.Entropy(), tol, tol) {
t.Errorf("Entropy mismatch case %v: want: %v, got: %v", i, entropy, e.Entropy())
}
}
示例3: Apply
func (sc ScaleAA) Apply(seq string, sw int) ([]float64, []float64) {
// sw must be odd (test before)
hydro := make([]float64, len(seq))
hydrosw := make([]float64, len(seq))
for i := 0; i < len(seq); i++ {
hydro[i] = sc[string(seq[i])]
}
for i := 0; i < len(hydro); i++ {
if (i >= (sw / 2)) && (i < (len(hydro) - (sw / 2))) {
b, e := i-(sw/2), i+(sw/2)+1
hydrosw[i] = stat.Mean(hydro[b:e], nil)
} else if i < (sw / 2) {
b, e := 0, i+(sw/2)+1
hydrosw[i] = stat.Mean(hydro[b:e], nil)
} else if i >= (len(hydro) - (sw / 2)) {
b, e := i-(sw/2), len(hydro)
hydrosw[i] = stat.Mean(hydro[b:e], nil)
}
}
return hydro, hydrosw
}
示例4: TestNormRand
func TestNormRand(t *testing.T) {
for _, test := range []struct {
mean []float64
cov []float64
}{
{
mean: []float64{0, 0},
cov: []float64{
1, 0,
0, 1,
},
},
{
mean: []float64{0, 0},
cov: []float64{
1, 0.9,
0.9, 1,
},
},
{
mean: []float64{6, 7},
cov: []float64{
5, 0.9,
0.9, 2,
},
},
} {
dim := len(test.mean)
cov := mat64.NewSymDense(dim, test.cov)
n, ok := NewNormal(test.mean, cov, nil)
if !ok {
t.Errorf("bad covariance matrix")
}
nSamples := 1000000
samps := mat64.NewDense(nSamples, dim, nil)
for i := 0; i < nSamples; i++ {
n.Rand(samps.RawRowView(i))
}
estMean := make([]float64, dim)
for i := range estMean {
estMean[i] = stat.Mean(mat64.Col(nil, i, samps), nil)
}
if !floats.EqualApprox(estMean, test.mean, 1e-2) {
t.Errorf("Mean mismatch: want: %v, got %v", test.mean, estMean)
}
estCov := stat.CovarianceMatrix(nil, samps, nil)
if !mat64.EqualApprox(estCov, cov, 1e-2) {
t.Errorf("Cov mismatch: want: %v, got %v", cov, estCov)
}
}
}
示例5: checkSkewness
func checkSkewness(t *testing.T, i int, x []float64, s skewnesser, tol float64) {
mean := s.Mean()
std := s.StdDev()
tmp := make([]float64, len(x))
for i, v := range x {
tmp[i] = math.Pow(v-mean, 3)
}
mu3 := stat.Mean(tmp, nil)
skewness := mu3 / math.Pow(std, 3)
if !floats.EqualWithinAbsOrRel(skewness, s.Skewness(), tol, tol) {
t.Errorf("ExKurtosis mismatch case %v: want: %v, got: %v", i, skewness, s.Skewness())
}
}
示例6: checkExKurtosis
func checkExKurtosis(t *testing.T, i int, x []float64, e exKurtosiser, tol float64) {
mean := e.Mean()
tmp := make([]float64, len(x))
for i, x := range x {
tmp[i] = math.Pow(x-mean, 4)
}
variance := stat.Variance(x, nil)
mu4 := stat.Mean(tmp, nil)
kurtosis := mu4/(variance*variance) - 3
if !floats.EqualWithinAbsOrRel(kurtosis, e.ExKurtosis(), tol, tol) {
t.Errorf("ExKurtosis mismatch case %v: want: %v, got: %v", i, kurtosis, e.ExKurtosis())
}
}
示例7: TestRejection
func TestRejection(t *testing.T) {
// Test by finding the expected value of a Normal.
trueMean := 3.0
target := distuv.Normal{Mu: trueMean, Sigma: 2}
proposal := distuv.Normal{Mu: 0, Sigma: 5}
nSamples := 100000
x := make([]float64, nSamples)
Rejection(x, target, proposal, 100, nil)
ev := stat.Mean(x, nil)
if math.Abs(ev-trueMean) > 1e-2 {
t.Errorf("Mean mismatch: Want %v, got %v", trueMean, ev)
}
}
示例8: TestImportance
func TestImportance(t *testing.T) {
// Test by finding the expected value of a Normal.
trueMean := 3.0
target := dist.Normal{Mu: trueMean, Sigma: 2}
proposal := dist.Normal{Mu: 0, Sigma: 5}
nSamples := 100000
x := make([]float64, nSamples)
weights := make([]float64, nSamples)
Importance(x, weights, target, proposal)
ev := stat.Mean(x, weights)
if math.Abs(ev-trueMean) > 1e-2 {
t.Errorf("Mean mismatch: Want %v, got %v", trueMean, ev)
}
}
示例9: TestMetropolisHastings
func TestMetropolisHastings(t *testing.T) {
// Test by finding the expected value of a Normal.
trueMean := 3.0
target := distuv.Normal{Mu: trueMean, Sigma: 2}
proposal := condNorm{Sigma: 5}
burnin := 500
nSamples := 100000 + burnin
x := make([]float64, nSamples)
MetropolisHastings(x, 100, target, proposal, nil)
// Remove burnin
x = x[burnin:]
ev := stat.Mean(x, nil)
if math.Abs(ev-trueMean) > 1e-2 {
t.Errorf("Mean mismatch: Want %v, got %v", trueMean, ev)
}
}
示例10: SuffStat
// SuffStat computes the sufficient statistics of set of samples to update
// the distribution. The sufficient statistics are stored in place, and the
// effective number of samples are returned.
//
// The exponential distribution has one sufficient statistic, the average rate
// of the samples.
//
// If weights is nil, the weights are assumed to be 1, otherwise panics if
// len(samples) != len(weights). Panics if len(suffStat) != 1.
func (Exponential) SuffStat(samples, weights, suffStat []float64) (nSamples float64) {
if len(weights) != 0 && len(samples) != len(weights) {
panic("dist: slice size mismatch")
}
if len(suffStat) != 1 {
panic("exponential: wrong suffStat length")
}
if len(weights) == 0 {
nSamples = float64(len(samples))
} else {
nSamples = floats.Sum(weights)
}
mean := stat.Mean(samples, weights)
suffStat[0] = 1 / mean
return nSamples
}
示例11: SuffStat
// SuffStat computes the sufficient statistics of set of samples to update
// the distribution. The sufficient statistics are stored in place, and the
// effective number of samples are returned.
//
// The exponential distribution has one sufficient statistic, the average rate
// of the samples.
//
// If weights is nil, the weights are assumed to be 1, otherwise panics if
// len(samples) != len(weights). Panics if len(suffStat) != 1.
func (Exponential) SuffStat(samples, weights, suffStat []float64) (nSamples float64) {
if len(weights) != 0 && len(samples) != len(weights) {
panic(badLength)
}
if len(suffStat) != 1 {
panic(badSuffStat)
}
if len(weights) == 0 {
nSamples = float64(len(samples))
} else {
nSamples = floats.Sum(weights)
}
mean := stat.Mean(samples, weights)
suffStat[0] = 1 / mean
return nSamples
}
示例12: SuffStat
// SuffStat computes the sufficient statistics of a set of samples to update
// the distribution. The sufficient statistics are stored in place, and the
// effective number of samples are returned.
//
// The normal distribution has two sufficient statistics, the mean of the samples
// and the standard deviation of the samples.
//
// If weights is nil, the weights are assumed to be 1, otherwise panics if
// len(samples) != len(weights). Panics if len(suffStat) != 2.
func (Normal) SuffStat(samples, weights, suffStat []float64) (nSamples float64) {
lenSamp := len(samples)
if len(weights) != 0 && len(samples) != len(weights) {
panic("dist: slice size mismatch")
}
if len(suffStat) != 2 {
panic("dist: incorrect suffStat length")
}
if len(weights) == 0 {
nSamples = float64(lenSamp)
} else {
nSamples = floats.Sum(weights)
}
mean := stat.Mean(samples, weights)
suffStat[0] = mean
// Use Moment and not StdDev because we want it to be uncorrected
variance := stat.Moment(2, samples, mean, weights)
suffStat[1] = math.Sqrt(variance)
return nSamples
}
示例13: compareNormal
func compareNormal(t *testing.T, want *distmv.Normal, batch *mat64.Dense, weights []float64) {
dim := want.Dim()
mu := want.Mean(nil)
sigma := want.CovarianceMatrix(nil)
n, _ := batch.Dims()
if weights == nil {
weights = make([]float64, n)
for i := range weights {
weights[i] = 1
}
}
for i := 0; i < dim; i++ {
col := mat64.Col(nil, i, batch)
ev := stat.Mean(col, weights)
if math.Abs(ev-mu[i]) > 1e-2 {
t.Errorf("Mean mismatch: Want %v, got %v", mu[i], ev)
}
}
cov := stat.CovarianceMatrix(nil, batch, weights)
if !mat64.EqualApprox(cov, sigma, 1.5e-1) {
t.Errorf("Covariance matrix mismatch")
}
}
示例14: testFullDist
// testFullDist tests all of the functions of a fullDist.
func testFullDist(t *testing.T, f fullDist, i int) {
tol := 1e-1
const n = 1e6
xs := make([]float64, n)
for i := range xs {
xs[i] = f.Rand()
}
sortedXs := make([]float64, n)
copy(sortedXs, xs)
sort.Float64s(sortedXs)
tmp := make([]float64, n)
// Mean check.
mean := stat.Mean(xs, nil)
if !floats.EqualWithinAbsOrRel(mean, f.Mean(), tol, tol) {
t.Errorf("Mean mismatch case %v: want: %v, got: %v", i, mean, f.Mean())
} else {
mean = f.Mean()
}
// Median check.
median := stat.Quantile(0.5, stat.Empirical, sortedXs, nil)
if !floats.EqualWithinAbsOrRel(median, f.Median(), tol, tol) {
t.Errorf("Median mismatch case %v: want: %v, got: %v", i, median, f.Median())
}
// Variance check.
variance := stat.Variance(xs, nil)
if !floats.EqualWithinAbsOrRel(variance, f.Variance(), tol, tol) {
t.Errorf("Variance mismatch case %v: want: %v, got: %v", i, mean, f.Variance())
} else {
variance = f.Variance()
}
std := math.Sqrt(variance)
if !floats.EqualWithinAbsOrRel(std, f.StdDev(), tol, tol) {
t.Errorf("StdDev mismatch case %v: want: %v, got: %v", i, mean, f.StdDev())
} else {
std = f.StdDev()
}
// Entropy check.
for i, x := range xs {
tmp[i] = -f.LogProb(x)
}
entropy := stat.Mean(tmp, nil)
if !floats.EqualWithinAbsOrRel(entropy, f.Entropy(), tol, tol) {
t.Errorf("Entropy mismatch case %v: want: %v, got: %v", i, entropy, f.Entropy())
}
// Excess Kurtosis check.
for i, x := range xs {
tmp[i] = math.Pow(x-mean, 4)
}
mu4 := stat.Mean(tmp, nil)
kurtosis := mu4/(variance*variance) - 3
if !floats.EqualWithinAbsOrRel(kurtosis, f.ExKurtosis(), tol, tol) {
t.Errorf("ExKurtosis mismatch case %v: want: %v, got: %v", i, kurtosis, f.ExKurtosis())
}
// Skewness check.
for i, x := range xs {
tmp[i] = math.Pow(x-mean, 3)
}
mu3 := stat.Mean(tmp, nil)
skewness := mu3 / math.Pow(std, 3)
if !floats.EqualWithinAbsOrRel(skewness, f.Skewness(), tol, tol) {
t.Errorf("ExKurtosis mismatch case %v: want: %v, got: %v", i, skewness, f.Skewness())
}
// Quantile, CDF, and survival check.
for i, p := range []float64{0.1, 0.25, 0.5, 0.75, 0.9} {
x := f.Quantile(p)
cdf := f.CDF(x)
estCDF := stat.CDF(x, stat.Empirical, sortedXs, nil)
if !floats.EqualWithinAbsOrRel(cdf, estCDF, tol, tol) {
t.Errorf("CDF mismatch case %v: want: %v, got: %v", i, estCDF, cdf)
}
if !floats.EqualWithinAbsOrRel(cdf, p, tol, tol) {
t.Errorf("Quantile/CDF mismatch case %v: want: %v, got: %v", i, p, cdf)
}
if math.Abs(1-cdf-f.Survival(x)) > 1e-14 {
t.Errorf("Survival/CDF mismatch case %v: want: %v, got: %v", i, 1-cdf, f.Survival(x))
}
}
// Prob and LogProb check.
m := 1001
bins := make([]float64, m)
dividers := make([]float64, m)
floats.Span(bins, 0, 1)
for i, v := range bins {
dividers[i] = f.Quantile(v)
}
counts := stat.Histogram(nil, dividers, sortedXs, nil)
// Test PDf against normalized count
for i, v := range counts {
v /= float64(n)
at := f.Quantile((bins[i] + bins[i+1]) / 2)
//.........这里部分代码省略.........
示例15: checkMean
func checkMean(t *testing.T, i int, x []float64, m meaner, tol float64) {
mean := stat.Mean(x, nil)
if !floats.EqualWithinAbsOrRel(mean, m.Mean(), tol, tol) {
t.Errorf("Mean mismatch case %v: want: %v, got: %v", i, mean, m.Mean())
}
}