  The initial mass function (IMF) is a key ingredient in many studies of galaxy
formation and evolution. Although the IMF is often assumed to be universal,
there is continuing evidence that it is not universal. Spectroscopic studies
that derive the IMF of the unresolved stellar populations of a galaxy often
assume that this spectrum can be described by a single stellar population
(SSP). To alleviate these limitations, in this paper we have developed a unique
hierarchical Bayesian framework for modelling composite stellar populations
(CSPs). Within this framework we use a parameterized IMF prior to regulate a
direct inference of the IMF. We use this new framework to determine the number
of SSPs that is required to fit a set of realistic CSP mock spectra. The CSP
mock spectra that we use are based on semi-analytic models and have an IMF that
varies as a function of stellar velocity dispersion of the galaxy. Our results
suggest that using a single SSP biases the determination of the IMF slope to a
higher value than the true slope, although the trend with stellar velocity
dispersion is overall recovered. If we include more SSPs in the fit, the
Bayesian evidence increases significantly and the inferred IMF slopes of our
mock spectra converge, within the errors, to their true values. Most of the
bias is already removed by using two SSPs instead of one. We show that we can
reconstruct the variable IMF of our mock spectra for signal-to-noise ratios
exceeding $\sim$75.
