0

我有一个随机变量,如下所示平滑:局部加权为二进制值的随机可变

F(X)= 1的概率为G(X)

F(X)= 0的概率为1-G (x)的

其中0<克(x)的< 1.

假设G(X)= X。比方说,我观察这个变量不知道函数g并获得了100个样本如下:现在

import numpy as np 
import matplotlib.pyplot as plt 
from scipy.stats import binned_statistic 

list = np.ndarray(shape=(200,2)) 

g = np.random.rand(200) 
for i in range(len(g)): 
    list[i] = (g[i], np.random.choice([0, 1], p=[1-g[i], g[i]])) 

print(list) 
plt.plot(list[:,0], list[:,1], 'o') 

Plot of 0s and 1s

,我想找回从这些点的函数g。我能想到的最好的就是用画一个柱状图,并使用平均统计:

bin_means, bin_edges, bin_number = binned_statistic(list[:,0], list[:,1], statistic='mean', bins=10) 
plt.hlines(bin_means, bin_edges[:-1], bin_edges[1:], lw=2) 

Histogram mean statistics

相反,我想有发电功能的连续估计。

我想这是关于内核密度估计,但我找不到合适的指针。

+0

你可以在'Statsmodels''sklearn'中找到kdes,'scipy'也有一个。如果你只想看一看'seaborn'并且它是'distplot'或'kdeplot'。但为什么你想要一个KDE二进制数据? –

+0

@MarvinTaschenberger有可能我对kde的评论可能会引起误解。似乎我有一个逻辑回归问题。 https://en.wikipedia.org/wiki/Logistic_regression#Example:_Probability_of_passing_an_exam_versus_hours_of_study。但我并不是想要适合一个模型。我想以平滑的方式绘制它。 – user1860037

+0

这也看起来相关:http://thestatsgeek.com/2014/09/13/checking-functional-form-in-logistic-regression-using-loess/ – user1860037

回答

0

简单而不明确装修的估计:

import seaborn as sns 
g = sns.lmplot(x= , y= , y_jitter=.02 , logistic=True) 

插上x=您的外生变量和类似y =因变量。 y_jitter如果您有很多数据点,则可以提高可视性。 logistic = True是这里的要点。它会给你数据的逻辑回归线。

Seaborn基本上是围绕matplotlib定制的,并且在pandas的情况下效果很好,以防您想要将数据扩展到DataFrame。

+0

现在,我明白我在找的是本地加权散点图平滑。谢谢指点sns。 df = pd.DataFrame() df ['x'] = list [:,0] df ['y'] = list [:,1] sns.lmplot(x ='x',y =' y',data = df,lowess = True) plt.show() – user1860037