Integral2 seems to substitute non-scalar values of variable into in... (2024)

Dear community,

I am struggling with an error that my code produces. I am trying to do a maximum-likelihood estimation (MLE). I am importing CSV data; each row is an observation and an observation is four-dimensional with column names "bad_bought", "good_bought", "bad_notbought", "good_notbought".

Each observation is assumed to have latent variables Integral2 seems to substitute non-scalar values of variable into in... (1) and Integral2 seems to substitute non-scalar values of variable into in... (2) that are drawn from a joint-normal distribution with mean Integral2 seems to substitute non-scalar values of variable into in... (3) and variance Integral2 seems to substitute non-scalar values of variable into in... (4) and Integral2 seems to substitute non-scalar values of variable into in... (5) and correlation ρ. An observation's numeric values for the four columns are given by

"bad_bought": Integral2 seems to substitute non-scalar values of variable into in... (6)

"good_bought": Integral2 seems to substitute non-scalar values of variable into in... (7)

"bad_notbought": Integral2 seems to substitute non-scalar values of variable into in... (8)

"good_notbought": Integral2 seems to substitute non-scalar values of variable into in... (9)

where the error terms are iid, normal, and have variance v.

I understand that I can calculate the joint distribution of the four answers (for given Integral2 seems to substitute non-scalar values of variable into in... (10) ) directly. However, I want to use numerical integration, as in the code below, because the model will eventually become more complicated necessitating it.

I receive two type of error messages:

First, and this is copy-pasted below, MATLAB tells me my mvnpdf arguments do not have the right dimensions. I do not understand that. [t1, t2], mu_vec are both of dimensions 1x2, i.e., they have the same number of columns. This error disappears if I replace the first argument of mvnpdf with [20, 20] (or some other numbers). I do not understand why; should integral2 not substitute scalar values for t1 and t2?

Second, and this appears if I replace [t1,t2] with [20,20] in the argument of mvnpdf, I receive the error message telling me I cannot use "*" to multiply the normpdf values together. This suggests to me that again t1 and t2 are not scalars.

Could you help? Thank you!

% Load necessary data

data = readtable('df_restricted.csv');

% Select relevant columns and convert to matrix

data_matrix = table2array(data(:, {'bad_bought', 'good_bought', 'bad_notbought', 'good_notbought'}));

% Define the negative log-likelihood function

function neg_log_lik = neg_log_lik_real(params, data_matrix)

% Extract parameters

mu1 = params(1);

mu2 = params(2);

v1 = params(3);

v2 = params(4);

rho = params(5);

v = params(6);

% Mean vector

mu_vec = [mu1, mu2];

disp(size(mu_vec))

% Covariance matrix

cov_matrix = [v1, rho * sqrt(v1 * v2); rho * sqrt(v1 * v2), v2];

disp(size(cov_matrix))

% Loop through each data point

for i = 1:size(data_matrix, 1)

% Define the integrand for 2-dimensional integration

f = @(t1, t2) ( ...

mvnpdf([t1, t2], mu_vec, cov_matrix) ...

* normpdf(data_matrix(i, 1) - (t1 - t2), 0, sqrt(v)) ...

* normpdf(data_matrix(i, 2) - (t1 + t2), 0, sqrt(v)) ...

* normpdf(data_matrix(i, 3) - (t1 + t2), 0, sqrt(v)) ...

* normpdf(data_matrix(i, 4) - (t1 - t2), 0, sqrt(v)) ...

);

% Before integral2 call

% Perform 2-dimensional numerical integration using integral2

pdf_value = integral2(f, -Inf, Inf, -Inf, Inf, 'AbsTol', 1e-6, 'RelTol', 1e-6);

% Take log of the result and accumulate

log_value = log(pdf_value);

log_sum = log_sum + log_value;

end

% Return negative log-likelihood

neg_log_lik = -log_sum;

end

% Set initial parameters

initial_params = [56, 11, 119, 131, -0.36, 419];

% Define the function handle for optimization

neg_log_lik_handle = @(params) neg_log_lik_real(params, data_matrix);

% Use fminunc to minimize the negative log-likelihood

options = optimoptions('fminunc', 'Algorithm', 'quasi-newton', 'MaxIterations', 500, 'TolFun', 1e-8);

[estimated_params, fval, exitflag, output] = fminunc(neg_log_lik_handle, initial_params, options);

% Display the results

disp('Estimated parameters:');

disp(estimated_params);

disp('Final log-likelihood value:');

disp(-fval);

First error:

Error using mvnpdf (line 67)

X and MU must have the same number of columns.

Error in

Structural_Estimation>@(t1,t2)(mvnpdf([t1,t2],mu_vec,cov_matrix)*normpdf(data_matrix(i,1)-(t1-t2),0,sqrt(v))*normpdf(data_matrix(i,2)-(t1+t2),0,sqrt(v))*normpdf(data_matrix(i,3)-(t1+t2),0,sqrt(v))*normpdf(data_matrix(i,4)-(t1-t2),0,sqrt(v)))

(line 34)

mvnpdf([t1, t2], mu_vec, cov_matrix) ...

First error:

Error in integral2Calc>@(y)fun(xi*ones(size(y)),y) (line 18)

@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions), ...

Error in integralCalc>iterateScalarValued (line 334)

fx = FUN(t);

Error in integralCalc>vadapt (line 148)

[q,errbnd] = iterateScalarValued(u,tinterval,pathlen, ...

Error in integralCalc (line 113)

[q,errbnd] = vadapt(@minusInfToInfInvTransform,interval, ...

Error in

integral2Calc>@(xi,y1i,y2i)integralCalc(@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions)

(line 17)

innerintegral = @(x)arrayfun(@(xi,y1i,y2i)integralCalc( ...

Error in

integral2Calc>@(x)arrayfun(@(xi,y1i,y2i)integralCalc(@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions),x,ymin(x),ymax(x))

(line 17)

innerintegral = @(x)arrayfun(@(xi,y1i,y2i)integralCalc( ...

Error in integralCalc>iterateScalarValued (line 334)

fx = FUN(t);

Error in integralCalc>vadapt (line 148)

[q,errbnd] = iterateScalarValued(u,tinterval,pathlen, ...

Error in integralCalc (line 113)

[q,errbnd] = vadapt(@minusInfToInfInvTransform,interval, ...

Error in integral2Calc>integral2i (line 20)

[q,errbnd] = integralCalc(innerintegral,xmin,xmax,opstruct.integralOptions);

Error in integral2Calc (line 7)

[q,errbnd] = integral2i(fun,xmin,xmax,ymin,ymax,optionstruct);

Error in integral2 (line 105)

Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);

Error in Structural_Estimation>neg_log_lik_real (line 43)

pdf_value = integral2(f, -Inf, Inf, -Inf, Inf, 'AbsTol', 1e-6, 'RelTol', 1e-6);

Error in Structural_Estimation>@(params)neg_log_lik_real(params,data_matrix) (line 58)

neg_log_lik_handle = @(params) neg_log_lik_real(params, data_matrix);

Error in fminunc (line 233)

f = feval(funfcn{3},x,varargin{:});

Error in Structural_Estimation (line 62)

[estimated_params, fval, exitflag, output] = fminunc(neg_log_lik_handle, initial_params, options);

Caused by:

Failure in initial objective function evaluation. FMINUNC cannot continue.

----

Second error:

Error using *

Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix

matches the number of rows in the second matrix. To operate on each element of the matrix

individually, use TIMES (.*) for elementwise multiplication.

Error in Structural_Estimation>@(t1,t2)(mvnpdf([20,20],mu_vec,cov_matrix)*normpdf(data_matrix(i,1)-(t1-t2),0,sqrt(v))*normpdf(data_matrix(i,2)-(t1+t2),0,sqrt(v))*normpdf(data_matrix(i,3)-(t1+t2),0,sqrt(v))*normpdf(data_matrix(i,4)-(t1-t2),0,sqrt(v))) (line 36)

* normpdf(data_matrix(i, 2) - (t1 + t2), 0, sqrt(v)) ...

^

Error in integral2Calc>@(y)fun(xi*ones(size(y)),y) (line 18)

@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions), ...

^^^^^^^^^^^^^^^^^^^^^^^

Error in integralCalc>iterateScalarValued (line 334)

fx = FUN(t);

^^^^^^

Error in integralCalc>vadapt (line 148)

[q,errbnd] = iterateScalarValued(u,tinterval,pathlen, ...

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error in integralCalc (line 113)

[q,errbnd] = vadapt(@minusInfToInfInvTransform,interval, ...

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error in integral2Calc>@(xi,y1i,y2i)integralCalc(@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions) (line 17)

innerintegral = @(x)arrayfun(@(xi,y1i,y2i)integralCalc( ...

^^^^^^^^^^^^^^^^^

Error in integral2Calc>@(x)arrayfun(@(xi,y1i,y2i)integralCalc(@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions),x,ymin(x),ymax(x)) (line 17)

innerintegral = @(x)arrayfun(@(xi,y1i,y2i)integralCalc( ...

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error in integralCalc>iterateScalarValued (line 334)

fx = FUN(t);

^^^^^^

Error in integralCalc>vadapt (line 148)

[q,errbnd] = iterateScalarValued(u,tinterval,pathlen, ...

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error in integralCalc (line 113)

[q,errbnd] = vadapt(@minusInfToInfInvTransform,interval, ...

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error in integral2Calc>integral2i (line 20)

[q,errbnd] = integralCalc(innerintegral,xmin,xmax,opstruct.integralOptions);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error in integral2Calc (line 7)

[q,errbnd] = integral2i(fun,xmin,xmax,ymin,ymax,optionstruct);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error in integral2 (line 105)

Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error in Structural_Estimation>neg_log_lik_real (line 43)

pdf_value = integral2(f, -Inf, Inf, -Inf, Inf, 'AbsTol', 1e-6, 'RelTol', 1e-6);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error in Structural_Estimation>@(params)neg_log_lik_real(params,data_matrix) (line 58)

neg_log_lik_handle = @(params) neg_log_lik_real(params, data_matrix);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error in fminunc (line 233)

f = feval(funfcn{3},x,varargin{:});

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error in Structural_Estimation (line 62)

[estimated_params, fval, exitflag, output] = fminunc(neg_log_lik_handle, initial_params, options);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Caused by:

Failure in initial objective function evaluation. FMINUNC cannot continue.

Related documentation

Integral2 seems to substitute non-scalar values of variable into in... (2024)

References

Top Articles
Controlled Heat Potion
European History/Age Of Revolutions - Wikibooks, open books for an open world
Obituary (Binghamton Press & Sun-Bulletin): Tully Area Historical Society
craigslist: south coast jobs, apartments, for sale, services, community, and events
Beds From Rent-A-Center
Mail Healthcare Uiowa
Chuckwagon racing 101: why it's OK to ask what a wheeler is | CBC News
Employeeres Ual
Giovanna Ewbank Nua
Does Publix Have Sephora Gift Cards
Raid Guides - Hardstuck
Med First James City
Healing Guide Dragonflight 10.2.7 Wow Warring Dueling Guide
Hijab Hookup Trendy
Curtains - Cheap Ready Made Curtains - Deconovo UK
Operation Cleanup Schedule Fresno Ca
My.tcctrack
Xxn Abbreviation List 2023
Velocity. The Revolutionary Way to Measure in Scrum
Illinois VIN Check and Lookup
Lowe's Garden Fence Roll
10 Fun Things to Do in Elk Grove, CA | Explore Elk Grove
Is The Yankees Game Postponed Tonight
VERHUURD: Barentszstraat 12 in 'S-Gravenhage 2518 XG: Woonhuis.
Craigslist Prescott Az Free Stuff
Music Go Round Music Store
Beverage Lyons Funeral Home Obituaries
Bekijk ons gevarieerde aanbod occasions in Oss.
Yugen Manga Jinx Cap 19
Drying Cloths At A Hammam Crossword Clue
Gen 50 Kjv
Jamielizzz Leaked
Mississippi Craigslist
Mercedes W204 Belt Diagram
Wcostream Attack On Titan
Kagtwt
Bridger Park Community Garden
Studentvue Columbia Heights
Gets Less Antsy Crossword Clue
How Many Dogs Can You Have in Idaho | GetJerry.com
Craigs List Hartford
Sun Tracker Pontoon Wiring Diagram
Cuckold Gonewildaudio
Dyi Urban Dictionary
American Bully Puppies for Sale | Lancaster Puppies
Devotion Showtimes Near Showplace Icon At Valley Fair
Sam's Club Gas Price Sioux City
Joy Taylor Nip Slip
Erica Mena Net Worth Forbes
Twizzlers Strawberry - 6 x 70 gram | bol
Tweedehands camper te koop - camper occasion kopen
Adams County 911 Live Incident
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 6491

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.