<jupyter_start><jupyter_text>Think BayesThis notebook presents example code and exercise solutions for Think Bayes.Copyright 2018 <NAME>MIT License: https://opensource.org/licenses/MIT<jupyter_code># Configure Jupyter so figures appear in the notebook
%matplotlib inline

# Configure Jupyter to display the assigned value after an assignment
%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'

# import classes from thinkbayes2
from thinkbayes2 import Pmf, Suite, Beta
import thinkplot

import numpy as np<jupyter_output><empty_output><jupyter_text>The social desirability problemWhenever you survey people about sensitive issues, you have to deal with [social desirability bias](https://en.wikipedia.org/wiki/Social_desirability_bias), which is the tendency of people to shade their answers in the direction they think shows them in the most positive light.One of the ways to improve the quality of the results is to collect responses in indirect ways.  For example, [here's a clever way one research group estimated the prevalence of atheists](https://fivethirtyeight.com/features/way-more-americans-may-be-atheists-than-we-thought/).Another way is [randomized response](https://en.wikipedia.org/wiki/Randomized_response), as described in [this presentation](http://www.soz.unibe.ch/ueber_uns/personen/jann/presentations_by_ben_jann/e131361/e131381/rrt_online07_kassel08_ger.pdf) or [this video](https://www.youtube.com/watch?v=nwJ0qY_rP0A).As an example, suppose you ask 100 people to flip a coin and:* If they get heads, they honestly answer the question "Do you believe in God?"* If they get tails, they flip a second coin and report YES for heads, tails for NO.Assume that you cannot observe whether they flip one coin or two.And suppose you get 55 YESes and 45 NOs.1. Estimate the prevalence of believers in the surveyed population (by which, as always, I mean compute a posterior distribution).2. How efficient is this method?  That is, how does the width of the posterior distribution compare to the distribution you would get if 100 people answered the question honestly?<jupyter_code># Solution

class Social(Suite):
    
    def Likelihood(self, data, hypo):
        """
        data: outcome of unreliable measurement, either 'YES' or 'NO'
        hypo: actual proportion of the thing we're measuring
        """
        p = hypo
        p_yes = 0.25 + p/2
        if data == 'YES':
            return p_yes
        else:
            return 1 - p_yes
# Solution

prior = np.linspace(0, 1, 101)
suite = Social(prior)

thinkplot.Pdf(suite, label='Prior')
thinkplot.decorate(xlabel='Fraction of the population',
                   ylabel='PDF')
# Solution

for i in range(55):
    suite.Update('YES')
    
for i in range(45):
    suite.Update('NO')
# Solution

thinkplot.Pdf(suite, label='Posterior')
thinkplot.decorate(xlabel='Fraction of the population',
                   ylabel='PDF')
# Solution goes here
# Solution goes here
# Solution goes here
# Solution goes here
# Solution goes here
# Solution goes here
# Solution goes here
# Solution goes here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07926829268292683, 'fraction_numerical': 0.016046213093709884, 'mean_word_length': 4.265202702702703, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '97236', 'license': 'MIT', 'n_tokens_mistral': 933, 'n_tokens_neox': 866, 'n_words': 342, 'path': 'examples/.ipynb_checkpoints/social2-checkpoint.ipynb', 'repo_name': 'sportsracer48/ThinkBayes2', 'size': '3125'}