Get distribution sample with similar trend

hi :grin: I’m trying to generate a sample from my distribution with similar proportions/similar trend as I have in observed data

how would I do so?

right now I only can just do a sample from my data with the same distribution, but not the same trend:
my data:
image
image

and
generated data:
image
image

so it’s only similar in it’s value distribution, not in it’s trend

my code:

import pymc3 as pm
import matplotlib.pyplot as plt
import seaborn as sns

obs = [56.0, 78.0, 36.0, 68.0, 50.0, 45.0, 58.0, 65.0, 90.0, 50.0, 60.0, 63.0, 48.0, 74.0, 73.0, 66.0, 55.0, 65.0, 73.0, 61.0, 55.0, 86.0, 42.0, 43.0, 51.0, 45.0, 49.0, 55.0, 43.0, 42.0, 33.0, 40.0, 39.0, 40.0, 60.0, 53.0, 46.0, 38.0, 36.0, 40.0, 49.0, 35.0, 24.0, 35.0, 45.0, 41.0, 37.0, 32.0, 30.0, 19.0, 28.0, 35.0, 33.0, 46.0, 43.0, 46.0, 52.0, 36.0, 54.0, 40.0, 22.0, 28.0, 37.0, 36.0, 27.0, 33.0, 30.0, 34.0, 23.0, 39.0, 21.0, 25.0, 29.0, 33.0, 26.0, 47.0, 18.0, 44.0, 22.0]

plt.plot(obs);
plt.show()

sns.distplot(obs, hist=True, kde=True, bins=int(180 / 5), color='darkblue')
plt.show()

with pm.Model() as model:
    alpha = pm.Exponential('alpha', lam=5)
    beta = pm.Exponential('beta', lam=5)

    g = pm.Gamma('g', alpha=alpha, beta=beta, observed=obs)

    trace = pm.sample(return_inferencedata=True, cores=1)
    post_pred = pm.sample_posterior_predictive(trace.posterior)


stats = pm.summary(trace, kind="stats")
alpha_param = stats['mean'][0]
beta_param = stats['mean'][1]

gg = pm.Gamma.dist(alpha=alpha_param, beta=beta_param)
distr = gg.random(size=1000)


sns.distplot(distr, hist=True, kde=True, bins=int(180 / 5), color='darkblue')
plt.show()

plt.plot(distr);
plt.show()

Your current model has no way of generating a trend. You will have to write a model that can do that if that’s your goal. The simplest case is a model whose mean changes linearly with time. Perhaps this notebook can be helpful: GLM: Linear regression — PyMC3 3.10.0 documentation

The model in the example uses a Normal distribution but the same logic can be applied to a Gamma distribution. There is a snippet example from another user in this thread: Gamma GLM for epidemic growth model

what if I don’t need trend prediction, but just to arrange my sample’s values in the right order, so it would be proportionally similar as in the initial data? is there such a function in pymc3?

Even if you don’t care about the trend parameters you will have to somehow model them / input them into your model. There are countless ways to write a model that can accompodate trends and I suggested just a simple one to get you started.

Is there a reason not to try a linear model at all? That can help give more directed advice.

i just thought there would be easier way to accomplish that by doing something like pm.sample.put_values_in_initial_proportional_order :laughing:
but if not, then going to follow your suggestions thanks ! :grin: