import numpy as np
import matplotlib.pyplot as plt
import ebayesthresh
ref
Import
import for code check
from scipy.stats import norm, stats
from scipy.optimize import minimize, root_scalar
from statsmodels.robust.scale import mad
import pywt
beta.cauchy
Function beta for the quasi-Cauchy prior
- Description
Given a value or vector x of values, find the value(s) of the function \(\beta(x) = g(x)/\phi(x) − 1\), where \(g\) is the convolution of the quasi-Cauchy with the normal density \(\phi(x)\).
x가 입력되면 코시 분포와 정규 분포를 혼합해서 함수 베타 구하기
= np.array([-2,1,0,-4,8,50]) x
= norm.pdf(x)
phix phix
array([5.39909665e-02, 2.41970725e-01, 3.98942280e-01, 1.33830226e-04,
5.05227108e-15, 0.00000000e+00])
- x의 확률밀도함수pdf 구하기
= (x != 0)
j j
array([ True, True, False, True, True, True])
- 0이 아닌 인덱스만 얻기
= x
beta beta
array([-2, 1, 0, -4, 8, 50])
= np.where(j == False, -1/2, beta)
beta beta
array([-2. , 1. , -0.5, -4. , 8. , 50. ])
- j가 False 즉 0이면 -1/2를 넣고 아니면 beta값 그대로 넣기
= (norm.pdf(0) / phix[j] - 1) / (x[j] ** 2) - 1
beta[j] beta
RuntimeWarning: divide by zero encountered in divide
beta[j] = (norm.pdf(0) / phix[j] - 1) / (x[j] ** 2) - 1
array([ 5.97264025e-01, -3.51278729e-01, -5.00000000e-01, 1.85247374e+02,
1.23379625e+12, inf])
0의 확률밀도함수pdf에서 x의 확률밀도함수phix로 나누어서 1을 빼고 그것을 x 중 0이 아닌 값들에 제곱한 값으로 나눠 1을 빼기
\(\beta(x) = \begin{cases} x & \text{ if } x = 0 \\ \frac{\frac{\phi(0)}{\phi(x)}-1}{x^2} - 1 & \\ \text{ if } x \ne 0\end{cases}\)
R code
<- function(x) {
beta.cauchy #
# Find the function beta for the mixed normal prior with Cauchy
# tails. It is assumed that the noise variance is equal to one.
#
<- dnorm(x)
phix <- (x != 0)
j <- x
beta !j] <- -1/2
beta[<- (dnorm(0)/phix[j] - 1)/x[j]^2 - 1
beta[j] return(beta)
}
결과
- Python
-2,1,0,-4,8,50])) ebayesthresh.beta_cauchy(np.array([
/home/csy/Dropbox/sy_hub/posts/1_Note/ebayesthresh/utils.py:21: RuntimeWarning: divide by zero encountered in divide
beta[j] = (norm.pdf(0) / phix[j] - 1) / (x[j] ** 2) - 1
array([ 5.97264025e-01, -3.51278729e-01, -5.00000000e-01, 1.85247374e+02,
1.23379625e+12, inf])
- R
> beta.cauchy(c(-2,1,0,-4,8,50))
1] 5.972640e-01 -3.512787e-01 -5.000000e-01 1.852474e+02 1.233796e+12 Inf [
- 무한히 꼬리가 긴 분포를 가진 코시분포
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import cauchy
# 모수 설정
= [0.0, 0.0, -2.0] # 위치 모수
loc_values = [0.5, 1.0, 2.0] # 척도 모수
scale_values
# 그래프 그리기
= np.linspace(-10, 10, 1000)
x =(12, 6))
plt.figure(figsize
# PDF 그리기
1, 2, 1)
plt.subplot(for loc, scale in zip(loc_values, scale_values):
=f'loc={loc}, scale={scale}')
plt.plot(x, cauchy.pdf(x, loc, scale), label'Cauchy PDF')
plt.title('x')
plt.xlabel('Probability Density')
plt.ylabel(
plt.legend()
# CDF 그리기
1, 2, 2)
plt.subplot(for loc, scale in zip(loc_values, scale_values):
=f'loc={loc}, scale={scale}')
plt.plot(x, cauchy.cdf(x, loc, scale), label'Cauchy CDF')
plt.title('x')
plt.xlabel('Cumulative Probability')
plt.ylabel(
plt.legend()
plt.show()
beta.laplace
Function beta for the Laplace prior
- Description
Given a single value or a vector of \(x\) and \(s\), find the value(s) of the function \(\beta(x; s, a) = \frac{g(x; s, a)}{f_n(x; 0, s)}−1\), where \(f_n(x; 0, s)\) is the normal density with mean \(0\) and standard deviation \(s\), and \(g\) is the convolution of the Laplace density with scale parameter a, \(γa(\mu)\), with the normal density \(f_n(x; µ, s)\) with mean mu and standard deviation \(s\).
평균이 \(\mu\)이며, 스케일 파라메터 a를 가진 라플라스와 정규분포의 합성함수 \(g\)와 평균이 0이고 표준편차가s인 f로 계산되는 함수 베타
# x = np.array([-2,1,0,-4,8,50])
= 2.14
x = 1
s # s = np.arange(1, 7)
= 0.5 a
- s는 표준편차
- a는 Laplaxe prior모수, 이 값이 클수록 부포 모양이 뾰족해진다.
= np.abs(x)
x x
2.14
- 확률변수 x에 절대값 취하고
= x/s + s*a
xpa xpa
2.64
= x/s - s*a
xma xma
1.6400000000000001
= 1/xpa
rat1 rat1
0.3787878787878788
< 35 xpa
True
< 35] rat1[xpa
array([0.37878788])
- 표준정규분포 누적 분포 함수cdf / 표준정규분포 밀도함수 pdf
if isinstance(rat1, (int, float, str, bool)) and xpa < 35:
= norm.cdf(-xpa) / norm.pdf(xpa)
rat1 elif isinstance(rat1, (int, float, str, bool)) and xpa > 35:
= rat1
rat1 else:
< 35] = norm.cdf(-xpa[xpa < 35]) / norm.pdf(xpa[xpa < 35]) rat1[xpa
rat1
0.33890303155852075
= 1/np.abs(xma)
rat2 rat2
0.6097560975609756
if isinstance(xma, (int, float, str, bool)) and xma > 35:
= 35
xma elif isinstance(xma, (int, float, str, bool)) and xma < 35:
= xma
xma else:
> 35] = 35 xma[xma
xma
1.6400000000000001
if isinstance(rat1, (int, float, str, bool)) and xma > -35:
= norm.cdf(xma) / norm.pdf(xma)
rat2 elif isinstance(rat1, (int, float, str, bool)) and xma < -35:
= rat2
rat2 else:
> -35] = norm.cdf(xma[xma > -35]) / norm.pdf(xma[xma > -35]) rat2[xma
rat2
9.133199429298308
- beta = \(\frac{g(x; s, a)}{f_n(x; 0, s)}−1\)
- g = 스케일 파라메터 a를 가진 라플라스 밀도의 convolution + \(\gamma (u ; a)\)
- 단, The Laplace density is given by \(\gamma (u ; a) = \frac{1}{2} a e^{-a |u|}\) and is also known as the double exponential density.
= (a * s) / 2 * (rat1 + rat2) - 1
beta beta
1.3680256152142074
- R code
<- function(x, s = 1, a = 0.5) {
beta.laplace #
# The function beta for the Laplace prior given parameter a and s (sd)
#
<- abs(x)
x <- x/s + s*a
xpa <- x/s - s*a
xma <- 1/xpa
rat1 < 35] <- pnorm( - xpa[xpa < 35])/dnorm(xpa[xpa < 35])
rat1[xpa <- 1/abs(xma)
rat2 > 35] <- 35
xma[xma > -35] <- pnorm(xma[xma > -35])/dnorm(xma[xma > -35])
rat2[xma <- (a * s) / 2 * (rat1 + rat2) - 1
beta return(beta)
}
결과
- Python
-2,1,0,-4,8,50]),s=1) ebayesthresh.beta_laplace(np.array([
array([ 8.89852030e-001, -3.80041717e-001, -5.61817772e-001,
2.85459467e+002, 1.02698062e+012, 6.34453954e+265])
-2,s=1,a=0.5) ebayesthresh.beta_laplace(
0.8898520296511427
-2,1,0,-4,8,50]),s=np.arange(1, 7)) ebayesthresh.beta_laplace(np.array([
array([ 8.89852030e-01, -3.03909953e-01, -2.26276543e-01, -3.97301589e-02,
1.53950123e-01, 5.64694733e+06])
- R
> beta.laplace(c(-2,1,0,-4,8,50), s=1)
1] 8.898520e-01 -3.800417e-01 -5.618178e-01 2.854595e+02 1.026981e+12 6.344540e+265
[> beta.laplace(-2, s=1, a=0.5)
1] 0.889852
[> beta.laplace(c(-2,1,0,-4,8,50), s=1:6, a=1)
1] 0.890821055 -0.129919250 -0.086229104 -0.005203193 0.054213718 112.493576777 [
라플라스 분포, 정규분포와 유사한 모습, 하지만 중간이 뾰족한 모형
\(f(x) = \frac{1}{2b} \exp \big[ \frac{-|x - \mu|}{b} \big]\)
import numpy as np
import matplotlib.pyplot as plt
# 라플라스 분포의 확률 밀도 함수 정의
def laplace_pdf(x, mu, b):
return 1/(2*b) * np.exp(-np.abs(x - mu) / b)
# 파라미터 설정
= 0 # 평균
mu = 1 # 스케일 파라미터
b
# x 값 생성
= np.linspace(-10, 10, 1000)
x_values
# 라플라스 분포의 확률 밀도 함수 계산
= laplace_pdf(x_values, mu, b)
pdf_values
# 그래프 그리기
=(8, 6))
plt.figure(figsize=f'Laplace Distribution (mu={mu}, b={b})')
plt.plot(x_values, pdf_values, label'Laplace Distribution')
plt.title('x')
plt.xlabel('Probability Density')
plt.ylabel(
plt.legend()True)
plt.grid( plt.show()
cauchy_medzero
the objective function that has to be zeroed, component by component, to find the posterior median when the quasi-Cauchy prior is used. x is the parameter vector, z is the data vector, w is the weight x and z may be scalars
- quasi-Cauchy prior에서 사후 중앙값 찾기 위한 함수
- x,z는 벡터일수도 있고, 스칼라일 수 도 있다.
# x = np.array([-2,1,0,-4,8,50])
= 4
x # z = np.array([1,0,2,3,-1,-1])
= 5
z = 0.5 w
= z - x
hh hh
1
= norm.pdf(hh)
dnhh dnhh
0.24197072451914337
= norm.cdf(hh) - z * dnhh + ((z * x - 1) * dnhh * norm.cdf(-x)) / norm.pdf(x)
yleft yleft
0.7194871459755684
= 1 + np.exp(-z**2 / 2) * (z**2 * (1 / w - 1) - 1)
yright2 yright2
1.00008943967613
/ 2 - yleft yright2
-0.21944242613750342
- R코드
<- function(x, z, w) {
cauchy.medzero #
# the objective function that has to be zeroed, component by
# component, to find the posterior median when the quasi-Cauchy prior
# is used. x is the parameter vector, z is the data vector, w is the
# weight x and z may be scalars
#
<- z - x
hh <- dnorm(hh)
dnhh <- pnorm(hh) - z * dnhh + ((z * x - 1) * dnhh * pnorm( - x))/
yleft dnorm(x)
<- 1 + exp( - z^2/2) * (z^2 * (1/w - 1) - 1)
yright2 return(yright2/2 - yleft)
}
결과
- Python
- 벡터, 스칼라일때 가능한지 확인
-2,1,0,-4,8,50]),np.array([1,0,2,3,-1,-1]),0.5) ebayesthresh.cauchy_medzero(np.array([
/home/csy/Dropbox/sy_hub/posts/1_Note/ebayesthresh/utils.py:203: RuntimeWarning: invalid value encountered in divide
yleft = norm.cdf(hh) - z * dnhh + ((z * x - 1) * dnhh * norm.cdf(-x)) / norm.pdf(x)
array([-0.25356559, 0. , -0.09859737, -0.45556313, 0.5 ,
nan])
4,5,0.5) ebayesthresh.cauchy_medzero(
-0.21944242613750342
- R
> cauchy.medzero(c(-2,1,0,-4,8,50),c(1,0,2,3,-1,-1),0.5)
1] -0.25356559 0.00000000 -0.09859737 -0.45556313 0.50000000 NaN
[> cauchy.medzero(4,5,0.5)
1] -0.2194424 [
cauchy_threshzero
- cauchy 임계값 찾기 위한 것
- 아래에서 반환되는 y가 0에 가깝도록 만들어주는 z를 찾는 과정
= np.array([1,0,2,3,-1,-1])
z # z = 0
# w = 0.5
= np.array([0.5,0.4,0.3,0.2,0,0.1]) w
if isinstance(z, (list)):
= np.array(z) z
= norm.cdf(z) - z * norm.pdf(z) - 1/2 - (z**2 * np.exp(-z**2/2) * (1/w - 1))/2
y y
RuntimeWarning: divide by zero encountered in divide
y = norm.cdf(z) - z * norm.pdf(z) - 1/2 - (z**2 * np.exp(-z**2/2) * (1/w - 1))/2
array([-0.20389131, 0. , -0.26229672, 0.28539262, -inf,
-2.82876199])
\(y = pnorm(z) - z \times dnorm(z) - \frac{1}{2} - \frac{z^2 \exp (\frac{z^2}{2})(\frac{1}{w} - 1)}{2}\)
- R 코드
<- function(z, w) {
cauchy.threshzero #
# The objective function that has to be zeroed to find the Cauchy
# threshold. z is the putative threshold vector, w is the weight w
# can be a vector
#
<- pnorm(z) - z * dnorm(z) - 1/2 -
y ^2 * exp( - z^2/2) * (1/w - 1))/2
(zreturn(y)
}
결과
- Python
1,0,2,3,-1,-1]),0.5) ebayesthresh.cauchy_threshzero(np.array([
array([-0.20389131, 0. , 0.09859737, 0.43536407, -0.40263935,
-0.40263935])
1,0,2,3,-1,-1]),np.array([0.5,0.4,0.3,0.2,0,0.1])) ebayesthresh.cauchy_threshzero(np.array([
/home/csy/Dropbox/sy_hub/posts/1_Note/ebayesthresh/utils.py:211: RuntimeWarning: divide by zero encountered in divide
y = norm.cdf(z) - z * norm.pdf(z) - 1/2 - (z**2 * np.exp(-z**2/2) * (1/w - 1))/2
array([-0.20389131, 0. , -0.26229672, 0.28539262, -inf,
-2.82876199])
- R
> cauchy.threshzero(c(1,0,2,3,-1,-1),0.5)
1] -0.20389131 0.00000000 0.09859737 0.43536407 -0.40263935 -0.40263935
[cauchy.threshzero(c(1,0,2,3,-1,-1), c(0.5,0.4,0.3,0.2,0,0.1))
1] -0.2038913 0.0000000 -0.2622967 0.2853926 -Inf -2.8287620 [
ebayesthresh
= np.concatenate([np.random.normal(0, 1, 90), np.random.normal(5, 1, 10)])
x = 1
sdev ="laplace"
prior=0.5
a=False
bayesfac=False
verbose="median"
threshrule=True
universalthresh=None stabadjustment
= prior[0:1]
pr pr
'l'
if sdev is None:
= ebayesthresh.mad(x, center=0)
sdev = True
stabadjustment_condition elif len(np.atleast_1d(sdev)) == 1:
if stabadjustment is not None:
raise ValueError("Argument stabadjustment is not applicable when variances are homogeneous.")
if np.isnan(sdev):
= ebayesthresh.mad(x, center=0)
sdev = True
stabadjustment_condition else:
if pr == "c":
raise ValueError("Standard deviation has to be homogeneous for Cauchy prior.")
if len(sdev) != len(x):
raise ValueError("Standard deviation has to be homogeneous or have the same length as observations.")
if stabadjustment is None:
= False
stabadjustment = stabadjustment stabadjustment_condition
stabadjustment_condition
True
if stabadjustment_condition:
= np.mean(sdev)
m_sdev = sdev / m_sdev
s = x / m_sdev
x else:
= sdev s
a
0.5
if (pr == "l") and np.isnan(a):
= ebayesthresh.wandafromx(x, s, universalthresh)
pp = pp['w']
w = pp['a']
a else:
= ebayesthresh.wfromx(x, s, prior=prior, a=a, universalthresh=universalthresh) w
w
0.3200062747569716
a
0.5
s
1.0
if pr != "m" or verbose:
= ebayesthresh.tfromw(w, s, prior=prior, bayesfac=bayesfac, a=a)[0]
tt if stabadjustment_condition:
= tt * m_sdev
tcor else:
= tt tcor
if threshrule == "median":
= ebayesthresh.postmed(x, s, w, prior=prior, a=a)
muhat elif threshrule == "mean":
= ebayesthresh.postmean(x, s, w, prior=prior, a=a)
muhat elif threshrule == "hard":
= ebayesthresh.threshld(x, tt)
muhat elif threshrule == "soft":
= ebayesthresh.threshld(x, tt, hard=False)
muhat elif threshrule == "none":
= None
muhat else:
raise ValueError(f"Unknown threshold rule: {threshrule}")
muhat
array([ 0. , 1.34329661, 0. , -0. , -0. ,
-0. , -0. , 0. , 0. , 0. ,
-0. , 0. , -0. , 0. , -0. ,
0. , 0. , 0. , 0. , 0. ,
-0. , 0. , -0. , 0. , 0. ,
-0. , -0. , 0. , -0. , -0. ,
-0. , 0. , -0. , 0. , -0. ,
-0. , -0. , -0. , 0. , 0. ,
0. , -0. , 0. , 0. , -0. ,
0. , 0. , 0. , -0. , 0. ,
1.38682932, -0. , -0. , 0. , 0. ,
-0. , -0. , -0. , -0. , 0. ,
-0. , -0. , -0. , -0. , -0. ,
-1.94078819, -0. , 0. , -0. , -0. ,
-0. , -0. , -0. , 0. , -0. ,
-0. , -0. , 0. , 0. , -0. ,
0. , -0. , -0. , 0. , -0. ,
-0. , 0. , -0. , -0. , -0. ,
7.23887854, 4.28032392, 4.38326438, 3.63340097, 4.98882396,
5.10584713, 4.50526557, 3.20664347, 4.56728499, 4.21031715])
if stabadjustment_condition:
= muhat * m_sdev muhat
muhat
array([ 0. , 1.34329661, 0. , -0. , -0. ,
-0. , -0. , 0. , 0. , 0. ,
-0. , 0. , -0. , 0. , -0. ,
0. , 0. , 0. , 0. , 0. ,
-0. , 0. , -0. , 0. , 0. ,
-0. , -0. , 0. , -0. , -0. ,
-0. , 0. , -0. , 0. , -0. ,
-0. , -0. , -0. , 0. , 0. ,
0. , -0. , 0. , 0. , -0. ,
0. , 0. , 0. , -0. , 0. ,
1.38682932, -0. , -0. , 0. , 0. ,
-0. , -0. , -0. , -0. , 0. ,
-0. , -0. , -0. , -0. , -0. ,
-1.94078819, -0. , 0. , -0. , -0. ,
-0. , -0. , -0. , 0. , -0. ,
-0. , -0. , 0. , 0. , -0. ,
0. , -0. , -0. , 0. , -0. ,
-0. , 0. , -0. , -0. , -0. ,
7.23887854, 4.28032392, 4.38326438, 3.63340097, 4.98882396,
5.10584713, 4.50526557, 3.20664347, 4.56728499, 4.21031715])
if not verbose:
muhatelse:
= {
retlist 'muhat': muhat,
'x': x,
'threshold.sdevscale': tt,
'threshold.origscale': tcor,
'prior': prior,
'w': w,
'a': a,
'bayesfac': bayesfac,
'sdev': sdev,
'threshrule': threshrule
}if pr == "c":
del retlist['a']
if threshrule == "none":
del retlist['muhat']
retlist
R코드
<- function (x, prior = "laplace", a = 0.5, bayesfac = FALSE,
ebayesthresh sdev = NA, verbose = FALSE, threshrule = "median",
universalthresh = TRUE, stabadjustment) {
#
# Given a vector of data x, find the marginal maximum likelihood
# estimator of the mixing weight w, and apply an appropriate
# thresholding rule using this weight.
#
# If the prior is laplace and a=NA, then the inverse scale parameter
# is also found by MML.
#
# Standard deviation sdev can be a vector (heterogeneous variance) or
# a single value (homogeneous variance). If sdev=NA, then it is
# estimated using the function mad(x). Heterogeneous variance is
# allowed only for laplace prior currently.
#
# The thresholding rules allowed are "median", "mean", "hard", "soft"
# and "none"; if "none" is used, then only the parameters are worked
# out.
#
# If hard or soft thresholding is used, the argument "bayesfac"
# specifies whether to use the bayes factor threshold or the
# posterior median threshold.
#
# If universalthresh=TRUE, the thresholds will be upper bounded by
# universal threshold adjusted by standard deviation; otherwise,
# weight w will be searched in [0, 1].
#
# If stabadjustment=TRUE, the observations and standard deviations
# will be first divided by the mean of all given standard deviations
# in case of inefficiency due to large value of standard
# deviation. In the case of homogeneous variance, the standard
# deviations will be normalized to 1 automatically.
#
# If verbose=TRUE then the routine returns a list with several
# arguments, including muhat which is the result of the
# thresholding. If verbose=FALSE then only muhat is returned.
#
# Find the standard deviation if necessary and estimate the parameters
<- substring(prior, 1, 1)
pr
if(length(sdev) == 1) {
if(!missing(stabadjustment))
stop(paste("Argument stabadjustment is not applicable when",
"variances are homogeneous."))
if(is.na(sdev)) {
<- mad(x, center = 0)
sdev
}= TRUE
stabadjustment_condition else{
} if(pr == "c")
stop("Standard deviation has to be homogeneous for Cauchy prior.")
if(length(sdev) != length(x))
stop(paste("Standard deviation has to be homogeneous or has the",
"same length as observations."))
if(missing(stabadjustment))
<- FALSE
stabadjustment = stabadjustment
stabadjustment_condition
}
if (stabadjustment_condition) {
<- mean(sdev)
m_sdev <- sdev/m_sdev
s <- x/m_sdev
x else { s <- sdev }
}
if ((pr == "l") & is.na(a)) {
<- wandafromx(x, s, universalthresh)
pp <- pp$w
w <- pp$a
a
}else
<- wfromx(x, s, prior = prior, a = a, universalthresh)
w if(pr != "m" | verbose) {
<- tfromw(w, s, prior = prior, bayesfac = bayesfac, a = a)
tt if(stabadjustment_condition) {
<- tt * m_sdev
tcor else {
} <- tt
tcor
}
}if(threshrule == "median")
<- postmed(x, s, w, prior = prior, a = a)
muhat if(threshrule == "mean")
<- postmean(x, s, w, prior = prior, a = a)
muhat if(threshrule == "hard")
<- threshld(x, tt)
muhat if(threshrule == "soft")
<- threshld(x, tt, hard = FALSE)
muhat if(threshrule == "none")
<- NA
muhat
# Now return desired output
if(stabadjustment_condition) {
<- muhat * m_sdev
muhat
}if(!verbose)
return(muhat)
<- list(muhat = muhat, x = x, threshold.sdevscale = tt,
retlist threshold.origscale = tcor, prior = prior, w = w,
a = a, bayesfac = bayesfac, sdev = sdev,
threshrule = threshrule)
if(pr == "c")
<- retlist[-7]
retlist if(threshrule == "none")
<- retlist[-1]
retlist return(retlist)
}
결과
- Python
=np.concatenate([np.random.normal(0, 1, 90), np.random.normal(5, 1, 10)]), sdev = 1) ebayesthresh.ebayesthresh(x
array([-0. , 0. , -0. , 0. , -0. ,
0. , 0. , -0. , 0. , 0. ,
0. , 0. , 0. , 0. , -0. ,
0. , 0. , 0. , -0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
-0. , 0. , 0. , 0. , 0.83181094,
-0. , -0. , -0. , 0. , -0. ,
-0. , -0. , -0. , -0. , -0. ,
-0. , 0. , -0. , 0. , -2.89468824,
-0. , 0. , -0. , 0. , 0. ,
0. , 0. , -0. , -0. , -0. ,
0. , -0. , 0. , 0. , 0. ,
0. , -0. , -0. , -0. , 0. ,
-0. , 0. , -0. , -0. , 0. ,
0. , -0. , 0. , -0. , -0. ,
0. , -0. , 0. , 0. , -0. ,
-0. , -0. , -0. , 0. , -0. ,
-0. , -0. , 0. , 0. , 0. ,
2.99222302, 4.60992013, 3.68760194, 3.16329967, 3.93131817,
4.87250552, 3.47040456, 3.28492248, 5.09923275, 4.07454939])
- R
> ebayesthresh(x = rnorm(100, c(rep(0,90), rep(5,10))),
+ prior = "laplace", sdev = 1)
1] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
[15] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
[29] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
[43] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
[57] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
[71] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 -0.4480064
[85] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 5.1534865 6.2732386 4.4612851 5.9931848 4.5828731 4.6154038 4.8247775 3.6219544
[99] 4.4480080 5.4084453 [
ebayesthresh.wavelet
R코드
<-
ebayesthresh.wavelet function (xtr, vscale = "independent", smooth.levels = Inf,
prior = "laplace", a = 0.5, bayesfac = FALSE,
threshrule = "median") {
<- class(xtr)
xcl if (class(xcl) == "dwt " && length(xcl) > 1) {
<- ebayesthresh.wavelet.splus(xtr, vscale, smooth.levels,
xtr
prior, a, bayesfac, threshrule)return(xtr)
}if (xcl == "wd") {
<- ebayesthresh.wavelet.wd(xtr, vscale, smooth.levels, prior,
xtr
a, bayesfac, threshrule)return(xtr)
}if (xcl == "dwt" || xcl=="modwt") {
<- ebayesthresh.wavelet.dwt(xtr, vscale, smooth.levels,
xtr
prior, a, bayesfac, threshrule)return(xtr)
}stop("Unknown wavelet transform type; no smoothing performed")
return(xtr)
}
결과
Python
R
ebayesthresh.wavelet.dwt
= np.concatenate([np.random.normal(0, 1, 90), np.random.normal(5, 1, 10)])
data = len(data)
n = data[::2] - data[1::2]
level1 = level1[::2] - level1[1::2]
level2 = level2[::2] + level2[::2]
approximation
# Create a list of wavelet coefficients for each level
= [approximation, level2, level1]
x_dwt x_dwt
[array([ 1.4836815 , 2.5460343 , -1.306332 , 1.31723319, -2.55056374,
5.08187009, -0.83959987, -3.73303537, 2.84676984, 4.08970155,
7.31864861, -0.9809799 , 1.16959737]),
array([ 0.74184075, -1.25167459, 1.27301715, -2.76291158, -0.653166 ,
-1.67217533, 0.6586166 , 2.04732727, -1.27528187, 1.14064582,
2.54093505, -3.09342206, -0.41979993, -1.14225822, -1.86651769,
1.20685697, 1.42338492, 1.94833839, 2.04485077, -3.20509258,
3.6593243 , -0.02077669, -0.49048995, 1.47917861, 0.58479869]),
array([ 0.47868431, -0.26315645, 0.31878975, 1.57046433, 1.13616511,
-0.13685204, 0.4748337 , 3.23774528, 1.05890084, 1.71206684,
-0.47029185, 1.20188347, -1.31592992, -1.97454652, 0.96714734,
-1.08017994, -1.82143641, -0.54615454, -1.44897433, -2.58962015,
0.4946747 , -2.04626034, -1.47187691, 1.62154515, -0.0205145 ,
0.39928543, -2.00296752, -0.86070929, -1.84618836, 0.02032933,
1.884058 , 0.67720103, 0.44194886, -0.98143606, 0.12154746,
-1.82679094, 0.41042574, -1.63442503, -2.87055564, 0.33453694,
2.38411684, -1.27520746, 1.96971695, 1.99049364, 0.07055637,
0.56104632, 3.25314351, 1.7739649 , 0.34259736, -0.24220132])]
# x_dwt
="independent"
vscale=float('inf')
smooth_levels="laplace"
prior=0.5
a=False,
bayesfac="median" threshrule
= len(x_dwt) - 1
nlevs nlevs
2
= min(nlevs, smooth_levels)
slevs slevs
2
if isinstance(vscale, str):
= vscale[0].lower()
vs if vs == "i":
= ebayesthresh.mad(x_dwt[0], center=0)
vscale if vs == "l":
= None vscale
for j in range(slevs):
= ebayesthresh.ebayesthresh(x_dwt[j], prior=prior, a=a, bayesfac=bayesfac,
x_dwt[j] =vscale, verbose=False, threshrule=threshrule) sdev
Unexpected exception formatting exception. Falling back to standard exception
Traceback (most recent call last):
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3398, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-23-7fc60b3f848b>", line 2, in <cell line: 1>
x_dwt[j] = EbayesThresh.ebayesthresh(x_dwt[j], prior=prior, a=a, bayesfac=bayesfac,
File "/home/csy/Dropbox/sy_hub/posts/1_Note/EbayesThresh/utils.py", line 100, in ebayesthresh
tt = tfromw(w, s, prior=prior, bayesfac=bayesfac, a=a)[0]
File "/home/csy/Dropbox/sy_hub/posts/1_Note/EbayesThresh/utils.py", line 713, in tfromw
TypeError: object of type 'numpy.float64' has no len()
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 1993, in showtraceback
stb = self.InteractiveTB.structured_traceback(
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/IPython/core/ultratb.py", line 1118, in structured_traceback
return FormattedTB.structured_traceback(
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/IPython/core/ultratb.py", line 1012, in structured_traceback
return VerboseTB.structured_traceback(
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/IPython/core/ultratb.py", line 865, in structured_traceback
formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/IPython/core/ultratb.py", line 818, in format_exception_as_a_whole
frames.append(self.format_record(r))
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/IPython/core/ultratb.py", line 736, in format_record
result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/stack_data/utils.py", line 144, in cached_property_wrapper
value = obj.__dict__[self.func.__name__] = self.func(obj)
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/stack_data/core.py", line 734, in lines
pieces = self.included_pieces
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/stack_data/utils.py", line 144, in cached_property_wrapper
value = obj.__dict__[self.func.__name__] = self.func(obj)
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/stack_data/core.py", line 681, in included_pieces
pos = scope_pieces.index(self.executing_piece)
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/stack_data/utils.py", line 144, in cached_property_wrapper
value = obj.__dict__[self.func.__name__] = self.func(obj)
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/stack_data/core.py", line 660, in executing_piece
return only(
File "/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/executing/executing.py", line 190, in only
raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0
x_dwt
[array([-3.97210513, -4.0287387 , 0.03812726, 1.98061813, -0.20712241,
-1.94263661, -3.20640339, -1.52713519, 3.36779351, 3.58251383,
-1.57906486, 0.59753974, -3.67964951]),
array([-1.98605257, -2.03819191, -2.01436935, -0.23136287, 0.01906363,
2.48481231, 0.99030907, 0.80823516, -0.1035612 , 2.53250102,
-0.9713183 , -2.40256619, -1.60320169, -1.59307862, -0.7635676 ,
0.88817825, 1.68389675, -1.00917812, 1.79125692, 1.06362513,
-0.78953243, 1.3533214 , 0.29876987, -1.80890545, -1.83982476]),
array([-0.90884553, 1.07720704, -1.07828366, 0.95990824, -1.96840177,
0.04596758, -0.70551575, -0.47415287, -1.34986168, -1.36892531,
0.40485746, -2.07995485, 0.54909349, -0.44121557, 0.1563934 ,
-0.65184176, -0.43358298, -0.33002178, 3.00455902, 0.472058 ,
0.79130012, 1.76261842, -1.14949085, 1.25307534, -2.67917775,
-1.07597605, 0.58772537, 2.18080399, -0.33286881, 0.43069878,
-0.90429141, -1.79246966, 1.98651968, 0.30262293, -2.10891536,
-1.09973725, 1.15116106, -0.64009585, 1.62734043, 0.56371529,
-1.83981846, -1.05028603, 0.0481002 , -1.30522119, -0.23335818,
-0.53212805, -1.39336681, 0.41553864, -1.58935501, 0.25046975])]
R코드
<-
ebayesthresh.wavelet.dwt function (x.dwt, vscale = "independent", smooth.levels = Inf,
prior = "laplace", a = 0.5, bayesfac = FALSE,
threshrule = "median") {
<- length(x.dwt)-1
nlevs <- min(nlevs, smooth.levels)
slevs if (is.character(vscale)) {
<- substring(vscale, 1, 1)
vs if (vs == "i")
<- mad(x.dwt[[1]])
vscale if (vs == "l")
<- NA
vscale
}for (j in 1:slevs) {
<-
x.dwt[[j]] ebayesthresh(x.dwt[[j]], prior, a, bayesfac, vscale,
FALSE, threshrule)
}return(x.dwt)
}
결과
- Python
ebayesthresh.ebayesthresh_wavelet_dwt(x_dwt)
[array([ 0., 0., -0., 0., -0., 0., -0., -0., 0., 0., 0., -0., 0.]),
array([ 0., -0., 0., -0., -0., -0., 0., 0., -0., 0., 0., -0., -0.,
-0., -0., 0., 0., 0., 0., -0., 0., -0., -0., 0., 0.]),
array([ 0.47868431, -0.26315645, 0.31878975, 1.57046433, 1.13616511,
-0.13685204, 0.4748337 , 3.23774528, 1.05890084, 1.71206684,
-0.47029185, 1.20188347, -1.31592992, -1.97454652, 0.96714734,
-1.08017994, -1.82143641, -0.54615454, -1.44897433, -2.58962015,
0.4946747 , -2.04626034, -1.47187691, 1.62154515, -0.0205145 ,
0.39928543, -2.00296752, -0.86070929, -1.84618836, 0.02032933,
1.884058 , 0.67720103, 0.44194886, -0.98143606, 0.12154746,
-1.82679094, 0.41042574, -1.63442503, -2.87055564, 0.33453694,
2.38411684, -1.27520746, 1.96971695, 1.99049364, 0.07055637,
0.56104632, 3.25314351, 1.7739649 , 0.34259736, -0.24220132])]
- R
확인 불가
> ebayesthresh.wavelet.dwt(x.dwt=x_dwt)
in ebayesthresh.wavelet.dwt(x.dwt = x_dwt) :
Error function "ebayesthresh.wavelet.dwt" could not find
ebayesthresh.wavelet.splus
= np.concatenate([np.random.normal(0, 1, 90), np.random.normal(5, 1, 10)])
data = len(data)
n = data[::2] - data[1::2]
level1 = level1[::2] - level1[1::2]
level2 = level2[::2] + level2[::2]
approximation
# Create a list of wavelet coefficients for each level
= [approximation, level2, level1]
x_dwt x_dwt
[array([ 1.99404911, -6.15701427, -3.99558976, -0.38391355,
-6.6016326 , -2.81007867, -17.52592145, -7.07965432,
-1.3653538 , -4.08821234, -2.66406197, -4.45001631,
-2.9337468 ]),
array([ 0.99702455, 2.06672342, -3.07850714, 3.12833675, -1.99779488,
0.82687941, -0.19195677, 0.73432995, -3.3008163 , 2.46746019,
-1.40503934, 0.45749726, -8.76296072, -1.28596396, -3.53982716,
3.44395235, -0.6826769 , -0.11415533, -2.04410617, 0.87670091,
-1.33203099, -1.5215132 , -2.22500816, 2.61347947, -1.4668734 ]),
array([ 0.26815468, -0.72886987, -0.47285771, -2.53958113, -2.01598499,
1.06252215, 2.86786944, -0.26046731, -2.45177824, -0.45398336,
0.9342025 , 0.10732309, -0.43102009, -0.23906331, 0.55549546,
-0.17883449, -1.11553065, 2.18528565, 1.17389913, -1.29356106,
-0.51051515, 0.89452418, -0.37799399, -0.83549125, -5.18513165,
3.57782907, -0.40410394, 0.88186002, -0.82947095, 2.71035621,
2.01683439, -1.42711796, -1.16240604, -0.47972914, -1.00833728,
-0.89418195, -1.4092027 , 0.63490347, 0.72968632, -0.1470146 ,
0.02978361, 1.3618146 , -0.03523747, 1.48627572, -1.02615173,
1.19885643, 0.62995346, -1.983526 , -0.21994701, 1.24692638])]
="independent"
vscale=float('inf')
smooth_levels="laplace"
prior=0.5
a=False
bayesfac="median" threshrule
= len(x_dwt)
nlevs nlevs
3
= min(nlevs, smooth_levels)
slevs slevs
3
if isinstance(vscale, str):
= vscale[0].lower()
vs if vs == "i":
= ebayesthresh.mad(x_dwt[-1]) # Use the last level for vscale
vscale elif vs == "l":
= None vscale
vscale
1.28961200438462
for j in range(nlevs - slevs + 1, nlevs + 1):
- 1] = ebayesthresh.ebayesthresh(x_dwt[j - 1], prior=prior, a=a, bayesfac=bayesfac,
x_dwt[j =vscale, verbose=False, threshrule=threshrule) sdev
x_dwt
[array([ 2.54686727, -3.93681469, 2.84493444, -0.22684436, -0. ,
0.69626776, -0.1937811 , -5.05610077, 0.73042895, 4.06889481,
0.09960291, 1.5871375 , 0.43377755]),
array([ 0. , -0. , -0. , 0. , 0. ,
-0. , -0. , -1.88673589, -0. , -0. ,
0. , -0. , -0. , 0. , -0. ,
-0. , 0. , 0. , 0. , 0. ,
0. , -0. , 0. , -2.86961148, 0. ]),
array([ 0., -0., 0., 0., -0., -0., 0., 0., 0., -0., -0., 0., 0.,
0., -0., 0., -0., -0., -0., 0., 0., 0., -0., 0., 0., 0.,
0., -0., -0., 0., 0., 0., -0., -0., 0., 0., 0., -0., 0.,
0., 0., 0., -0., -0., 0., -0., -0., 0., -0., -0.])]
R코드
<-
ebayesthresh.wavelet.splus function (x.dwt, vscale = "independent", smooth.levels = Inf,
prior = "laplace", a = 0.5, bayesfac = FALSE,
threshrule = "median") {
<- attributes(x.dwt)$n.levels
nlevs <- min(nlevs, smooth.levels)
slevs if (is.character(vscale)) {
<- substring(vscale, 1, 1)
vs if (vs == "i")
<- mad(x.dwt[[nlevs + 1]])
vscale if (vs == "l")
<- NA
vscale
}for (j in ((nlevs - slevs + 2):(nlevs + 1)))
<-
x.dwt[[j]] ebayesthresh(as.vector(x.dwt[[j]]), prior, a, bayesfac,
FALSE, threshrule)
vscale, return(x.dwt)
}
결과
- Python
=x_dwt) ebayesthresh.ebayesthresh_wavelet_splus(x_dwt
- R
확인 불가
> ebayesthresh.wavelet.splus(x.dwt=x_dwt)
in ebayesthresh.wavelet.splus(x.dwt = x_dwt) :
Error function "ebayesthresh.wavelet.splus" could not find
ebayesthresh.wavelet.wd
def get_mother_wavelet_coefficients(data, level):
# Perform wavelet decomposition
= pywt.wavedec(data, 'db1', level=level)
coeffs
# Access the coefficients at the desired resolution level
= coeffs[level]
mother_wavelet_coeffs
return mother_wavelet_coeffs
# Example usage:
= [1, 2, 3, 4, 5, 6, 7, 8]
data = 2
level = get_mother_wavelet_coefficients(data, level) mother_wavelet_coeffs
mother_wavelet_coeffs
array([-0.70710678, -0.70710678, -0.70710678, -0.70710678])
= pywt.Wavelet('db1')
x_wd x_wd
pywt._extensions._pywt.Wavelet(name='db1', filter_bank=([0.7071067811865476, 0.7071067811865476], [-0.7071067811865476, 0.7071067811865476], [0.7071067811865476, 0.7071067811865476], [0.7071067811865476, -0.7071067811865476]))
="independent"
vscale=float('inf')
smooth_levels="laplace"
prior=0.5
a=False
bayesfac="median" threshrule
= x_wd['nlevels']
nlevs nlevs
= min(nlevs - 1, smooth_levels)
slevs slevs
if isinstance(vscale, str):
= vscale[0].lower()
vs if vs == "i":
= ebayesthresh.mad(get_mother_wavelet_coefficients(x_wd,-1)) # Use the last level for vscale
vscale elif vs == "l":
= None vscale
for j in range(nlevs - slevs, nlevs - 1):
= ebayesthresh(x_wd.d[j], prior=prior, a=a, bayesfac=bayesfac,
x_wd.d[j] =vscale, verbose=False, threshrule=threshrule) sdev
x_wd
R코드
<-
ebayesthresh.wavelet.wd function (x.wd, vscale = "independent", smooth.levels = Inf,
prior = "laplace", a = 0.5, bayesfac = FALSE,
threshrule = "median") {
<- x.wd$nlevels
nlevs <- min(nlevs - 1, smooth.levels)
slevs if (is.character(vscale)) {
<- substring(vscale, 1, 1)
vs if (vs == "i")
<- mad(accessD(x.wd, level = nlevs - 1))
vscale if (vs == "l")
<- NA
vscale
}for (j in (nlevs - slevs):(nlevs - 1)) {
<- putD(x.wd, level = j,
x.wd v = ebayesthresh(accessD(x.wd, level = j), prior, a,
FALSE, threshrule))
bayesfac, vscale,
}return(x.wd)
}
결과
- Python
ebayesthresh.ebayesthresh_wavelet_wd(x_wd)
- R
확인 불가
> ebayesthresh.wavelet.wd(x.dwt=x_dwt)
in ebayesthresh.wavelet.wd(x.dwt = x_dwt) :
Error function "ebayesthresh.wavelet.wd" could not find
isotone
Isotonic Regression은 입력 변수에 따른 출력 변수의 단조 증가(monotonic increasing) 또는 감소(monotonic decreasing) 패턴을 찾는 방법
= ebayesthresh.beta_cauchy(np.array([-2,1,0,-4]))
beta = np.ones(len(beta))
w = w + 1/beta
aa = w + aa
x = 1/aa**2
wt = False increasing
= len(x) nn
if nn == 1:
= x x
if not increasing:
= -x x
= np.arange(1, nn+1)
ip = np.diff(x)
dx = len(x) nx
= np.arange(nx)[(np.concatenate((dx <= 0, [False])) & np.concatenate(([True], dx > 0)))]
jmax = np.arange(nx)[(np.concatenate((dx > 0, [True])) & np.concatenate(([False], dx <= 0)))] jmin
=0
jb= np.arange(jmax[jb], jmin[jb]+1)
ind = np.sum(wt[ind]) wtn
= np.sum(wt[ind] * x[ind]) / wtn x[jmax[jb]]
= wtn wt[jmax[jb]]
+1:jmin[jb]+1] = np.nan x[jmax[jb]
# Clean up within iteration, eliminating the parts of sequences that
# were set to NA
= ~np.isnan(x)
ind = x[ind]
x = wt[ind]
wt = ip[ind]
ip = np.diff(x)
dx = len(x) nx
# Final cleanup: reconstruct z at all points by repeating the pooled
# values the appropriate number of times
= np.zeros(nn, dtype=int)
jj - 1] = 1
jj[ip = x[np.cumsum(jj) - 1] z
if not increasing:
= -z z
z
array([3.67430141, 0.76041105, 0.76041105, 0.76041105])
R코드
<- function(x, wt = rep(1, length(x)), increasing = FALSE) {
isotone #
# find the weighted least squares isotone fit to the
# sequence x, the weights given by the sequence wt
#
# if increasing == TRUE the curve is set to be increasing,
# otherwise to be decreasing
#
# the vector ip contains the indices on the original scale of the
# breaks in the regression at each stage
#
<- length(x)
nn if(nn == 1)
return(x)
if(!increasing)
<- - x
x <- (1:nn)
ip <- diff(x)
dx <- length(x)
nx while((nx > 1) && (min(dx) < 0)) {
#
# do single pool-adjacent-violators step
#
# find all local minima and maxima
#
<- (1:nx)[c(dx <= 0, FALSE) & c(TRUE, dx > 0)]
jmax <- (1:nx)[c(dx > 0, TRUE) & c(FALSE, dx <= 0)]
jmin # do pav step for each pair of maxima and minima
#
# add up weights within subsequence that is pooled
# set first element of subsequence to the weighted average
# the first weight to the sum of the weights within the subsequence
# and remainder of the subsequence to NA
#
for(jb in (1:length(jmax))) {
<- (jmax[jb]:jmin[jb])
ind <- sum(wt[ind])
wtn <- sum(wt[ind] * x[ind])/wtn
x[jmax[jb]] <- wtn
wt[jmax[jb]] + 1):jmin[jb]] <- NA
x[(jmax[jb]
}#
# clean up within iteration, eliminating the parts of sequences that
# were set to NA
#
<- !is.na(x)
ind <- x[ind]
x <- wt[ind]
wt <- ip[ind]
ip <- diff(x)
dx <- length(x)
nx
}#
# final cleanup: reconstruct z at all points by repeating the pooled
# values the appropriate number of times
#
<- rep(0, nn)
jj <- 1
jj[ip] <- x[cumsum(jj)]
z if(!increasing)
<- - z
z return(z)
}
결과
- Python
= ebayesthresh.beta_cauchy(np.array([-2,1,0,-4]))
beta = np.ones(len(beta))
w = w + 1/beta
aa = w + aa
ps = 1/aa**2
ww = ebayesthresh.isotone(ps, ww, increasing = False)
wnew wnew
[3.67430141208924, 0.760411047043364, 0.760411047043364, 0.760411047043364]
R
> beta <- beta.cauchy(c(-2,1,0,-4))
> w <- rep(1, length(x))
> aa = w + 1/beta
> ps = w + aa
> ww = 1/aa**2
> wnew = isotone(ps, ww, increasing = FALSE)
> wnew
1] 3.674301 0.760411 0.760411 0.760411 [
laplace_threshzero
= np.array([-2,1,0,-4,8,50])
x = 1
s = 0.5
w = 0.5 a
= min(a, 20)
a a
0.5
if isinstance(x, list):
= []
z for elem in x:
= elem / s - s * a
xma = norm.cdf(xma) - (1 / a) * (1 / s * norm.pdf(xma)) * (1 / w + ebayesthresh.beta_laplace(elem, s, a))
z_add
z.append(z_add)else:
= x / s - s * a
xma = norm.cdf(xma) - (1 / a) * (1 / s * norm.pdf(xma)) * (1 / w + ebayesthresh.beta_laplace(x, s, a)) z
xma
array([-2.5, 0.5, -0.5, -4.5, 7.5, 49.5])
z
array([-0.09509872, -0.44919982, -0.70413065, -0.00918596, 0.5 ,
1. ])
R코드
<- function(x, s = 1, w = 0.5, a = 0.5) {
laplace.threshzero #
# The function that has to be zeroed to find the threshold with the
# Laplace prior. Only allow a < 20 for input value.
#
<- min(a, 20)
a <- x/s - s*a
xma <- pnorm(xma) - 1/a * (1/s*dnorm(xma)) * (1/w + beta.laplace(x, s, a))
z return(z)
}
결과
- Python
-2,1,0,-4,8,50]), s = 1, w = 0.5, a = 0.5) ebayesthresh.laplace_threshzero(np.array([
array([-0.09509872, -0.44919982, -0.70413065, -0.00918596, 0.5 ,
1. ])
-5, s = 1, w = 0.5, a = 0.5) ebayesthresh.laplace_threshzero(
-0.0033691679532916346
- R
> laplace.threshzero(c(-2,1,0,-4,8,50), s = 1, w = 0.5, a = 0.5)
1] -0.095098724 -0.449199824 -0.704130654 -0.009185958 0.500000000 1.000000000
[> laplace.threshzero(-5, s = 1, w = 0.5, a = 0.5)
1] -0.003369168 [
negloglik_laplace
Marginal negative log likelihood function for laplace prior.
- 라플라스 프라이어에 대한 한계음의로그우도함수 계산
= np.array([0.5,0.6,0.3])
xpar = np.array([1,2,3,4,5])
xx = np.array([1])
ss = np.sqrt(2 * np.log(len(np.array([1,2,3,4,5])))) * 1
tlo = np.array([0,0,0]) thi
= xpar[1]
a a
0.6
= ebayesthresh.wfromt(thi, ss, a=a)
wlo wlo
array([1., 1., 1.])
= ebayesthresh.wfromt(tlo, ss, a=a)
whi whi
array([0.44528236])
= np.max(wlo)
wlo wlo
1.0
= np.min(whi)
whi whi
0.44528236200207744
= np.sum(np.log(1 + (xpar[0] * (whi - wlo) + wlo) *
loglik
ebayesthresh.beta_laplace(xx, ss, a)))-loglik
-16.797273710172206
R코드
<- function(xpar, xx, ss, tlo, thi) {
negloglik.laplace #
# Marginal negative log likelihood function for laplace prior.
# Constraints for thresholds need to be passed externally.
#
# xx :data
# xpar :vector of two parameters:
# xpar[1] : a value between [0, 1] which will be adjusted to range of w
# xpar[2] : inverse scale (rate) parameter ("a")
# ss :vector of standard deviations
# tlo :lower bound of thresholds
# thi :upper bound of thresholds
#
<- xpar[2]
a
# Calculate the range of w given a, using negative monotonicity
# between w and t
<- wfromt(thi, ss, a = a)
wlo <- wfromt(tlo, ss, a = a)
whi <- max(wlo)
wlo <- min(whi)
whi <- sum(log(1 + (xpar[1] * (whi - wlo) + wlo) *
loglik beta.laplace(xx, ss, a)))
return(-loglik)
}
결과
- Python
= np.array([0.5,0.6,0.3])
xpar = np.array([1,2,3,4,5])
xx = np.array([1])
ss = np.sqrt(2 * np.log(len(np.array([1,2,3,4,5])))) * 1
tlo = np.array([0,0,0]) thi
ebayesthresh.negloglik_laplace(xpar, xx, ss, tlo, thi)
-16.797273710172206
- R
> xpar <- c(0.5, 0.6, 0.3)
> xx <- c(1, 2, 3, 4, 5)
> ss <- c(1)
> tlo <- sqrt(2 * log(length(c(1, 2, 3, 4, 5)))) * 1
> thi <- c(0, 0, 0)
> negloglik.laplace(xpar, xx, ss, tlo, thi)
1] -16.79727 [
postmean
Given a single value or a vector of data and sampling standard deviations (sd equals 1 for Cauchy prior), find the corresponding posterior mean estimate(s) of the underlying signal value(s).
- 적절한 사후 평균 찾기
= np.array([-2,1,0,-4,8,50])
x = 1
s = 0.5
w = "cauchy"
prior # prior = "laplace"
= 0.5 a
= prior[0:1]
pr pr
'c'
if pr == "l":
= ebayesthresh.postmean_laplace(x, s, w, a=a)
mutilde elif pr == "c":
if np.any(s != 1):
raise ValueError("Only standard deviation of 1 is allowed for Cauchy prior.")
= ebayesthresh.postmean_cauchy(x, w)
mutilde else:
raise ValueError("Unknown prior type.")
mutilde
array([ 0, 0, 0, -3, 7, 49])
R코드
<- function(x, s = 1, w = 0.5, prior = "laplace", a = 0.5) {
postmean #
# Find the posterior mean for the appropriate prior for
# given x, s (sd), w and a.
#
<- substring(prior, 1, 1)
pr if(pr == "l")
<- postmean.laplace(x, s, w, a = a)
mutilde if(pr == "c"){
if(any(s != 1))
stop(paste("Only standard deviation of 1 is allowed",
"for Cauchy prior."))
<- postmean.cauchy(x, w)
mutilde
}return(mutilde)
}
결과
- Python
-2,1,0,-4,8,50]), s=1, w = 0.5, prior = "laplace", a = 0.5) ebayesthresh.postmean(np.array([
array([-1.01158962, 0.27095331, 0. , -3.48800924, 7.5 ,
49.5 ])
- R
> postmean(c(-2,1,0,-4,8,50), s=1, w = 0.5, prior = "laplace", a = 0.5)
1] -1.0115896 0.2709533 0.0000000 -3.4880092 7.5000000 49.5000000 [
postmean_cauchy
Find the posterior mean for the quasi-Cauchy prior with mixing weight w given data x, which may be a scalar or a vector.
- quasi-Cauch에 대한 사후 평균 구하기
= np.array([-2,1,0,-4,8,50], dtype=float)
x = 0.5 w
= x muhat
= (x == 0)
ind ind
array([False, False, True, False, False, False])
= x[~ind]
x x
array([-2., 1., -4., 8., 50.])
= np.exp(-x**2/2)
ex ex
array([1.35335283e-01, 6.06530660e-01, 3.35462628e-04, 1.26641655e-14,
0.00000000e+00])
= w * (x - (2 * (1 - ex))/x)
z z
array([-0.56766764, 0.10653066, -1.75008387, 3.875 , 24.98 ])
= z / (w * (1 - ex) + (1 - w) * ex * x**2)
z z
array([-0.80748973, 0.21306132, -3.48264328, 7.75 , 49.96 ])
~ind] = z
muhat[ muhat
array([-0.80748973, 0.21306132, 0. , -3.48264328, 7.75 ,
49.96 ])
R코드
<- function(x, w) {
postmean.cauchy #
# Find the posterior mean for the quasi-Cauchy prior with mixing
# weight w given data x, which may be a scalar or a vector.
#
<- x
muhat <- (x == 0)
ind <- x[!ind]
x <- exp( - x^2/2)
ex <- w * (x - (2 * (1 - ex))/x)
z <- z/(w * (1 - ex) + (1 - w) * ex * x^2)
z !ind] <- z
muhat[return(muhat)
}
결과
- Python
-2,1,0,-4,8,50], dtype=float),0.5) ebayesthresh.postmean_cauchy(np.array([
array([-0.80748973, 0.21306132, 0. , -3.48264328, 7.75 ,
49.96 ])
- R
> postmean.cauchy(c(-2,1,0,-4,8,50),0.5)
1] -0.8074897 0.2130613 0.0000000 -3.4826433 7.7500000 49.9600000 [
postmean.laplace
Find the posterior mean for the double exponential prior for given \(x, s (sd), w\), and \(a\).
- 이전 지수 분포에 대한 사후 평균
= np.array([-2,1,0,-4,8,50])
x = 1
s = 0.5
w = 0.5 a
= min(a, 20)
a a
0.5
= ebayesthresh.wpost_laplace(w, x, s, a)
w_post w_post
array([0.65396152, 0.38270015, 0.30467782, 0.99652125, 1. ,
1. ])
= np.sign(x)
sx sx
array([-1, 1, 0, -1, 1, 1])
= np.abs(x)
x x
array([ 2, 1, 0, 4, 8, 50])
= x/s + s*a
xpa xpa
array([ 2.5, 1.5, 0.5, 4.5, 8.5, 50.5])
= x/s - s*a
xma xma
array([ 1.5, 0.5, -0.5, 3.5, 7.5, 49.5])
> 35] = 35
xpa[xpa xpa
array([ 2.5, 1.5, 0.5, 4.5, 8.5, 35. ])
< -35] = -35
xma[xma xma
array([ 1.5, 0.5, -0.5, 3.5, 7.5, 49.5])
= norm.cdf(xma)
cp1 cp1
array([0.9331928 , 0.69146246, 0.30853754, 0.99976737, 1. ,
1. ])
= norm.cdf(-xpa)
cp2 cp2
array([6.20966533e-003, 6.68072013e-002, 3.08537539e-001, 3.39767312e-006,
9.47953482e-018, 1.12491071e-268])
= np.exp(np.minimum(2 * a * x, 100))
ef ef
array([7.38905610e+00, 2.71828183e+00, 1.00000000e+00, 5.45981500e+01,
2.98095799e+03, 5.18470553e+21])
= x - a * s**2 * (2 * cp1 / (cp1 + ef * cp2) - 1)
postmean_cond postmean_cond
array([ 1.54686413, 0.70800417, 0. , 3.50018552, 7.5 ,
49.5 ])
* w_post * postmean_cond sx
array([-1.01158962, 0.27095331, 0. , -3.48800924, 7.5 ,
49.5 ])
R코드
<- function(x, s = 1, w = 0.5, a = 0.5) {
postmean.laplace #
# Find the posterior mean for the double exponential prior for
# given x, s (sd), w and a.
#
# Only allow a < 20 for input value.
<- min(a, 20)
a
# First find the probability of being non-zero
<- wpost.laplace(w, x, s, a)
wpost
# Now find the posterior mean conditional on being non-zero
<- sign(x)
sx <- abs(x)
x <- x/s + s*a
xpa <- x/s - s*a
xma > 35] <- 35
xpa[xpa < -35] <- -35
xma[xma
<- pnorm(xma)
cp1 <- pnorm( - xpa)
cp2 <- exp(pmin(2 * a * x, 100))
ef <- x - a * s^2 * ( 2 * cp1/(cp1 + ef * cp2) - 1)
postmeancond
# Calculate posterior mean and return
return(sx * wpost * postmeancond)
}
결과
- Python
-2,1,0,-4,8,50])) ebayesthresh.postmean_laplace(np.array([
array([-1.01158962, 0.27095331, 0. , -3.48800924, 7.5 ,
49.5 ])
- R
> postmean.laplace(c(-2,1,0,-4,8,50))
1] -1.0115896 0.2709533 0.0000000 -3.4880092 7.5000000 49.5000000 [
postmed
Description
Given a single value or a vector of data and sampling standard deviations (sd is 1 for Cauchy prior), find the corresponding posterior median estimate(s) of the underlying signal value(s).
사후 확률 중앙값 추정치 구하기
= np.array([1.5, 2.5, 3.5])
x = 1
s = 0.5
w = "laplace"
prior = 0.5 a
= prior[0:1]
pr pr
'l'
if pr == "l":
= ebayesthresh.postmed_laplace(x, s, w, a)
muhat elif pr == "c":
if np.any(s != 1):
raise ValueError("Only standard deviation of 1 is allowed for Cauchy prior.")
= ebayesthresh.postmed_cauchy(x, w)
muhat else:
raise ValueError(f"Unknown prior: {prior}")
muhat
array([0. , 1.73413235, 2.97815763])
R코드
<- function (x, s = 1, w = 0.5, prior = "laplace", a = 0.5) {
postmed #
# Find the posterior median for the appropriate prior for
# given x, s (sd), w and a.
#
<- substring(prior, 1, 1)
pr if(pr == "l")
<- postmed.laplace(x, s, w, a)
muhat if(pr == "c") {
if(any(s != 1))
stop(paste("Only standard deviation of 1 is allowed",
"for Cauchy prior."))
<- postmed.cauchy(x, w)
muhat
}return(muhat)
}
결과
- Python
= np.array([1.5, 2.5, 3.5])) ebayesthresh.postmed(x
array([0. , 1.73413235, 2.97815763])
- R
postmed(x=c(1.5, 2.5, 3.5))
1] 0.000000 1.734132 2.978158 [
postmed_cauchy
= np.array([10, 15, 20, 25])
x = 0.5 w
= len(x)
nx nx
4
= np.full(nx, np.nan)
zest zest
array([nan, nan, nan, nan])
= np.full(nx, w)
w w
array([0.5, 0.5, 0.5, 0.5])
= np.abs(x)
ax ax
array([10, 15, 20, 25])
= (ax < 20)
j j
array([ True, True, False, False])
~j] = ax[~j] - 2 / ax[~j]
zest[ zest
array([ nan, nan, 19.9 , 24.92])
if isinstance(np.max(ax[j]), (int, float, np.int64)):
= np.array([np.max(ax[j])] * 4) a
a
array([15, 15, 15, 15])
if np.sum(j) > 0:
= ebayesthresh.vecbinsolv(zf=np.zeros(np.sum(j)), fun=ebayesthresh.cauchy_medzero, tlo=0, thi=np.max(ax[j]), z=ax[j], w=w[j]) zest[j]
< 1e-7] = 0
zest[zest zest
array([ 9.80064279, 14.86686095, 19.9 , 24.92 ])
= np.sign(x) * zest
zest zest
array([ 9.80064279, 14.86686095, 19.9 , 24.92 ])
R코드
<- function(x, w) {
postmed.cauchy #
# find the posterior median of the Cauchy prior with mixing weight w,
# pointwise for each of the data points x
#
<- length(x)
nx <- rep(NA, length(x))
zest <- rep(w, length.out = nx)
w <- abs(x)
ax <- (ax < 20)
j !j] <- ax[!j] - 2/ax[!j]
zest[if(sum(j) > 0) {
<- vecbinsolv(zf = rep(0, sum(j)), fun = cauchy.medzero,
zest[j] tlo = 0, thi = max(ax[j]), z = ax[j],
w = w[j])
}< 1e-007] <- 0
zest[zest <- sign(x) * zest
zest return(zest)
결과
- Python
=np.array([10, 15, 20, 25]), w=0.5) ebayesthresh.postmed_cauchy(x
array([ 9.80064279, 14.86686095, 19.9 , 24.92 ])
- R
> postmed.cauchy(x=c(10, 15, 20, 25),w=0.5)
1] 9.800643 14.866861 19.900000 24.920000 [
postmed_laplace
= np.array([1.5, 2.5, 3.5])
x = 1
s = 0.5
w = 0.5 a
= min(a, 20)
a a
0.5
= np.sign(x)
sx sx
array([1., 1., 1.])
= np.abs(x)
x x
array([1.5, 2.5, 3.5])
= x / s - s * a
xma xma
array([1., 2., 3.])
= 1 / a * (1 / s * norm.pdf(xma)) * (1 / w + ebayesthresh.beta_laplace(x, s, a))
zz zz
array([0.95559333, 0.60482943, 0.50871315])
> 25] = 0.5
zz[xma zz
array([0.95559333, 0.60482943, 0.50871315])
= norm.ppf(np.minimum(zz, 1))
mucor mucor
array([1.70169084, 0.26586765, 0.02184237])
= sx * np.maximum(0, xma - mucor) * s
muhat muhat
array([0. , 1.73413235, 2.97815763])
R코드
<- function(x, s = 1, w = 0.5, a = 0.5) {
postmed.laplace #
# Find the posterior median for the Laplace prior for
# given x (observations), s (sd), w and a.
#
# Only allow a < 20 for input value
<- min(a, 20)
a
# Work with the absolute value of x, and for x > 25 use the approximation
# to dnorm(x-a)*beta.laplace(x, a)
<- sign(x)
sx <- abs(x)
x <- x/s - s*a
xma <- 1/a * (1/s*dnorm(xma)) * (1/w + beta.laplace(x, s, a))
zz > 25] <- 1/2
zz[xma <- qnorm(pmin(zz, 1))
mucor <- sx * pmax(0, xma - mucor) * s
muhat return(muhat)
}
결과
- Python
= np.array([1.5, 2.5, 3.5])) ebayesthresh.postmed_laplace(x
array([0. , 1.73413235, 2.97815763])
- R
> postmed.laplace(x=c(1.5, 2.5, 3.5), s = 1, w = 0.5, a = 0.5)
1] 0.000000 1.734132 2.978158 [
threshld
임계값 t를 이용해서 데이터 조정
= np.array(range(-5,5))
x =1.4
t=False hard
if hard:
= x * (np.abs(x) >= t)
z else:
= np.sign(x) * np.maximum(0, np.abs(x) - t) z
z
array([-3.6, -2.6, -1.6, -0.6, -0. , 0. , 0. , 0.6, 1.6, 2.6])
R코드
<- function(x, t, hard = TRUE) {
threshld #
# threshold the data x using threshold t
# if hard=TRUE use hard thresholding
# if hard=FALSE use soft thresholding
if(hard) z <- x * (abs(x) >= t) else {
<- sign(x) * pmax(0, abs(x) - t)
z
}return(z)
}
결과
- Python
range(-5,5)), t=1.4, hard=False) ebayesthresh.threshld(np.array(
array([-3.6, -2.6, -1.6, -0.6, -0. , 0. , 0. , 0.6, 1.6, 2.6])
- R
> threshld(as.array(seq(-5, 5)), t=1.4, hard=FALSE)
1] -3.6 -2.6 -1.6 -0.6 0.0 0.0 0.0 0.6 1.6 2.6 3.6 [
wandafromx
Given a vector of data and a single value or vector of sampling standard deviations, find the marginal maximum likelihood choice of both weight and scale factor under the Laplace prior
= np.array([-2,1,0,-4,8], dtype=float)
x = 1
s = True universalthresh
if universalthresh:
= np.sqrt(2 * np.log(len(x))) * s
thi else:
= np.inf thi
thi
1.7941225779941015
if isinstance(s, int):
= np.zeros(len(str(s)))
tlo else:
= np.zeros(len(s)) tlo
tlo
array([0.])
= np.array([0, 0.04])
lo lo
array([0. , 0.04])
= np.array([1, 3])
hi hi
array([1, 3])
= np.array([0.5, 0.5])
startpar startpar
array([0.5, 0.5])
negloglik_laplace 결과도 같음
round(5) ebayesthresh.negloglik_laplace(startpar, x, s, tlo, thi).
-32.32783
- R
> negloglik.laplace(startpar,x,s,thi,tlo)
1] -32.32783 [
if 'optim' in globals():
= minimize(ebayesthresh.negloglik_laplace, startpar, method='L-BFGS-B', bounds=[(lo[0], hi[0]), (lo[1], hi[1])], args=(x, s, tlo, thi))
result = result.x
uu else:
= minimize(ebayesthresh.negloglik_laplace, startpar, bounds=[(lo[0], hi[0]), (lo[1], hi[1])], args=(x, s, tlo, thi))
result = result.x uu
='L-BFGS-B',
minimize(ebayesthresh.negloglik_laplace, startpar, method=[(lo[0], hi[0]), (lo[1], hi[1])], args=(x, s, tlo, thi)).x bounds
array([0.69481312, 0.30010834])
='L-BFGS-B',
minimize(ebayesthresh.negloglik_laplace, startpar, method=[(lo[0], hi[0]), (lo[1], hi[1])], args=(x, s, thi, tlo)).fun.round(5) bounds
-33.07559
> optim(startpar, negloglik.laplace, method="L-BFGS-B",
+ lower = lo, upper = hi, xx = x, ss = s, thi = thi,
+ tlo = tlo)$par
1] 0.6948121 0.3001091
[> optim(startpar, negloglik.laplace, method="L-BFGS-B",
+ lower = lo, upper = hi, xx = x, ss = s, thi = thi,
+ tlo = tlo)$value
1] -33.07559 [
= uu[1]
a a
0.30010833999317726
= ebayesthresh.wfromt(thi, s, a=a)
wlo wlo
0.49762405565749024
= ebayesthresh.wfromt(tlo, s, a=a)
whi whi
array([1.])
= np.max(wlo)
wlo wlo
0.49762405565749024
= np.min(whi)
whi whi
1.0
여기서 uu[0]값이 R과 다름
= uu[0] * (whi - wlo) + wlo
w w
0.8466814522110238
R코드
<- function(x, s = 1, universalthresh = TRUE) {
wandafromx #
# Find the marginal max lik estimators of w and a given standard
# deviation s, using a bivariate optimization;
#
# If universalthresh=TRUE, the thresholds will be upper bounded by
# universal threshold adjusted by standard deviation. The threshold
# is constrained to lie between 0 and sqrt ( 2 log (n)) *
# s. Otherwise, threshold can take any nonnegative value;
#
# If running R, the routine optim is used; in S-PLUS the routine is
# nlminb.
#
# Range for thresholds
if(universalthresh) {
<- sqrt(2 * log(length(x))) * s
thi else{
} <- Inf
thi
}
<- rep(0, length(s))
tlo <- c(0,0.04)
lo <- c(1,3)
hi <- c(0.5,0.5)
startpar if (exists("optim")) {
<- optim(startpar, negloglik.laplace, method="L-BFGS-B",
uu lower = lo, upper = hi, xx = x, ss = s, thi = thi,
tlo = tlo)
<- uu$par
uu
}else {
<- nlminb(startpar, negloglik.laplace, lower = lo,
uu upper = hi, xx = x, ss = s, thi = thi, tlo = tlo)
<- uu$parameters
uu
}
<- uu[2]
a <- wfromt(thi, s, a = a)
wlo <- wfromt(tlo, s, a = a)
whi <- max(wlo)
wlo <- min(whi)
whi <- uu[1]*(whi - wlo) + wlo
w return(list(w=w, a=a))
}
결과
- Pythom
-2,1,0,-4,8,50], dtype=float)) ebayesthresh.wandafromx(np.array([
{'w': 1.0, 'a': 0.41641372425894785}
-2,1,0,-4,8], dtype=float)) ebayesthresh.wandafromx(np.array([
{'w': 0.8466814522110238, 'a': 0.30010833999317726}
- R
> wandafromx(c(-2,1,0,-4,8,50))
$w
1] 1
[
$a
1] 0.4163946
[> wandafromx(c(-2,1,0,-4,8))
$w
1] 0.8466808
[
$a
1] 0.3001091 [
Mad(Median Absolute Deviation)
중앙값 절대 편차, 분산이나 퍼진 정도 확인 가능
결과
- Python
1, 2, 3, 3, 4, 4, 4, 5, 5.5, 6, 6, 6.5, 7, 7, 7.5, 8, 9, 12, 52, 90])) ebayesthresh.mad(np.array([
2.9652
- R
> mad(c(1, 2, 3, 3, 4, 4, 4, 5, 5.5, 6, 6, 6.5, 7, 7, 7.5, 8, 9, 12, 52, 90))
1] 2.9652 [
wfromt
Description
Given a value or vector of thresholds and sampling standard deviations (sd equals 1 for Cauchy prior), find the mixing weight for which this is(these are) the threshold(s) of the posterior median estimator. If a vector of threshold values is provided, the vector of corresponding weights is returned.
주어진 임계값과 표준편차에 대해, posterior median estimator에서 이 임계값이 나오도록 하는 혼합 가중치를 계산하는 함수가 제공된다.
# tt = np.array([2,3,5])
= 2.14
tt = 1
s = 'laplace"'
prior = 0.5 a
= prior[0:1]
pr pr
'l'
if pr == "l":
= tt / s - s * a
tma = 1 / np.abs(tma)
wi if isinstance(wi, (int, float, str, bool)) and tma > -35:
= norm.cdf(tma) / norm.pdf(tma)
wi elif isinstance(wi, (int, float, str, bool)) and tma < -35:
= wi
wi else:
> -35] = norm.cdf(tma[tma > -35])/norm.pdf(tma[tma > -35])
wi[tma = a * s * wi - ebayesthresh.beta_laplace(tt, s, a) wi
tma
1.6400000000000001
wi
3.1985740994349467
if pr == "c":
= norm.pdf(tt)
dnz = 1 + (norm.cdf(tt) - tt * dnz - 1/2) / (np.sqrt(np.pi/2) * dnz * tt**2)
wi if isinstance(wi, np.ndarray):
for i in range(len(wi)):
if not np.isfinite(wi[i]):
= 1
wi[i] else:
if not np.isfinite(wi):
= 1 wi
1 / wi
0.31263931017782515
- R코드
<- function(tt, s = 1, prior = "laplace", a = 0.5) {
wfromt #
# Find the weight that has posterior median threshold tt,
# given s (sd) and a.
#
<- substring(prior, 1, 1)
pr if(pr == "l"){
<- tt/s - s*a
tma <- 1/abs(tma)
wi > -35] <- pnorm(tma[tma > -35])/dnorm(tma[tma > -35])
wi[tma <- a * s * wi - beta.laplace(tt, s, a)
wi
}if(pr == "c") {
<- dnorm(tt)
dnz <- 1 + (pnorm(tt) - tt * dnz - 1/2)/
wi sqrt(pi/2) * dnz * tt^2)
(!is.finite(wi)] <- 1
wi[
}1/wi
}
결과
- Python
2,3,5]),prior='cachy') ebayesthresh.wfromt(np.array([
array([4.22963403e-01, 9.33799336e-02, 9.31590884e-05])
2,prior='cachy') ebayesthresh.wfromt(
0.4229634032635055
2,prior='laplace') ebayesthresh.wfromt(
0.3686337675493349
- R
> wfromt(c(2,3,5),prior='cachy')
1] 4.229634e-01 9.337993e-02 9.315909e-05
[> wfromt(2,prior='cachy')
1] 0.4229634
[> wfromt(2,prior='laplace')
1] 0.3686338 [
wfromx
Description
The weight is found by marginal maximum likelihood. The search is over weights corresponding to threshold \(t_i\) in the range \([0, s_i \sqrt{2 log n}]\) if universalthresh=TRUE, where n is the length of the data vector and \((s_1, ..., s_n\)) (\(s_i\) is \(1\) for Cauchy prior) is the vector of sampling standard deviation of data (\(x_1, ..., x_n\)); otherwise, the search is over \([0, 1]\). The search is by binary search for a solution to the equation \(S(w) = 0\), where \(S\) is the derivative of the log likelihood. The binary search is on a logarithmic scale in \(w\). If the Laplace prior is used, the scale parameter is fixed at the value given for \(a\), and defaults to \(0.5\) if no value is provided. To estimate a as well as \(w\) by marginal maximum likelihood, use the routine wandafromx.
Suppose the vector \((x_1, \cdots, x_n)\) is such that \(x_i\) is drawn independently from a normal distribution with mean \(\theta_i\) and standard deviation \(s_i\) (\(s_i\) equals \(1\) for Cauchy prior). The prior distribution of the \(\theta_i\) is a mixture with probability \(1 − w\) of zero and probability \(w\) of a given symmetric heavy-tailed distribution. This routine finds the marginal maximum likelihood estimate of the parameter \(w\).
주어진 정규 분포 데이터에 대해 \(\theta_𝑖\)의 사전 분포가 주어진 상황에서, 모수 \(w\)의 최대우도 추정치를 계산하는 방법을 제공한다
= np.concatenate((np.repeat(0, 90), np.repeat(5, 10)))
s = np.random.normal(0, s, size=100)
x = "cauchy"
prior = 0.5
a = True universalthresh
= prior[0:1]
pr pr
'c'
if pr == "c":
= 1 s
if universalthresh:
= np.sqrt(2 * np.log(len(x))) * s
tuniv = ebayesthresh.wfromt(tuniv, s, prior, a)
wlo = np.max(wlo)
wlo else:
= 0 wlo
if pr == "l":
= ebayesthresh.beta_laplace(x, s, a)
beta elif pr == "c":
= ebayesthresh.beta_cauchy(x) beta
= 1
whi = np.minimum(beta, 1e20)
beta
= np.sum(beta / (1 + beta)) shi
if shi >= 0:
= 1 shi
= np.sum(beta / (1 + wlo * beta)) slo
if slo <= 0:
= wlo slo
for _ in range(1,31):
= np.sqrt(wlo * whi)
wmid = np.sum(beta / (1 + wmid * beta))
smid if smid == 0:
= wmid
smid if smid > 0:
= wmid
wlo else:
= wmid whi
* whi) np.sqrt(wlo
0.1397274377272373
- R코드
<- function (x, s = 1, prior = "laplace", a = 0.5,
wfromx universalthresh = TRUE) {
#
# Given the vector of data x and s (sd),
# find the value of w that zeroes S(w) in the
# range by successive bisection, carrying out nits harmonic bisections
# of the original interval between wlo and 1.
#
<- substring(prior, 1, 1)
pr if(pr == "c")
= 1
s if(universalthresh) {
<- sqrt(2 * log(length(x))) * s
tuniv <- wfromt(tuniv, s, prior, a)
wlo <- max(wlo)
wlo else
} = 0
wlo if(pr == "l")
<- beta.laplace(x, s, a)
beta if(pr == "c")
<- beta.cauchy(x)
beta <- 1
whi <- pmin(beta, 1e20)
beta <- sum(beta/(1 + beta))
shi if(shi >= 0)
return(1)
<- sum(beta/(1 + wlo * beta))
slo if(slo <= 0)
return(wlo)
for(j in (1:30)) {
<- sqrt(wlo * whi)
wmid <- sum(beta/(1 + wmid * beta))
smid if(smid == 0)
return(wmid)
if(smid > 0)
<- wmid
wlo else
<- wmid
whi
}return(sqrt(wlo * whi))
}
결과
- Python
= np.random.normal(0, np.concatenate((np.repeat(0, 90), np.repeat(5, 10))), size=100), prior = "cauchy") ebayesthresh.wfromx(x
0.08644292644513768
- R
> wfromx(x = rnorm(100, s = c(rep(0,90),rep(5,10))), prior = "cauchy")
1] 0.116067 [
wmonfromx
Given a vector of data, find the marginal maximum likelihood choice of weight sequence subject to the constraints that the weights are monotone decreasing
데이터에 대해 가중치 시퀀스를 선택하는 과정에서 조건이 주어지는데, 이 가중치 시퀀스는 각각의 가중치 값이 단조 감소해야 하며, 주어진 데이터에 대한 최대 우도를 갖도록 선택되어야 함.
= np.random.normal(0, 1, size=10)
xd = "laplace"
prior = 0.5
a = 1e-08
tol = 20 maxits
= prior[0:1]
pr pr
'l'
= len(xd)
nx nx
10
= ebayesthresh.wfromt(np.sqrt(2 * np.log(len(xd))), prior=prior, a=a)
wmin wmin
0.31029680918570224
= 1
winit winit
1
if pr == "l":
= ebayesthresh.beta_laplace(xd, a=a)
beta if pr == "c":
= ebayesthresh.beta_cauchy(xd) beta
= np.repeat(winit, len(beta))
w w
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
for j in range(maxits):
= w + 1 / beta
aa = w + aa
ps = 1 / aa ** 2
ww = ebayesthresh.isotone(ps, ww, increasing=False)
wnew = np.maximum(wmin, wnew)
wnew = np.minimum(1, wnew)
wnew = np.max(np.abs(np.diff(wnew)))
zinc = wnew
w # if zinc < tol:
# return w
# warnings.filterwarnings("More iterations required to achieve convergence")
R코드
<- function (xd, prior = "laplace", a = 0.5,
wmonfromx tol = 1e-08, maxits = 20) {
#
# Find the monotone marginal maximum likelihood estimate of the
# mixing weights for the Laplace prior with parameter a. It is
# assumed that the noise variance is equal to one.
#
# Find the beta values and the minimum weight
#
# Current version allows for standard deviation of 1 only.
#
<- substring(prior, 1, 1)
pr <- length(xd)
nx <- wfromt(sqrt(2 * log(length(xd))), prior=prior, a=a)
wmin <- 1
winit if(pr == "l")
<- beta.laplace(xd, a=a)
beta if(pr == "c")
<- beta.cauchy(xd)
beta
# now conduct iterated weighted least squares isotone regression
#
<- rep(winit, length(beta))
w for(j in (1:maxits)) {
<- w + 1/beta
aa <- w + aa
ps <- 1/aa^2
ww <- isotone(ps, ww, increasing = FALSE)
wnew <- pmax(wmin, wnew)
wnew <- pmin(1, wnew)
wnew <- max(abs(range(wnew - w)))
zinc <- wnew
w if(zinc < tol)
return(w)
}
warning("More iterations required to achieve convergence")
return(w)
}
결과
- Python
= np.random.normal(0, 1, size=10), prior = "laplace") ebayesthresh.wmonfromx(xd
array([0.31029681, 0.31029681, 0.31029681, 0.31029681, 0.31029681,
0.31029681, 0.31029681, 0.31029681, 0.31029681, 0.31029681])
- R
> wmonfromx(xd <- rnorm(10,0,1), prior = "laplace")
1] 0.3102968 0.3102968 0.3102968 0.3102968 0.3102968 0.3102968 0.3102968 0.3102968 0.3102968 0.3102968 [
wmonfromx(xd=rnorm(5, s = 1), prior = “laplace”, a = 0.5, tol = 1e-08, maxits = 20) [1] 0.9363989 0.9363989 0.9363989 0.4522184 0.4522184
vecbinsolv
= np.array([1, 2, 3])
zf # zf = 0
def fun(t, *args, **kwargs):
= kwargs.get('c', 0)
c return [x**2+c for x in t]
= 0
tlo = 10
thi = 30 nits
if isinstance(zf, int):
= len(str(zf))
nz else:
= len(zf) nz
nz
3
if isinstance(tlo, (int, float)):
= [tlo] * nz tlo
tlo
[0, 0, 0]
if len(tlo) != nz:
raise ValueError("Lower constraint has to be homogeneous or has the same length as #functions.")
if isinstance(thi, (int, float)):
= [thi] * nz thi
thi
[10, 10, 10]
if len(thi) != nz:
raise ValueError("Upper constraint has to be homogeneous or has the same length as #functions.")
=2
c
for _ in range(nits):
= np.array([(lo + hi) / 2 for lo, hi in zip(tlo, thi)])
tmid if fun == ebayesthresh.cauchy_threshzero:
= fun(tmid, s=s,w=w)
fmid elif fun == ebayesthresh.laplace_threshzero:
= fun(tmid, s=s,w=w,a=a)
fmid else:
= fun(tmid)
fmid if isinstance(fmid, (list,np.ndarray)) and isinstance(zf, (list,np.ndarray)):
= [f <= z for f, z in zip(fmid, zf)]
indt else:
= fmid <= zf
indt = [tm if ind else lo for tm, lo, ind in zip(tmid, tlo, indt)]
tlo = [tm if not ind else hi for tm, hi, ind in zip(tmid, thi, indt)] thi
tmid
array([1.00000001, 1.41421356, 1.7320508 ])
fmid
[1.000000011175871, 2.0000000000315445, 2.999999989096492]
indt
[False, False, True]
tlo
[0.9999999962747097, 1.414213553071022, 1.7320508044213057]
thi
[1.0000000055879354, 1.4142135623842478, 1.7320508137345314]
= [(lo + hi) / 2 for lo, hi in zip(tlo, thi)]
tsol tsol
[1.0000000009313226, 1.414213557727635, 1.7320508090779185]
R코드
<- function(zf, fun, tlo, thi, nits = 30, ...) {
vecbinsolv #
# Given a monotone function fun, and a vector of values
# zf find a vector of numbers t such that f(t) = zf.
# The solution is constrained to lie on the interval (tlo, thi)
#
# The function fun may be a vector of increasing functions
#
# Present version is inefficient because separate calculations
# are done for each element of z, and because bisections are done even
# if the solution is outside the range supplied
#
# It is important that fun should work for vector arguments.
# Additional arguments to fun can be passed through ...
#
# Works by successive bisection, carrying out nits harmonic bisections
# of the interval between tlo and thi
#
<- length(zf)
nz if(length(tlo)==1) tlo <- rep(tlo, nz)
if(length(tlo)!=nz)
stop(paste("Lower constraint has to be homogeneous",
"or has the same length as #functions."))
if(length(thi)==1) thi <- rep(thi, nz)
if(length(thi)!=nz)
stop(paste("Upper constraint has to be homogeneous",
"or has the same length as #functions."))
# carry out nits bisections
#
for(jj in (1:nits)) {
<- (tlo + thi)/2
tmid <- fun(tmid, ...)
fmid <- (fmid <= zf)
indt <- tmid[indt]
tlo[indt] !indt] <- tmid[!indt]
thi[
}<- (tlo + thi)/2
tsol return(tsol)
}
결과
- Python
= np.array([1, 2, 3])
zf def fun(t, *args, **kwargs):
return [2*x for x in t]
= 0
tlo = 10
thi ebayesthresh.vecbinsolv(zf, fun, tlo, thi)
[0.5000000027939677, 1.0000000009313226, 1.4999999990686774]
def fun(t, *args, **kwargs):
return [x**2 for x in t]
ebayesthresh.vecbinsolv(zf, fun, tlo, thi)
[1.0000000009313226, 1.414213557727635, 1.7320508090779185]
- R
> zf <- c(1, 2, 3)
> fun <- function(x) 2*x
> tlo <- 0
> thi <- 10
> vecbinsolv(zf, fun, tlo, thi)
1] 0.5 1.0 1.5
[> fun <- function(x) x**2
> vecbinsolv(zf, fun, tlo, thi)
1] 1.000000 1.414214 1.732051 [
tfromw
Given a single value or a vector of weights (i.e. prior probabilities that the parameter is nonzero) and sampling standard deviations (sd equals 1 for Cauchy prior), find the corresponding threshold(s) under the specified prior.
주어진 가중치 벡터 w와 s(표준 편차)에 대해 지정된 사전 분포를 사용하여 임계값 또는 해당 가중치에 대한 임계값 벡터 찾기. 만약 bayesfac=True이면 베이즈 요인 임계값을 찾고, 그렇지 않으면 사후 중앙값 임계값을 찾음. 만약 Laplace 사전 분포를 사용하는 경우, a는 역 스케일(즉, rate) 매개변수의 값 나옴.
Parameters:
- w (array-like): 가중치 벡터
- s (float): 표준 편차(default: 1)
- prior (str): 사전 분포 (default: “laplace”)
- bayesfac (bool): 베이즈 요인 임계값을 찾는지 여부 (default: False)
- a (float): a < 20인 입력 값 (default: 0.5)
= np.array([0.05, 0.1])
w # w = 0.5
= 1
s = "laplace"
prior # prior = "c"
= False
bayesfac = 0.5 a
=np.array([0.05,0.1])
w= 1
s = "laplace"
prior = False
bayesfac = 0.5
a = True universalthresh
= prior[0:1]
pr pr
'l'
if bayesfac:
= 1 / w - 2
z if pr == "l":
if isinstance(s, (int, float, str, bool)) and len(w) >= len(str(s)):
= z
zz elif isinstance(s, (int, float, str, bool)) and len(w) <len(str(s)):
= [z] * len(str(s))
zz elif len(w) >= len(s):
= z
zz elif len(w) < len(str(s)):
= [z] * len(s)
zz = ebayesthresh.vecbinsolv(zz, ebayesthresh.beta_laplace, 0, 10, 30, s=s,w=w, a=a)
tt elif pr == "c":
= ebayesthresh.vecbinsolv(z, ebayesthresh.beta_cauchy, 0, 10, 30, w=w)
tt
else:
= 0
z if pr == "l":
if isinstance(s, (int, float, str, bool)) and not isinstance(w, (int, float, str, bool)):
= np.array([0] * max(len(str(s)), len(w)))
zz elif not isinstance(s, (int, float, str, bool)) and isinstance(w, (int, float, str, bool)):
= np.array([0] * max(len(s), len(str(w))))
zz elif isinstance(s, (int, float, str, bool)) and isinstance(w, (int, float, str, bool)):
= np.array([0] * max(len(str(s)), len(str(w))))
zz else:
= [0] * max(len(s), len(w))
zz = ebayesthresh.vecbinsolv(zz, ebayesthresh.laplace_threshzero, 0, s * (25 + s * a), 30, s=s, w=w, a=a)
tt elif pr == "c":
= ebayesthresh.vecbinsolv(z, ebayesthresh.cauchy_threshzero, 0, 10, 30, w=w) tt
tt
[3.1152117408346385, 2.8163059337530285]
R코드
<- function(w, s = 1, prior = "laplace", bayesfac = FALSE, a = 0.5) {
tfromw #
# Given the vector of weights w and s (sd), find the threshold or
# vector of thresholds corresponding to these weights, under the
# specified prior.
# If bayesfac=TRUE the Bayes factor thresholds are found, otherwise
# the posterior median thresholds are found.
# If the Laplace prior is used, a gives the value of the inverse scale
# (i.e., rate) parameter
#
<- substring(prior, 1, 1)
pr if(bayesfac) {
<- 1/w - 2
z if(pr == "l"){
if(length(w)>=length(s)) {
<- z
zz else { zz <- rep(z, length(s)) }
} <- vecbinsolv(zz, beta.laplace, 0, 10, s = s, a = a)
tt
}if(pr == "c")
<- vecbinsolv(z, beta.cauchy, 0, 10)
tt
}else {
<- 0
z if(pr == "l"){
<- rep(0, max(length(s), length(w)))
zz
# When x/s-s*a>25, laplace.threshzero has value
# close to 1/2; The boundary value of x can be
# treated as the upper bound for search.
<- vecbinsolv(zz, laplace.threshzero, 0, s*(25+s*a),
tt s = s, w = w, a = a)
}if(pr == "c")
<- vecbinsolv(z, cauchy.threshzero, 0, 10, w = w)
tt
}return(tt)
}
결과
- Python
0.05, 0.1]), s = 1) ebayesthresh.tfromw(np.array([
[3.1152117408346385, 2.8163059337530285]
0.05, 0.1]), prior = "cauchy", bayesfac = True) ebayesthresh.tfromw(np.array([
[3.2596351904794574, 2.9597404645755887]
- R
> tfromw(c(0.05, 0.1), s = 1)
1] 3.115212 2.816306
[> tfromw(c(0.05, 0.1), prior = "cauchy", bayesfac = TRUE)
1] 3.259635 2.959740 [
tfromx
Given a vector of data and standard deviations (sd equals 1 for Cauchy prior), find the value or vector (heterogeneous sampling standard deviation with Laplace prior) of thresholds corresponding to the marginal maximum likelihood choice of weight.
데이터가 주어졌을때, 가중치의 한계 최대 우도로 임계값 찾는 함수
=np.array([0.05,0.1]),
x=1
s="laplace"
prior=False
bayesfac=0.5
a=True universalthresh
= prior[0:1] pr
if pr == "c":
= 1 s
if pr == "l" and np.isnan(a):
= ebayesthresh.wandafromx(x, s, universalthresh)
wa = wa['w']
w = wa['a']
a else:
= ebayesthresh.wfromx(x, s, prior=prior, a=a) w
=prior, bayesfac=bayesfac, a=a) ebayesthresh.tfromw(w, s, prior
[1.1874362826347351e-08, 1.1874362826347351e-08, 1.1874362826347351e-08]
R코드
<- function (x, s = 1, prior = "laplace", bayesfac = FALSE, a = 0.5,
tfromx universalthresh = TRUE) {
#
# Given the data x, the prior, and any other parameters, find the
# threshold corresponding to the marginal maximum likelihood
# estimator of the mixing weight.
#
<- substring(prior, 1, 1)
pr if(pr == "c")
= 1
s if ( pr=="l" && is.na (a) ) {
<- wandafromx(x, s, universalthresh)
wa <- wa$w
w <- wa$a
a else {
} <- wfromx(x, s, prior = prior, a = a)
w
}return(tfromw(w, s, prior = prior, bayesfac = bayesfac, a = a))
}
결과
- Python
=np.array([0.05,0.1]), s = 1, prior = "laplace", bayesfac = False, a = 0.5, universalthresh = True) ebayesthresh.tfromx(x
1.1774100337643176
= np.concatenate([np.random.normal(0, 1, 90), np.random.normal(5, 1, 10)]), prior = "cauchy") ebayesthresh.tfromx(x
2.2073050821200013
- R
> tfromx(x=c(0.05,0.1), s = 1, prior = "laplace", bayesfac = FALSE, a = 0.5,universalthresh = TRUE)
1] 1.17741
[> tfromx(x=c(rnorm(90, mean=0, sd=1), rnorm(10, mean=5, sd=1)), prior = "cauchy")
1] 2.301196 [
wpost_laplace
Calculate the posterior weight for non-zero effect
- 0이 아닌 효과에 대한 사후 가중치 계산
= 0.5
ㅈ = np.array([-2,1,0,-4,8,50])
x = 1
s = 0.5 a
= ebayesthresh.beta_laplace(x, s, a)
laplace_beta laplace_beta
array([ 8.89852030e-001, -3.80041717e-001, -5.61817772e-001,
2.85459467e+002, 1.02698062e+012, 6.34453954e+265])
1 - (1 - w) / (1 + w * laplace_beta)
array([1., 1., 1., 1., 1., 1.])
R코드
<- function(w, x, s = 1, a = 0.5)
wpost.laplace #
# Calculate the posterior weight for non-zero effect
#
1 - (1 - w)/(1 + w * beta.laplace(x, s, a))
결과
- Python
0.5,np.array([-2,1,0,-4,8,50])) ebayesthresh.wpost_laplace(
array([0.65396152, 0.38270015, 0.30467782, 0.99652125, 1. ,
1. ])
- R
이베이즈 깃헙에 있는데 R 패키지에는 없음.
zetafromx
Description
Suppose a sequence of data has underlying mean vector with elements \(\theta_i\). Given the sequence of data, and a vector of scale factors cs and a lower limit pilo, this routine finds the marginal maximum likelihood estimate of the parameter zeta such that the prior probability of \(\theta_i\) being nonzero is of the form median(pilo, zeta*cs, 1).
파라메터 제타의 최대 우도 추정치 계산
= np.array([-2,1,0,-4,8,50])
xd = np.array([2,3,5,6,1,-1])
cs = None
pilo ="laplace"
prior# priir="c"
=0.5 a
= prior[0:1]
pr pr
'l'
= len(xd)
nx nx
6
if pilo is None:
= ebayesthresh.wfromt(np.sqrt(2 * np.log(len(xd))), prior=prior, a=a) pilo
pilo
0.4121150272751926
if pr == "l":
= ebayesthresh.beta_laplace(xd, a=a)
beta elif pr == "c":
= ebayesthresh.beta_cauchy(xd) beta
beta
array([ 8.89852030e-001, -3.80041717e-001, -5.61817772e-001,
2.85459467e+002, 1.02698062e+012, 6.34453954e+265])
= pilo / cs
zs1 zs1
array([ 0.20605751, 0.13737168, 0.08242301, 0.06868584, 0.41211503,
-0.41211503])
= 1 / cs
zs2 zs2
array([ 0.5 , 0.33333333, 0.2 , 0.16666667, 1. ,
-1. ])
= np.sort(np.unique(np.concatenate((zs1, zs2))))
zj zj
array([-1. , -0.41211503, 0.06868584, 0.08242301, 0.13737168,
0.16666667, 0.2 , 0.20605751, 0.33333333, 0.41211503,
0.5 , 1. ])
= cs * beta
cb cb
array([ 1.77970406e+000, -1.14012515e+000, -2.80908886e+000,
1.71275680e+003, 1.02698062e+012, -6.34453954e+265])
= len(zj)
mz mz
12
= None
zlmax zlmax
= np.zeros(mz, dtype=bool)
lmin lmin
array([False, False, False, False, False, False, False, False, False,
False, False, False])
for j in range(1, mz - 1):
= zj[j]
ze = cb[(ze > zs1) & (ze <= zs2)]
cbil = np.sum(cbil / (1 + ze * cbil))
ld if ld <= 0:
= cb[(ze >= zs1) & (ze < zs2)]
cbir = np.sum(cbir / (1 + ze * cbir))
rd = rd >= 0 lmin[j]
= cb[zj[0] == zs1]
cbir cbir
array([], dtype=float64)
= np.sum(cbir / (1 + zj[0] * cbir))
rd rd
0.0
if rd > 0:
0] = True
lmin[else:
= zj[0] zlmax
= cb[zj[mz - 1] == zs2]
cbil cbil
array([1.02698062e+12])
= np.sum(cbil / (1 + zj[mz - 1] * cbil))
ld ld
0.9999999999990262
if ld < 0:
- 1] = True
lmin[mz else:
= zj[mz - 1] zlmax
= zj[lmin]
zlmin zlmin
array([-0.41211503, 0.06868584, 0.33333333])
= len(zlmin)
nlmin nlmin
3
for j in range(1, nlmin):
= zlmin[j - 1]
zlo = zlmin[j]
zhi = (zlo + zhi) / 2
ze = (zhi - zlo) / 2
zstep for nit in range(10):
= cb[(ze >= zs1) & (ze <= zs2)]
cbi = np.sum(cbi / (1 + ze * cbi))
likd /= 2
zstep += zstep * np.sign(likd)
ze = np.append(zlmax, ze) zlmax
= len(zlmax)
nlmax nlmax
3
= np.empty(nlmax)
zm zm
array([ 0.82238932, 0.56239524, 267.81623818])
for j in range(nlmax):
= np.maximum(zs1, np.minimum(zlmax[j], zs2))
pz = np.sum(np.log(1 + cb * pz)) zm[j]
pz
array([ 0.20605751, 0.15591096, 0.15591096, 0.15591096, 0.41211503,
-0.41211503])
zm
array([643.79470765, 642.57221213, 643.04900807])
= zlmax[zm == np.max(zm)]
zeta zeta
array([1.])
= np.min(zeta)
zeta zeta
1.0
= np.minimum(1, np.maximum(zeta * cs, pilo))
w w
array([1. , 1. , 1. , 1. , 1. ,
0.41211503])
R 코드
<- function(xd, cs, pilo = NA, prior = "laplace", a = 0.5) {
zetafromx #
# Given a sequence xd, a vector of scale factors cs and
# a lower limit pilo, find the marginal maximum likelihood
# estimate of the parameter zeta such that the prior prob
# is of the form median( pilo, zeta*cs, 1)
#
# If pilo=NA then it is calculated according to the sample size
# to corrrespond to the universal threshold
#
# Find the beta values and the minimum weight if necessary
#
# Current version allows for standard deviation of 1 only.
#
<- substring(prior, 1, 1)
pr <- length(xd)
nx if (is.na(pilo))
<- wfromt(sqrt(2 * log(length(xd))), prior=prior, a=a)
pilo if(pr == "l")
<- beta.laplace(xd, a=a)
beta if(pr == "c") beta <- beta.cauchy(xd)
#
# Find jump points zj in derivative of log likelihood as function
# of z, and other preliminary calculations
#
<- pilo/cs
zs1 <- 1/cs
zs2 <- sort(unique(c(zs1, zs2)))
zj <- cs * beta
cb <- length(zj)
mz <- NULL
zlmax
# Find left and right derivatives at each zj and check which are
# local minima Check internal zj first
<- rep(FALSE, mz)
lmin for(j in (2:(mz - 1))) {
<- zj[j]
ze <- cb[(ze > zs1) & (ze <= zs2)]
cbil <- sum(cbil/(1 + ze * cbil))
ld if(ld <= 0) {
<- cb[(ze >= zs1) & (ze < zs2)]
cbir <- sum(cbir/(1 + ze * cbir))
rd <- (rd >= 0)
lmin[j]
}
}#
# Deal with the two end points in turn, finding right deriv
# at lower end and left deriv at upper
#
# In each case the corresponding end point is either a local min
# or a local max depending on the sign of the relevant deriv
#
<- cb[zj[1] == zs1]
cbir <- sum(cbir/(1 + zj[1] * cbir))
rd if(rd > 0) lmin[1] <- TRUE else zlmax <- zj[1]
<- cb[zj[mz] == zs2]
cbil <- sum(cbil/(1 + zj[mz] * cbil))
ld if(ld < 0) lmin[mz] <- TRUE else zlmax <- zj[mz]
# Flag all local minima and do a binary search between them to find
# the local maxima
#
<- zj[lmin]
zlmin <- length(zlmin)
nlmin if(nlmin >= 2) for(j in (2:nlmin)) {
<- zlmin[j - 1]
zlo <- zlmin[j]
zhi <- (zlo + zhi)/2
ze <- (zhi - zlo)/2
zstep for(nit in (1:10)) {
<- cb[(ze >= zs1) & (ze <= zs2)]
cbi <- sum(cbi/(1 + ze * cbi))
likd <- zstep/2
zstep <- ze + zstep * sign(likd)
ze
}<- c(zlmax, ze)
zlmax
}#
# Evaluate all local maxima and find global max; use smaller value
# if there is an exact tie for the global maximum.
#
<- length(zlmax)
nlmax <- rep(NA, nlmax)
zm for(j in (1:nlmax)) {
<- pmax(zs1, pmin(zlmax[j], zs2))
pz <- sum(log(1 + cb * pz))
zm[j]
}<- zlmax[zm == max(zm)]
zeta <- min(zeta)
zeta <- pmin(1, pmax(zeta*cs, pilo))
w return(list(zeta=zeta, w=w, cs=cs, pilo=pilo))
}
결과
- Python
-2,1,0,-4,8,50]),np.array([2,3,5,6,1,-1])) ebayesthresh.zetafromx(np.array([
{'zeta': 1.0,
'w': array([1. , 1. , 1. , 1. , 1. ,
0.41211503]),
'cs': array([ 2, 3, 5, 6, 1, -1]),
'pilo': 0.4121150272751926}
- R
> zetafromx(c(-2,1,0,-4,8,50),c(2,3,5,6,1,-1))
$zeta
1] 1
[
$w
1] 1.000000 1.000000 1.000000 1.000000 1.000000 0.412115
[
$cs
1] 2 3 5 6 1 -1
[
$pilo
1] 0.412115 [
Layer
import torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
def make_Psi(T):
= np.zeros((T,T))
W for i in range(T):
for j in range(T):
if i==j :
= 0
W[i,j] elif np.abs(i-j) <= 1 :
= 1
W[i,j] = np.array(W.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-W) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi return Psi
= 100
T = np.arange(T)/T * 10
t = 3*np.sin(0.5*t) + 1.2*np.sin(1.0*t) + 0.5*np.sin(1.2*t)
y_true = y_true + np.random.normal(size=T)
y =(10,6))
plt.figure(figsize plt.plot(t,y_true)
'o')
plt.plot(t,y,'--') plt.plot(t,y_true,
= np.array(y)
f if len(f.shape)==1: f = f.reshape(-1,1)
= f.shape
T,N = make_Psi(T)
Psi = Psi.T @ f # apply dft fbar
**2) # periodogram plt.plot(t,fbar
= ebayesthresh.ebayesthresh(fbar[:,0]) fbar_threshed
# fbar_threshed = np.stack([ebayesthresh(FloatVector(fbar[:,i])) for i in range(N)],axis=1)
**2)) # periodogram
plt.plot((fbar**2)) plt.plot((fbar_threshed
**2)[20:80]) # periodogram
plt.plot((fbar**2)[20:80]) plt.plot((fbar_threshed
= Psi @ fbar_threshed # inverse dft
yhat =(10,6))
plt.figure(figsize'.')
plt.plot(t,y,'--')
plt.plot(t,y_true, plt.plot(t,yhat)
class ebayesthresh_nn(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self,input):
return ebayesthresh.ebayesthresh(input)
= ebayesthresh_nn() thresh_layer
0]) plt.plot(fbar[:,
0])) plt.plot(thresh_layer(fbar[:,