In [7]:
from scipy import stats
import numpy as np
def measure(n):
"""Measurement model, return two coupled measurements.
"""
m1 = np.random.normal(size=n)
m2 = np.random.normal(scale=0.5, size=n)
return m1+m2, m1-m2
In [8]:
m1, m2 = measure(2000)
xmin = m1.min()
xmax = m1.max()
ymin = m2.min()
ymax = m2.max()
In [9]:
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([m1, m2])
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)
In [159]:
from scipy import stats
import numpy as np
# making kde
values = np.arange(10)
kde = stats.gaussian_kde(values)
In [160]:
np.median(kde.resample(100000))
Out[160]:
In [161]:
def KDE_make_means(kde, size=10):
func = lambda x : np.random.randint(0, x.n, size=x.d)
kde.means = [kde.dataset[:, func(kde)] for i in xrange(size)]
kde.means_n = len(kde.means)
#kde.dataset = None
#KDE_make_means(kde)
In [162]:
def KDE_save_means(kde, size):
indices = np.random.randint(0, kde.n, size=size)
kde.means = kde.dataset[:, indices]
kde.means_size = size
KDE_save_means(kde, size=10)
kde.dataset = None
In [166]:
def KDE_resample(kde):
norm = np.transpose(np.random.multivariate_normal(np.zeros((kde.d,), float), kde.covariance,
size=kde.means_size))
print(kde.means)
return kde.means + norm
KDE_resample(kde)
Out[166]:
In [1]:
%load_ext rpy2.ipython
In [4]:
%%R
library(dplyr)
library(tidyr)
library(ggplot2)
library(vegan)
In [6]:
%%R
data(varespec)
varespec %>% head
In [ ]: