Skip to content

Conversation

@HumphreyYang
Copy link
Member

This PR resolves #125.

@netlify
Copy link

netlify bot commented Sep 28, 2023

Deploy Preview for incomparable-parfait-2417f8 ready!

Name Link
🔨 Latest commit 4f8f451
🔍 Latest deploy log https://app.netlify.com/sites/incomparable-parfait-2417f8/deploys/6514cc3da922750007d16820
😎 Deploy Preview https://deploy-preview-126--incomparable-parfait-2417f8.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions
Copy link

@github-actions github-actions bot temporarily deployed to pull request September 28, 2023 00:55 Inactive
@HumphreyYang
Copy link
Member Author

HumphreyYang commented Sep 28, 2023

Hi @jstac,

The reshaping of g should not affect the final output due to broadcasting (as the first three dimensions can be broadcasted). To ensure my guess is correct and the output is identical, I had a quick experiment here:

def A(g, sv_model, shapes):
    # Set up
    P, hc_grid, Q, hd_grid, R, z_grid, β, γ, bar_σ, μ_c, μ_d = sv_model
    I, J, K = shapes
    # Reshape and broadcast over (i, j, k, i', j', k')
    hc = np.reshape(hc_grid,     (I, 1, 1, 1,  1,  1))
    hd = np.reshape(hd_grid,     (1, J, 1, 1,  1,  1))
    z = np.reshape(z_grid,       (1, 1, K, 1,  1,  1))
    P = np.reshape(P,            (I, 1, 1, I,  1,  1))
    Q = np.reshape(Q,            (1, J, 1, 1,  J,  1))
    R = np.reshape(R,            (1, 1, K, 1,  1,  K))
    a = μ_d - γ * μ_c
    b = bar_σ**2 * (jnp.exp(2 * hd) + γ**2 * jnp.exp(2 * hc)) / 2
    κ = jnp.exp(a + (1 - γ) * z + b)
    A = β * κ * P * Q * R
    Ag = jnp.sum(A * g, axis=(3, 4, 5))
    return Ag


def A_modified(g, sv_model, shapes):
    # Set up
    P, hc_grid, Q, hd_grid, R, z_grid, β, γ, bar_σ, μ_c, μ_d = sv_model
    I, J, K = shapes
    # Reshape and broadcast over (i, j, k, i', j', k')
    hc = np.reshape(hc_grid,     (I, 1, 1, 1,  1,  1))
    hd = np.reshape(hd_grid,     (1, J, 1, 1,  1,  1))
    z = np.reshape(z_grid,       (1, 1, K, 1,  1,  1))
    P = np.reshape(P,            (I, 1, 1, I,  1,  1))
    Q = np.reshape(Q,            (1, J, 1, 1,  J,  1))
    R = np.reshape(R,            (1, 1, K, 1,  1,  K))
    g = np.reshape(g,            (1, 1, 1, I,  J,  K))
    a = μ_d - γ * μ_c
    b = bar_σ**2 * (jnp.exp(2 * hd) + γ**2 * jnp.exp(2 * hc)) / 2
    κ = jnp.exp(a + (1 - γ) * z + b)
    A = β * κ * P * Q * R
    Ag = jnp.sum(A * g, axis=(3, 4, 5))
    return Ag


np.allclose(A(np.ones(shapes), sv_model, shapes), A_modified(np.ones(shapes), sv_model, shapes))
True

Please let me know if there is anything else I can further improve.

Many thanks in advance.

@jstac
Copy link
Contributor

jstac commented Sep 29, 2023

Thanks @HumphreyYang , nice work, and thanks also @shlff, @sgcbayli024 and @pilarbarros. Merging.

@jstac jstac merged commit 1924e23 into main Sep 29, 2023
@jstac jstac deleted the fix-asset-pricing branch September 29, 2023 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error in asset pricing code

3 participants