@@ -633,6 +633,101 @@ def trunc_norm_draws(unif_vals, mu, sigma, cut_lb, cut_ub):
633633
634634What would one simulation of 161 test scores look like from a truncated normal with mean $\mu=300$, $\sigma=30$?
635635
636+ ``` {code-cell} ipython3
637+ :tags: []
638+
639+ mu_1 = 300.0
640+ sig_1 = 30.0
641+ cut_lb_1 = 0.0
642+ cut_ub_1 = 450.0
643+ np.random.seed(seed=1975) # Set seed so the simulation values are always the same
644+ unif_vals_1 = sts.uniform.rvs(0, 1, size=161)
645+ draws_1 = trunc_norm_draws(unif_vals_1, mu_1, sig_1, cut_lb_1, cut_ub_1)
646+ print('Mean of simulated score =', draws_1.mean())
647+ print('Variance of simulated scores =', draws_1.var())
648+ print('Standard deviation of simulated scores =', draws_1.std())
649+ ```
650+
651+ ``` {code-cell} ipython3
652+ :tags: ["remove-output"]
653+
654+ # Plot data histogram vs. simulated data histogram
655+ count_d, bins_d, ignored_d = \
656+ plt.hist(data, 30, density=True, color='b', edgecolor='black',
657+ linewidth=0.8, label='Data')
658+ count_m, bins_m, ignored_m = \
659+ plt.hist(draws_1, 30, density=True, color='r', edgecolor='black',
660+ linewidth=0.8, alpha=0.5, label='Simulated data')
661+ xvals = np.linspace(0, 450, 500)
662+ plt.plot(xvals, trunc_norm_pdf(xvals, mu_1, sig_1, cut_lb_1, cut_ub_1),
663+ linewidth=2, color='k', label='PDF, simulated data')
664+ plt.title('Econ 381 scores: 2011-2012', fontsize=20)
665+ plt.xlabel('Total points')
666+ plt.ylabel('Percent of scores')
667+ plt.xlim([0, 550]) # This gives the xmin and xmax to be plotted"
668+ plt.legend(loc='upper left')
669+
670+ plt.show()
671+ ```
672+
673+ ``` {figure} ../../../images/smm/Econ381scores_sim1.png
674+ ---
675+ height: 500px
676+ name: FigSMM_EconScoreSim1
677+ ---
678+ Histograms of one simulation of 161 Econ 381 test scores (2011-2012) from arbitrary truncated normal distribution compared to data
679+ ```
680+
681+ From that simulation, we can calculate moments from the simulated data just like we did from the actual data. The following function ` data_moments2() ` computes the mean and the variance of the simulated data $x$, where $x$ is an $N\times S$ matrix of $S$ simulations of $N$ observations each.
682+
683+ ``` {code-cell} ipython3
684+ :tags: []
685+
686+ def data_moments2(xvals):
687+ '''
688+ --------------------------------------------------------------------
689+ This function computes the two data moments for SMM
690+ (mean(data), variance(data)) from both the actual data and from the
691+ simulated data.
692+ --------------------------------------------------------------------
693+ INPUTS:
694+ xvals = (N, S) matrix or (N,) vector, or scalar in (cut_lb, cut_ub),
695+ test scores data, either real world or simulated. Real world
696+ data will come in the form (N,). Simulated data comes in the
697+ form (N,) or (N, S).
698+
699+ OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION: None
700+
701+ OBJECTS CREATED WITHIN FUNCTION:
702+ mean_data = scalar or (S,) vector, mean value of test scores data
703+ var_data = scalar > 0 or (S,) vector, variance of test scores data
704+
705+ FILES CREATED BY THIS FUNCTION: None
706+
707+ RETURNS: mean_data, var_data
708+ --------------------------------------------------------------------
709+ '''
710+ if xvals.ndim == 1:
711+ mean_data = xvals.mean()
712+ var_data = xvals.var()
713+ elif xvals.ndim == 2:
714+ mean_data = xvals.mean(axis=0)
715+ var_data = xvals.var(axis=0)
716+
717+ return mean_data, var_data
718+ ```
719+
720+ ``` {code-cell} ipython3
721+ :tags: []
722+
723+ mean_data, var_data = data_moments2(data)
724+ print('Data mean =', mean_data)
725+ print('Data variance =', var_data)
726+ mean_sim, var_sim = data_moments2(draws_1)
727+ print('Sim. mean =', mean_sim)
728+ print('Sim. variance =', var_sim)
729+ ```
730+
636731
637732(SecSMM_CodeExmp_BM72)=
638733### Brock and Mirman (1972) estimation by SMM
0 commit comments