diff --git a/icom/encoder.py b/icom/encoder.py new file mode 100644 index 0000000..9b6336b --- /dev/null +++ b/icom/encoder.py @@ -0,0 +1,23 @@ +import numpy as np + + +# encode a message using poisson rate coding +def encode_message(symbol: str, + signal_rate: float, + noise_rate: float, + duration: int, # in seconds + **kwargs): + codebook = kwargs.get("codebook",None) + + if codebook is None: raise TypeError("Invalid code book.") + + #TODO + spikes = np.random.poisson(lam=signal_rate, size=duration) + spikes = [np.sum(spikes[1:i]) for i in range(len(spikes))] + + print(np.mean(spikes)) + + # add leak spikes + return spikes + +encode_message("1", 5, .3, 10, codebook=[]) diff --git a/icom/encoder.py b/icom/encoder.py new file mode 100644 index 0000000..9b6336b --- /dev/null +++ b/icom/encoder.py @@ -0,0 +1,23 @@ +import numpy as np + + +# encode a message using poisson rate coding +def encode_message(symbol: str, + signal_rate: float, + noise_rate: float, + duration: int, # in seconds + **kwargs): + codebook = kwargs.get("codebook",None) + + if codebook is None: raise TypeError("Invalid code book.") + + #TODO + spikes = np.random.poisson(lam=signal_rate, size=duration) + spikes = [np.sum(spikes[1:i]) for i in range(len(spikes))] + + print(np.mean(spikes)) + + # add leak spikes + return spikes + +encode_message("1", 5, .3, 10, codebook=[]) diff --git a/py/poisson_generator.py b/py/poisson_generator.py index cbba848..3feab60 100644 --- a/py/poisson_generator.py +++ b/py/poisson_generator.py @@ -30,7 +30,7 @@ #2 (points) -poi = np.random.poisson(5,size=100) +poi = np.random.poisson(lam=5,size=100) print(f"avergae is {np.average(poi)}") #poi = [i-j for i,j in zip(poi[:-1],poi[1:])] diff --git a/icom/encoder.py b/icom/encoder.py new file mode 100644 index 0000000..9b6336b --- /dev/null +++ b/icom/encoder.py @@ -0,0 +1,23 @@ +import numpy as np + + +# encode a message using poisson rate coding +def encode_message(symbol: str, + signal_rate: float, + noise_rate: float, + duration: int, # in seconds + **kwargs): + codebook = kwargs.get("codebook",None) + + if codebook is None: raise TypeError("Invalid code book.") + + #TODO + spikes = np.random.poisson(lam=signal_rate, size=duration) + spikes = [np.sum(spikes[1:i]) for i in range(len(spikes))] + + print(np.mean(spikes)) + + # add leak spikes + return spikes + +encode_message("1", 5, .3, 10, codebook=[]) diff --git a/py/poisson_generator.py b/py/poisson_generator.py index cbba848..3feab60 100644 --- a/py/poisson_generator.py +++ b/py/poisson_generator.py @@ -30,7 +30,7 @@ #2 (points) -poi = np.random.poisson(5,size=100) +poi = np.random.poisson(lam=5,size=100) print(f"avergae is {np.average(poi)}") #poi = [i-j for i,j in zip(poi[:-1],poi[1:])] diff --git a/py/stan.py b/py/stan.py index 738f7fc..f4ce9ae 100644 --- a/py/stan.py +++ b/py/stan.py @@ -5,7 +5,7 @@ #%% cell 1. inference using Stan -# PyStan +# PyStan dependendency import pystan # visualization @@ -24,8 +24,7 @@ sns.set() np.random.seed(101) -# initialize Stan model code - +# initialize Stan model code (basic conjigate case) model = """ data { int N; @@ -141,9 +140,9 @@ plt.axhline(np.mean(alpha), linestyle='--', lw=1, color='lightblue') # CI lines -low_conf, high_conf = np.percentile(alpha, 5), np.percentile(alpha, 95) +low_conf, high_conf = np.percentile(alpha, 2.5), np.percentile(alpha, 97.5) plt.axhline(low_conf, linestyle = '--', lw=1, color='darkblue') -plt.axhline(high_conf, linestyle = '--', lw=1, color='darkblue', label='90% CI') +plt.axhline(high_conf, linestyle = '--', lw=1, color='darkblue', label='95% CI') plt.title("Diagnostic trace for alpha") plt.ylabel("alpha") @@ -167,3 +166,46 @@ plt.title("Posterior distribution of alpha") plt.show() + + +#%% [markdown] +# The following code illustrates a bayesian model with conjugate case for beta distribution. +# +# Prior: uniform, likelihood: binomial, posterior: beta + +import pystan +import numpy as np + +# 0. define grand truth and random data +#-------------------------------------- +grand_truth = 0.5 +# sample data = {k/n=5/10,6/10} + +# 1. define and compile model +#-------------------------------------- +model_code = """ + data { + int cnt; + vector[cnt] k; + vector[cnt] n; + //int k; // number of success + //int n; // number of trials + } + parameters { + real theta; + + } + model { + theta ~ uniform(0,1); // or beta(1,1) which is also a uniform prior + k ~ beta(theta, n); + } +""" +model = pystan.StanModel(model_code = model_code) + +# 2. train model +#-------------------------------------- +# number of chains = 2, thin=1, 20000, refresh=100 + +# 3. diagnose convergence +#-------------------------------------- +# 3.1 draw dynamic trace \ No newline at end of file